-
Notifications
You must be signed in to change notification settings - Fork 4
/
dapr_instance_controller_support.go
129 lines (105 loc) · 2.94 KB
/
dapr_instance_controller_support.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
package instance
import (
"context"
"fmt"
"strconv"
"github.com/dapr-sandbox/dapr-kubernetes-operator/pkg/helm"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
ctrlCli "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/dapr-sandbox/dapr-kubernetes-operator/pkg/controller/predicates"
)
func gcSelector(rc *ReconciliationRequest) (labels.Selector, error) {
namespace, err := labels.NewRequirement(
helm.ReleaseNamespace,
selection.Equals,
[]string{rc.Resource.Namespace})
if err != nil {
return nil, fmt.Errorf("cannot determine release namespace requirement: %w", err)
}
name, err := labels.NewRequirement(
helm.ReleaseName,
selection.Equals,
[]string{rc.Resource.Name})
if err != nil {
return nil, fmt.Errorf("cannot determine release name requirement: %w", err)
}
generation, err := labels.NewRequirement(
helm.ReleaseGeneration,
selection.LessThan,
[]string{strconv.FormatInt(rc.Resource.Generation, 10)})
if err != nil {
return nil, fmt.Errorf("cannot determine generation requirement: %w", err)
}
selector := labels.NewSelector().
Add(*namespace).
Add(*name).
Add(*generation)
return selector, nil
}
func labelsToRequest(_ context.Context, object ctrlCli.Object) []reconcile.Request {
allLabels := object.GetLabels()
if allLabels == nil {
return nil
}
name := allLabels[helm.ReleaseName]
if name == "" {
return nil
}
namespace := allLabels[helm.ReleaseNamespace]
if namespace == "" {
return nil
}
return []reconcile.Request{{
NamespacedName: types.NamespacedName{
Name: name,
Namespace: namespace,
},
}}
}
func dependantWithLabels(watchUpdate bool, watchDelete bool, watchStatus bool) predicate.Predicate {
return predicate.And(
&predicates.HasLabel{
Name: helm.ReleaseName,
},
&predicates.HasLabel{
Name: helm.ReleaseNamespace,
},
&predicates.DependentPredicate{
WatchUpdate: watchUpdate,
WatchDelete: watchDelete,
WatchStatus: watchStatus,
},
)
}
func CurrentReleaseSelector(rc *ReconciliationRequest) (labels.Selector, error) {
namespace, err := labels.NewRequirement(
helm.ReleaseNamespace,
selection.Equals,
[]string{rc.Resource.Namespace})
if err != nil {
return nil, fmt.Errorf("cannot determine release namespace requirement: %w", err)
}
name, err := labels.NewRequirement(
helm.ReleaseName,
selection.Equals,
[]string{rc.Resource.Name})
if err != nil {
return nil, fmt.Errorf("cannot determine release name requirement: %w", err)
}
generation, err := labels.NewRequirement(
helm.ReleaseGeneration,
selection.Equals,
[]string{strconv.FormatInt(rc.Resource.Generation, 10)})
if err != nil {
return nil, fmt.Errorf("cannot determine generation requirement: %w", err)
}
selector := labels.NewSelector().
Add(*namespace).
Add(*name).
Add(*generation)
return selector, nil
}