@@ -17,9 +17,11 @@ limitations under the License.
17
17
package controllers
18
18
19
19
import (
20
+ "bytes"
20
21
"context"
21
22
"errors"
22
23
"fmt"
24
+ "html/template"
23
25
"time"
24
26
25
27
"github.com/go-logr/logr"
@@ -249,23 +251,24 @@ func (r *KThreesConfigReconciler) joinControlplane(ctx context.Context, scope *S
249
251
}
250
252
251
253
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 )
257
258
}
258
- files = append (files , etcdProxyFile )
259
+
260
+ files = append (files , * etcdProxyFile )
259
261
}
260
262
261
263
cpInput := & cloudinit.ControlPlaneInput {
262
264
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 ,
269
272
},
270
273
}
271
274
@@ -320,12 +323,13 @@ func (r *KThreesConfigReconciler) joinWorker(ctx context.Context, scope *Scope)
320
323
321
324
winput := & cloudinit.WorkerInput {
322
325
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 ,
329
333
},
330
334
}
331
335
@@ -380,6 +384,38 @@ func (r *KThreesConfigReconciler) resolveSecretFileContent(ctx context.Context,
380
384
return data , nil
381
385
}
382
386
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
+
383
419
func (r * KThreesConfigReconciler ) handleClusterNotInitialized (ctx context.Context , scope * Scope ) (_ ctrl.Result , reterr error ) {
384
420
// initialize the DataSecretAvailableCondition if missing.
385
421
// 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
465
501
}
466
502
467
503
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 )
473
508
}
474
- files = append (files , etcdProxyFile )
509
+ files = append (files , * etcdProxyFile )
475
510
}
476
511
477
512
cpinput := & cloudinit.ControlPlaneInput {
478
513
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 ,
485
521
},
486
522
Certificates : certificates ,
487
523
}
0 commit comments