Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNF-16211: Expose the CT and PR conditions in the provisioning API package #468

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions api/provisioning/v1alpha1/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package v1alpha1

// ConditionType is a string representing the condition's type
type ConditionType string

// The following constants define the different types of conditions that will be set for ClusterTemplate
var CTconditionTypes = struct {
Validated ConditionType
}{
Validated: "ClusterTemplateValidated",
}

// The following constants define the different types of conditions that will be set for ProvisioningRequest
var PRconditionTypes = struct {
Validated ConditionType
HardwareTemplateRendered ConditionType
HardwareProvisioned ConditionType
HardwareNodeConfigApplied ConditionType
HardwareConfigured ConditionType
ClusterInstanceRendered ConditionType
ClusterResourcesCreated ConditionType
ClusterInstanceProcessed ConditionType
ClusterProvisioned ConditionType
ConfigurationApplied ConditionType
UpgradeCompleted ConditionType
}{
Validated: "ProvisioningRequestValidated",
HardwareTemplateRendered: "HardwareTemplateRendered",
HardwareProvisioned: "HardwareProvisioned",
HardwareNodeConfigApplied: "HardwareNodeConfigApplied",
HardwareConfigured: "HardwareConfigured",
ClusterInstanceRendered: "ClusterInstanceRendered",
ClusterResourcesCreated: "ClusterResourcesCreated",
ClusterInstanceProcessed: "ClusterInstanceProcessed",
ClusterProvisioned: "ClusterProvisioned",
ConfigurationApplied: "ConfigurationApplied",
UpgradeCompleted: "UpgradeCompleted",
}

// ConditionReason is a string representing the condition's reason
type ConditionReason string

// The following constants define the different reasons that conditions will be set for ClusterTemplate
var CTconditionReasons = struct {
Completed ConditionReason
Failed ConditionReason
}{
Completed: "Completed",
Failed: "Failed",
}

