Skip to content

Commit

Permalink
chore: refine ut
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Nov 14, 2024
1 parent b81487f commit 22a29b8
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 183 deletions.
26 changes: 26 additions & 0 deletions controllers/apps/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
appsv1beta1 "github.com/apecloud/kubeblocks/apis/apps/v1beta1"
dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
opsv1alpha1 "github.com/apecloud/kubeblocks/apis/operations/v1alpha1"
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
workloadsv1 "github.com/apecloud/kubeblocks/apis/workloads/v1"
"github.com/apecloud/kubeblocks/controllers/dataprotection"
"github.com/apecloud/kubeblocks/controllers/k8score"
Expand Down Expand Up @@ -150,6 +151,10 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
model.AddScheme(workloadsv1.AddToScheme)

err = parametersv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
model.AddScheme(parametersv1alpha1.AddToScheme)

// +kubebuilder:scaffold:rscheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expand Down Expand Up @@ -244,6 +249,27 @@ var _ = BeforeSuite(func() {
}).SetupWithManager(k8sManager, nil)
Expect(err).ToNot(HaveOccurred())

err = (&parameters.ComponentDrivenParameterReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Recorder: k8sManager.GetEventRecorderFor("component-parameter-controller"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&parameters.ParametersDefinitionReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Recorder: k8sManager.GetEventRecorderFor("parameters-definition-controller"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&parameters.ParameterDrivenConfigRenderReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Recorder: k8sManager.GetEventRecorderFor("parameter-driven-config-render-controller"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&dataprotection.BackupPolicyTemplateReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Expand Down
5 changes: 5 additions & 0 deletions controllers/apps/transformer_component_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (t *componentTLSTransformer) Transform(ctx graph.TransformContext, dag *gra
transCtx, _ := ctx.(*componentTransformContext)
synthesizedComp := transCtx.SynthesizeComponent

if model.IsObjectDeleting(transCtx.ComponentOrig) {
return nil
}

// update podSpec tls volume and volumeMount
if err := updateTLSVolumeAndVolumeMount(synthesizedComp.PodSpec, synthesizedComp.ClusterName, *synthesizedComp); err != nil {
return err
Expand All @@ -62,6 +66,7 @@ func (t *componentTLSTransformer) Transform(ctx graph.TransformContext, dag *gra
return err
}

// TODO: removed
if err := checkAndTriggerReRender(transCtx.Context, *synthesizedComp, t.Client); err != nil {
return err
}
Expand Down
38 changes: 25 additions & 13 deletions controllers/apps/transformer_component_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
appsv1beta1 "github.com/apecloud/kubeblocks/apis/apps/v1beta1"
cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
Expand All @@ -41,6 +40,7 @@ import (
"github.com/apecloud/kubeblocks/pkg/generics"
testapps "github.com/apecloud/kubeblocks/pkg/testutil/apps"
testk8s "github.com/apecloud/kubeblocks/pkg/testutil/k8s"
testparameters "github.com/apecloud/kubeblocks/pkg/testutil/parameters"
)

var _ = Describe("TLS self-signed cert function", func() {
Expand All @@ -50,6 +50,8 @@ var _ = Describe("TLS self-signed cert function", func() {
serviceKind = "mysql"
defaultCompName = "mysql"
configTemplateName = "mysql-config-tpl"
paramsDef = "mysql-pd"
pdcrName = "mysql-pd"
)

var (
Expand All @@ -67,13 +69,18 @@ var _ = Describe("TLS self-signed cert function", func() {
// create the new objects.
By("clean resources")

inNs := client.InNamespace(testCtx.DefaultNamespace)
hasLabels := client.HasLabels{testCtx.TestObjLabelKey}

// delete cluster(and all dependent sub-resources), cluster definition
testapps.ClearClusterResourcesWithRemoveFinalizerOption(&testCtx)

// delete rest configurations
testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.ConfigMapSignature, true, inNs, hasLabels)
ml := client.HasLabels{testCtx.TestObjLabelKey}
// non-namespaced
testapps.ClearResources(&testCtx, generics.ConfigConstraintSignature, ml)
testapps.ClearResources(&testCtx, generics.ParameterDrivenConfigRenderSignature, ml)
testapps.ClearResources(&testCtx, generics.ParametersDefinitionSignature, ml)
testapps.ClearResources(&testCtx, generics.BackupPolicyTemplateSignature, ml)
}

Expand All @@ -83,26 +90,31 @@ var _ = Describe("TLS self-signed cert function", func() {

Context("tls is enabled/disabled", func() {
BeforeEach(func() {
configMapObj := testapps.CheckedCreateCustomizedObj(&testCtx,
"resources/mysql-tls-config-template.yaml",
&corev1.ConfigMap{},
testCtx.UseDefaultNamespace(),
testapps.WithAnnotations(constant.CMInsEnableRerenderTemplateKey, "true"))
configMapObj := testparameters.NewComponentTemplateFactory(configTemplateName, testCtx.DefaultNamespace).
Create(&testCtx).
GetObject()

configConstraintObj := testapps.CheckedCreateCustomizedObj(&testCtx,
"resources/mysql-config-constraint.yaml",
&appsv1beta1.ConfigConstraint{})
paramsdef := testparameters.NewParametersDefinitionFactory(paramsDef).
SetReloadAction(testparameters.WithNoneAction()).
Create(&testCtx).
GetObject()

By("Create a componentDefinition obj")
compDefObj = testapps.NewComponentDefinitionFactory(compDefName).
WithRandomName().
AddAnnotations(constant.SkipImmutableCheckAnnotationKey, "true").
SetDefaultSpec().
SetServiceKind(serviceKind).
AddConfigTemplate(configTemplateName, configMapObj.Name, configConstraintObj.Name, testCtx.DefaultNamespace, testapps.ConfVolumeName).
AddConfigTemplate(configTemplateName, configMapObj.Name, testCtx.DefaultNamespace, testapps.ConfVolumeName).
AddEnv(testapps.DefaultMySQLContainerName, corev1.EnvVar{Name: "MYSQL_ALLOW_EMPTY_PASSWORD", Value: "yes"}).
Create(&testCtx).
GetObject()

testparameters.NewParametersDrivenConfigFactory(pdcrName).
SetParametersDefs(paramsdef.Name).
SetComponentDefinition(compDefObj.GetName()).
SetTemplateName(configTemplateName).
Create(&testCtx)
})

Context("when issuer is UserProvided", func() {
Expand Down Expand Up @@ -215,8 +227,8 @@ var _ = Describe("TLS self-signed cert function", func() {
Expect(k8sClient.Patch(ctx, clusterObj, patch)).Should(Succeed())
Eventually(hasTLSSettings).Should(BeTrue())

testapps.DeleteObject(&testCtx, clusterKey, &appsv1.Cluster{})
Eventually(testapps.CheckObjExists(&testCtx, clusterKey, &appsv1.Cluster{}, false)).Should(Succeed())
// testapps.DeleteObject(&testCtx, clusterKey, &appsv1.Cluster{})
// Eventually(testapps.CheckObjExists(&testCtx, clusterKey, &appsv1.Cluster{}, false)).Should(Succeed())
})
})

Expand Down
5 changes: 5 additions & 0 deletions controllers/operations/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
appsv1beta1 "github.com/apecloud/kubeblocks/apis/apps/v1beta1"
dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
opsv1alpha1 "github.com/apecloud/kubeblocks/apis/operations/v1alpha1"
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
workloadsv1 "github.com/apecloud/kubeblocks/apis/workloads/v1"
"github.com/apecloud/kubeblocks/controllers/apps"
"github.com/apecloud/kubeblocks/controllers/dataprotection"
Expand Down Expand Up @@ -151,6 +152,10 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
model.AddScheme(workloadsv1.AddToScheme)

err = parametersv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
model.AddScheme(parametersv1alpha1.AddToScheme)

// +kubebuilder:scaffold:rscheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expand Down
18 changes: 17 additions & 1 deletion controllers/parameters/componentdrivenparameter_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (r *ComponentDrivenParameterReconciler) create(reqCtx intctrlutil.RequestCt
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "")
}
reqCtx.Log.Info("ComponentParameter created")
intctrlutil.RecordCreatedEvent(r.Recorder, object)
return intctrlutil.Reconciled()
}

Expand Down Expand Up @@ -204,7 +205,9 @@ func buildComponentParameter(reqCtx intctrlutil.RequestCtx, reader client.Reader
if err = intctrlutil.SetOwnerReference(comp, parameterObj); err != nil {
return nil, err
}
_, err = configctrl.UpdateConfigPayload(&parameterObj.Spec, &comp.Spec, &configRender.Spec)
if configRender != nil {
_, err = configctrl.UpdateConfigPayload(&parameterObj.Spec, &comp.Spec, &configRender.Spec)
}
return parameterObj, err
}

Expand All @@ -225,5 +228,18 @@ func (r *ComponentDrivenParameterReconciler) mergeComponentParameter(expected *p
if len(dest.ConfigFileParams) == 0 && len(expected.ConfigFileParams) != 0 {
dest.ConfigFileParams = expected.ConfigFileParams
}
payload := expected.Payload
// TODO: removed tls
if tls, ok := dest.Payload.Data[constant.TLSPayload]; ok {
mergeTLSPayload(&payload, tls)
}
dest.Payload = payload
})
}

func mergeTLSPayload(payload *parametersv1alpha1.Payload, tls interface{}) {
if payload.Data == nil {
payload.Data = make(map[string]interface{})
}
payload.Data[constant.TLSPayload] = tls
}
54 changes: 12 additions & 42 deletions controllers/parameters/componentparameter_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (

"sigs.k8s.io/controller-runtime/pkg/client"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/configuration/core"
cfgutil "github.com/apecloud/kubeblocks/pkg/configuration/util"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
testapps "github.com/apecloud/kubeblocks/pkg/testutil/apps"
)

Expand All @@ -47,30 +48,21 @@ var _ = Describe("ComponentParameter Controller", func() {
Name: core.GenerateComponentParameterName(clusterName, defaultCompName),
Namespace: testCtx.DefaultNamespace,
}
checkCfgStatus := func(phase appsv1alpha1.ConfigurationPhase) func() bool {
checkCfgStatus := func(phase parametersv1alpha1.ParameterPhase) func() bool {
return func() bool {
cfg := &appsv1alpha1.Configuration{}
cfg := &parametersv1alpha1.ComponentParameter{}
Expect(k8sClient.Get(ctx, cfgKey, cfg)).Should(Succeed())
itemStatus := cfg.Status.GetItemStatus(configSpecName)
itemStatus := intctrlutil.GetItemStatus(&cfg.Status, configSpecName)
return itemStatus != nil && itemStatus.Phase == phase
}
}

By("wait for configuration status to be init phase.")
Eventually(checkCfgStatus(appsv1alpha1.CInitPhase)).Should(BeFalse())
// Expect(initConfiguration(&configctrl.ResourceCtx{
// Client: k8sClient,
// Context: ctx,
// Namespace: testCtx.DefaultNamespace,
// ClusterName: clusterName,
// ComponentName: defaultCompName,
// }, synthesizedComp, clusterObj, componentObj)).Should(Succeed())

Eventually(checkCfgStatus(appsv1alpha1.CFinishedPhase)).Should(BeTrue())
Eventually(checkCfgStatus(parametersv1alpha1.CFinishedPhase)).Should(BeTrue())

By("reconfiguring parameters.")
Eventually(testapps.GetAndChangeObj(&testCtx, cfgKey, func(cfg *appsv1alpha1.Configuration) {
cfg.Spec.GetConfigurationItem(configSpecName).ConfigFileParams = map[string]appsv1alpha1.ConfigParams{
Eventually(testapps.GetAndChangeObj(&testCtx, cfgKey, func(cfg *parametersv1alpha1.ComponentParameter) {
item := intctrlutil.GetConfigTemplateItem(&cfg.Spec, configSpecName)
item.ConfigFileParams = map[string]parametersv1alpha1.ParametersInFile{
"my.cnf": {
Parameters: map[string]*string{
"max_connections": cfgutil.ToPointer("1000"),
Expand All @@ -81,37 +73,15 @@ var _ = Describe("ComponentParameter Controller", func() {
})).Should(Succeed())

Eventually(func(g Gomega) {
cfg := &appsv1alpha1.Configuration{}
cfg := &parametersv1alpha1.ComponentParameter{}
g.Expect(k8sClient.Get(ctx, cfgKey, cfg)).Should(Succeed())
itemStatus := cfg.Status.GetItemStatus(configSpecName)
itemStatus := intctrlutil.GetItemStatus(&cfg.Status, configSpecName)
g.Expect(itemStatus).ShouldNot(BeNil())
g.Expect(itemStatus.UpdateRevision).Should(BeEquivalentTo("2"))
g.Expect(itemStatus.Phase).Should(BeEquivalentTo(appsv1alpha1.CFinishedPhase))
g.Expect(itemStatus.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
}, time.Second*60, time.Second*1).Should(Succeed())
})

It("Invalid component test", func() {
mockReconcileResource()

cfgKey := client.ObjectKey{
Name: core.GenerateComponentParameterName(clusterName, "invalid-component"),
Namespace: testCtx.DefaultNamespace,
}

// Expect(initConfiguration(&configctrl.ResourceCtx{
// Client: k8sClient,
// Context: ctx,
// Namespace: testCtx.DefaultNamespace,
// ClusterName: clusterName,
// ComponentName: "invalid-component",
// }, synthesizedComp, clusterObj, componentObj)).Should(Succeed())

Eventually(func(g Gomega) {
cfg := &appsv1alpha1.Configuration{}
g.Expect(k8sClient.Get(ctx, cfgKey, cfg)).Should(Succeed())
g.Expect(cfg.Status.Message).Should(ContainSubstring("not found cluster component"))
}, time.Second*60, time.Second*1).Should(Succeed())
})
})

// Context("When updating configuration with injectEnvTo", func() {
Expand Down
Loading

0 comments on commit 22a29b8

Please sign in to comment.