Skip to content

Commit 27e3255

Browse files
committed
Enhance Status with IgnitionDataFetched and IPXEScriptFetched Conditions
1 parent 1681dd3 commit 27e3255

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

api/v1alpha1/constants.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@ const (
99
SystemUUIDIndexKey = "spec.systemUUID" // Field to index resources by their system UUID.
1010
SystemIPIndexKey = "spec.systemIPs" // Field to index resources by their system IP addresses.
1111
DefaultFormatKey = "format" // Key for determining the format of the data stored in a Secret, such as fcos or plain-ignition.
12-
IgnitionV2Format = "ignitionv2" // Specifies the format value corresponding to Ignition config version 2.
13-
IgnitionV3Format = "ignitionv3" // Specifies the format value corresponding to Ignition config version 3.
1412
FCOSFormat = "fcos" // Specifies the format value used for Fedora CoreOS specific configurations.
1513
)

api/v1alpha1/httpbootconfig_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type HTTPBootConfigSpec struct {
2626
// HTTPBootConfigStatus defines the observed state of HTTPBootConfig
2727
type HTTPBootConfigStatus struct {
2828
State HTTPBootConfigState `json:"state,omitempty"`
29+
30+
// Conditions represent the latest available observations of the IPXEBootConfig's state
31+
Conditions []metav1.Condition `json:"conditions,omitempty"`
2932
}
3033

3134
type HTTPBootConfigState string

api/v1alpha1/ipxebootconfig_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ const (
5858
type IPXEBootConfigStatus struct {
5959
// Important: Run "make" to regenerate code after modifying this file
6060
State IPXEBootConfigState `json:"state,omitempty"`
61+
62+
// Conditions represent the latest available observations of the IPXEBootConfig's state
63+
Conditions []metav1.Condition `json:"conditions,omitempty"`
6164
}
6265

6366
//+kubebuilder:object:root=true

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/bootserver.go

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
3651
func 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

139161
func 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

211240
func 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

313349
func 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

Comments
 (0)