From 13cbf8debef81c2752e84c0a3b195f116b6a1c08 Mon Sep 17 00:00:00 2001 From: mlycore Date: Tue, 4 Jul 2023 19:32:53 +0800 Subject: [PATCH 1/3] feat: introduce PodBuilder Signed-off-by: mlycore --- .../pkg/kubernetes/pod/builder.go | 150 ++++++++++++++++++ .../pkg/kubernetes/pod/pod.go | 18 +++ .../pkg/kubernetes/pod/pod_test.go | 18 +++ 3 files changed, 186 insertions(+) create mode 100644 shardingsphere-operator/pkg/kubernetes/pod/builder.go create mode 100644 shardingsphere-operator/pkg/kubernetes/pod/pod.go create mode 100644 shardingsphere-operator/pkg/kubernetes/pod/pod_test.go diff --git a/shardingsphere-operator/pkg/kubernetes/pod/builder.go b/shardingsphere-operator/pkg/kubernetes/pod/builder.go new file mode 100644 index 00000000..8c10c9d7 --- /dev/null +++ b/shardingsphere-operator/pkg/kubernetes/pod/builder.go @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pod + +import ( + // appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + // metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// PodBuilder represents the configuration of a pod +type PodBuilder interface { + SetName(name string) PodBuilder + SetNamespace(namespace string) PodBuilder + SetLabels(labels map[string]string) PodBuilder + SetAnnotations(annos map[string]string) PodBuilder + + SetVolumes(vs []corev1.Volume) PodBuilder + AppendVolumes(vs []corev1.Volume) PodBuilder + + SetInitContainers(cs []corev1.Container) PodBuilder + AppendInitContainers(cs []corev1.Container) PodBuilder + + SetContainers(cs []corev1.Container) PodBuilder + AppendContainers(cs []corev1.Container) PodBuilder + + SetTerminationGracePeriodSeconds(secs *int64) PodBuilder + SetImagePullSecrets(secs []corev1.LocalObjectReference) PodBuilder +} + +type podBuilder struct { + pod *corev1.Pod +} + +// SetName sets the name of the pod +func (b *podBuilder) SetName(name string) PodBuilder { + b.pod.Name = name + return b +} + +// SetNamespace sets the namespace of the pod +func (b *podBuilder) SetNamespace(namespace string) PodBuilder { + b.pod.Namespace = namespace + return b +} + +// SetLabels sets the labels of the pod +func (b *podBuilder) SetLabels(labels map[string]string) PodBuilder { + if b.pod.Labels == nil { + b.pod.Labels = map[string]string{} + } + b.pod.Labels = labels + return b +} + +// SetAnnotations set the annotations of the pod +func (b *podBuilder) SetAnnotations(annos map[string]string) PodBuilder { + if b.pod.Annotations == nil { + b.pod.Annotations = map[string]string{} + } + b.pod.Annotations = annos + return b +} + +// SetVolumes sets the volumes +func (b *podBuilder) SetVolumes(vs []corev1.Volume) PodBuilder { + if b.pod.Spec.Volumes == nil { + b.pod.Spec.Volumes = []corev1.Volume{} + } + + b.pod.Spec.Volumes = vs + return b +} + +// AppendVolumes append volumes to the container +func (b *podBuilder) AppendVolumes(vs []corev1.Volume) PodBuilder { + if b.pod.Spec.Volumes == nil { + b.pod.Spec.Volumes = []corev1.Volume{} + } + + b.pod.Spec.Volumes = append(b.pod.Spec.Volumes, vs...) + return b +} + +// SetInintContainers sets the int containers to the container +func (b *podBuilder) SetInitContainers(cs []corev1.Container) PodBuilder { + if b.pod.Spec.InitContainers == nil { + b.pod.Spec.InitContainers = cs + } + b.pod.Spec.InitContainers = cs + return b +} + +// AppendInitContainers append init containers to the container +func (b *podBuilder) AppendInitContainers(cs []corev1.Container) PodBuilder { + if b.pod.Spec.InitContainers == nil { + b.pod.Spec.InitContainers = cs + } + b.pod.Spec.InitContainers = append(b.pod.Spec.InitContainers, cs...) + return b +} + +// SetContainer set the container to the pod +func (b *podBuilder) SetContainers(cs []corev1.Container) PodBuilder { + if b.pod.Spec.Containers == nil { + b.pod.Spec.Containers = cs + } + b.pod.Spec.Containers = cs + return b +} + +// AppendContainers appends containers to the pod +func (b *podBuilder) AppendContainers(cs []corev1.Container) PodBuilder { + if b.pod.Spec.Containers == nil { + b.pod.Spec.Containers = cs + } + b.pod.Spec.Containers = append(b.pod.Spec.Containers, cs...) + return b +} + +// SetImagePullSecrets sets the image pull secrets +func (b *podBuilder) SetImagePullSecrets(secs []corev1.LocalObjectReference) PodBuilder { + if b.pod.Spec.ImagePullSecrets == nil { + b.pod.Spec.ImagePullSecrets = []corev1.LocalObjectReference{} + } + + b.pod.Spec.ImagePullSecrets = secs + return b +} + +// SetTerminationGracePeriodSeconds sets the grace period +func (b *podBuilder) SetTerminationGracePeriodSeconds(secs *int64) PodBuilder { + b.pod.Spec.TerminationGracePeriodSeconds = secs + return b +} diff --git a/shardingsphere-operator/pkg/kubernetes/pod/pod.go b/shardingsphere-operator/pkg/kubernetes/pod/pod.go new file mode 100644 index 00000000..2d2233a7 --- /dev/null +++ b/shardingsphere-operator/pkg/kubernetes/pod/pod.go @@ -0,0 +1,18 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pod diff --git a/shardingsphere-operator/pkg/kubernetes/pod/pod_test.go b/shardingsphere-operator/pkg/kubernetes/pod/pod_test.go new file mode 100644 index 00000000..2d2233a7 --- /dev/null +++ b/shardingsphere-operator/pkg/kubernetes/pod/pod_test.go @@ -0,0 +1,18 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pod From 2551d0fef21c1fad82a166c6a0d0b8b89c969691 Mon Sep 17 00:00:00 2001 From: mlycore Date: Tue, 4 Jul 2023 19:33:48 +0800 Subject: [PATCH 2/3] refactor: Split DeploymentBuilder to ShardingSphereDeploymentBuilder Signed-off-by: mlycore --- .../pkg/kubernetes/deployment/builder.go | 294 ++++++++++-------- .../kubernetes/deployment/deployment_test.go | 7 +- 2 files changed, 170 insertions(+), 131 deletions(-) diff --git a/shardingsphere-operator/pkg/kubernetes/deployment/builder.go b/shardingsphere-operator/pkg/kubernetes/deployment/builder.go index df290a00..74841da1 100644 --- a/shardingsphere-operator/pkg/kubernetes/deployment/builder.go +++ b/shardingsphere-operator/pkg/kubernetes/deployment/builder.go @@ -156,15 +156,13 @@ type DeploymentBuilder interface { SetNamespace(namespace string) DeploymentBuilder SetLabelsAndSelectors(labels map[string]string, selectors *metav1.LabelSelector) DeploymentBuilder SetAnnotations(annos map[string]string) DeploymentBuilder - SetShardingSphereProxyPodTemplate(tpl *corev1.PodTemplateSpec) DeploymentBuilder - SetShardingSphereProxyPodTemplateLabels(labels map[string]string) DeploymentBuilder - SetShardingSphereProxyPodTemplateAnnotations(annos map[string]string) DeploymentBuilder - SetShardingSphereProxyContainer(con *corev1.Container) DeploymentBuilder - SetMySQLConnector(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) DeploymentBuilder - SetAgentBin(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) DeploymentBuilder - SetInitContainer(con *corev1.Container) DeploymentBuilder + + SetPodTemplateLabels(labels map[string]string) DeploymentBuilder + SetPodTemplateAnnotations(annos map[string]string) DeploymentBuilder + SetPodTemplateSpec(tpl *corev1.PodTemplateSpec) DeploymentBuilder SetVolume(volume *corev1.Volume) DeploymentBuilder SetReplicas(r *int32) DeploymentBuilder + Build() *appsv1.Deployment } @@ -212,26 +210,76 @@ func (d *deploymentBuilder) SetReplicas(r *int32) DeploymentBuilder { return d } -// SetShardingSphereProxyPodTemplate sets Deployment PodTemplateSpec for ShardingSphereProxy Pod -func (d *deploymentBuilder) SetShardingSphereProxyPodTemplate(tpl *corev1.PodTemplateSpec) DeploymentBuilder { +// SetPodTemplate sets Deployment PodTemplateSpec for ShardingSphereProxy Pod +func (d *deploymentBuilder) SetPodTemplateSpec(tpl *corev1.PodTemplateSpec) DeploymentBuilder { d.deployment.Spec.Template = *tpl return d } -// SetShardingSphereProxyPodTemplateAnnotations sets annotations for ShardingSphereProxy Pod -func (d *deploymentBuilder) SetShardingSphereProxyPodTemplateAnnotations(annotations map[string]string) DeploymentBuilder { +// SetPodTemplateAnnotations sets annotations for ShardingSphereProxy Pod +func (d *deploymentBuilder) SetPodTemplateAnnotations(annotations map[string]string) DeploymentBuilder { d.deployment.Spec.Template.Annotations = annotations return d } -// SetShardingSphereProxyPodTemplateLabels sets labels for ShardingSphereProxy Pod -func (d *deploymentBuilder) SetShardingSphereProxyPodTemplateLabels(labels map[string]string) DeploymentBuilder { +// SetPodTemplateLabels sets labels for ShardingSphereProxy Pod +func (d *deploymentBuilder) SetPodTemplateLabels(labels map[string]string) DeploymentBuilder { d.deployment.Spec.Template.Labels = labels return d } +// SetVolume sets a volume for Deployment +func (d *deploymentBuilder) SetVolume(vol *corev1.Volume) DeploymentBuilder { + if d.deployment.Spec.Template.Spec.Volumes == nil { + d.deployment.Spec.Template.Spec.Volumes = []corev1.Volume{*vol} + } + + for idx := range d.deployment.Spec.Template.Spec.Volumes { + if d.deployment.Spec.Template.Spec.Volumes[idx].Name == vol.Name { + d.deployment.Spec.Template.Spec.Volumes[idx] = *vol + return d + } + } + + d.deployment.Spec.Template.Spec.Volumes = append(d.deployment.Spec.Template.Spec.Volumes, *vol) + return d +} + +// Build returns a Deployment +func (d *deploymentBuilder) Build() *appsv1.Deployment { + return d.deployment +} + +type ShardingSphereDeploymentBuilder interface { + DeploymentBuilder + + // FIXME: this should be refactored into PodBuilder + SetContainer(con *corev1.Container) ShardingSphereDeploymentBuilder + SetInitContainer(con *corev1.Container) ShardingSphereDeploymentBuilder + + SetMySQLConnector(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) ShardingSphereDeploymentBuilder + SetAgentBin(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) ShardingSphereDeploymentBuilder +} + +// NewShardingSphereDeploymentBuilder creates a new ShardingSphereDeploymentBuilder +func NewShardingSphereDeploymentBuilder(meta metav1.Object, gvk schema.GroupVersionKind) ShardingSphereDeploymentBuilder { + dp := DefaultDeployment(meta, gvk) + + return &shardingsphereDeploymentBuilder{ + DeploymentBuilder: &deploymentBuilder{ + deployment: dp, + }, + deployment: dp, + } +} + +type shardingsphereDeploymentBuilder struct { + DeploymentBuilder + deployment *appsv1.Deployment +} + // SetShardingSphereProxyContainer sets a container for ShardingSphereProxy -func (d *deploymentBuilder) SetShardingSphereProxyContainer(proxy *corev1.Container) DeploymentBuilder { +func (d *shardingsphereDeploymentBuilder) SetContainer(proxy *corev1.Container) ShardingSphereDeploymentBuilder { if d.deployment.Spec.Template.Spec.Containers == nil { d.deployment.Spec.Template.Spec.Containers = []corev1.Container{*proxy} } @@ -248,7 +296,7 @@ func (d *deploymentBuilder) SetShardingSphereProxyContainer(proxy *corev1.Contai } // SetInitContainer sets the a init container for bootstrapping -func (d *deploymentBuilder) SetInitContainer(init *corev1.Container) DeploymentBuilder { +func (d *shardingsphereDeploymentBuilder) SetInitContainer(init *corev1.Container) ShardingSphereDeploymentBuilder { if d.deployment.Spec.Template.Spec.InitContainers == nil { d.deployment.Spec.Template.Spec.InitContainers = []corev1.Container{} } @@ -265,6 +313,93 @@ func (d *deploymentBuilder) SetInitContainer(init *corev1.Container) DeploymentB return d } +// SetMySQLConnector will set an init container to download mysql jar and mount files for proxy container. +func (d *shardingsphereDeploymentBuilder) SetMySQLConnector(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) ShardingSphereDeploymentBuilder { + scb.AppendEnv([]corev1.EnvVar{ + { + Name: defaultMySQLDriverEnvName, + Value: cn.Spec.StorageNodeConnector.Version, + }, + }) + + vb := NewSharedVolumeAndMountBuilder(). + SetVolumeMountSize(2). + SetName(defaultMySQLDriverVolumeName). + SetVolumeSourceEmptyDir(). + SetMountPath(0, defaultExtlibPath). + SetMountPath(1, absoluteMySQLDriverMountName(defaultExtlibPath, cn.Spec.StorageNodeConnector.Version)). + SetSubPath(1, relativeMySQLDriverMountName(cn.Spec.StorageNodeConnector.Version)) + + v, vms := vb.Build() + d.SetVolume(v) + scb.SetVolumeMount(vms[1]) + + cb := NewBootstrapContainerBuilderForMysqlJar().SetVolumeMount(vms[0]).AppendEnv([]corev1.EnvVar{ + { + Name: defaultMySQLDriverEnvName, + Value: cn.Spec.StorageNodeConnector.Version, + }, + }) + con := cb.Build() + d.SetInitContainer(con) + + sc := scb.Build() + d.SetContainer(sc) + + return d +} + +// SetAgentBin set `agent bin` for ShardingSphereProxy with [observability](https://shardingsphere.apache.org/document/current/en/user-manual/shardingsphere-proxy/observability/) +func (d *shardingsphereDeploymentBuilder) SetAgentBin(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) ShardingSphereDeploymentBuilder { + // set env JAVA_TOOL_OPTIONS to proxy container, make sure proxy will apply agent-bin.jar + // agent-bin's version is always equals to shardingsphere proxy image's version + + scb.AppendEnv([]corev1.EnvVar{ + { + Name: defaultJavaToolOptionsName, + Value: fmt.Sprintf(defaultJavaAgentEnvValue, cn.Spec.ServerVersion), + }, + }) + + // mount agent-bin dir + vbAgent := NewSharedVolumeAndMountBuilder(). + SetVolumeMountSize(1). + SetName(defaultJavaAgentVolumeName). + SetVolumeSourceEmptyDir(). + SetMountPath(0, defaultJavaAgentVolumeMountPath) + va, vma := vbAgent.Build() + d.SetVolume(va) + scb.SetVolumeMount(vma[0]) + + // mount agent config to overwrite agent-bin's config + vbAgentConf := NewSharedVolumeAndMountBuilder(). + SetVolumeMountSize(1). + SetName(defaultJavaAgentConfigVolumeName). + SetVolumeSourceConfigMap(cn.Name, corev1.KeyToPath{Key: configmap.ConfigDataKeyForAgent, Path: configmap.ConfigDataKeyForAgent}). + SetMountPath(0, defaultJavaAgentConfigVolumeMountPath) + vc, vmc := vbAgentConf.Build() + d.SetVolume(vc) + scb.SetVolumeMount(vmc[0]) + + cb := NewBootstrapContainerBuilderForAgentBin().SetVolumeMount(vma[0]).AppendEnv([]corev1.EnvVar{ + { + Name: defaultAgentBinVersionEnvName, + Value: cn.Spec.ServerVersion, + }, + }) + con := cb.Build() + d.SetInitContainer(con) + + sc := scb.Build() + d.SetContainer(sc) + + return d +} + +func (d *shardingsphereDeploymentBuilder) Build() *appsv1.Deployment { + return d.DeploymentBuilder.Build() +} + // SharedVolumeAndMountBuilder build a Volume which could be mounted by different containers type SharedVolumeAndMountBuilder interface { SetName(name string) SharedVolumeAndMountBuilder @@ -415,32 +550,13 @@ func (b *volumeAndMountBuilder) Build() (*corev1.Volume, *corev1.VolumeMount) { return b.volume, b.volumemount } -// SetVolume sets a volume for Deployment -func (d *deploymentBuilder) SetVolume(vol *corev1.Volume) DeploymentBuilder { - if d.deployment.Spec.Template.Spec.Volumes == nil { - d.deployment.Spec.Template.Spec.Volumes = []corev1.Volume{*vol} - } - - for idx := range d.deployment.Spec.Template.Spec.Volumes { - if d.deployment.Spec.Template.Spec.Volumes[idx].Name == vol.Name { - d.deployment.Spec.Template.Spec.Volumes[idx] = *vol - return d - } - } - - d.deployment.Spec.Template.Spec.Volumes = append(d.deployment.Spec.Template.Spec.Volumes, *vol) - return d -} - -// Build returns a Deployment -func (d *deploymentBuilder) Build() *appsv1.Deployment { - return d.deployment -} - // NewDeployment creates a new Deployment func NewDeployment(cn *v1alpha1.ComputeNode) *appsv1.Deployment { - builder := NewDeploymentBuilder(cn.GetObjectMeta(), cn.GetObjectKind().GroupVersionKind()) - builder.SetName(cn.Name).SetNamespace(cn.Namespace).SetLabelsAndSelectors(cn.Labels, cn.Spec.Selector).SetAnnotations(cn.Annotations).SetReplicas(&cn.Spec.Replicas) + // builder := NewDeploymentBuilder(cn.GetObjectMeta(), cn.GetObjectKind().GroupVersionKind()) + ssbuilder := NewShardingSphereDeploymentBuilder(cn.GetObjectMeta(), cn.GetObjectKind().GroupVersionKind()) + // builder := ssbuilder.DeploymentBuilder + // builder.SetName(cn.Name).SetNamespace(cn.Namespace).SetLabelsAndSelectors(cn.Labels, cn.Spec.Selector).SetAnnotations(cn.Annotations).SetReplicas(&cn.Spec.Replicas) + ssbuilder.SetName(cn.Name).SetNamespace(cn.Namespace).SetLabelsAndSelectors(cn.Labels, cn.Spec.Selector).SetAnnotations(cn.Annotations).SetReplicas(&cn.Spec.Replicas) ports := []corev1.ContainerPort{} for idx := range cn.Spec.PortBindings { @@ -466,12 +582,12 @@ func NewDeployment(cn *v1alpha1.ComputeNode) *appsv1.Deployment { SetMountPath(0, defaultConfigVolumeMountPath) vc, vmc := vcb.Build() - builder.SetVolume(vc) + ssbuilder.SetVolume(vc) scb.SetVolumeMount(vmc[0]) // set agent for proxy if enabled, ok := cn.Annotations[DefaultAnnotationJavaAgentEnabled]; ok && enabled == "true" { - builder.SetAgentBin(scb, cn) + ssbuilder.SetAgentBin(scb, cn) metricsAnnos := map[string]string{} metricsAnnos[commonAnnotationPrometheusMetricsPath] = cn.Annotations[commonAnnotationPrometheusMetricsPath] @@ -479,7 +595,8 @@ func NewDeployment(cn *v1alpha1.ComputeNode) *appsv1.Deployment { metricsAnnos[commonAnnotationPrometheusMetricsScrape] = cn.Annotations[commonAnnotationPrometheusMetricsScrape] metricsAnnos[commonAnnotationPrometheusMetricsScheme] = cn.Annotations[commonAnnotationPrometheusMetricsScheme] - builder.SetShardingSphereProxyPodTemplateAnnotations(metricsAnnos) + // builder.SetShardingSphereProxyPodTemplateAnnotations(metricsAnnos) + ssbuilder.SetPodTemplateAnnotations(metricsAnnos) if cn.Spec.ServerVersion == "5.3.2" { sv := NewSharedVolumeAndMountBuilder(). @@ -488,28 +605,28 @@ func NewDeployment(cn *v1alpha1.ComputeNode) *appsv1.Deployment { SetVolumeSourceEmptyDir(). SetMountPath(0, "/opt/shardingsphere-proxy/bin") va, vma := sv.Build() - builder.SetVolume(va) + ssbuilder.SetVolume(va) scb.SetVolumeMount(vma[0]) // NOTE: This mountpath is not same with init container vma[0].MountPath = "/opt/shardingsphere-proxy/tmpbin" cb := NewBootstrapContainerBuilderForStartScripts().SetVolumeMount(vma[0]) con := cb.Build() - builder.SetInitContainer(con) + ssbuilder.SetInitContainer(con) } } if cn.Spec.StorageNodeConnector != nil { switch cn.Spec.StorageNodeConnector.Type { case v1alpha1.ConnectorTypeMySQL: - builder.SetMySQLConnector(scb, cn) + ssbuilder.SetMySQLConnector(scb, cn) case v1alpha1.ConnectorTypePostgreSQL: sc := scb.Build() - builder.SetShardingSphereProxyContainer(sc) + ssbuilder.SetContainer(sc) } } - return builder.Build() + return ssbuilder.Build() } func setProbes(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) { @@ -528,89 +645,6 @@ func setProbes(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) { } } -// SetMySQLConnector will set an init container to download mysql jar and mount files for proxy container. -func (d *deploymentBuilder) SetMySQLConnector(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) DeploymentBuilder { - scb.AppendEnv([]corev1.EnvVar{ - { - Name: defaultMySQLDriverEnvName, - Value: cn.Spec.StorageNodeConnector.Version, - }, - }) - - vb := NewSharedVolumeAndMountBuilder(). - SetVolumeMountSize(2). - SetName(defaultMySQLDriverVolumeName). - SetVolumeSourceEmptyDir(). - SetMountPath(0, defaultExtlibPath). - SetMountPath(1, absoluteMySQLDriverMountName(defaultExtlibPath, cn.Spec.StorageNodeConnector.Version)). - SetSubPath(1, relativeMySQLDriverMountName(cn.Spec.StorageNodeConnector.Version)) - - v, vms := vb.Build() - d.SetVolume(v) - scb.SetVolumeMount(vms[1]) - - cb := NewBootstrapContainerBuilderForMysqlJar().SetVolumeMount(vms[0]).AppendEnv([]corev1.EnvVar{ - { - Name: defaultMySQLDriverEnvName, - Value: cn.Spec.StorageNodeConnector.Version, - }, - }) - con := cb.Build() - d.SetInitContainer(con) - - sc := scb.Build() - d.SetShardingSphereProxyContainer(sc) - - return d -} - -// SetAgentBin set `agent bin` for ShardingSphereProxy with [observability](https://shardingsphere.apache.org/document/current/en/user-manual/shardingsphere-proxy/observability/) -func (d *deploymentBuilder) SetAgentBin(scb common.ContainerBuilder, cn *v1alpha1.ComputeNode) DeploymentBuilder { - // set env JAVA_TOOL_OPTIONS to proxy container, make sure proxy will apply agent-bin.jar - // agent-bin's version is always equals to shardingsphere proxy image's version - - scb.AppendEnv([]corev1.EnvVar{ - { - Name: defaultJavaToolOptionsName, - Value: fmt.Sprintf(defaultJavaAgentEnvValue, cn.Spec.ServerVersion), - }, - }) - - // mount agent-bin dir - vbAgent := NewSharedVolumeAndMountBuilder(). - SetVolumeMountSize(1). - SetName(defaultJavaAgentVolumeName). - SetVolumeSourceEmptyDir(). - SetMountPath(0, defaultJavaAgentVolumeMountPath) - va, vma := vbAgent.Build() - d.SetVolume(va) - scb.SetVolumeMount(vma[0]) - - // mount agent config to overwrite agent-bin's config - vbAgentConf := NewSharedVolumeAndMountBuilder(). - SetVolumeMountSize(1). - SetName(defaultJavaAgentConfigVolumeName). - SetVolumeSourceConfigMap(cn.Name, corev1.KeyToPath{Key: configmap.ConfigDataKeyForAgent, Path: configmap.ConfigDataKeyForAgent}). - SetMountPath(0, defaultJavaAgentConfigVolumeMountPath) - vc, vmc := vbAgentConf.Build() - d.SetVolume(vc) - scb.SetVolumeMount(vmc[0]) - - cb := NewBootstrapContainerBuilderForAgentBin().SetVolumeMount(vma[0]).AppendEnv([]corev1.EnvVar{ - { - Name: defaultAgentBinVersionEnvName, - Value: cn.Spec.ServerVersion, - }, - }) - con := cb.Build() - d.SetInitContainer(con) - - sc := scb.Build() - d.SetShardingSphereProxyContainer(sc) - - return d -} - // DefaultDeployment describes the default deployment func DefaultDeployment(meta metav1.Object, gvk schema.GroupVersionKind) *appsv1.Deployment { defaultMaxUnavailable := intstr.FromInt(0) diff --git a/shardingsphere-operator/pkg/kubernetes/deployment/deployment_test.go b/shardingsphere-operator/pkg/kubernetes/deployment/deployment_test.go index 188bf18e..f740b514 100644 --- a/shardingsphere-operator/pkg/kubernetes/deployment/deployment_test.go +++ b/shardingsphere-operator/pkg/kubernetes/deployment/deployment_test.go @@ -507,6 +507,11 @@ func TestDeploymentBuilder_SetShardingSphereProxyContainer(t *testing.T) { }, } + ssbuilder := &shardingsphereDeploymentBuilder{ + deployment: builder.deployment, + DeploymentBuilder: builder, + } + // 2. define a container to be set as a proxy container proxy := &corev1.Container{ Name: "shardingsphere-proxy", @@ -514,7 +519,7 @@ func TestDeploymentBuilder_SetShardingSphereProxyContainer(t *testing.T) { } // 3. call the SetShardingSphereProxyContainer function - builder.SetShardingSphereProxyContainer(proxy) + ssbuilder.SetContainer(proxy) // 4. check whether the proxy container was added or replaced in the Containers slice if len(builder.deployment.Spec.Template.Spec.Containers) != 2 { From 15844e0838d6dca095d7bed0b60ca3fd3c0a0715 Mon Sep 17 00:00:00 2001 From: mlycore Date: Wed, 5 Jul 2023 09:23:08 +0800 Subject: [PATCH 3/3] chore: add skip lint comment Signed-off-by: mlycore --- .../pkg/kubernetes/pod/builder.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/shardingsphere-operator/pkg/kubernetes/pod/builder.go b/shardingsphere-operator/pkg/kubernetes/pod/builder.go index 8c10c9d7..db24b27d 100644 --- a/shardingsphere-operator/pkg/kubernetes/pod/builder.go +++ b/shardingsphere-operator/pkg/kubernetes/pod/builder.go @@ -24,6 +24,7 @@ import ( ) // PodBuilder represents the configuration of a pod +// nolint:unused type PodBuilder interface { SetName(name string) PodBuilder SetNamespace(namespace string) PodBuilder @@ -43,23 +44,27 @@ type PodBuilder interface { SetImagePullSecrets(secs []corev1.LocalObjectReference) PodBuilder } +// nolint:unused type podBuilder struct { pod *corev1.Pod } // SetName sets the name of the pod +// nolint:unused func (b *podBuilder) SetName(name string) PodBuilder { b.pod.Name = name return b } // SetNamespace sets the namespace of the pod +// nolint:unused func (b *podBuilder) SetNamespace(namespace string) PodBuilder { b.pod.Namespace = namespace return b } // SetLabels sets the labels of the pod +// nolint:unused func (b *podBuilder) SetLabels(labels map[string]string) PodBuilder { if b.pod.Labels == nil { b.pod.Labels = map[string]string{} @@ -69,6 +74,7 @@ func (b *podBuilder) SetLabels(labels map[string]string) PodBuilder { } // SetAnnotations set the annotations of the pod +// nolint:unused func (b *podBuilder) SetAnnotations(annos map[string]string) PodBuilder { if b.pod.Annotations == nil { b.pod.Annotations = map[string]string{} @@ -78,6 +84,7 @@ func (b *podBuilder) SetAnnotations(annos map[string]string) PodBuilder { } // SetVolumes sets the volumes +// nolint:unused func (b *podBuilder) SetVolumes(vs []corev1.Volume) PodBuilder { if b.pod.Spec.Volumes == nil { b.pod.Spec.Volumes = []corev1.Volume{} @@ -88,6 +95,7 @@ func (b *podBuilder) SetVolumes(vs []corev1.Volume) PodBuilder { } // AppendVolumes append volumes to the container +// nolint:unused func (b *podBuilder) AppendVolumes(vs []corev1.Volume) PodBuilder { if b.pod.Spec.Volumes == nil { b.pod.Spec.Volumes = []corev1.Volume{} @@ -98,6 +106,7 @@ func (b *podBuilder) AppendVolumes(vs []corev1.Volume) PodBuilder { } // SetInintContainers sets the int containers to the container +// nolint:unused func (b *podBuilder) SetInitContainers(cs []corev1.Container) PodBuilder { if b.pod.Spec.InitContainers == nil { b.pod.Spec.InitContainers = cs @@ -107,6 +116,7 @@ func (b *podBuilder) SetInitContainers(cs []corev1.Container) PodBuilder { } // AppendInitContainers append init containers to the container +// nolint:unused func (b *podBuilder) AppendInitContainers(cs []corev1.Container) PodBuilder { if b.pod.Spec.InitContainers == nil { b.pod.Spec.InitContainers = cs @@ -116,6 +126,7 @@ func (b *podBuilder) AppendInitContainers(cs []corev1.Container) PodBuilder { } // SetContainer set the container to the pod +// nolint:unused func (b *podBuilder) SetContainers(cs []corev1.Container) PodBuilder { if b.pod.Spec.Containers == nil { b.pod.Spec.Containers = cs @@ -125,6 +136,7 @@ func (b *podBuilder) SetContainers(cs []corev1.Container) PodBuilder { } // AppendContainers appends containers to the pod +// nolint:unused func (b *podBuilder) AppendContainers(cs []corev1.Container) PodBuilder { if b.pod.Spec.Containers == nil { b.pod.Spec.Containers = cs @@ -134,6 +146,7 @@ func (b *podBuilder) AppendContainers(cs []corev1.Container) PodBuilder { } // SetImagePullSecrets sets the image pull secrets +// nolint:unused func (b *podBuilder) SetImagePullSecrets(secs []corev1.LocalObjectReference) PodBuilder { if b.pod.Spec.ImagePullSecrets == nil { b.pod.Spec.ImagePullSecrets = []corev1.LocalObjectReference{} @@ -144,6 +157,7 @@ func (b *podBuilder) SetImagePullSecrets(secs []corev1.LocalObjectReference) Pod } // SetTerminationGracePeriodSeconds sets the grace period +// nolint:unused func (b *podBuilder) SetTerminationGracePeriodSeconds(secs *int64) PodBuilder { b.pod.Spec.TerminationGracePeriodSeconds = secs return b