Skip to content

Commit

Permalink
chore: remove kbcli config subcommands dependence on clusterdefinitio…
Browse files Browse the repository at this point in the history
…n/clusterversion/componentdefinition (#277) (#279)
  • Loading branch information
sophon-zt authored Mar 4, 2024
1 parent 91b8150 commit b426fed
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 401 deletions.
53 changes: 0 additions & 53 deletions pkg/cluster/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,56 +432,3 @@ func GetDefaultVersionByCompDefs(dynamic dynamic.Interface, compDefs []string) (
}
return cv, nil
}

func GetComponentDefinitionByName(dynamic dynamic.Interface, compDef string) (*appsv1alpha1.ComponentDefinition, error) {
result := &appsv1alpha1.ComponentDefinition{}
if err := util.GetK8SClientObject(dynamic, result, types.CompDefGVR(), "", compDef); err != nil {
return nil, err
}
return result, nil
}

func GetComponetByName(dynamic dynamic.Interface, component, namespace string) (*appsv1alpha1.Component, error) {
result := &appsv1alpha1.Component{}
if err := util.GetK8SClientObject(dynamic, result, types.ComponentGVR(), namespace, component); err != nil {
return nil, err
}
return result, nil
}

func GetComponentNameByClusterName(clusterName, compSpecName string) string {
return fmt.Sprintf("%s-%s", clusterName, compSpecName)
}

func GetConfigTemplateListByCompDefs(clusterCompSpecs []appsv1alpha1.ClusterComponentSpec, compDefs []*appsv1alpha1.ComponentDefinition, compName string, reloadTpl bool) ([]appsv1alpha1.ComponentConfigSpec, error) {
var (
compDefName = ""
configSpecs []appsv1alpha1.ComponentConfigSpec
)

for _, spec := range clusterCompSpecs {
if spec.Name == compName {
compDefName = spec.ComponentDef
break
}
}
for i, compDef := range compDefs {
if compDef.Name == compDefName {
configSpecs = compDefs[i].Spec.Configs
}
}
if configSpecs == nil {
return nil, fmt.Errorf("fail to get the config template from componentdefinition %s", compDefName)
}
if !reloadTpl || len(configSpecs) == 1 {
return configSpecs, nil
}

validConfigSpecs := make([]appsv1alpha1.ComponentConfigSpec, 0, len(configSpecs))
for _, configSpec := range configSpecs {
if configSpec.ConfigConstraintRef != "" && configSpec.TemplateRef != "" {
validConfigSpecs = append(validConfigSpecs, configSpec)
}
}
return validConfigSpecs, nil
}
2 changes: 1 addition & 1 deletion pkg/cmd/cluster/config_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (o *configOpsOptions) Complete() error {
}
}

wrapper, err := newConfigWrapper(o.CreateOptions, o.Name, o.ComponentName, o.CfgTemplateName, o.CfgFile, o.KeyValues)
wrapper, err := newConfigWrapper(o.CreateOptions, o.ComponentName, o.CfgTemplateName, o.CfgFile, o.KeyValues)
if err != nil {
return err
}
Expand Down
22 changes: 18 additions & 4 deletions pkg/cmd/cluster/config_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericiooptions"
clientfake "k8s.io/client-go/rest/fake"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/controller/builder"
testapps "github.com/apecloud/kubeblocks/pkg/testutil/apps"

"github.com/apecloud/kbcli/pkg/testing"
Expand Down Expand Up @@ -95,13 +95,27 @@ var _ = Describe("reconfigure test", func() {
clusterVersionObj := testapps.NewClusterVersionFactory(clusterVersionName, clusterDefObj.GetName()).
AddComponentVersion(statefulCompDefName).
GetObject()
By("Create a configuration obj")
configObj := builder.NewConfigurationBuilder(ns, cfgcore.GenerateComponentConfigurationName(clusterName, statefulCompName)).
ClusterRef(clusterName).
Component(statefulCompName).
AddConfigurationItem(appsv1alpha1.ComponentConfigSpec{
ComponentTemplateSpec: appsv1alpha1.ComponentTemplateSpec{
Name: configSpecName,
TemplateRef: configmap.Name,
Namespace: ns,
VolumeName: configVolumeName,
},
ConfigConstraintRef: constraint.Name,
}).
GetObject()
By("creating a cluster")
clusterObj := testapps.NewClusterFactory(ns, clusterName,
clusterDefObj.Name, "").
AddComponent(statefulCompName, statefulCompDefName).GetObject()

objs := []runtime.Object{configmap, constraint, clusterDefObj, clusterVersionObj, clusterObj, componentConfig}
ttf, ops := NewFakeOperationsOptions(ns, clusterObj.Name, appsv1alpha1.ReconfiguringType, objs...)
objs := []runtime.Object{configmap, constraint, clusterDefObj, clusterVersionObj, clusterObj, componentConfig, configObj}
ttf, ops := NewFakeOperationsOptions(ns, clusterObj.Name, objs...)
o := &configOpsOptions{
// nil cannot be set to a map struct in CueLang, so init the map of KeyValues.
OperationsOptions: &OperationsOptions{
Expand Down
Loading

0 comments on commit b426fed

Please sign in to comment.