-
Notifications
You must be signed in to change notification settings - Fork 18
/
common.go
82 lines (65 loc) · 2.13 KB
/
common.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
package controllers
import (
"context"
"strconv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/aiven/aiven-go-client"
)
const (
conditionTypeRunning = "Running"
conditionTypeInitialized = "Initialized"
secretProtectionFinalizer = "finalizers.aiven.io/needed-to-delete-services"
instanceDeletionFinalizer = "finalizers.aiven.io/delete-remote-resource"
processedGenerationAnnotation = "controllers.aiven.io/generation-was-processed"
instanceIsRunningAnnotation = "controllers.aiven.io/instance-is-running"
)
var operatorUserAgent = "k8s-operator/" + aiven.Version()
func checkServiceIsRunning(c *aiven.Client, project, serviceName string) (bool, error) {
s, err := c.Services.Get(project, serviceName)
if err != nil {
return false, err
}
return s.State == "RUNNING", nil
}
func getInitializedCondition(reason, message string) metav1.Condition {
return metav1.Condition{
Type: conditionTypeInitialized,
Status: metav1.ConditionTrue,
Reason: reason,
Message: message,
}
}
func getRunningCondition(status metav1.ConditionStatus, reason, message string) metav1.Condition {
return metav1.Condition{
Type: conditionTypeRunning,
Status: status,
Reason: reason,
Message: message,
}
}
func isMarkedForDeletion(o client.Object) bool {
return !o.GetDeletionTimestamp().IsZero()
}
func addFinalizer(ctx context.Context, client client.Client, o client.Object, f string) error {
controllerutil.AddFinalizer(o, f)
return client.Update(ctx, o)
}
func removeFinalizer(ctx context.Context, client client.Client, o client.Object, f string) error {
controllerutil.RemoveFinalizer(o, f)
return client.Update(ctx, o)
}
func isAlreadyProcessed(o client.Object) bool {
return o.GetAnnotations()[processedGenerationAnnotation] == strconv.FormatInt(o.GetGeneration(), formatIntBaseDecimal)
}
func isAlreadyRunning(o client.Object) bool {
_, found := o.GetAnnotations()[instanceIsRunningAnnotation]
return found
}
func optionalStringPointer(u string) *string {
if len(u) == 0 {
return nil
}
return &u
}