@@ -17,6 +17,7 @@ limitations under the License.
1717package v1alpha1
1818
1919import (
20+ "github.com/SovereignCloudStack/cluster-stack-operator/pkg/clusteraddon"
2021 corev1 "k8s.io/api/core/v1"
2122 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -27,6 +28,52 @@ const (
2728 ClusterAddonFinalizer = "clusteraddon.clusterstack.x-k8s.io"
2829)
2930
31+ // StageAnnotation is the annotation name and key for the stage annotation.
32+ const StageAnnotation = "ClusterAddonStage"
33+
34+ // StageAnnotationValue is the value of the stage annotation.
35+ type StageAnnotationValue string
36+
37+ const (
38+ // StageAnnotationValueCreated signifies the stage annotation created.
39+ StageAnnotationValueCreated = StageAnnotationValue ("created" )
40+ // StageAnnotationValueUpgraded signifies the stage annotation upgraded.
41+ StageAnnotationValueUpgraded = StageAnnotationValue ("upgraded" )
42+ )
43+
44+ // StagePhase defines the status of helm chart in the cluster addon.
45+ type StagePhase string
46+
47+ var (
48+ // StagePhaseNone signifies the empty stage phase.
49+ StagePhaseNone = StagePhase ("" )
50+ // StagePhasePending signifies the stage phase 'pending'.
51+ StagePhasePending = StagePhase ("Pending" )
52+ // StagePhaseWaitingForPreCondition signifies the stage phase 'waitingForPreCondition'.
53+ StagePhaseWaitingForPreCondition = StagePhase ("waitingForPreCondition" )
54+ // StagePhaseApplyingOrDeleting signifies the stage phase 'applyingOrDeleting'.
55+ StagePhaseApplyingOrDeleting = StagePhase ("applyingOrDeleting" )
56+ // StagePhaseWaitingForPostCondition signifies the stage phase 'waitingForPostCondition'.
57+ StagePhaseWaitingForPostCondition = StagePhase ("waitingForPostCondition" )
58+ // StagePhaseDone signifies the stage phase 'done'.
59+ StagePhaseDone = StagePhase ("done" )
60+ )
61+
62+ // StageStatus represents the helm charts of the hook and it's phases.
63+ type StageStatus struct {
64+ // Name represent name of the helm chart
65+ // +optional
66+ Name string `json:"name"`
67+
68+ // Action is the action of the helm chart. e.g. - apply and delete.
69+ // +optional
70+ Action clusteraddon.Action `json:"action,omitempty"`
71+
72+ // Phase is the current phase of the helm chart.
73+ // +optional
74+ Phase StagePhase `json:"phase"`
75+ }
76+
3077// ClusterAddonSpec defines the desired state of a ClusterAddon object.
3178type ClusterAddonSpec struct {
3279 // ClusterStack is the full string <provider>-<name>-<Kubernetes version>-<version> that will be filled with the cluster stack that
@@ -38,6 +85,10 @@ type ClusterAddonSpec struct {
3885 // +optional
3986 Version string `json:"version,omitempty"`
4087
88+ // Hook specifies the runtime hook for the Cluster event.
89+ // +optional
90+ Hook string `json:"hook,omitempty"`
91+
4192 // ClusterRef is the reference to the clusterv1.Cluster object that corresponds to the workload cluster where this
4293 // controller applies the cluster addons.
4394 ClusterRef * corev1.ObjectReference `json:"clusterRef"`
@@ -49,6 +100,10 @@ type ClusterAddonStatus struct {
49100 // +optional
50101 Resources []* Resource `json:"resources,omitempty"`
51102
103+ // Stages shows the state of all stages in the current running hook.
104+ // +optional
105+ Stages []StageStatus `json:"stages,omitempty"`
106+
52107 // +optional
53108 // +kubebuilder:default:=false
54109 Ready bool `json:"ready"`
@@ -61,6 +116,7 @@ type ClusterAddonStatus struct {
61116// +kubebuilder:object:root=true
62117// +kubebuilder:subresource:status
63118// +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".metadata.ownerReferences[?(@.kind==\"Cluster\")].name"
119+ // +kubebuilder:printcolumn:name="Hook",type="string",JSONPath=".spec.hook",description="Present running hook"
64120// +kubebuilder:printcolumn:name="Ready",type="boolean",JSONPath=".status.ready"
65121// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Cluster Addon"
66122// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].reason"
@@ -76,6 +132,48 @@ type ClusterAddon struct {
76132 Status ClusterAddonStatus `json:"status,omitempty"`
77133}
78134
135+ // GetStagePhase returns helm chart status for the helm chart.
136+ func (r * ClusterAddon ) GetStagePhase (stageName string , action clusteraddon.Action ) StagePhase {
137+ for _ , stage := range r .Status .Stages {
138+ if stage .Name == stageName && stage .Action == action {
139+ return stage .Phase
140+ }
141+ }
142+
143+ // This cannot occur as we populate phase value with "pending".
144+ return StagePhaseNone
145+ }
146+
147+ // SetStagePhase sets the helm chart status phase.
148+ func (r * ClusterAddon ) SetStagePhase (stageName string , action clusteraddon.Action , phase StagePhase ) {
149+ for i := range r .Status .Stages {
150+ if r .Status .Stages [i ].Name == stageName && r .Status .Stages [i ].Action == action {
151+ r .Status .Stages [i ].Phase = phase
152+ }
153+ }
154+ }
155+
156+ // SetStageAnnotations sets the annotation whether the cluster got created or upgraded.
157+ func (r * ClusterAddon ) SetStageAnnotations (value StageAnnotationValue ) {
158+ if r .Annotations == nil {
159+ r .Annotations = make (map [string ]string , 0 )
160+ }
161+ _ , found := r .Annotations [StageAnnotation ]
162+ if ! found {
163+ r .Annotations [StageAnnotation ] = string (value )
164+ }
165+ }
166+
167+ // HasStageAnnotation returns whether the stage annotation exists with a certain value.
168+ func (r * ClusterAddon ) HasStageAnnotation (value StageAnnotationValue ) bool {
169+ val , found := r .Annotations [StageAnnotation ]
170+ if found && val == string (value ) {
171+ return true
172+ }
173+
174+ return false
175+ }
176+
79177// GetConditions returns the observations of the operational state of the ClusterAddon resource.
80178func (r * ClusterAddon ) GetConditions () clusterv1.Conditions {
81179 return r .Status .Conditions
0 commit comments