generated from kyma-project/template-repository
/
checkGcpOperations.go
71 lines (63 loc) · 2.53 KB
/
checkGcpOperations.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
package scope
import (
"context"
"fmt"
gcpclient "github.com/kyma-project/cloud-manager/pkg/kcp/provider/gcp/client"
"os"
"github.com/kyma-project/cloud-manager/pkg/composed"
)
func checkGcpOperations(ctx context.Context, st composed.State) (error, context.Context) {
state := st.(*State)
logger := composed.LoggerFromCtx(ctx)
scope := state.ObjAsScope()
saJsonKeyPath := os.Getenv("GCP_SA_JSON_KEY_PATH")
if saJsonKeyPath == "" {
return composed.LogErrorAndReturn(
fmt.Errorf("GCP_SA_JSON_KEY_PATH not set"),
"Error enabling GCP APIs",
composed.StopAndForget, // Without this env var, we can't call any APIs anyway, so we can't recover from this error
ctx)
}
client, err := state.gcpServiceUsageClientProvider(ctx, saJsonKeyPath)
if err != nil {
return composed.LogErrorAndReturn(
fmt.Errorf("error getting ServiceUsageClient: %w", err),
"Error enabling GCP APIs",
composed.StopAndForget, // Without this env var, we can't call any APIs anyway, so we can't recover from this error
ctx)
}
var unfinishedOperations = make([]string, 0)
for _, opName := range scope.Status.GcpOperations {
logger.WithValues("Scope :", scope.Name).Info("Checking Service Enablement GCP Operation Status")
op, err := client.GetServiceUsageOperation(ctx, opName)
if err != nil {
logger.Error(err, "Error getting Service Usage Operation from GCP. Retry via requeue.")
return composed.StopWithRequeueDelay(gcpclient.GcpRetryWaitTime), nil
}
// This should not ever happen, but if it does, we can't do anything except removing the operation from the status
if op == nil {
logger.WithValues("Operation Name :", opName).Info("Operation not found in GCP.")
}
//Operation not completed yet
if op != nil && !op.Done {
unfinishedOperations = append(unfinishedOperations, opName)
}
if op != nil && op.Done {
if op.Error != nil {
logger.WithValues("Operation Name :", opName).Info("Operation failed. Removing from status.")
}
}
}
scope.Status.GcpOperations = unfinishedOperations
if len(unfinishedOperations) > 0 {
scope.Status.GcpOperations = unfinishedOperations
return composed.UpdateStatus(scope).
SuccessError(composed.StopWithRequeueDelay(gcpclient.GcpOperationWaitTime)).
Run(ctx, state)
}
// Even if all operations are done (which might take us several iterations to figure out), we have issues with failed or not found operations.
// We should requeue and verify that all APIs are really enabled.
return composed.UpdateStatus(scope).
SuccessError(composed.StopWithRequeue).
Run(ctx, state)
}