Skip to content

Commit 16442ca

Browse files
committed
fix many state transitions
1 parent aa8740e commit 16442ca

9 files changed

+359
-240
lines changed

api/v1alpha1/terraform_types.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,17 @@ const (
206206
TerraformFinalizer = "finalizers.tf.contrib.fluxcd.io"
207207
MaxConditionMessageLength = 20000
208208
DisabledValue = "disabled"
209-
HealthyCondition = "Healthy"
210209

211210
// ArtifactFailedReason represents the fact that the
212211
// source artifact download failed.
213212
ArtifactFailedReason = "ArtifactFailed"
214213

215-
TFExecInstallFailedReason = "TFExecInstallFailed"
216-
TFExecNewFailedReason = "TFExecNewFailed"
217-
TFExecInitFailedReason = "TFExecInitFailed"
218-
TFExecPlanFailedReason = "TFExecPlanFailed"
219-
TFExecApplyFailedReason = "TFExecApplyFailed"
214+
TFExecNewFailedReason = "TFExecNewFailed"
215+
TFExecInitFailedReason = "TFExecInitFailed"
216+
TFExecPlanFailedReason = "TFExecPlanFailed"
217+
TFExecApplyFailedReason = "TFExecApplyFailed"
218+
TFExecOutputFailedReason = "TFExecOutputFailed"
219+
OutputsWritingFailedReason = "OutputsWritingFailed"
220220
)
221221

222222
// SetKustomizationReadiness sets the ReadyCondition, ObservedGeneration, and LastAttemptedRevision, on the Kustomization.
@@ -240,17 +240,25 @@ func SetTerraformReadiness(terraform *Terraform, status metav1.ConditionStatus,
240240
terraform.Status.LastAttemptedRevision = revision
241241
}
242242

