Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: the config template referenced might be empty #8880

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion controllers/apps/configuration/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func checkConfigLabels(object client.Object, requiredLabs []string) bool {

func getConfigMapByTemplateName(cli client.Client, ctx intctrlutil.RequestCtx, templateName, ns string) (*corev1.ConfigMap, error) {
if len(templateName) == 0 {
return nil, fmt.Errorf("required configmap reference name is empty! [%v]", templateName)
return nil, nil
}

configObj := &corev1.ConfigMap{}
Expand Down Expand Up @@ -216,6 +216,9 @@ func updateConfigMapFinalizerImpl(cli client.Client, ctx intctrlutil.RequestCtx,
ctx.Log.Error(err, "failed to get template cm object!", "configMapName", cmObj.Name)
return err
}
if cmObj == nil {
return nil
}

if controllerutil.ContainsFinalizer(cmObj, constant.ConfigFinalizerName) {
return nil
Expand All @@ -241,6 +244,9 @@ func deleteConfigMapFinalizer(cli client.Client, ctx intctrlutil.RequestCtx, con
ctx.Log.Error(err, "failed to get config template cm object!", "configMapName", configSpec.TemplateRef)
return err
}
if cmObj == nil {
return nil
}

if !controllerutil.ContainsFinalizer(cmObj, constant.ConfigFinalizerName) {
return nil
Expand Down
16 changes: 13 additions & 3 deletions pkg/controller/component/synthesize_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func BuildSynthesizedComponent(ctx context.Context, cli client.Reader,
// override componentService
overrideComponentServices(synthesizeComp, comp)

if err = overrideConfigTemplates(synthesizeComp, comp); err != nil {
if err = overrideNCheckConfigTemplates(synthesizeComp, comp); err != nil {
return nil, err
}

Expand Down Expand Up @@ -363,9 +363,9 @@ func overrideComponentServices(synthesizeComp *SynthesizedComponent, comp *appsv
}
}

func overrideConfigTemplates(synthesizedComp *SynthesizedComponent, comp *appsv1.Component) error {
func overrideNCheckConfigTemplates(synthesizedComp *SynthesizedComponent, comp *appsv1.Component) error {
if comp == nil || len(comp.Spec.Configs) == 0 {
return nil
return checkConfigTemplates(synthesizedComp)
}

templates := make(map[string]*appsv1.ComponentConfigSpec)
Expand All @@ -392,10 +392,20 @@ func overrideConfigTemplates(synthesizedComp *SynthesizedComponent, comp *appsv1
return fmt.Errorf("partial overriding is not supported, config template: %s", *config.Name)
case specified():
template.TemplateRef = config.ConfigMap.Name
template.Namespace = synthesizedComp.Namespace
default:
// do nothing
}
}
return checkConfigTemplates(synthesizedComp)
}

func checkConfigTemplates(synthesizedComp *SynthesizedComponent) error {
for _, template := range synthesizedComp.ConfigTemplates {
if len(template.TemplateRef) == 0 {
return fmt.Errorf("required config template is empty: %s", template.Name)
}
}
return nil
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/controller/component/synthesize_component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,22 @@ var _ = Describe("synthesized component", func() {
}
})

It("comp def", func() {
It("ok", func() {
compDef.Spec.Configs[1].TemplateRef = "external"

synthesizedComp, err := BuildSynthesizedComponent(ctx, cli, compDef, comp)
Expect(err).Should(BeNil())

Expect(synthesizedComp).ShouldNot(BeNil())
Expect(synthesizedComp.ConfigTemplates).Should(BeEquivalentTo(compDef.Spec.Configs))
})

It("has config template not specified", func() {
_, err := BuildSynthesizedComponent(ctx, cli, compDef, comp)
Expect(err).ShouldNot(BeNil())
Expect(err.Error()).Should(ContainSubstring("required config template is empty"))
})

It("w/ comp override - ok", func() {
comp.Spec.Configs = append(comp.Spec.Configs, appsv1.ClusterComponentConfig{
Name: func() *string { name := "external"; return &name }(),
Expand All @@ -123,6 +131,7 @@ var _ = Describe("synthesized component", func() {

expectExternalConfig := compDef.Spec.Configs[1]
expectExternalConfig.TemplateRef = comp.Spec.Configs[0].ConfigMap.Name
expectExternalConfig.Namespace = comp.Namespace
Expect(synthesizedComp.ConfigTemplates[1]).Should(BeEquivalentTo(expectExternalConfig))
})

Expand Down