/
adoption.go
61 lines (54 loc) · 1.46 KB
/
adoption.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
package controllers
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/selection"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func relabel(
ctx context.Context, c client.Client,
adoption client.Object, specLabels map[string]string,
objListType *unstructured.UnstructuredList,
gvk schema.GroupVersionKind,
) error {
// Build selector
var requirements []labels.Requirement
for k := range specLabels {
requirement, err := labels.NewRequirement(
k, selection.DoesNotExist, nil)
if err != nil {
return fmt.Errorf("building selector: %w", err)
}
requirements = append(requirements, *requirement)
}
selector := labels.NewSelector().Add(requirements...)
// List all the things!
if err := c.List(
ctx, objListType,
// can also set this for ClusterAdoption without issue,
// because the namespace will be ignored for Cluster scoped objects.
client.InNamespace(adoption.GetNamespace()),
client.MatchingLabelsSelector{
Selector: selector,
},
); err != nil {
return fmt.Errorf("listing %s: %w", gvk, err)
}
for _, obj := range objListType.Items {
labels := obj.GetLabels()
if labels == nil {
labels = map[string]string{}
}
for k, v := range specLabels {
labels[k] = v
}
obj.SetLabels(labels)
if err := c.Update(ctx, &obj); err != nil {
return fmt.Errorf("setting labels: %w", err)
}
}
return nil
}