243-
func TerraformApplying(terraform Terraform, message string) Terraform {
243+
func TerraformApplying(terraform Terraform, revision string, message string) Terraform {
244244
meta.SetResourceCondition(&terraform, "Apply", metav1.ConditionUnknown, meta.ProgressingReason, message)
245+
if revision != "" {
246+
(&terraform).Status.LastAppliedRevision = revision
247+
}
245248
return terraform
246249
}
247250

248-
func TerraformOutputAvailable(terraform Terraform, availableOutputs []string, message string) Terraform {
249-
meta.SetResourceCondition(&terraform, "Output", metav1.ConditionTrue, "TerraformOutputAvailable", message)
251+
func TerraformOutputsAvailable(terraform Terraform, availableOutputs []string, message string) Terraform {
252+
meta.SetResourceCondition(&terraform, "Output", metav1.ConditionTrue, "TerraformOutputsAvailable", message)
250253
(&terraform).Status.AvailableOutputs = availableOutputs
251254
return terraform
252255
}
253256

257+
func TerraformOutputsWritten(terraform Terraform, message string) Terraform {
258+
meta.SetResourceCondition(&terraform, "Output", metav1.ConditionTrue, "TerraformOutputsWritten", message)
259+
return terraform
260+
}
261+
254262
func TerraformApplied(terraform Terraform, revision string, message string) Terraform {
255263
meta.SetResourceCondition(&terraform, "Apply", metav1.ConditionTrue, "TerraformAppliedSucceed", message)
256264
plan := terraform.Status.Plan.Pending
@@ -266,26 +274,30 @@ func TerraformApplied(terraform Terraform, revision string, message string) Terr
266274

267275
func TerraformPlannedWithChanges(terraform Terraform, revision string, message string) Terraform {
268276
planRev := strings.Replace(revision, "/", "-", 1)
269-
meta.SetResourceCondition(&terraform, "Plan", metav1.ConditionTrue, "TerraformPlannedSucceed", message)
277+
meta.SetResourceCondition(&terraform, "Plan", metav1.ConditionTrue, "TerraformPlannedWithChanges", message)
270278
(&terraform).Status.Plan = PlanStatus{
271279
LastApplied: terraform.Status.Plan.LastApplied,
272280
Pending: fmt.Sprintf("plan-%s", planRev),
273281
}
274282
if revision != "" {
275283
(&terraform).Status.LastAttemptedRevision = revision
276284
}
285+
286+
SetTerraformReadiness(&terraform, metav1.ConditionUnknown, "TerraformPlannedWithChanges", message, revision)
277287
return terraform
278288
}
279289

280290
func TerraformPlannedNoChanges(terraform Terraform, revision string, message string) Terraform {
281-
meta.SetResourceCondition(&terraform, "Plan", metav1.ConditionFalse, "TerraformPlannedSucceed", message)
291+
meta.SetResourceCondition(&terraform, "Plan", metav1.ConditionFalse, "TerraformPlannedNoChanges", message)
282292
(&terraform).Status.Plan = PlanStatus{
283293
LastApplied: terraform.Status.Plan.LastApplied,
284294
Pending: "",
285295
}
286296
if revision != "" {
287297
(&terraform).Status.LastAttemptedRevision = revision
288298
}
299+
300+
SetTerraformReadiness(&terraform, metav1.ConditionTrue, "TerraformPlannedNoChanges", message, revision)
289301
return terraform
290302
}
291303

controllers/suite_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ func TestMain(m *testing.M) {
140140
}
141141

142142
err = (&TerraformReconciler{
143-
Client: k8sManager.GetClient(),
144-
Scheme: k8sManager.GetScheme(),
145-
StatusPoller: polling.NewStatusPoller(k8sManager.GetClient(), k8sManager.GetRESTMapper()),
143+
Client: k8sManager.GetClient(),
144+
Scheme: k8sManager.GetScheme(),
145+
EventRecorder: k8sManager.GetEventRecorderFor("tf-controller"),
146+
StatusPoller: polling.NewStatusPoller(k8sManager.GetClient(), k8sManager.GetRESTMapper()),
146147
}).SetupWithManager(k8sManager)
147148
if err != nil {
148149
panic(err.Error())

controllers/tc000010_no_outputs_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,24 @@ func Test_0000010_no_outputs_test(t *testing.T) {
121121

122122
it("should have its plan reconciled")
123123
by("checking that the Plan's Status of the TF program is Planned Succeed.")
124-
g.Eventually(func() map[string]interface{} {
124+
g.Eventually(func() interface{} {
125125
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
126126
if err != nil {
127127
return nil
128128
}
129-
return map[string]interface{}{
130-
"Type": createdHelloWorldTF.Status.Conditions[0].Type,
131-
"Reason": createdHelloWorldTF.Status.Conditions[0].Reason,
132-
"Message": createdHelloWorldTF.Status.Conditions[0].Message,
129+
for _, c := range createdHelloWorldTF.Status.Conditions {
130+
if c.Type == "Plan" {
131+
return map[string]interface{}{
132+
"Type": c.Type,
133+
"Reason": c.Reason,
134+
"Message": c.Message,
135+
}
136+
}
133137
}
138+
return createdHelloWorldTF.Status
134139
}, timeout, interval).Should(Equal(map[string]interface{}{
135140
"Type": "Plan",
136-
"Reason": "TerraformPlannedSucceed",
141+
"Reason": "TerraformPlannedWithChanges",
137142
"Message": "Terraform Plan Generated Successfully",
138143
}))
139144

@@ -166,6 +171,7 @@ func Test_0000010_no_outputs_test(t *testing.T) {
166171
return map[string]interface{}{
167172
"Type": c.Type,
168173
"Reason": c.Reason,
174+
"Message": c.Message,
169175
"LastAppliedPlan": createdHelloWorldTF.Status.Plan.LastApplied,
170176
}
171177
}
@@ -174,6 +180,7 @@ func Test_0000010_no_outputs_test(t *testing.T) {
174180
}, timeout, interval).Should(Equal(map[string]interface{}{
175181
"Type": "Apply",
176182
"Reason": "TerraformAppliedSucceed",
183+
"Message": "Terraform Applied Successfully",
177184
"LastAppliedPlan": "plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
178185
}))
179186
// TODO check Output condition

controllers/tc000020_with_backend_no_outputs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func Test_0000020_with_backend_no_outputs_test(t *testing.T) {
157157
return nil
158158
}, timeout, interval).Should(Equal(map[string]interface{}{
159159
"Type": "Output",
160-
"Reason": "TerraformOutputAvailable",
160+
"Reason": "TerraformOutputsAvailable",
161161
}))
162162

163163
by("checking that we have outputs available in the TF object")

controllers/tc000030_plan_only_no_outputs_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,27 @@ func Test_0000030_plan_only_no_outputs_test(t *testing.T) {
105105
return -1
106106
}
107107
return len(createdHelloWorldTF.Status.Conditions)
108-
}, timeout*3, interval).Should(Equal(1))
108+
}, timeout*3, interval).ShouldNot(BeZero())
109109

110110
by("checking that the planned status of the TF program is created successfully")
111111
g.Eventually(func() map[string]interface{} {
112112
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
113113
if err != nil {
114114
return nil
115115
}
116-
return map[string]interface{}{
117-
"Type": createdHelloWorldTF.Status.Conditions[0].Type,
118-
"Reason": createdHelloWorldTF.Status.Conditions[0].Reason,
119-
"Pending": createdHelloWorldTF.Status.Plan.Pending,
116+
for _, c := range createdHelloWorldTF.Status.Conditions {
117+
if c.Type == "Plan" {
118+
return map[string]interface{}{
119+
"Type": c.Type,
120+
"Reason": c.Reason,
121+
"Pending": createdHelloWorldTF.Status.Plan.Pending,
122+
}
123+
}
120124
}
125+
return nil
121126
}, timeout, interval).Should(Equal(map[string]interface{}{
122127
"Type": "Plan",
123-
"Reason": "TerraformPlannedSucceed",
128+
"Reason": "TerraformPlannedWithChanges",
124129
"Pending": "plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
125130
}))
126131

controllers/tc000050_plan_and_manual_approve_no_outputs_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,27 @@ func Test_000050_plan_and_manual_approve_no_outputs_test(t *testing.T) {
104104
return -1
105105
}
106106
return len(createdHelloWorldTF.Status.Conditions)
107-
}, timeout*3, interval).Should(Equal(1))
107+
}, timeout*3, interval).ShouldNot(BeZero())
108108

