-
Notifications
You must be signed in to change notification settings - Fork 129
/
tf_controller_reconcile.go
194 lines (159 loc) · 5.94 KB
/
tf_controller_reconcile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"fmt"
"strings"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
infrav1 "github.com/weaveworks/tf-controller/api/v1alpha1"
"github.com/weaveworks/tf-controller/runner"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)
func (r *TerraformReconciler) reconcile(ctx context.Context, runnerClient runner.RunnerClient, terraform infrav1.Terraform, sourceObj sourcev1.Source, reconciliationLoopID string) (*infrav1.Terraform, error) {
log := ctrl.LoggerFrom(ctx)
revision := sourceObj.GetArtifact().Revision
objectKey := types.NamespacedName{Namespace: terraform.Namespace, Name: terraform.Name}
var (
tfInstance string
tmpDir string
err error
lastKnownAction string
)
log.Info("setting up terraform")
terraform, tfInstance, tmpDir, err = r.setupTerraform(ctx, runnerClient, terraform, sourceObj, revision, objectKey, reconciliationLoopID)
lastKnownAction = "Setup"
defer func() {
cleanupDirReply, err := runnerClient.CleanupDir(ctx, &runner.CleanupDirRequest{TmpDir: tmpDir})
if err != nil {
log.Error(err, "clean up error")
}
if cleanupDirReply != nil {
log.Info(fmt.Sprintf("clean up dir: %s", cleanupDirReply.Message))
}
}()
if err != nil {
log.Error(err, "error in terraform setup")
return &terraform, err
}
if r.shouldDetectDrift(terraform, revision) {
var driftDetectionErr error // declared here to avoid shadowing on terraform variable
terraform, driftDetectionErr = r.detectDrift(ctx, terraform, tfInstance, runnerClient, revision)
// immediately return if no drift - reconciliation will retry normally
if driftDetectionErr == nil {
// reconcile outputs only when outputs are missing
if outputsDrifted, err := r.outputsMayBeDrifted(ctx, terraform); outputsDrifted == true && err == nil {
terraform, err = r.processOutputs(ctx, runnerClient, terraform, tfInstance, revision)
if err != nil {
log.Error(err, "error processing outputs")
return &terraform, err
}
} else if err != nil {
log.Error(err, "error checking for output drift")
return &terraform, err
}
return &terraform, nil
}
// immediately return if err is not about drift
if driftDetectionErr.Error() != infrav1.DriftDetectedReason {
log.Error(driftDetectionErr, "detected non drift error")
return &terraform, driftDetectionErr
}
// immediately return if drift is detected, but it's not "force" or "auto"
if driftDetectionErr.Error() == infrav1.DriftDetectedReason && !r.forceOrAutoApply(terraform) {
log.Error(driftDetectionErr, "will not force / auto apply detected drift")
return &terraform, driftDetectionErr
}
// ok Drift Detected, patch and continue
if err := r.patchStatus(ctx, objectKey, terraform.Status); err != nil {
log.Error(err, "unable to update status after drift detection")
return &terraform, err
}
lastKnownAction = "Drift Detection"
}
// return early if we're in drift-detection-only mode
if terraform.Spec.ApprovePlan == infrav1.ApprovePlanDisableValue {
log.Info("approve plan disabled")
return &terraform, nil
}
// if we should plan this Terraform CR, do so
if r.shouldPlan(terraform) {
terraform, err = r.plan(ctx, terraform, tfInstance, runnerClient, revision)
if err != nil {
log.Error(err, "error planning")
return &terraform, err
}
if err := r.patchStatus(ctx, objectKey, terraform.Status); err != nil {
log.Error(err, "unable to update status after planing")
return &terraform, err
}
lastKnownAction = "Planned"
}
// if we should apply the generated plan, do so
if r.shouldApply(terraform) {
terraform, err = r.apply(ctx, terraform, tfInstance, runnerClient, revision)
if err != nil {
log.Error(err, "error applying")
return &terraform, err
}
if err := r.patchStatus(ctx, objectKey, terraform.Status); err != nil {
log.Error(err, "unable to update status after applying")
return &terraform, err
}
lastKnownAction = "Applied"
} else {
log.Info("should apply == false")
}
terraform, err = r.processOutputs(ctx, runnerClient, terraform, tfInstance, revision)
if err != nil {
log.Error(err, "error process outputs")
return &terraform, err
}
lastKnownAction = "Outputs Processed"
if r.shouldDoHealthChecks(terraform) {
terraform, err = r.doHealthChecks(ctx, terraform, revision, runnerClient)
if err != nil {
log.Error(err, "error with health check")
return &terraform, err
}
if err := r.patchStatus(ctx, objectKey, terraform.Status); err != nil {
log.Error(err, "unable to update status after doing health checks")
return &terraform, err
}
lastKnownAction = "Health Checked"
}
var (
readyCondition *metav1.Condition
readyConditionIndex int
)
for i, condition := range terraform.Status.Conditions {
if condition.Type == meta.ReadyCondition {
readyCondition = &condition
readyConditionIndex = i
break
}
}
if readyCondition != nil && readyCondition.Status == metav1.ConditionUnknown {
log.Info("Last known action was: " + lastKnownAction)
cond := terraform.Status.Conditions[readyConditionIndex]
if cond.Reason == infrav1.PlannedWithChangesReason && strings.HasPrefix(cond.Message, "Plan generated") {
// do nothing
} else if cond.Reason != meta.ProgressingReason {
terraform.Status.Conditions[readyConditionIndex].Status = metav1.ConditionTrue
}
}
return &terraform, nil
}