@@ -17,9 +17,11 @@ limitations under the License.
1717package controllers
1818
1919import (
20+ "bytes"
2021 "context"
2122 "errors"
2223 "fmt"
24+ "html/template"
2325 "time"
2426
2527 "github.com/go-logr/logr"
@@ -249,23 +251,24 @@ func (r *KThreesConfigReconciler) joinControlplane(ctx context.Context, scope *S
249251 }
250252
251253 if scope .Config .Spec .IsEtcdEmbedded () {
252- etcdProxyFile := bootstrapv1.File {
253- Path : etcd .EtcdProxyDaemonsetYamlLocation ,
254- Content : etcd .EtcdProxyDaemonsetYaml ,
255- Owner : "root:root" ,
256- Permissions : "0640" ,
254+ etcdProxyFile , err := r .resolveEtcdProxyFile (scope .Config )
255+ if err != nil {
256+ conditions .MarkFalse (scope .Config , bootstrapv1 .DataSecretAvailableCondition , bootstrapv1 .DataSecretGenerationFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
257+ return fmt .Errorf ("failed to resolve etcd proxy file: %w" , err )
257258 }
258- files = append (files , etcdProxyFile )
259+
260+ files = append (files , * etcdProxyFile )
259261 }
260262
261263 cpInput := & cloudinit.ControlPlaneInput {
262264 BaseUserData : cloudinit.BaseUserData {
263- PreK3sCommands : scope .Config .Spec .PreK3sCommands ,
264- PostK3sCommands : scope .Config .Spec .PostK3sCommands ,
265- AdditionalFiles : files ,
266- ConfigFile : workerConfigFile ,
267- K3sVersion : scope .Config .Spec .Version ,
268- AirGapped : scope .Config .Spec .AgentConfig .AirGapped ,
265+ PreK3sCommands : scope .Config .Spec .PreK3sCommands ,
266+ PostK3sCommands : scope .Config .Spec .PostK3sCommands ,
267+ AdditionalFiles : files ,
268+ ConfigFile : workerConfigFile ,
269+ K3sVersion : scope .Config .Spec .Version ,
270+ AirGapped : scope .Config .Spec .AgentConfig .AirGapped ,
271+ AirGappedInstallScriptPath : scope .Config .Spec .AgentConfig .AirGappedInstallScriptPath ,
269272 },
270273 }
271274
@@ -320,12 +323,13 @@ func (r *KThreesConfigReconciler) joinWorker(ctx context.Context, scope *Scope)
320323
321324 winput := & cloudinit.WorkerInput {
322325 BaseUserData : cloudinit.BaseUserData {
323- PreK3sCommands : scope .Config .Spec .PreK3sCommands ,
324- PostK3sCommands : scope .Config .Spec .PostK3sCommands ,
325- AdditionalFiles : files ,
326- ConfigFile : workerConfigFile ,
327- K3sVersion : scope .Config .Spec .Version ,
328- AirGapped : scope .Config .Spec .AgentConfig .AirGapped ,
326+ PreK3sCommands : scope .Config .Spec .PreK3sCommands ,
327+ PostK3sCommands : scope .Config .Spec .PostK3sCommands ,
328+ AdditionalFiles : files ,
329+ ConfigFile : workerConfigFile ,
330+ K3sVersion : scope .Config .Spec .Version ,
331+ AirGapped : scope .Config .Spec .AgentConfig .AirGapped ,
332+ AirGappedInstallScriptPath : scope .Config .Spec .AgentConfig .AirGappedInstallScriptPath ,
329333 },
330334 }
331335
@@ -380,6 +384,38 @@ func (r *KThreesConfigReconciler) resolveSecretFileContent(ctx context.Context,
380384 return data , nil
381385}
382386
387+ func (r * KThreesConfigReconciler ) resolveEtcdProxyFile (cfg * bootstrapv1.KThreesConfig ) (* bootstrapv1.File , error ) {
388+ // Parse the template
389+ tpl , err := template .New ("etcd-proxy" ).Parse (etcd .EtcdProxyDaemonsetYamlTemplate )
390+ if err != nil {
391+ return nil , fmt .Errorf ("failed to parse etcd-proxy template: %w" , err )
392+ }
393+
394+ // If user has set the systemDefaultRegistry, will prefix the image with it.
395+ systemDefaultRegistry := cfg .Spec .ServerConfig .SystemDefaultRegistry
396+ if systemDefaultRegistry != "" {
397+ systemDefaultRegistry = fmt .Sprintf ("%s/" , systemDefaultRegistry )
398+ }
399+
400+ // Render the template, the image name will be ${EtcdProxyImage} if the user
401+ // has set it, otherwise it will be ${SystemDefaultRegistry}alpine/socat
402+ var buf bytes.Buffer
403+ err = tpl .Execute (& buf , map [string ]string {
404+ "EtcdProxyImage" : cfg .Spec .ServerConfig .EtcdProxyImage ,
405+ "SystemDefaultRegistry" : systemDefaultRegistry ,
406+ })
407+ if err != nil {
408+ return nil , fmt .Errorf ("failed to render etcd-proxy template: %w" , err )
409+ }
410+
411+ return & bootstrapv1.File {
412+ Path : etcd .EtcdProxyDaemonsetYamlLocation ,
413+ Content : buf .String (),
414+ Owner : "root:root" ,
415+ Permissions : "0640" ,
416+ }, nil
417+ }
418+
383419func (r * KThreesConfigReconciler ) handleClusterNotInitialized (ctx context.Context , scope * Scope ) (_ ctrl.Result , reterr error ) {
384420 // initialize the DataSecretAvailableCondition if missing.
385421 // this is required in order to avoid the condition's LastTransitionTime to flicker in case of errors surfacing
@@ -465,23 +501,23 @@ func (r *KThreesConfigReconciler) handleClusterNotInitialized(ctx context.Contex
465501 }
466502
467503 if scope .Config .Spec .IsEtcdEmbedded () {
468- etcdProxyFile := bootstrapv1.File {
469- Path : etcd .EtcdProxyDaemonsetYamlLocation ,
470- Content : etcd .EtcdProxyDaemonsetYaml ,
471- Owner : "root:root" ,
472- Permissions : "0640" ,
504+ etcdProxyFile , err := r .resolveEtcdProxyFile (scope .Config )
505+ if err != nil {
506+ conditions .MarkFalse (scope .Config , bootstrapv1 .DataSecretAvailableCondition , bootstrapv1 .DataSecretGenerationFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
507+ return ctrl.Result {}, fmt .Errorf ("failed to resolve etcd proxy file: %w" , err )
473508 }
474- files = append (files , etcdProxyFile )
509+ files = append (files , * etcdProxyFile )
475510 }
476511
477512 cpinput := & cloudinit.ControlPlaneInput {
478513 BaseUserData : cloudinit.BaseUserData {
479- PreK3sCommands : scope .Config .Spec .PreK3sCommands ,
480- PostK3sCommands : scope .Config .Spec .PostK3sCommands ,
481- AdditionalFiles : files ,
482- ConfigFile : initConfigFile ,
483- K3sVersion : scope .Config .Spec .Version ,
484- AirGapped : scope .Config .Spec .AgentConfig .AirGapped ,
514+ PreK3sCommands : scope .Config .Spec .PreK3sCommands ,
515+ PostK3sCommands : scope .Config .Spec .PostK3sCommands ,
516+ AdditionalFiles : files ,
517+ ConfigFile : initConfigFile ,
518+ K3sVersion : scope .Config .Spec .Version ,
519+ AirGapped : scope .Config .Spec .AgentConfig .AirGapped ,
520+ AirGappedInstallScriptPath : scope .Config .Spec .AgentConfig .AirGappedInstallScriptPath ,
485521 },
486522 Certificates : certificates ,
487523 }
0 commit comments