@@ -13,7 +13,6 @@ import (
1313 ctrl "sigs.k8s.io/controller-runtime"
1414 "sigs.k8s.io/controller-runtime/pkg/client"
1515 "sigs.k8s.io/controller-runtime/pkg/handler"
16- "sigs.k8s.io/controller-runtime/pkg/log"
1716 "sigs.k8s.io/controller-runtime/pkg/reconcile"
1817
1918 "github.com/go-logr/logr"
@@ -32,55 +31,53 @@ type HTTPBootConfigReconciler struct {
3231//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
3332
3433func (r * HTTPBootConfigReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
35- log := log .FromContext (ctx )
36-
37- HTTPBootConfig := & bootv1alpha1.HTTPBootConfig {}
38- if err := r .Get (ctx , req .NamespacedName , HTTPBootConfig ); err != nil {
34+ log := ctrl .LoggerFrom (ctx )
35+ config := & bootv1alpha1.HTTPBootConfig {}
36+ if err := r .Get (ctx , req .NamespacedName , config ); err != nil {
3937 return ctrl.Result {}, client .IgnoreNotFound (err )
4038 }
41-
42- return r .reconcileExists (ctx , log , HTTPBootConfig )
39+ return r .reconcileExists (ctx , log , config )
4340}
4441
45- func (r * HTTPBootConfigReconciler ) reconcileExists (ctx context.Context , log logr.Logger , HTTPBootConfig * bootv1alpha1.HTTPBootConfig ) (ctrl.Result , error ) {
46- if ! HTTPBootConfig .DeletionTimestamp .IsZero () {
47- return r .delete (ctx , log , HTTPBootConfig )
42+ func (r * HTTPBootConfigReconciler ) reconcileExists (ctx context.Context , log logr.Logger , config * bootv1alpha1.HTTPBootConfig ) (ctrl.Result , error ) {
43+ if ! config .DeletionTimestamp .IsZero () {
44+ return r .delete (ctx , log , config )
4845 }
49-
50- return r .reconcile (ctx , log , HTTPBootConfig )
46+ return r .reconcile (ctx , log , config )
5147}
5248
53- func (r * HTTPBootConfigReconciler ) reconcile (ctx context.Context , log logr.Logger , HTTPBootConfig * bootv1alpha1.HTTPBootConfig ) (ctrl.Result , error ) {
49+ func (r * HTTPBootConfigReconciler ) reconcile (ctx context.Context , log logr.Logger , config * bootv1alpha1.HTTPBootConfig ) (ctrl.Result , error ) {
50+ log .V (1 ).Info ("Reconciling HTTPBootConfig" )
51+
5452 log .V (1 ).Info ("Ensuring Ignition" )
55- state , ignitionErr := r .ensureIgnition (ctx , log , HTTPBootConfig )
56- if ignitionErr != nil {
57- patchError := r .patchStatus (ctx , HTTPBootConfig , state )
58- if patchError != nil {
59- return ctrl.Result {}, fmt .Errorf ("failed to patch status %w %w" , ignitionErr , patchError )
53+ state , err := r .ensureIgnition (ctx , log , config )
54+ if err != nil {
55+ if err := r .patchStatus (ctx , config , state ); err != nil {
56+ return ctrl.Result {}, err
6057 }
61-
62- log .V (1 ).Info ("Failed to Ensure Ignition" , "Error" , ignitionErr )
58+ log .V (1 ).Info ("Failed to Ensure Ignition" , "Error" , err )
6359 return ctrl.Result {}, nil
6460 }
61+ log .V (1 ).Info ("Ensured Ignition" )
6562
66- patchErr := r .patchStatus (ctx , HTTPBootConfig , state )
67- if patchErr != nil {
68- return ctrl.Result {}, fmt .Errorf ("failed to patch status %w" , patchErr )
63+ if err := r .patchStatus (ctx , config , state ); err != nil {
64+ return ctrl.Result {}, err
6965 }
7066
67+ log .V (1 ).Info ("Reconciled HTTPBootConfig" )
7168 return ctrl.Result {}, nil
7269}
7370
74- func (r * HTTPBootConfigReconciler ) ensureIgnition (ctx context.Context , _ logr.Logger , HTTPBootConfig * bootv1alpha1.HTTPBootConfig ) (bootv1alpha1.HTTPBootConfigState , error ) {
71+ func (r * HTTPBootConfigReconciler ) ensureIgnition (ctx context.Context , _ logr.Logger , config * bootv1alpha1.HTTPBootConfig ) (bootv1alpha1.HTTPBootConfigState , error ) {
7572 // Verify if the IgnitionRef is set, and it has the intended data key.
76- if HTTPBootConfig .Spec .IgnitionSecretRef != nil {
77- IgnitionSecret := & corev1.Secret {}
78- if err := r .Get (ctx , client.ObjectKey {Name : HTTPBootConfig .Spec .IgnitionSecretRef .Name , Namespace : HTTPBootConfig .Namespace }, IgnitionSecret ); err != nil {
73+ if config .Spec .IgnitionSecretRef != nil {
74+ ignitionSecret := & corev1.Secret {}
75+ if err := r .Get (ctx , client.ObjectKey {Name : config .Spec .IgnitionSecretRef .Name , Namespace : config .Namespace }, ignitionSecret ); err != nil {
7976 return bootv1alpha1 .HTTPBootConfigStateError , err
80- // TODO: Add some validation steps to ensure that the IgntionData is compliant, if necessary.
77+ // TODO: Add some validation steps to ensure that the IgnitionData is compliant, if necessary.
8178 // Assume for now, that it's going to json format.
8279 }
83- if IgnitionSecret .Data [bootv1alpha1 .DefaultIgnitionKey ] == nil {
80+ if ignitionSecret .Data [bootv1alpha1 .DefaultIgnitionKey ] == nil {
8481 return bootv1alpha1 .HTTPBootConfigStateError , fmt .Errorf ("ignition data is missing" )
8582 }
8683 }
@@ -93,64 +90,61 @@ func (r *HTTPBootConfigReconciler) delete(_ context.Context, log logr.Logger, _
9390
9491 // TODO
9592
93+ log .V (1 ).Info ("Deleted HTTPBootConfig" )
9694 return ctrl.Result {}, nil
9795}
9896
99- // SetupWithManager sets up the controller with the Manager.
100- func (r * HTTPBootConfigReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
101- return ctrl .NewControllerManagedBy (mgr ).
102- For (& bootv1alpha1.HTTPBootConfig {}).
103- Watches (
104- & corev1.Secret {},
105- handler .EnqueueRequestsFromMapFunc (r .enqueueHTTPBootConfigReferencingIgnitionSecret ),
106- ).
107- Complete (r )
108- }
109-
110- func (r * HTTPBootConfigReconciler ) patchStatus (
111- ctx context.Context ,
112- HTTPBootConfig * bootv1alpha1.HTTPBootConfig ,
113- state bootv1alpha1.HTTPBootConfigState ,
114- ) error {
115- if HTTPBootConfig .Status .State == state {
97+ func (r * HTTPBootConfigReconciler ) patchStatus (ctx context.Context , config * bootv1alpha1.HTTPBootConfig , state bootv1alpha1.HTTPBootConfigState ) error {
98+ if config .Status .State == state {
11699 return nil
117100 }
118101
119- base := HTTPBootConfig .DeepCopy ()
120- HTTPBootConfig .Status .State = state
102+ base := config .DeepCopy ()
103+ config .Status .State = state
121104
122- if err := r .Status ().Patch (ctx , HTTPBootConfig , client .MergeFrom (base )); err != nil {
123- return fmt . Errorf ( "error patching HTTPBootConfig: %w" , err )
105+ if err := r .Status ().Patch (ctx , config , client .MergeFrom (base )); err != nil {
106+ return err
124107 }
125108 return nil
126109}
127110
128111func (r * HTTPBootConfigReconciler ) enqueueHTTPBootConfigReferencingIgnitionSecret (ctx context.Context , secret client.Object ) []reconcile.Request {
129- log := log . Log . WithValues ( "secret" , secret . GetName () )
112+ log := ctrl . LoggerFrom ( ctx )
130113 secretObj , ok := secret .(* corev1.Secret )
131114 if ! ok {
132115 log .Error (nil , "cant decode object into Secret" , secret )
133116 return nil
134117 }
135118
136- list := & bootv1alpha1.HTTPBootConfigList {}
137- if err := r .List (ctx , list , client .InNamespace ("" )); err != nil {
138- log .Error (err , "failed to list HTTPBootConfig for secret " , secret )
119+ configList := & bootv1alpha1.HTTPBootConfigList {}
120+ if err := r .List (ctx , configList , client .InNamespace ("" )); err != nil {
121+ log .Error (err , "failed to list HTTPBootConfig for Secret " , "Secret" , client . ObjectKeyFromObject ( secretObj ) )
139122 return nil
140123 }
141124
142125 var requests []reconcile.Request
143- for _ , HTTPBootConfig := range list .Items {
144- if HTTPBootConfig .Spec .IgnitionSecretRef != nil &&
145- HTTPBootConfig .Spec .IgnitionSecretRef .Name == secretObj .Name &&
146- HTTPBootConfig .Namespace == secretObj .Namespace {
126+ for _ , config := range configList .Items {
127+ if config .Spec .IgnitionSecretRef != nil &&
128+ config .Spec .IgnitionSecretRef .Name == secretObj .Name &&
129+ config .Namespace == secretObj .Namespace {
147130 requests = append (requests , reconcile.Request {
148131 NamespacedName : types.NamespacedName {
149- Name : HTTPBootConfig .Name ,
150- Namespace : HTTPBootConfig .Namespace ,
132+ Name : config .Name ,
133+ Namespace : config .Namespace ,
151134 },
152135 })
153136 }
154137 }
155138 return requests
156139}
140+
141+ // SetupWithManager sets up the controller with the Manager.
142+ func (r * HTTPBootConfigReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
143+ return ctrl .NewControllerManagedBy (mgr ).
144+ For (& bootv1alpha1.HTTPBootConfig {}).
145+ Watches (
146+ & corev1.Secret {},
147+ handler .EnqueueRequestsFromMapFunc (r .enqueueHTTPBootConfigReferencingIgnitionSecret ),
148+ ).
149+ Complete (r )
150+ }
0 commit comments