@@ -136,15 +136,15 @@ func (r *AgentMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request
136
136
return ctrl.Result {}, r .updateStatus (ctx , log , agentMachine , err )
137
137
}
138
138
139
- machineConfigPool , ignitionTokenSecretRef , err := r .processBootstrapDataSecret (ctx , log , machine )
139
+ machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders , err := r .processBootstrapDataSecret (ctx , log , machine )
140
140
if err != nil {
141
141
return ctrl.Result {}, err
142
142
}
143
143
144
144
// If the AgentMachine doesn't have an agent, find one and set the agentRef
145
145
if agentMachine .Status .AgentRef == nil {
146
146
var foundAgent * aiv1beta1.Agent
147
- foundAgent , err = r .findAgent (ctx , log , agentMachine , agentCluster .Status .ClusterDeploymentRef , machineConfigPool , ignitionTokenSecretRef )
147
+ foundAgent , err = r .findAgent (ctx , log , agentMachine , agentCluster .Status .ClusterDeploymentRef , machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders )
148
148
if foundAgent == nil || err != nil {
149
149
return ctrl.Result {}, r .updateStatus (ctx , log , agentMachine , err )
150
150
}
@@ -257,6 +257,7 @@ func (r *AgentMachineReconciler) handleDeletionHook(ctx context.Context, log log
257
257
delete (agent .ObjectMeta .Annotations , AgentMachineRefNamespace )
258
258
agent .Spec .MachineConfigPool = ""
259
259
agent .Spec .IgnitionEndpointTokenReference = nil
260
+ agent .Spec .IgnitionEndpointHTTPHeaders = nil
260
261
agent .Spec .ClusterDeploymentName = nil
261
262
if err := r .Update (ctx , agent ); err != nil {
262
263
log .WithError (err ).Error ("failed to remove the Agent's ClusterDeployment ref" )
@@ -307,7 +308,7 @@ func (r *AgentMachineReconciler) getAgentCluster(ctx context.Context, log logrus
307
308
308
309
func (r * AgentMachineReconciler ) findAgent (ctx context.Context , log logrus.FieldLogger , agentMachine * capiproviderv1.AgentMachine ,
309
310
clusterDeploymentRef capiproviderv1.ClusterDeploymentReference , machineConfigPool string ,
310
- ignitionTokenSecretRef * aiv1beta1.IgnitionEndpointTokenReference ) (* aiv1beta1.Agent , error ) {
311
+ ignitionTokenSecretRef * aiv1beta1.IgnitionEndpointTokenReference , ignitionEndpointHTTPHeaders map [ string ] string ) (* aiv1beta1.Agent , error ) {
311
312
312
313
foundAgent , err := r .findAgentWithAgentMachineLabel (ctx , log , agentMachine )
313
314
if err != nil {
@@ -343,7 +344,7 @@ func (r *AgentMachineReconciler) findAgent(ctx context.Context, log logrus.Field
343
344
if isValidAgent (& agents .Items [i ]) {
344
345
foundAgent = & agents .Items [i ]
345
346
log .Infof ("Found agent to associate with AgentMachine: %s/%s" , foundAgent .Namespace , foundAgent .Name )
346
- err = r .updateFoundAgent (ctx , log , agentMachine , foundAgent , clusterDeploymentRef , machineConfigPool , ignitionTokenSecretRef )
347
+ err = r .updateFoundAgent (ctx , log , agentMachine , foundAgent , clusterDeploymentRef , machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders )
347
348
if err != nil {
348
349
// If we failed to update the agent then it might have already been taken, try the others
349
350
log .WithError (err ).Infof ("failed to update found agent, trying other agents" )
@@ -412,7 +413,7 @@ func (r *AgentMachineReconciler) updateAgentMachineWithFoundAgent(ctx context.Co
412
413
func (r * AgentMachineReconciler ) updateFoundAgent (ctx context.Context , log logrus.FieldLogger ,
413
414
agentMachine * capiproviderv1.AgentMachine , agent * aiv1beta1.Agent ,
414
415
clusterDeploymentRef capiproviderv1.ClusterDeploymentReference , machineConfigPool string ,
415
- ignitionTokenSecretRef * aiv1beta1.IgnitionEndpointTokenReference ) error {
416
+ ignitionTokenSecretRef * aiv1beta1.IgnitionEndpointTokenReference , ignitionEndpointHTTPHeaders map [ string ] string ) error {
416
417
417
418
log .Infof ("Updating Agent %s/%s to be referenced by AgentMachine" , agent .Namespace , agent .Name )
418
419
if agent .ObjectMeta .Labels == nil {
@@ -426,6 +427,7 @@ func (r *AgentMachineReconciler) updateFoundAgent(ctx context.Context, log logru
426
427
agent .Spec .ClusterDeploymentName = & aiv1beta1.ClusterReference {Namespace : clusterDeploymentRef .Namespace , Name : clusterDeploymentRef .Name }
427
428
agent .Spec .MachineConfigPool = machineConfigPool
428
429
agent .Spec .IgnitionEndpointTokenReference = ignitionTokenSecretRef
430
+ agent .Spec .IgnitionEndpointHTTPHeaders = ignitionEndpointHTTPHeaders
429
431
430
432
if err := r .AgentClient .Update (ctx , agent ); err != nil {
431
433
log .WithError (err ).Errorf ("failed to update found Agent %s" , agent .Name )
@@ -435,22 +437,23 @@ func (r *AgentMachineReconciler) updateFoundAgent(ctx context.Context, log logru
435
437
}
436
438
437
439
func (r * AgentMachineReconciler ) processBootstrapDataSecret (ctx context.Context , log logrus.FieldLogger ,
438
- machine * clusterv1.Machine ) (string , * aiv1beta1.IgnitionEndpointTokenReference , error ) {
440
+ machine * clusterv1.Machine ) (string , * aiv1beta1.IgnitionEndpointTokenReference , map [ string ] string , error ) {
439
441
440
442
machineConfigPool := ""
441
443
var ignitionTokenSecretRef * aiv1beta1.IgnitionEndpointTokenReference
444
+ ignitionEndpointHTTPHeaders := make (map [string ]string )
442
445
443
446
if machine .Spec .Bootstrap .DataSecretName == nil {
444
447
log .Info ("No data secret, continuing" )
445
- return machineConfigPool , ignitionTokenSecretRef , nil
448
+ return machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders , nil
446
449
}
447
450
448
451
// For now we assume that if we have bootstrap data then it is an ignition config containing the ignition source and token.
449
452
bootstrapDataSecret := & corev1.Secret {}
450
453
bootstrapDataSecretRef := types.NamespacedName {Namespace : machine .Namespace , Name : * machine .Spec .Bootstrap .DataSecretName }
451
454
if err := r .Get (ctx , bootstrapDataSecretRef , bootstrapDataSecret ); err != nil {
452
455
log .WithError (err ).Errorf ("Failed to get user-data secret %s" , * machine .Spec .Bootstrap .DataSecretName )
453
- return machineConfigPool , ignitionTokenSecretRef , err
456
+ return machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders , err
454
457
}
455
458
if err := ensureSecretLabel (ctx , r .AgentClient , bootstrapDataSecret ); err != nil {
456
459
log .WithError (err ).Warnf ("Failed to label secret %s/%s for backup" , bootstrapDataSecret .Name , bootstrapDataSecret .Namespace )
@@ -459,28 +462,30 @@ func (r *AgentMachineReconciler) processBootstrapDataSecret(ctx context.Context,
459
462
ignitionConfig := & ignitionapi.Config {}
460
463
if err := json .Unmarshal (bootstrapDataSecret .Data ["value" ], ignitionConfig ); err != nil {
461
464
log .WithError (err ).Errorf ("Failed to unmarshal user-data secret %s" , * machine .Spec .Bootstrap .DataSecretName )
462
- return machineConfigPool , ignitionTokenSecretRef , err
465
+ return machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders , err
463
466
}
464
467
465
468
if len (ignitionConfig .Ignition .Config .Merge ) != 1 {
466
469
log .Errorf ("expected one ignition source in secret %s but found %d" , * machine .Spec .Bootstrap .DataSecretName , len (ignitionConfig .Ignition .Config .Merge ))
467
- return machineConfigPool , ignitionTokenSecretRef , errors .New ("did not find one ignition source as expected" )
470
+ return machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders , errors .New ("did not find one ignition source as expected" )
468
471
}
469
472
470
473
ignitionSource := ignitionConfig .Ignition .Config .Merge [0 ]
471
474
machineConfigPool = (* ignitionSource .Source )[strings .LastIndex ((* ignitionSource .Source ), "/" )+ 1 :]
472
475
473
476
token := ""
474
477
for _ , header := range ignitionSource .HTTPHeaders {
475
- if header .Name != "Authorization" {
476
- continue
477
- }
478
- expectedPrefix := "Bearer "
479
- if ! strings .HasPrefix (* header .Value , expectedPrefix ) {
480
- log .Errorf ("did not find expected prefix for bearer token in user-data secret %s" , * machine .Spec .Bootstrap .DataSecretName )
481
- return machineConfigPool , ignitionTokenSecretRef , errors .New ("did not find expected prefix for bearer token" )
478
+ if header .Name == "Authorization" {
479
+ expectedPrefix := "Bearer "
480
+ if ! strings .HasPrefix (* header .Value , expectedPrefix ) {
481
+ log .Errorf ("did not find expected prefix for bearer token in user-data secret %s" , * machine .Spec .Bootstrap .DataSecretName )
482
+ return machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders , errors .New ("did not find expected prefix for bearer token" )
483
+ }
484
+ token = (* header .Value )[len (expectedPrefix ):]
485
+ } else {
486
+ ignitionEndpointHTTPHeaders [header .Name ] = * header .Value
482
487
}
483
- token = ( * header . Value )[ len ( expectedPrefix ):]
488
+
484
489
}
485
490
486
491
ignitionTokenSecretName := fmt .Sprintf ("agent-%s" , * machine .Spec .Bootstrap .DataSecretName )
@@ -505,10 +510,10 @@ func (r *AgentMachineReconciler) processBootstrapDataSecret(ctx context.Context,
505
510
}
506
511
if err != nil {
507
512
log .WithError (err ).Error ("Failed to create ignitionTokenSecret" )
508
- return machineConfigPool , ignitionTokenSecretRef , err
513
+ return machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders , err
509
514
}
510
515
511
- return machineConfigPool , ignitionTokenSecretRef , nil
516
+ return machineConfigPool , ignitionTokenSecretRef , ignitionEndpointHTTPHeaders , nil
512
517
}
513
518
514
519
func isValidAgent (agent * aiv1beta1.Agent ) bool {
0 commit comments