@@ -33,6 +33,21 @@ type IPXETemplateData struct {
3333 IPXEServerURL string
3434}
3535
36+ var predefinedConditions = map [string ]v1.Condition {
37+ "IgnitionDataFetched" : {
38+ Type : "IgnitionDataFetched" ,
39+ Status : v1 .ConditionTrue ,
40+ Reason : "IgnitionDataDelivered" ,
41+ Message : "Ignition data has been successfully delivered to the client." ,
42+ },
43+ "IPXEScriptFetched" : {
44+ Type : "IPXEScriptFetched" ,
45+ Status : v1 .ConditionTrue ,
46+ Reason : "IPXEScriptDelivered" ,
47+ Message : "IPXE script has been successfully delivered to the client." ,
48+ },
49+ }
50+
3651func RunBootServer (ipxeServerAddr string , ipxeServiceURL string , k8sClient client.Client , log logr.Logger , defaultUKIURL string ) {
3752 http .HandleFunc ("/ipxe/" , func (w http.ResponseWriter , r * http.Request ) {
3853 handleIPXE (w , r , k8sClient , log , ipxeServiceURL )
@@ -134,6 +149,13 @@ func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client,
134149 SquashfsURL : config .Spec .SquashfsURL ,
135150 IPXEServerURL : ipxeServiceURL ,
136151 })
152+
153+ go func () {
154+ err = SetStatusCondition (ctx , k8sClient , log , & config , "IPXEScriptFetched" )
155+ if err != nil {
156+ log .Info ("Failed to set IPXEScriptFetched status condition" , "error" , err )
157+ }
158+ }()
137159}
138160
139161func handleIgnitionIPXEBoot (w http.ResponseWriter , r * http.Request , k8sClient client.Client , log logr.Logger , uuid string ) {
@@ -142,7 +164,7 @@ func handleIgnitionIPXEBoot(w http.ResponseWriter, r *http.Request, k8sClient cl
142164
143165 ipxeBootConfigList := & bootv1alpha1.IPXEBootConfigList {}
144166 if err := k8sClient .List (ctx , ipxeBootConfigList , client.MatchingFields {bootv1alpha1 .SystemUUIDIndexKey : uuid }); err != nil {
145- http .Error (w , "Resource Not Found " , http .StatusNotFound )
167+ http .Error (w , "Internal Server Error " , http .StatusInternalServerError )
146168 log .Info ("Failed to find IPXEBootConfig" , "error" , err .Error ())
147169 return
148170 }
@@ -206,6 +228,13 @@ func handleIgnitionIPXEBoot(w http.ResponseWriter, r *http.Request, k8sClient cl
206228 http .Error (w , "Internal Server Error" , http .StatusInternalServerError )
207229 return
208230 }
231+
232+ go func () {
233+ err = SetStatusCondition (ctx , k8sClient , log , & ipxeBootConfig , "IgnitionDataFetched" )
234+ if err != nil {
235+ log .Info ("Failed to set IgnitionDataFetched status condition" , "error" , err )
236+ }
237+ }()
209238}
210239
211240func serveDefaultIPXETemplate (w http.ResponseWriter , log logr.Logger , data IPXETemplateData ) {
@@ -308,6 +337,13 @@ func handleIgnitionHTTPBoot(w http.ResponseWriter, r *http.Request, k8sClient cl
308337 http .Error (w , "Internal Server Error" , http .StatusInternalServerError )
309338 return
310339 }
340+
341+ go func () {
342+ err = SetStatusCondition (ctx , k8sClient , log , & httpBootConfig , "IgnitionDataFetched" )
343+ if err != nil {
344+ log .Info ("Failed to set IgnitionDataFetched status condition" , "error" , err )
345+ }
346+ }()
311347}
312348
313349func fetchIgnitionData (ctx context.Context , k8sClient client.Client , ignitionSecret corev1.Secret ) ([]byte , string , error ) {
@@ -412,3 +448,41 @@ func handleHTTPBoot(w http.ResponseWriter, r *http.Request, k8sClient client.Cli
412448 http .Error (w , "Internal Server Error" , http .StatusInternalServerError )
413449 }
414450}
451+
452+ func SetStatusCondition (ctx context.Context , k8sClient client.Client , log logr.Logger , obj client.Object , conditionType string ) error {
453+ condition , exists := predefinedConditions [conditionType ]
454+ if ! exists {
455+ log .Error (fmt .Errorf ("condition type not found" ), "Invalid condition type" , "conditionType" , conditionType )
456+ return fmt .Errorf ("condition type %s not found" , conditionType )
457+ }
458+
459+ switch resource := obj .(type ) {
460+ case * bootv1alpha1.IPXEBootConfig :
461+ resource .Status .Conditions = updateCondition (resource .Status .Conditions , condition )
462+ if err := k8sClient .Status ().Patch (ctx , resource , client .MergeFrom (resource .DeepCopy ())); err != nil {
463+ log .Error (err , "Failed to set the condition in the IPXEBootConfig status" )
464+ return err
465+ }
466+ case * bootv1alpha1.HTTPBootConfig :
467+ resource .Status .Conditions = updateCondition (resource .Status .Conditions , condition )
468+ if err := k8sClient .Status ().Patch (ctx , resource , client .MergeFrom (resource .DeepCopy ())); err != nil {
469+ log .Error (err , "Failed to set the condition in the HTTPBootConfig status" )
470+ return err
471+ }
472+ default :
473+ log .Error (fmt .Errorf ("unsupported resource type" ), "Failed to set the condition" )
474+ return fmt .Errorf ("unsupported resource type" )
475+ }
476+
477+ return nil
478+ }
479+
480+ func updateCondition (conditions []v1.Condition , newCondition v1.Condition ) []v1.Condition {
481+ for i , condition := range conditions {
482+ if condition .Type == newCondition .Type {
483+ conditions [i ] = newCondition
484+ return conditions
485+ }
486+ }
487+ return append (conditions , newCondition )
488+ }
0 commit comments