-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathstatus.go
57 lines (48 loc) · 1.75 KB
/
status.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
package terraform
import (
"context"
"strings"
"github.com/pkg/errors"
"k8s.io/klog/v2"
"github.com/oam-dev/terraform-controller/api/types"
"github.com/oam-dev/terraform-controller/controllers/client"
)
// GetTerraformStatus will get Terraform execution status
func GetTerraformStatus(ctx context.Context, jobNamespace, jobName, containerName, initContainerName string) (types.ConfigurationState, error) {
klog.InfoS("checking Terraform init and execution status", "Namespace", jobNamespace, "Job", jobName)
clientSet, err := client.Init()
if err != nil {
klog.ErrorS(err, "failed to init clientSet")
return types.ConfigurationProvisioningAndChecking, err
}
// check the stage of the pod
stage, logs, err := getPodLog(ctx, clientSet, jobNamespace, jobName, containerName, initContainerName)
if err != nil {
klog.ErrorS(err, "failed to get pod logs")
return types.ConfigurationProvisioningAndChecking, err
}
success, state, errMsg := analyzeTerraformLog(logs, stage)
if success {
return state, nil
}
return state, errors.New(errMsg)
}
// analyzeTerraformLog will analyze the logs of Terraform apply pod, returns true if check is ok.
func analyzeTerraformLog(logs string, stage types.Stage) (bool, types.ConfigurationState, string) {
lines := strings.Split(logs, "\n")
for i, line := range lines {
if strings.Contains(line, "31mError:") {
errMsg := strings.Join(lines[i:], "\n")
if strings.Contains(errMsg, "Invalid Alibaba Cloud region") {
return false, types.InvalidRegion, errMsg
}
switch stage {
case types.InitStage:
return false, types.TerraformInitError, errMsg
case types.ApplyStage:
return false, types.ConfigurationApplyFailed, errMsg
}
}
}
return true, types.ConfigurationProvisioningAndChecking, ""
}