-
Notifications
You must be signed in to change notification settings - Fork 196
/
reconcile_policy.go
82 lines (67 loc) · 2.97 KB
/
reconcile_policy.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
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/
package reconcilers
import (
"reflect"
"github.com/pkg/errors"
"github.com/Azure/azure-service-operator/v2/internal/util/to"
)
// ReconcilePolicyAnnotation describes the reconcile policy for the resource in question.
// A reconcile policy describes what action (if any) the operator is allowed to take when
// reconciling the resource.
// If no reconcile policy is specified, the default is "run"
const ReconcilePolicyAnnotation = "serviceoperator.azure.com/reconcile-policy"
type ReconcilePolicy string
const (
// ReconcilePolicyManage instructs the operator to manage the resource in question.
// This includes issuing PUTs to update it and DELETE's to delete it from Azure if deleted in Kuberentes.
// This is the default policy when no policy is specified.
ReconcilePolicyManage = ReconcilePolicy("manage")
// ReconcilePolicySkip instructs the operator to skip all reconciliation actions. This includes creating
// the resource.
ReconcilePolicySkip = ReconcilePolicy("skip")
// ReconcilePolicyDetachOnDelete instructs the operator to skip deletion of resources in Azure. This allows
// deletion of the resource in Kubernetes to go through but does not delete the underlying Azure resource.
ReconcilePolicyDetachOnDelete = ReconcilePolicy("detach-on-delete")
)
// ParseReconcilePolicy parses the provided reconcile policy.
func ParseReconcilePolicy(policy string) (ReconcilePolicy, error) {
switch policy {
case "":
return ReconcilePolicyManage, nil
case string(ReconcilePolicyManage):
return ReconcilePolicyManage, nil
case string(ReconcilePolicySkip):
return ReconcilePolicySkip, nil
case string(ReconcilePolicyDetachOnDelete):
return ReconcilePolicyDetachOnDelete, nil
default:
// Defaulting to manage.
return ReconcilePolicyManage, errors.Errorf("%q is not a known reconcile policy", policy)
}
}
// AllowsDelete determines if the policy allows deletion of the backing Azure resource
func (r ReconcilePolicy) AllowsDelete() bool {
return r == ReconcilePolicyManage
}
// AllowsModify determines if the policy allows modification of the backing Azure resource
func (r ReconcilePolicy) AllowsModify() bool {
return r == ReconcilePolicyManage || r == ReconcilePolicyDetachOnDelete
}
// HasReconcilePolicyAnnotationChanged returns true if the reconcile-policy annotation has
// changed in a way that needs to trigger a reconcile.
func HasReconcilePolicyAnnotationChanged(old *string, new *string) bool {
equal := reflect.DeepEqual(old, new)
if equal {
// If the annotations are equal there's been no change
return false
}
oldStr := to.Value(old)
newStr := to.Value(new)
// We only care about transitions to or from ReconcilePolicySkip. We don't need to
// trigger an event if ReconcilePolicyDetachOnDelete is added or removed, as that annotation
// only applies on delete (which we will always run reconcile on).
return oldStr == string(ReconcilePolicySkip) || newStr == string(ReconcilePolicySkip)
}