Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
d2285 committed Jan 28, 2025
1 parent b24166e commit 174bb36
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions internal/platform/cmd/module/enable/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func enableModule(cmd *cobra.Command, moduleName []string) error {
if err != nil {
return fmt.Errorf("Failed to setup Kubernetes client: %w", err)
}

err = operatemodule.OperateModule(config, moduleName[0], true)
if err != nil {
return fmt.Errorf("Error enable module: %w", err)
Expand Down
25 changes: 16 additions & 9 deletions internal/platform/cmd/module/operatemodule/switchmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import (
"k8s.io/utils/ptr"
)

func OperateModule(config *rest.Config, name string, enabled bool) error {
type ModuleState bool

const (
ModuleEnabled ModuleState = true
ModuleDisabled ModuleState = false
)

func OperateModule(config *rest.Config, name string, moduleState ModuleState) error {
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return fmt.Errorf("Failed to create dynamic client: %v", err)
Expand All @@ -33,18 +40,18 @@ func OperateModule(config *rest.Config, name string, enabled bool) error {
if err != nil {
//fmt.Errorf("Error get options module '%s': %w", name, err)
}
patchSpec, err := patchSpec(enabled)
patchSpec, err := patchSpec(moduleState)
if customResource != nil {
if err = unstructured.SetNestedField(customResource.Object, enabled, "spec", "enabled"); err != nil {
return fmt.Errorf("failed to change spec.enabled to %v in the '%s' module config: %w", enabled, name, err)
if err = unstructured.SetNestedField(customResource.Object, moduleState, "spec", "enabled"); err != nil {
return fmt.Errorf("failed to change spec.enabled to %v in the '%s' module config: %w", moduleState, name, err)
}
if _, err = resourceClient.Patch(context.TODO(), name, types.MergePatchType, patchSpec, metav1.PatchOptions{}); err != nil {
return fmt.Errorf("failed to update the '%s' module config: %w", name, err)
}
return nil
}

obj, err := createModuleConfig(name, enabled)
obj, err := createModuleConfig(name, moduleState)
if err != nil {
return fmt.Errorf("failed to convert the '%s' module config: %w", name, err)
}
Expand All @@ -54,7 +61,7 @@ func OperateModule(config *rest.Config, name string, enabled bool) error {
return err
}

func createModuleConfig(name string, enabled bool) (*unstructured.Unstructured, error) {
func createModuleConfig(name string, moduleState ModuleState) (*unstructured.Unstructured, error) {
newCfg := &v1alpha1.ModuleConfigMeta{
TypeMeta: metav1.TypeMeta{
Kind: "ModuleConfig",
Expand All @@ -64,17 +71,17 @@ func createModuleConfig(name string, enabled bool) (*unstructured.Unstructured,
Name: name,
},
Spec: v1alpha1.ModuleConfigSpec{
Enabled: ptr.To(enabled),
Enabled: (*bool)(ptr.To(moduleState)),
},
}
content, err := runtime.DefaultUnstructuredConverter.ToUnstructured(newCfg)
return &unstructured.Unstructured{Object: content}, err
}

func patchSpec(enabled bool) ([]byte, error) {
func patchSpec(moduleState ModuleState) ([]byte, error) {
patchData := map[string]interface{}{
"spec": map[string]interface{}{
"enabled": enabled,
"enabled": moduleState,
},
}

Expand Down

0 comments on commit 174bb36

Please sign in to comment.