Skip to content

Commit

Permalink
refactor: use configmap to specify the bootup args
Browse files Browse the repository at this point in the history
Signed-off-by: Phoeniix Zhao <[email protected]>
  • Loading branch information
Phoenix500526 committed Feb 16, 2024
1 parent 9650010 commit 2937643
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
13 changes: 5 additions & 8 deletions api/v1alpha1/xlinecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/reconciler/cluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions internal/transformer/xlinecluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/cases/manifests/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ spec:
pubKey: auth-jwt.pub
priKey: auth-jwt.pri
config:
ClientUseBackoff: true
LogLevel: debug

0 comments on commit 2937643

Please sign in to comment.