Skip to content
Closed
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
44 changes: 44 additions & 0 deletions api/v1alpha1/common_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CommonStatus defines the common status fields that can be shared across different CRs
type CommonStatus struct {
// Phase represents the current phase of the resource deployment
// +kubebuilder:validation:Enum=Pending;Progressing;Ready;Failed
Phase string `json:"phase,omitempty"`

// Conditions represent the latest available observations of the resource's current state
// +patchMergeKey=type
// +patchStrategy=merge
// +listType=map
// +listMapKey=type
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
}

// Common phase constants
const (
PhasePending = "Pending"
PhaseProgressing = "Progressing"
PhaseReady = "Ready"
PhaseFailed = "Failed"
)

// Common condition types
const (
ConditionTypeReady = "Ready"
ConditionTypeProgressing = "Progressing"
)

// Common condition reasons
const (
ReasonReady = "Ready"
ReasonNotReady = "NotReady"
ReasonProgressing = "Progressing"
ReasonReconciling = "Reconciling"
ReasonReconcileError = "ReconcileError"
ReasonConfigurationError = "ConfigurationError"
ReasonResourceError = "ResourceError"
)
13 changes: 13 additions & 0 deletions api/v1alpha1/scalityui_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ type AuthConfig struct {
type ScalityUIStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Embed CommonStatus to inherit phase and conditions
CommonStatus `json:",inline"`
}

// GetCommonStatus returns a pointer to the embedded CommonStatus
func (s *ScalityUIStatus) GetCommonStatus() *CommonStatus {
return &s.CommonStatus
}

// SetCommonStatus updates the embedded CommonStatus
func (s *ScalityUIStatus) SetCommonStatus(status CommonStatus) {
s.CommonStatus = status
}

// +kubebuilder:object:root=true
Expand Down
35 changes: 29 additions & 6 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions config/crd/bases/ui.scality.com_scalityuis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,76 @@ spec:
type: object
status:
description: ScalityUIStatus defines the observed state of ScalityUI
properties:
conditions:
description: Conditions represent the latest available observations
of the resource's current state
items:
description: Condition contains details for one aspect of the current
state of this API Resource.
properties:
lastTransitionTime:
description: |-
lastTransitionTime is the last time the condition transitioned from one status to another.
This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: |-
message is a human readable message indicating details about the transition.
This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: |-
observedGeneration represents the .metadata.generation that the condition was set based upon.
For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date
with respect to the current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: |-
reason contains a programmatic identifier indicating the reason for the condition's last transition.
Producers of specific condition types may define expected values and meanings for this field,
and whether the values are considered a guaranteed API.
The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
phase:
description: Phase represents the current phase of the resource deployment
enum:
- Pending
- Progressing
- Ready
- Failed
type: string
type: object
type: object
served: true
Expand Down
120 changes: 120 additions & 0 deletions internal/controller/common_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package controller

import (
"context"
"time"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

uiscalitycomv1alpha1 "github.com/scality/ui-operator/api/v1alpha1"
)

// StatusAware interface defines methods that resources with status should implement
// This interface is in the controller package to avoid controller-gen issues
type StatusAware interface {
GetCommonStatus() *uiscalitycomv1alpha1.CommonStatus
SetCommonStatus(status uiscalitycomv1alpha1.CommonStatus)
}

// CommonStatusReconciler provides generic status reconciliation functionality
type CommonStatusReconciler struct {
client.Client
}

// NewCommonStatusReconciler creates a new CommonStatusReconciler
func NewCommonStatusReconciler(client client.Client) *CommonStatusReconciler {
return &CommonStatusReconciler{
Client: client,
}
}

// UpdateResourceStatus updates the status of a resource
func (r *CommonStatusReconciler) UpdateResourceStatus(ctx context.Context, obj client.Object) error {
return r.Status().Update(ctx, obj)
}

// UpdateResourceConditions updates the conditions of a StatusAware resource
func (r *CommonStatusReconciler) UpdateResourceConditions(statusAware StatusAware,
conditionUpdates []ConditionUpdate) {

now := metav1.NewTime(time.Now())
commonStatus := statusAware.GetCommonStatus()

for _, update := range conditionUpdates {
r.setResourceCondition(commonStatus, update.Type, update.Status, update.Reason, update.Message, now)
}
}

// UpdateResourcePhase updates the phase of a StatusAware resource based on its conditions
func (r *CommonStatusReconciler) UpdateResourcePhase(statusAware StatusAware) {
commonStatus := statusAware.GetCommonStatus()
readyCondition := meta.FindStatusCondition(commonStatus.Conditions, uiscalitycomv1alpha1.ConditionTypeReady)
progressingCondition := meta.FindStatusCondition(commonStatus.Conditions, uiscalitycomv1alpha1.ConditionTypeProgressing)

if readyCondition != nil && readyCondition.Status == metav1.ConditionTrue {
commonStatus.Phase = uiscalitycomv1alpha1.PhaseReady
} else if progressingCondition != nil && progressingCondition.Status == metav1.ConditionTrue {
commonStatus.Phase = uiscalitycomv1alpha1.PhaseProgressing
} else if readyCondition != nil && readyCondition.Status == metav1.ConditionFalse {
if readyCondition.Reason == uiscalitycomv1alpha1.ReasonReconcileError ||
readyCondition.Reason == uiscalitycomv1alpha1.ReasonConfigurationError {
commonStatus.Phase = uiscalitycomv1alpha1.PhaseFailed
} else {
commonStatus.Phase = uiscalitycomv1alpha1.PhaseProgressing
}
} else {
commonStatus.Phase = uiscalitycomv1alpha1.PhasePending
}
}

// setResourceCondition sets a condition on a CommonStatus
func (r *CommonStatusReconciler) setResourceCondition(commonStatus *uiscalitycomv1alpha1.CommonStatus,
conditionType string, status metav1.ConditionStatus, reason, message string, now metav1.Time) {

condition := metav1.Condition{
Type: conditionType,
Status: status,
LastTransitionTime: now,
Reason: reason,
Message: message,
// Note: ObservedGeneration will be set by the specific controller
}

meta.SetStatusCondition(&commonStatus.Conditions, condition)
}

// SetResourceConditionWithError is a helper to set error conditions on any StatusAware resource
func (r *CommonStatusReconciler) SetResourceConditionWithError(statusAware StatusAware,
conditionType, reason string, err error, observedGeneration int64) {

now := metav1.NewTime(time.Now())
message := "Operation successful"
status := metav1.ConditionTrue

if err != nil {
message = err.Error()
status = metav1.ConditionFalse
}

commonStatus := statusAware.GetCommonStatus()
condition := metav1.Condition{
Type: conditionType,
Status: status,
LastTransitionTime: now,
Reason: reason,
Message: message,
ObservedGeneration: observedGeneration,
}

meta.SetStatusCondition(&commonStatus.Conditions, condition)
}

// ConditionUpdate represents a condition update to be applied
type ConditionUpdate struct {
Type string
Status metav1.ConditionStatus
Reason string
Message string
}
Loading