// The following constants define the different reasons that conditions will be set for ProvisioningRequest
var CRconditionReasons = struct {
NotApplied ConditionReason
ClusterNotReady ConditionReason
Completed ConditionReason
Failed ConditionReason
InProgress ConditionReason
Missing ConditionReason
OutOfDate ConditionReason
TimedOut ConditionReason
Unknown ConditionReason
}{
NotApplied: "NotApplied",
ClusterNotReady: "ClusterNotReady",
Completed: "Completed",
Failed: "Failed",
InProgress: "InProgress",
Missing: "Missing",
OutOfDate: "OutOfDate",
TimedOut: "TimedOut",
Unknown: "Unknown",
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (r *ProvisioningRequest) GetClusterTemplateRef(ctx context.Context, client
if ct.Name == clusterTemplateRefName {
validatedCond := meta.FindStatusCondition(
ct.Status.Conditions,
"ClusterTemplateValidated") // TODO: consider exposing the conditions in the API
string(CTconditionTypes.Validated))
if validatedCond != nil && validatedCond.Status == metav1.ConditionTrue {
return &ct, nil
}
Expand Down
6 changes: 3 additions & 3 deletions api/provisioning/v1alpha1/provisioningrequest_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func (r *ProvisioningRequest) validateCreateOrUpdate(oldPr *ProvisioningRequest)
// Once provisioning has started or reached a final state (Completed or Failed),
// updates to immutable fields in the ClusterInstance input are disallowed,
// with the exception of scaling up/down when Cluster provisioning is completed.
// TODO: consider exposing the conditions in the API.
crProvisionedCond := meta.FindStatusCondition(r.Status.Conditions, "ClusterProvisioned")
if crProvisionedCond != nil && crProvisionedCond.Reason != "Unknown" {
crProvisionedCond := meta.FindStatusCondition(
r.Status.Conditions, string(PRconditionTypes.ClusterProvisioned))
if crProvisionedCond != nil && crProvisionedCond.Reason != string(CRconditionReasons.Unknown) {
oldPrClusterInstanceInput, err := ExtractMatchingInput(
oldPr.Spec.TemplateParameters.Raw, TemplateParamClusterInstance)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/controllers/clustertemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,15 +588,15 @@ func checkSchemaContains(actual, expected map[string]any, currentPath string) er
func (t *clusterTemplateReconcilerTask) updateStatusConditionValidated(ctx context.Context, errMsg string) error {
if errMsg != "" {
utils.SetStatusCondition(&t.object.Status.Conditions,
utils.CTconditionTypes.Validated,
utils.CTconditionReasons.Failed,
provisioningv1alpha1.CTconditionTypes.Validated,
provisioningv1alpha1.CTconditionReasons.Failed,
metav1.ConditionFalse,
errMsg,
)
} else {
utils.SetStatusCondition(&t.object.Status.Conditions,
utils.CTconditionTypes.Validated,
utils.CTconditionReasons.Completed,
provisioningv1alpha1.CTconditionTypes.Validated,
provisioningv1alpha1.CTconditionReasons.Completed,
metav1.ConditionTrue,
"The cluster template validation succeeded",
)
Expand Down
24 changes: 12 additions & 12 deletions internal/controllers/clustertemplate_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ clustertemplate-a-policy-v1-defaultHugepagesSize: "1G"`,
Expect(c.Get(ctx, req.NamespacedName, updatedCT)).To(Succeed())
conditions := updatedCT.Status.Conditions
Expect(conditions).To(HaveLen(1))
Expect(conditions[0].Type).To(Equal(string(utils.CTconditionTypes.Validated)))
Expect(conditions[0].Type).To(Equal(string(provisioningv1alpha1.CTconditionTypes.Validated)))
Expect(conditions[0].Status).To(Equal(metav1.ConditionTrue))
Expect(conditions[0].Reason).To(Equal(string(utils.CTconditionReasons.Completed)))
Expect(conditions[0].Reason).To(Equal(string(provisioningv1alpha1.CTconditionReasons.Completed)))
Expect(conditions[0].Message).To(Equal("The cluster template validation succeeded"))
})

Expand All @@ -156,9 +156,9 @@ clustertemplate-a-policy-v1-defaultHugepagesSize: "1G"`,
Expect(c.Get(ctx, req.NamespacedName, updatedCT)).To(Succeed())
conditions := updatedCT.Status.Conditions
Expect(conditions).To(HaveLen(1))
Expect(conditions[0].Type).To(Equal(string(utils.CTconditionTypes.Validated)))
Expect(conditions[0].Type).To(Equal(string(provisioningv1alpha1.CTconditionTypes.Validated)))
Expect(conditions[0].Status).To(Equal(metav1.ConditionFalse))
Expect(conditions[0].Reason).To(Equal(string(utils.CTconditionReasons.Failed)))
Expect(conditions[0].Reason).To(Equal(string(provisioningv1alpha1.CTconditionReasons.Failed)))
Expect(conditions[0].Message).To(ContainSubstring(fmt.Sprintf(
"the ConfigMap '%s' is not found in the namespace '%s'", ciDefaultsCm, ctNamespace)))
Expect(conditions[0].Message).To(ContainSubstring(fmt.Sprintf(
Expand Down Expand Up @@ -517,9 +517,9 @@ clustertemplate-a-policy-v1-defaultHugepagesSize: "1G"`,
// Check the status condition
conditions := t.object.Status.Conditions
Expect(conditions).To(HaveLen(1))
Expect(conditions[0].Type).To(Equal(string(utils.CTconditionTypes.Validated)))
Expect(conditions[0].Type).To(Equal(string(provisioningv1alpha1.CTconditionTypes.Validated)))
Expect(conditions[0].Status).To(Equal(metav1.ConditionTrue))
Expect(conditions[0].Reason).To(Equal(string(utils.CTconditionReasons.Completed)))
Expect(conditions[0].Reason).To(Equal(string(provisioningv1alpha1.CTconditionReasons.Completed)))
Expect(conditions[0].Message).To(Equal("The cluster template validation succeeded"))
})

Expand All @@ -532,9 +532,9 @@ clustertemplate-a-policy-v1-defaultHugepagesSize: "1G"`,
// Check the status condition
conditions := t.object.Status.Conditions
Expect(conditions).To(HaveLen(1))
Expect(conditions[0].Type).To(Equal(string(utils.CTconditionTypes.Validated)))
Expect(conditions[0].Type).To(Equal(string(provisioningv1alpha1.CTconditionTypes.Validated)))
Expect(conditions[0].Status).To(Equal(metav1.ConditionFalse))
Expect(conditions[0].Reason).To(Equal(string(utils.CTconditionReasons.Failed)))
Expect(conditions[0].Reason).To(Equal(string(provisioningv1alpha1.CTconditionReasons.Failed)))
Expect(conditions[0].Message).To(ContainSubstring(fmt.Sprintf(
"the ConfigMap '%s' is not found in the namespace '%s'", ciDefaultsCm, ctNamespace)))
Expect(conditions[0].Message).To(ContainSubstring(fmt.Sprintf(
Expand All @@ -555,9 +555,9 @@ clustertemplate-a-policy-v1-defaultHugepagesSize: "1G"`,
// Check the status condition
conditions := t.object.Status.Conditions
Expect(conditions).To(HaveLen(1))
Expect(conditions[0].Type).To(Equal(string(utils.CTconditionTypes.Validated)))
Expect(conditions[0].Type).To(Equal(string(provisioningv1alpha1.CTconditionTypes.Validated)))
Expect(conditions[0].Status).To(Equal(metav1.ConditionFalse))
Expect(conditions[0].Reason).To(Equal(string(utils.CTconditionReasons.Failed)))
Expect(conditions[0].Reason).To(Equal(string(provisioningv1alpha1.CTconditionReasons.Failed)))
Expect(conditions[0].Message).To(ContainSubstring(fmt.Sprintf(
"the value of key %s from ConfigMap %s is not a valid duration string", utils.ClusterConfigurationTimeoutConfigKey, ptDefaultsCm)))
Expect(conditions[0].Message).To(ContainSubstring(fmt.Sprintf(
Expand All @@ -576,9 +576,9 @@ clustertemplate-a-policy-v1-defaultHugepagesSize: "1G"`,
conditions := t.object.Status.Conditions
Expect(conditions).To(HaveLen(1))
errMessage := fmt.Sprintf("the value of HardwareProvisioningTimeout from hardware template %s is not a valid duration string", hwtmpl.Name)
Expect(conditions[0].Type).To(Equal(string(utils.CTconditionTypes.Validated)))
Expect(conditions[0].Type).To(Equal(string(provisioningv1alpha1.CTconditionTypes.Validated)))
Expect(conditions[0].Status).To(Equal(metav1.ConditionFalse))
Expect(conditions[0].Reason).To(Equal(string(utils.CTconditionReasons.Failed)))
Expect(conditions[0].Reason).To(Equal(string(provisioningv1alpha1.CTconditionReasons.Failed)))
Expect(conditions[0].Message).To(ContainSubstring(errMessage))

// Check the HardwareTemplate status condition
Expand Down
40 changes: 20 additions & 20 deletions internal/controllers/provisioningrequest_clusterconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (t *provisioningRequestReconcilerTask) updateConfigurationAppliedStatus(
if len(targetPolicies) == 0 {
t.object.Status.Extensions.ClusterDetails.NonCompliantAt = metav1.Time{}
utils.SetStatusCondition(&t.object.Status.Conditions,
utils.PRconditionTypes.ConfigurationApplied,
utils.CRconditionReasons.Missing,
provisioningv1alpha1.PRconditionTypes.ConfigurationApplied,
provisioningv1alpha1.CRconditionReasons.Missing,
metav1.ConditionFalse,
"No configuration present",
)
Expand All @@ -114,8 +114,8 @@ func (t *provisioningRequestReconcilerTask) updateConfigurationAppliedStatus(
if allPoliciesCompliant {
t.object.Status.Extensions.ClusterDetails.NonCompliantAt = metav1.Time{}
utils.SetStatusCondition(&t.object.Status.Conditions,
utils.PRconditionTypes.ConfigurationApplied,
utils.CRconditionReasons.Completed,
provisioningv1alpha1.PRconditionTypes.ConfigurationApplied,
provisioningv1alpha1.CRconditionReasons.Completed,
metav1.ConditionTrue,
"The configuration is up to date",
)
Expand All @@ -140,8 +140,8 @@ func (t *provisioningRequestReconcilerTask) updateConfigurationAppliedStatus(
),
)
utils.SetStatusCondition(&t.object.Status.Conditions,
utils.PRconditionTypes.ConfigurationApplied,
utils.CRconditionReasons.ClusterNotReady,
provisioningv1alpha1.PRconditionTypes.ConfigurationApplied,
provisioningv1alpha1.CRconditionReasons.ClusterNotReady,
metav1.ConditionFalse,
"The Cluster is not yet ready",
)
Expand All @@ -157,26 +157,26 @@ func (t *provisioningRequestReconcilerTask) updateConfigurationAppliedStatus(
// No timeout is computed if all policies are in inform, just out of date.
t.object.Status.Extensions.ClusterDetails.NonCompliantAt = metav1.Time{}
utils.SetStatusCondition(&t.object.Status.Conditions,
utils.PRconditionTypes.ConfigurationApplied,
utils.CRconditionReasons.OutOfDate,
provisioningv1alpha1.PRconditionTypes.ConfigurationApplied,
provisioningv1alpha1.CRconditionReasons.OutOfDate,
metav1.ConditionFalse,
"The configuration is out of date",
)
} else {
policyConfigTimedOut = t.hasPolicyConfigurationTimedOut(ctx)

message := "The configuration is still being applied"
reason := utils.CRconditionReasons.InProgress
reason := provisioningv1alpha1.CRconditionReasons.InProgress
utils.SetProvisioningStateInProgress(t.object,
"Cluster configuration is being applied")
if policyConfigTimedOut {
message += ", but it timed out"
reason = utils.CRconditionReasons.TimedOut
reason = provisioningv1alpha1.CRconditionReasons.TimedOut
utils.SetProvisioningStateFailed(t.object,
"Cluster configuration timed out")
}
utils.SetStatusCondition(&t.object.Status.Conditions,
utils.PRconditionTypes.ConfigurationApplied,
provisioningv1alpha1.PRconditionTypes.ConfigurationApplied,
reason,
metav1.ConditionFalse,
message,
Expand All @@ -189,7 +189,7 @@ func (t *provisioningRequestReconcilerTask) updateConfigurationAppliedStatus(
// updateZTPStatus updates status.ClusterDetails.ZtpStatus.
func (t *provisioningRequestReconcilerTask) updateZTPStatus(ctx context.Context, allPoliciesCompliant bool) error {
// Check if the cluster provision has started.
crProvisionedCond := meta.FindStatusCondition(t.object.Status.Conditions, string(utils.PRconditionTypes.ClusterProvisioned))
crProvisionedCond := meta.FindStatusCondition(t.object.Status.Conditions, string(provisioningv1alpha1.PRconditionTypes.ClusterProvisioned))
if crProvisionedCond != nil {
// If the provisioning has started, and the ZTP status is empty or not done.
if t.object.Status.Extensions.ClusterDetails.ZtpStatus != utils.ClusterZtpDone {
Expand Down Expand Up @@ -254,7 +254,7 @@ func (t *provisioningRequestReconcilerTask) hasPolicyConfigurationTimedOut(ctx c
// Get the ConfigurationApplied condition.
configurationAppliedCondition := meta.FindStatusCondition(
t.object.Status.Conditions,
string(utils.PRconditionTypes.ConfigurationApplied))
string(provisioningv1alpha1.PRconditionTypes.ConfigurationApplied))

// If the condition does not exist, set the non compliant timestamp since we
// get here just for policies that have a status different from Compliant.
Expand All @@ -266,7 +266,7 @@ func (t *provisioningRequestReconcilerTask) hasPolicyConfigurationTimedOut(ctx c
// If the current status of the Condition is false.
if configurationAppliedCondition.Status == metav1.ConditionFalse {
switch configurationAppliedCondition.Reason {
case string(utils.CRconditionReasons.InProgress):
case string(provisioningv1alpha1.CRconditionReasons.InProgress):
// Check if the configuration application has timed out.
if t.object.Status.Extensions.ClusterDetails.NonCompliantAt.IsZero() {
t.object.Status.Extensions.ClusterDetails.NonCompliantAt = metav1.Now()
Expand All @@ -276,13 +276,13 @@ func (t *provisioningRequestReconcilerTask) hasPolicyConfigurationTimedOut(ctx c
t.object.Status.Extensions.ClusterDetails.NonCompliantAt.Time,
t.timeouts.clusterConfiguration)
}
case string(utils.CRconditionReasons.TimedOut):
case string(provisioningv1alpha1.CRconditionReasons.TimedOut):
policyTimedOut = true
case string(utils.CRconditionReasons.Missing):
case string(provisioningv1alpha1.CRconditionReasons.Missing):
t.object.Status.Extensions.ClusterDetails.NonCompliantAt = metav1.Now()
case string(utils.CRconditionReasons.OutOfDate):
case string(provisioningv1alpha1.CRconditionReasons.OutOfDate):
t.object.Status.Extensions.ClusterDetails.NonCompliantAt = metav1.Now()
case string(utils.CRconditionReasons.ClusterNotReady):
case string(provisioningv1alpha1.CRconditionReasons.ClusterNotReady):
// The cluster might not be ready because its being initially provisioned or
// there are problems after provisionion, so it might be that NonCompliantAt
// has been previously set.
Expand All @@ -295,11 +295,11 @@ func (t *provisioningRequestReconcilerTask) hasPolicyConfigurationTimedOut(ctx c
default:
t.logger.InfoContext(ctx,
fmt.Sprintf("Unexpected Reason for condition type %s",
utils.PRconditionTypes.ConfigurationApplied,
provisioningv1alpha1.PRconditionTypes.ConfigurationApplied,
),
)
}
} else if configurationAppliedCondition.Reason == string(utils.CRconditionReasons.Completed) {
} else if configurationAppliedCondition.Reason == string(provisioningv1alpha1.CRconditionReasons.Completed) {
t.object.Status.Extensions.ClusterDetails.NonCompliantAt = metav1.Now()
}

Expand Down
Loading