Skip to content

Commit de2bcd1

Browse files
committed
Add phase
1 parent 1078e91 commit de2bcd1

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

api/v1alpha1/rolloutscaledown_types.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,31 @@ import (
2323
// RolloutScaleDownSpec defines the desired state of RolloutScaleDown
2424
type RolloutScaleDownSpec struct {
2525
// Foo is an example field of RolloutScaleDown. Edit rolloutscaledown_types.go to remove/update
26-
TargetRollout string `json:"targetRollout,omitempty"`
27-
TerminatePerOnce int `json:"terminatePerOnce,1"`
28-
CoolTimeSeconds int `json:"coolTimeSeconds,30"`
26+
TargetRollout string `json:"targetRollout,omitempty"`
27+
// +kubebuilder:default=1
28+
TerminatePerOnce int `json:"terminatePerOnce"`
29+
// +kubebuilder:default=30
30+
CoolTimeSeconds int `json:"coolTimeSeconds"`
2931
}
3032

3133
// RolloutScaleDownStatus defines the observed state of RolloutScaleDown
3234
type RolloutScaleDownStatus struct {
35+
// +optional
3336
LastScaleDownTime metav1.Time `json:"lastScaleDownTime,omitempty"`
37+
// +kubebuilder:default=Healthy
38+
Phase RolloutScaleDownPhase `json:"phase,omitempty"`
3439
}
3540

36-
//+kubebuilder:object:root=true
37-
//+kubebuilder:subresource:status
41+
type RolloutScaleDownPhase string
42+
43+
const (
44+
RolloutScaleDownPhaseHealthy RolloutScaleDownPhase = "Healthy"
45+
RolloutScaleDownPhaseScaling RolloutScaleDownPhase = "Scaling"
46+
)
47+
48+
// +kubebuilder:object:root=true
49+
// +kubebuilder:subresource:status
50+
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="RolloutScaleDown deployment phase"
3851

3952
// RolloutScaleDown is the Schema for the rolloutscaledowns API
4053
type RolloutScaleDown struct {
@@ -45,7 +58,7 @@ type RolloutScaleDown struct {
4558
Status RolloutScaleDownStatus `json:"status,omitempty"`
4659
}
4760

48-
//+kubebuilder:object:root=true
61+
// +kubebuilder:object:root=true
4962

5063
// RolloutScaleDownList contains a list of RolloutScaleDown
5164
type RolloutScaleDownList struct {

config/crd/bases/rollout.ovice.com_rolloutscaledowns.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ spec:
1414
singular: rolloutscaledown
1515
scope: Namespaced
1616
versions:
17-
- name: v1alpha1
17+
- additionalPrinterColumns:
18+
- description: RolloutScaleDown deployment phase
19+
jsonPath: .status.phase
20+
name: Phase
21+
type: string
22+
name: v1alpha1
1823
schema:
1924
openAPIV3Schema:
2025
description: RolloutScaleDown is the Schema for the rolloutscaledowns API
@@ -40,12 +45,14 @@ spec:
4045
description: RolloutScaleDownSpec defines the desired state of RolloutScaleDown
4146
properties:
4247
coolTimeSeconds:
48+
default: 30
4349
type: integer
4450
targetRollout:
4551
description: Foo is an example field of RolloutScaleDown. Edit rolloutscaledown_types.go
4652
to remove/update
4753
type: string
4854
terminatePerOnce:
55+
default: 1
4956
type: integer
5057
required:
5158
- coolTimeSeconds
@@ -57,6 +64,9 @@ spec:
5764
lastScaleDownTime:
5865
format: date-time
5966
type: string
67+
phase:
68+
default: Healthy
69+
type: string
6070
type: object
6171
type: object
6272
served: true

internal/controller/argorollout_controller.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,25 @@ func (r *ArgoRolloutReconciler) Reconcile(ctx context.Context, req ctrl.Request)
6262
}
6363

6464
klog.Infof("Target ArgoRollout: %s/%s", argoRollout.Namespace, argoRollout.Name)
65+
6566
if isCompleted(&argoRollout.Status) {
6667
klog.V(1).Infof("Rollout is completed: %s/%s", argoRollout.Namespace, argoRollout.Name)
68+
69+
if argoRollout.Status.Replicas == argoRollout.Status.ReadyReplicas {
70+
if targetScaleDown.Status.Phase != optimizerv1alpha1.RolloutScaleDownPhaseHealthy {
71+
newStatus := targetScaleDown.Status.DeepCopy()
72+
newStatus.Phase = optimizerv1alpha1.RolloutScaleDownPhaseHealthy
73+
targetScaleDown.Status = *newStatus
74+
if err := r.Status().Update(ctx, targetScaleDown); err != nil {
75+
klog.Errorf("Failed to update RolloutScaleDown: %v", err)
76+
return ctrl.Result{}, err
77+
}
78+
klog.Infof("RolloutScaleDown is healthy: %s/%s", targetScaleDown.Namespace, targetScaleDown.Name)
79+
r.Recorder.Eventf(targetScaleDown, corev1.EventTypeNormal, "Healthy", "RolloutScaleDown is healthy")
80+
}
81+
return ctrl.Result{}, nil
82+
}
83+
6784
rsHash := argoRollout.Status.StableRS
6885
oldRS := []*appsv1.ReplicaSet{}
6986
RSList := &appsv1.ReplicaSetList{}
@@ -100,11 +117,20 @@ func (r *ArgoRolloutReconciler) Reconcile(ctx context.Context, req ctrl.Request)
100117
return ctrl.Result{}, err
101118
}
102119
if retry > 0 {
120+
if targetScaleDown.Status.Phase != optimizerv1alpha1.RolloutScaleDownPhaseScaling {
121+
newStatus := targetScaleDown.Status.DeepCopy()
122+
newStatus.Phase = optimizerv1alpha1.RolloutScaleDownPhaseScaling
123+
targetScaleDown.Status = *newStatus
124+
if err := r.Status().Update(ctx, targetScaleDown); err != nil {
125+
klog.Errorf("Failed to update RolloutScaleDown: %v", err)
126+
return ctrl.Result{}, err
127+
}
128+
}
103129
return ctrl.Result{RequeueAfter: retry, Requeue: true}, nil
104130
}
105-
106131
}
107132
}
133+
108134
return ctrl.Result{}, nil
109135
}
110136

@@ -155,6 +181,7 @@ func (r *ArgoRolloutReconciler) scaleDown(ctx context.Context, rs *appsv1.Replic
155181

156182
newStatus := scaleDown.Status.DeepCopy()
157183
newStatus.LastScaleDownTime = metav1.Now()
184+
newStatus.Phase = optimizerv1alpha1.RolloutScaleDownPhaseScaling
158185
scaleDown.Status = *newStatus
159186
if err := r.Status().Update(ctx, scaleDown); err != nil {
160187
klog.Errorf("Failed to update RolloutScaleDown: %v", err)

0 commit comments

Comments
 (0)