109109
by("checking that the planned status of the TF program is created successfully")
110110
g.Eventually(func() map[string]interface{} {
111111
err := k8sClient.Get(ctx, helloWorldTFKey, &createdHelloWorldTF)
112112
if err != nil {
113113
return nil
114114
}
115-
return map[string]interface{}{
116-
"Type": createdHelloWorldTF.Status.Conditions[0].Type,
117-
"Reason": createdHelloWorldTF.Status.Conditions[0].Reason,
118-
"Pending": createdHelloWorldTF.Status.Plan.Pending,
115+
for _, c := range createdHelloWorldTF.Status.Conditions {
116+
if c.Type == "Plan" {
117+
return map[string]interface{}{
118+
"Type": c.Type,
119+
"Reason": c.Reason,
120+
"Pending": createdHelloWorldTF.Status.Plan.Pending,
121+
}
122+
}
119123
}
124+
return nil
120125
}, timeout, interval).Should(Equal(map[string]interface{}{
121126
"Type": "Plan",
122-
"Reason": "TerraformPlannedSucceed",
127+
"Reason": "TerraformPlannedWithChanges",
123128
"Pending": "plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
124129
}))
125130

controllers/tc000100_applied_should_tx_to_plan_when_source_changed_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func Test_000100_applied_resource_should_transit_back_to_plan_when_source_change
105105
return -1
106106
}
107107
return len(createdHelloWorldTF.Status.Conditions)
108-
}, timeout*3, interval).Should(Equal(1))
108+
}, timeout*3, interval).ShouldNot(BeZero())
109109

