Skip to content

Commit b137735

Browse files
dymurrayShawn Hurley
authored andcommitted
pkg/ansible: Add Phase to CR status field for Ansible Operator (#543)
* Add Phase to CR status field for Ansible Operator
1 parent eadded4 commit b137735

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

pkg/ansible/controller/reconcile.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,42 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
7070
return reconcile.Result{}, nil
7171
}
7272

73-
s := u.Object["spec"]
74-
_, ok := s.(map[string]interface{})
73+
spec := u.Object["spec"]
74+
_, ok := spec.(map[string]interface{})
7575
if !ok {
76-
logrus.Warnf("spec was not found")
76+
logrus.Debugf("spec was not found")
7777
u.Object["spec"] = map[string]interface{}{}
78-
r.Client.Update(context.TODO(), u)
78+
err = r.Client.Update(context.TODO(), u)
79+
if err != nil {
80+
return reconcile.Result{}, err
81+
}
82+
return reconcile.Result{Requeue: true}, nil
83+
}
84+
status := u.Object["status"]
85+
_, ok = status.(map[string]interface{})
86+
if !ok {
87+
logrus.Debugf("status was not found")
88+
u.Object["status"] = map[string]interface{}{}
89+
err = r.Client.Update(context.TODO(), u)
90+
if err != nil {
91+
return reconcile.Result{}, err
92+
}
7993
return reconcile.Result{Requeue: true}, nil
8094
}
95+
96+
// If status is an empty map we can assume CR was just created
97+
if len(u.Object["status"].(map[string]interface{})) == 0 {
98+
logrus.Debugf("Setting phase status to %v", StatusPhaseCreating)
99+
u.Object["status"] = ResourceStatus{
100+
Phase: StatusPhaseCreating,
101+
}
102+
err = r.Client.Update(context.TODO(), u)
103+
if err != nil {
104+
return reconcile.Result{}, err
105+
}
106+
return reconcile.Result{Requeue: true}, nil
107+
}
108+
81109
ownerRef := metav1.OwnerReference{
82110
APIVersion: u.GetAPIVersion(),
83111
Kind: u.GetKind(),

pkg/ansible/controller/types.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import (
1919
)
2020

2121
const (
22-
host = "localhost"
22+
host = "localhost"
23+
StatusPhaseCreating = "Creating"
24+
StatusPhaseRunning = "Running"
25+
StatusPhaseFailed = "Failed"
2326
)
2427

2528
type Status struct {
@@ -96,13 +99,15 @@ func NewStatusFromMap(sm map[string]interface{}) Status {
9699

97100
type ResourceStatus struct {
98101
Status `json:",inline"`
102+
Phase string `json:"phase"`
99103
FailureMessage string `json:"reason,omitempty"`
100104
History []Status `json:"history,omitempty"`
101105
}
102106

103107
func UpdateResourceStatus(sm map[string]interface{}, je eventapi.StatusJobEvent) (bool, ResourceStatus) {
104108
newStatus := NewStatusFromStatusJobEvent(je)
105109
oldStatus := NewStatusFromMap(sm)
110+
phase := StatusPhaseRunning
106111
// Don't update the status if new status and old status are equal.
107112
if IsStatusEqual(newStatus, oldStatus) {
108113
return false, ResourceStatus{}
@@ -117,9 +122,15 @@ func UpdateResourceStatus(sm map[string]interface{}, je eventapi.StatusJobEvent)
117122
history = append(history, NewStatusFromMap(ma))
118123
}
119124
}
125+
126+
if newStatus.Failures > 0 {
127+
phase = StatusPhaseFailed
128+
}
129+
120130
history = append(history, oldStatus)
121131
return true, ResourceStatus{
122132
Status: newStatus,
133+
Phase: phase,
123134
History: history,
124135
}
125136
}

0 commit comments

Comments
 (0)