Skip to content

Commit 577ad48

Browse files
committed
Add CommonTemplates field for selective common template injection
Add a CommonTemplates []string field to the Template struct that allows service operators to specify which embedded common templates to include. This avoids injecting common templates (e.g. ssl.conf) into components that don't need them (e.g. cinder-scheduler, cinder-volume). Signed-off-by: Francesco Pantano <fpantano@redhat.com>
1 parent 9cbc91b commit 577ad48

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

modules/common/util/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ var (
2929
ErrTemplateSubdirUnset = errors.New("template subdir not set")
3030
// ErrInstanceTypeUnsetWithMultiTemplateDir indicates InstanceType is empty while MultiTemplateDir is set
3131
ErrInstanceTypeUnsetWithMultiTemplateDir = errors.New("instance type not set")
32+
// ErrCommonTemplateNotFound indicates a requested common template does not exist
33+
ErrCommonTemplateNotFound = errors.New("common template not found")
3234
)

modules/common/util/template_util.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Template struct {
6565
SkipSetOwner bool // skip setting ownership on the associated configmap
6666
Version string // optional version string to separate templates inside the InstanceType/Type directory. E.g. placementapi/config/18.0
6767
MultiTemplateDir string // templates dir for multi-group operators, e.g. nova/api; requires InstanceType to be set
68+
CommonTemplates []string // list of common embedded templates to include (e.g. []string{"ssl.conf"}).
6869
}
6970

7071
// GetTemplatesPath get path to templates, either running local or deployed as container
@@ -298,6 +299,24 @@ func GetTemplateData(t Template) (map[string]string, error) {
298299

299300
data := make(map[string]string)
300301

302+
// Render requested common embedded templates as fallback defaults.
303+
// Note that local operator templates rendered below overwrite the common
304+
// ones: if they share the same filename, service operators definition take
305+
// precedence
306+
if len(t.CommonTemplates) > 0 {
307+
commonData, err := GetCommonTemplates(opts)
308+
if err != nil {
309+
return nil, err
310+
}
311+
for _, name := range t.CommonTemplates {
312+
v, ok := commonData[name]
313+
if !ok {
314+
return nil, fmt.Errorf("%w: %s", ErrCommonTemplateNotFound, name)
315+
}
316+
data[name] = v
317+
}
318+
}
319+
301320
if t.Type != TemplateTypeNone {
302321
// If MultiTemplateDir is set but InstanceType is not, return an error
303322
// though we do not use InstanceType here, but it is used in secret.go

modules/common/util/template_util_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,66 @@ function common_func {
575575
want: map[string]string{},
576576
error: true,
577577
},
578+
{
579+
name: "CommonTemplates are injected",
580+
tmpl: Template{
581+
Name: "testservice",
582+
Namespace: "somenamespace",
583+
Type: TemplateTypeConfig,
584+
InstanceType: "testservice",
585+
CommonTemplates: []string{"ssl.conf"},
586+
ConfigOptions: map[string]any{
587+
"ServiceUser": "foo",
588+
"Count": 1,
589+
"Upper": "BAR",
590+
},
591+
},
592+
want: map[string]string{
593+
"bar.conf": "[DEFAULT]\nstate_path = /var/lib/nova\ndebug=true\nsome_parameter_with_brackets=[test]\ncompute_driver = libvirt.LibvirtDriver\n\n[oslo_concurrency]\nlock_path = /var/lib/nova/tmp\n",
594+
"config.json": "{\n \"command\": \"/usr/sbin/httpd -DFOREGROUND\",\n}\n",
595+
"foo.conf": "username = foo\ncount = 1\nadd = 3\nlower = bar\n",
596+
"ssl.conf": "", // placeholder, replaced below
597+
},
598+
error: false,
599+
},
600+
{
601+
name: "Unknown CommonTemplate returns error",
602+
tmpl: Template{
603+
Name: "testservice",
604+
Namespace: "somenamespace",
605+
Type: TemplateTypeConfig,
606+
InstanceType: "testservice",
607+
CommonTemplates: []string{"sls.conf"},
608+
},
609+
want: nil,
610+
error: true,
611+
},
612+
{
613+
name: "Local service templates take precedence",
614+
tmpl: Template{
615+
Name: "override",
616+
Namespace: "somenamespace",
617+
Type: TemplateTypeConfig,
618+
InstanceType: "override",
619+
CommonTemplates: []string{"ssl.conf"},
620+
ConfigOptions: map[string]any{},
621+
},
622+
want: map[string]string{
623+
"ssl.conf": "# operator-specific ssl.conf override\nSSLProtocol -all +TLSv1.3\n",
624+
},
625+
error: false,
626+
},
627+
}
628+
629+
// Fill in the expected ssl.conf content for the CommonTemplates test
630+
commonTmpls, err := GetCommonTemplates(map[string]any{})
631+
if err != nil {
632+
panic("Failed to get common templates: " + err.Error())
633+
}
634+
for i, tt := range tests {
635+
if v, ok := tt.want["ssl.conf"]; ok && v == "" {
636+
tests[i].want["ssl.conf"] = commonTmpls["ssl.conf"]
637+
}
578638
}
579639

580640
for _, tt := range tests {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# operator-specific ssl.conf override
2+
SSLProtocol -all +TLSv1.3

0 commit comments

Comments
 (0)