@@ -8,8 +8,10 @@ import (
8
8
"context"
9
9
"fmt"
10
10
"io"
11
+ "log"
11
12
"os"
12
13
"path/filepath"
14
+ "text/template"
13
15
14
16
"github.com/open-edge-platform/edge-manageability-framework/installer/internal"
15
17
"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
60
62
{"rke2-config.yaml" , "/etc/rancher/rke2" , "config.yaml" },
61
63
{"rke2-coredns-config.yaml" , "/var/lib/rancher/rke2/server/manifests" , "rke2-coredns-config.yaml" },
62
64
} {
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 {
64
66
return runtimeState , & internal.OrchInstallerError {
65
67
ErrorMsg : fmt .Sprintf ("failed install %s into %s: %s" , entry [0 ], entry [1 ], err ),
66
68
ErrorCode : internal .OrchInstallerErrorCodeInternal ,
67
69
}
68
70
}
69
71
}
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
+ }
70
80
}
71
81
72
82
return runtimeState , nil
@@ -76,7 +86,7 @@ func (s *CustomizeRKE2Step) PostStep(ctx context.Context, config config.OrchInst
76
86
return runtimeState , prevStepError
77
87
}
78
88
79
- func copyAssetFile (assetsDir , srcFile , destDir , destFile string ) error {
89
+ func copyConfig (assetsDir , srcFile , destDir , destFile string ) error {
80
90
srcPath := filepath .Join (assetsDir , srcFile )
81
91
if _ , err := os .Stat (srcPath ); err != nil {
82
92
return fmt .Errorf ("file does not exist at path: %s" , srcPath )
@@ -106,3 +116,23 @@ func copyAssetFile(assetsDir, srcFile, destDir, destFile string) error {
106
116
fmt .Printf ("Copied %s to %s\n " , srcFile , destPath )
107
117
return nil
108
118
}
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
+ }
0 commit comments