Skip to content

Commit bb52a79

Browse files
committed
Add render config function
1 parent 264d481 commit bb52a79

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

new-installer/internal/steps/onprem/customize_rke2.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"context"
99
"fmt"
1010
"io"
11+
"log"
1112
"os"
1213
"path/filepath"
14+
"text/template"
1315

1416
"github.com/open-edge-platform/edge-manageability-framework/installer/internal"
1517
"github.com/open-edge-platform/edge-manageability-framework/installer/internal/config"
@@ -60,13 +62,21 @@ func (s *CustomizeRKE2Step) RunStep(ctx context.Context, config config.OrchInsta
6062
{"rke2-config.yaml", "/etc/rancher/rke2", "config.yaml"},
6163
{"rke2-coredns-config.yaml", "/var/lib/rancher/rke2/server/manifests", "rke2-coredns-config.yaml"},
6264
} {
63-
if err := copyAssetFile(s.AssetsDir, entry[0], entry[1], entry[2]); err != nil {
65+
if err := copyConfig(s.AssetsDir, entry[0], entry[1], entry[2]); err != nil {
6466
return runtimeState, &internal.OrchInstallerError{
6567
ErrorMsg: fmt.Sprintf("failed install %s into %s: %s", entry[0], entry[1], err),
6668
ErrorCode: internal.OrchInstallerErrorCodeInternal,
6769
}
6870
}
6971
}
72+
73+
// Render the
74+
if err := renderConfig("/var/lib/rancher/rke2/server/manifests/rke2-coredns-config.yaml", map[string]string{"Namespace": "kube-system"}); err != nil {
75+
return runtimeState, &internal.OrchInstallerError{
76+
ErrorMsg: fmt.Sprintf("failed to render rke2-config.yaml: %s", err),
77+
ErrorCode: internal.OrchInstallerErrorCodeInternal,
78+
}
79+
}
7080
}
7181

7282
return runtimeState, nil
@@ -76,7 +86,7 @@ func (s *CustomizeRKE2Step) PostStep(ctx context.Context, config config.OrchInst
7686
return runtimeState, prevStepError
7787
}
7888

79-
func copyAssetFile(assetsDir, srcFile, destDir, destFile string) error {
89+
func copyConfig(assetsDir, srcFile, destDir, destFile string) error {
8090
srcPath := filepath.Join(assetsDir, srcFile)
8191
if _, err := os.Stat(srcPath); err != nil {
8292
return fmt.Errorf("file does not exist at path: %s", srcPath)
@@ -106,3 +116,23 @@ func copyAssetFile(assetsDir, srcFile, destDir, destFile string) error {
106116
fmt.Printf("Copied %s to %s\n", srcFile, destPath)
107117
return nil
108118
}
119+
120+
func renderConfig(file string, data interface{}) error {
121+
tmpl, err := template.ParseFiles(file)
122+
if err != nil {
123+
return fmt.Errorf("parsing template: %w", err)
124+
}
125+
126+
f, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, 0)
127+
if err != nil {
128+
return fmt.Errorf("opening file: %w", err)
129+
}
130+
defer f.Close()
131+
132+
if err := tmpl.Execute(f, data); err != nil {
133+
return fmt.Errorf("executing template: %w", err)
134+
}
135+
136+
log.Printf("Rendered config file in place: %s\n", file)
137+
return nil
138+
}

new-installer/targets/onprem/rke2/rke2-coredns-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: helm.cattle.io/v1
22
kind: HelmChartConfig
33
metadata:
44
name: rke2-coredns
5-
namespace: kube-system
5+
namespace: {{ .Namespace }}
66
spec:
77
valuesContent: |-
88
global:

0 commit comments

Comments
 (0)