From f17870a38a91e83a8cd6cb7a9aa2e3bb8de22a54 Mon Sep 17 00:00:00 2001 From: Phoeniix Zhao Date: Wed, 7 Feb 2024 11:08:14 +0800 Subject: [PATCH] refactor: use configmap to specify the bootup args Signed-off-by: Phoeniix Zhao --- api/v1alpha1/xlinecluster_types.go | 13 ++++----- api/v1alpha1/zz_generated.deepcopy.go | 8 ++++-- internal/reconciler/cluster_reconciler.go | 5 ++++ internal/transformer/xlinecluster_resource.go | 27 +++++++++++++++++++ tests/e2e/cases/manifests/cluster.yaml | 2 +- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/api/v1alpha1/xlinecluster_types.go b/api/v1alpha1/xlinecluster_types.go index d3eeaa2..f6df2e2 100644 --- a/api/v1alpha1/xlinecluster_types.go +++ b/api/v1alpha1/xlinecluster_types.go @@ -124,7 +124,7 @@ type XlineClusterSpec struct { /// Xline container bootstrap arguments /// Set additional arguments except [`--name`, `--members`, `--storage-engine`, `--data-dir`] - BootstrapArgs XlineArgs `json:"config,omitempty"` + BootstrapArgs *XlineArgs `json:"config,omitempty"` // ImagePullPolicy of Xline cluster Pods // +optional @@ -153,9 +153,9 @@ type XlineAuthSecret struct { PriKey *string `json:"priKey"` } -func (s *XlineClusterSpec) BootArgs() []string { +func (s *XlineClusterSpec) BootArgs() map[string]string { bytes, err := json.Marshal(s.BootstrapArgs) - args := make([]string, 0) + args := map[string]string{} if err != nil { return args } @@ -164,11 +164,7 @@ func (s *XlineClusterSpec) BootArgs() []string { return args } for k, v := range data { - if bv, ok := v.(bool); ok && bv { - args = append(args, fmt.Sprintf("--%s", k)) - continue - } - args = append(args, fmt.Sprintf("--%s", k), fmt.Sprintf("%v", v)) + args[k] = fmt.Sprintf("%v", v) } return args } @@ -185,6 +181,7 @@ type XlineClusterOprStage string const ( StageXlineScriptCM XlineClusterOprStage = "Xline/ScriptCM" + StageXlineConfigMap XlineClusterOprStage = "Xline/ConfigMap" StageXlineService XlineClusterOprStage = "Xline/Service" StageXlineStatefulSet XlineClusterOprStage = "Xline/Statefulset" StageComplete XlineClusterOprStage = "complete" diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 51bbe27..42d3ead 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -21,7 +21,7 @@ limitations under the License. package v1alpha1 import ( - "k8s.io/api/apps/v1" + v1 "k8s.io/api/apps/v1" runtime "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ) @@ -288,7 +288,11 @@ func (in *XlineClusterSpec) DeepCopyInto(out *XlineClusterSpec) { *out = new(string) **out = **in } - in.BootstrapArgs.DeepCopyInto(&out.BootstrapArgs) + if in.BootstrapArgs != nil { + in, out := &in.BootstrapArgs, &out.BootstrapArgs + *out = new(XlineArgs) + (*in).DeepCopyInto(*out) + } if in.AuthSecrets != nil { in, out := &in.AuthSecrets, &out.AuthSecrets *out = new(XlineAuthSecret) diff --git a/internal/reconciler/cluster_reconciler.go b/internal/reconciler/cluster_reconciler.go index 3b8bee2..e494ad7 100644 --- a/internal/reconciler/cluster_reconciler.go +++ b/internal/reconciler/cluster_reconciler.go @@ -73,6 +73,11 @@ func (r *XlineClusterReconciler) recXlineResources() ClusterStageRecResult { if err := r.CreateOrUpdate(script, &corev1.ConfigMap{}); err != nil { return clusterStageFail(xapi.StageXlineScriptCM, err) } + // create an xline configmap + configMap := tran.MakeConfigMap(r.CR, r.Schema) + if err := r.CreateOrUpdate(configMap, &corev1.ConfigMap{}); err != nil { + return clusterStageFail(xapi.StageXlineConfigMap, err) + } // create an xline service service := tran.MakeService(r.CR, r.Schema) if err := r.CreateOrUpdate(service, &corev1.Service{}); err != nil { diff --git a/internal/transformer/xlinecluster_resource.go b/internal/transformer/xlinecluster_resource.go index 1f162d6..f9f848b 100644 --- a/internal/transformer/xlinecluster_resource.go +++ b/internal/transformer/xlinecluster_resource.go @@ -51,6 +51,19 @@ func getAuthInfo(auth_sec *xapi.XlineAuthSecret) ([]corev1.Volume, []corev1.Volu } } +func getConfigInfo(cr *xapi.XlineCluster) []corev1.EnvFromSource { + if cr.Spec.BootstrapArgs == nil { + return []corev1.EnvFromSource{} + } + return []corev1.EnvFromSource{ + {ConfigMapRef: &corev1.ConfigMapEnvSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: fmt.Sprintf("%s-config", cr.Name), + }, + }}, + } +} + func MakeService(cr *xapi.XlineCluster, scheme *runtime.Scheme) *corev1.Service { svcLabel := GetXlineInstanceLabels(cr.ObjKey()) service := &corev1.Service{ @@ -88,6 +101,19 @@ func MakeScriptCM(cr *xapi.XlineCluster, scheme *runtime.Scheme) *corev1.ConfigM return cm } +func MakeConfigMap(cr *xapi.XlineCluster, scheme *runtime.Scheme) *corev1.ConfigMap { + cm := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: fmt.Sprintf("%s-config", cr.Name), + Namespace: cr.Namespace, + Labels: GetXlineInstanceLabels(cr.ObjKey()), + }, + Data: cr.Spec.BootArgs(), + } + _ = controllerutil.SetOwnerReference(cr, cm, scheme) + return cm +} + func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.StatefulSet { crName := types.NamespacedName{Namespace: cr.Namespace, Name: cr.Name} stsLabels := GetXlineInstanceLabels(crName) @@ -134,6 +160,7 @@ func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.State }, Command: []string{"/bin/bash", "/usr/local/script/xline_start_script.sh"}, Env: envs, + EnvFrom: getConfigInfo(cr), VolumeMounts: volumeMounts, } diff --git a/tests/e2e/cases/manifests/cluster.yaml b/tests/e2e/cases/manifests/cluster.yaml index f1015d0..f2417fe 100644 --- a/tests/e2e/cases/manifests/cluster.yaml +++ b/tests/e2e/cases/manifests/cluster.yaml @@ -15,4 +15,4 @@ spec: pubKey: auth-jwt.pub priKey: auth-jwt.pri config: - ClientUseBackoff: true + LogLevel: debug