From 97f6393ffb2377e043a692b27248d3e38c7bbc76 Mon Sep 17 00:00:00 2001 From: zhangtao <111836083+sophon-zt@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:32:26 +0800 Subject: [PATCH] chore: remove dependence on characterType (#7500) --- go.mod | 2 +- pkg/configuration/config_manager/builder.go | 78 ++++++++++++------- .../config_manager/handler_util.go | 1 - pkg/controller/configuration/config_utils.go | 1 - .../configuration/tool_image_builder_test.go | 1 - pkg/controller/factory/builder.go | 32 -------- pkg/controller/factory/builder_test.go | 2 - 7 files changed, 53 insertions(+), 64 deletions(-) diff --git a/go.mod b/go.mod index 1d03e8a292d..6e6d19933ad 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/google/uuid v1.6.0 github.com/hashicorp/go-hclog v1.5.0 github.com/hashicorp/vault/sdk v0.9.2 + github.com/imdario/mergo v0.3.14 github.com/jackc/pgx/v5 v5.5.4 github.com/klauspost/compress v1.17.6 github.com/kubernetes-csi/external-snapshotter/client/v3 v3.0.0 @@ -185,7 +186,6 @@ require ( github.com/hashicorp/hcl v1.0.1-vault-5 // indirect github.com/hashicorp/yamux v0.1.1 // indirect github.com/huandu/xstrings v1.4.0 // indirect - github.com/imdario/mergo v0.3.14 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect diff --git a/pkg/configuration/config_manager/builder.go b/pkg/configuration/config_manager/builder.go index f4488eca6ca..96cdb074734 100644 --- a/pkg/configuration/config_manager/builder.go +++ b/pkg/configuration/config_manager/builder.go @@ -25,12 +25,15 @@ import ( "path/filepath" "strconv" + "github.com/imdario/mergo" "gopkg.in/yaml.v2" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" apiruntime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/util/retry" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -76,7 +79,7 @@ func BuildConfigManagerContainerParams(cli client.Client, ctx context.Context, m buildParam = &managerParams.ConfigSpecsBuildParams[i] volume = FindVolumeMount(managerParams.Volumes, buildParam.ConfigSpec.VolumeName) if volume == nil { - logger.Info(fmt.Sprintf("volume mount not be use : %s", buildParam.ConfigSpec.VolumeName)) + ctrl.Log.Info(fmt.Sprintf("volume mount not be use : %s", buildParam.ConfigSpec.VolumeName)) continue } buildParam.MountPoint = volume.MountPath @@ -349,39 +352,62 @@ func buildLazyRenderedConfigVolume(cmName string, manager *CfgManagerBuildParams func checkOrCreateConfigMap(referenceCM client.ObjectKey, scriptCMKey client.ObjectKey, cli client.Client, ctx context.Context, cluster *appsv1alpha1.Cluster, fn func(cm *corev1.ConfigMap) error) error { var ( - err error - - refCM = corev1.ConfigMap{} - sidecarCM = corev1.ConfigMap{} + err error + op controllerutil.OperationResult + refCM = corev1.ConfigMap{} ) if err = cli.Get(ctx, referenceCM, &refCM); err != nil { return err } - if err = cli.Get(ctx, scriptCMKey, &sidecarCM, inDataContext()); err != nil { - if !apierrors.IsNotFound(err) { - return err - } - scheme, _ := appsv1alpha1.SchemeBuilder.Build() - sidecarCM.Data = refCM.Data - if fn != nil { - if err := fn(&sidecarCM); err != nil { - return err - } - } - sidecarCM.SetLabels(refCM.GetLabels()) - sidecarCM.SetName(scriptCMKey.Name) - sidecarCM.SetNamespace(scriptCMKey.Namespace) - sidecarCM.SetLabels(refCM.Labels) - if err := controllerutil.SetOwnerReference(cluster, &sidecarCM, scheme); err != nil { - return err - } - if err := cli.Create(ctx, &sidecarCM, inDataContext()); err != nil { - return err + expected, err := createScripts(&refCM, scriptCMKey, cluster, fn) + if err != nil { + return err + } + existing := expected.DeepCopy() + mutateFn := func() (err error) { + mergeWithOverride(existing.Labels, expected.Labels) + mergeWithOverride(existing.Annotations, expected.Annotations) + existing.SetOwnerReferences(expected.GetOwnerReferences()) + existing.Data = expected.Data + return + } + + updateLog := func() { + ctrl.Log.Info(fmt.Sprintf("expected cm object[%s] has been %s", scriptCMKey, op)) + } + + defer updateLog() + return retry.RetryOnConflict(retry.DefaultRetry, func() error { + result, createOrUpdateErr := ctrl.CreateOrUpdate(ctx, cli, existing, mutateFn) + op = result + return createOrUpdateErr + }) +} + +func createScripts(refCM *corev1.ConfigMap, key client.ObjectKey, owner client.Object, fn func(cm *corev1.ConfigMap) error) (*corev1.ConfigMap, error) { + expected := &corev1.ConfigMap{} + + scheme, _ := appsv1alpha1.SchemeBuilder.Build() + expected.Data = refCM.Data + if fn != nil { + if err := fn(expected); err != nil { + return nil, err } } - return nil + expected.SetLabels(refCM.GetLabels()) + expected.SetName(key.Name) + expected.SetNamespace(key.Namespace) + expected.SetAnnotations(refCM.GetAnnotations()) + if err := controllerutil.SetOwnerReference(owner, expected, scheme); err != nil { + return nil, err + } + return expected, nil +} + +func mergeWithOverride(dst, src interface{}) { + _ = mergo.Merge(dst, src, mergo.WithOverride) } func checkAndUpdateReloadYaml(data map[string]string, reloadConfig string, formatterConfig appsv1beta1.FileFormatConfig) (map[string]string, error) { diff --git a/pkg/configuration/config_manager/handler_util.go b/pkg/configuration/config_manager/handler_util.go index a661c6998e3..a4a9607012a 100644 --- a/pkg/configuration/config_manager/handler_util.go +++ b/pkg/configuration/config_manager/handler_util.go @@ -44,7 +44,6 @@ type CfgManagerBuildParams struct { Volumes []corev1.VolumeMount `json:"volumes"` ComponentName string `json:"componentName"` - CharacterType string `json:"characterType"` SecreteName string `json:"secreteName"` // add volume to pod diff --git a/pkg/controller/configuration/config_utils.go b/pkg/controller/configuration/config_utils.go index 1f2f460177a..e24894237d2 100644 --- a/pkg/controller/configuration/config_utils.go +++ b/pkg/controller/configuration/config_utils.go @@ -206,7 +206,6 @@ func getUsingVolumesByConfigSpecs(podSpec *corev1.PodSpec, configSpecs []appsv1a func buildConfigManagerParams(cli client.Client, ctx context.Context, cluster *appsv1alpha1.Cluster, comp *component.SynthesizedComponent, configSpecBuildParams []cfgcm.ConfigSpecMeta, volumeDirs []corev1.VolumeMount, podSpec *corev1.PodSpec) (*cfgcm.CfgManagerBuildParams, error) { cfgManagerParams := &cfgcm.CfgManagerBuildParams{ ManagerName: constant.ConfigSidecarName, - CharacterType: comp.CharacterType, ComponentName: comp.Name, SecreteName: constant.GenerateDefaultConnCredential(cluster.Name), Image: viper.GetString(constant.KBToolsImage), diff --git a/pkg/controller/configuration/tool_image_builder_test.go b/pkg/controller/configuration/tool_image_builder_test.go index f0950939f0a..d2c45316c36 100644 --- a/pkg/controller/configuration/tool_image_builder_test.go +++ b/pkg/controller/configuration/tool_image_builder_test.go @@ -62,7 +62,6 @@ var _ = Describe("ToolsImageBuilderTest", func() { cfgManagerParams := &cfgcm.CfgManagerBuildParams{ ManagerName: constant.ConfigSidecarName, - CharacterType: clusterComponent.CharacterType, ComponentName: clusterComponent.Name, SecreteName: constant.GenerateDefaultConnCredential(clusterObj.Name), Image: viper.GetString(constant.KBToolsImage), diff --git a/pkg/controller/factory/builder.go b/pkg/controller/factory/builder.go index 4b593b5befa..095153f5ad1 100644 --- a/pkg/controller/factory/builder.go +++ b/pkg/controller/factory/builder.go @@ -394,38 +394,6 @@ func BuildCfgManagerContainer(sidecarRenderedParam *cfgcm.CfgManagerBuildParams) }, }, }) - if len(sidecarRenderedParam.CharacterType) > 0 { - env = append(env, corev1.EnvVar{ - Name: "DB_TYPE", - Value: sidecarRenderedParam.CharacterType, - }) - } - // TODO: Remove hard coding - if sidecarRenderedParam.CharacterType == "mysql" { - env = append(env, corev1.EnvVar{ - Name: "MYSQL_USER", - ValueFrom: &corev1.EnvVarSource{ - SecretKeyRef: &corev1.SecretKeySelector{ - Key: "username", - LocalObjectReference: corev1.LocalObjectReference{Name: sidecarRenderedParam.SecreteName}, - }, - }, - }, - corev1.EnvVar{ - Name: "MYSQL_PASSWORD", - ValueFrom: &corev1.EnvVarSource{ - SecretKeyRef: &corev1.SecretKeySelector{ - Key: "password", - LocalObjectReference: corev1.LocalObjectReference{Name: sidecarRenderedParam.SecreteName}, - }, - }, - }, - corev1.EnvVar{ - Name: "DATA_SOURCE_NAME", - Value: "$(MYSQL_USER):$(MYSQL_PASSWORD)@(localhost:3306)/", - }, - ) - } containerBuilder := builder.NewContainerBuilder(sidecarRenderedParam.ManagerName). AddCommands("env"). AddArgs("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$(TOOLS_PATH)"). diff --git a/pkg/controller/factory/builder_test.go b/pkg/controller/factory/builder_test.go index 974b1d254b6..9c032d2b750 100644 --- a/pkg/controller/factory/builder_test.go +++ b/pkg/controller/factory/builder_test.go @@ -367,7 +367,6 @@ var _ = Describe("builder", func() { ManagerName: "cfgmgr", SecreteName: "test-secret", ComponentName: synthesizedComponent.Name, - CharacterType: synthesizedComponent.CharacterType, Image: constant.KBToolsImage, Args: []string{}, Envs: []corev1.EnvVar{}, @@ -384,7 +383,6 @@ var _ = Describe("builder", func() { _, cluster, _ := newClusterObjs(nil) sidecarRenderedParam := &cfgcm.CfgManagerBuildParams{ ManagerName: "cfgmgr", - CharacterType: mysqlCharacterType, SecreteName: "test-secret", Image: constant.KBToolsImage, ShareProcessNamespace: true,