110110
by("checking that the planned status of the TF program is created successfully")
111111
g.Eventually(func() map[string]interface{} {
@@ -116,17 +116,17 @@ func Test_000100_applied_resource_should_transit_back_to_plan_when_source_change
116116
for _, c := range createdHelloWorldTF.Status.Conditions {
117117
if c.Type == "Plan" {
118118
return map[string]interface{}{
119-
"Type": createdHelloWorldTF.Status.Conditions[0].Type,
120-
"Reason": createdHelloWorldTF.Status.Conditions[0].Reason,
119+
"Type": c.Type,
120+
"Reason": c.Reason,
121121
"Pending": createdHelloWorldTF.Status.Plan.Pending,
122-
"Message": createdHelloWorldTF.Status.Conditions[0].Message,
122+
"Message": c.Message,
123123
}
124124
}
125125
}
126126
return nil
127127
}, timeout, interval).Should(Equal(map[string]interface{}{
128128
"Type": "Plan",
129-
"Reason": "TerraformPlannedSucceed",
129+
"Reason": "TerraformPlannedWithChanges",
130130
"Pending": "plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
131131
"Message": "Terraform Plan Generated Successfully",
132132
}))
@@ -252,7 +252,7 @@ func Test_000100_applied_resource_should_transit_back_to_plan_when_source_change
252252
return nil
253253
}, timeout, interval).Should(Equal(map[string]interface{}{
254254
"Type": "Plan",
255-
"Reason": "TerraformPlannedSucceed",
255+
"Reason": "TerraformPlannedWithChanges",
256256
"LastAppliedPlan": "plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
257257
"Pending": "plan-master-ed22ced771a0056455a2fbb8e362c206e3d0cbb7",
258258
}))

controllers/tc000110_auto_applied_should_tx_to_plan_then_apply_when_source_changed_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func Test_000110_auto_applied_resource_should_transit_to_plan_then_apply_when_so
105105
return -1
106106
}
107107
return len(createdHelloWorldTF.Status.Conditions)
108-
}, timeout*3, interval).Should(Equal(1))
108+
}, timeout*3, interval).ShouldNot(BeZero())
109109

110110
by("checking that the planned status of the TF program is created successfully")
111111
g.Eventually(func() map[string]interface{} {
@@ -116,17 +116,17 @@ func Test_000110_auto_applied_resource_should_transit_to_plan_then_apply_when_so
116116
for _, c := range createdHelloWorldTF.Status.Conditions {
117117
if c.Type == "Plan" {
118118
return map[string]interface{}{
119-
"Type": createdHelloWorldTF.Status.Conditions[0].Type,
120-
"Reason": createdHelloWorldTF.Status.Conditions[0].Reason,
119+
"Type": c.Type,
120+
"Reason": c.Reason,
121121
"Pending": createdHelloWorldTF.Status.Plan.Pending,
122-
"Message": createdHelloWorldTF.Status.Conditions[0].Message,
122+
"Message": c.Message,
123123
}
124124
}
125125
}
126126
return nil
127127
}, timeout, interval).Should(Equal(map[string]interface{}{
128128
"Type": "Plan",
129-
"Reason": "TerraformPlannedSucceed",
129+
"Reason": "TerraformPlannedWithChanges",
130130
"Pending": "plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
131131
"Message": "Terraform Plan Generated Successfully",
132132
}))
@@ -263,6 +263,6 @@ func Test_000110_auto_applied_resource_should_transit_to_plan_then_apply_when_so
263263
"Reason": "TerraformAppliedSucceed",
264264
"LastAppliedPlan": "plan-master-ed22ced771a0056455a2fbb8e362c206e3d0cbb7",
265265
"Pending": "",
266-
"Message": "Terraform Apply Run Successfully",
266+
"Message": "Terraform Applied Successfully",
267267
}))
268268
}

0 commit comments

Comments
 (0)