/
dynamic.go
83 lines (68 loc) · 2.98 KB
/
dynamic.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package k8s provides a client for interacting with a Kubernetes cluster.
package k8s
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
)
// AddLabelsAndAnnotations adds the provided labels and annotations to the specified K8s resource
func (k *K8s) AddLabelsAndAnnotations(resourceNamespace string, resourceName string, groupKind schema.GroupKind, labels map[string]string, annotations map[string]string) error {
return k.updateLabelsAndAnnotations(resourceNamespace, resourceName, groupKind, labels, annotations, false)
}
// RemoveLabelsAndAnnotations removes the provided labels and annotations to the specified K8s resource
func (k *K8s) RemoveLabelsAndAnnotations(resourceNamespace string, resourceName string, groupKind schema.GroupKind, labels map[string]string, annotations map[string]string) error {
return k.updateLabelsAndAnnotations(resourceNamespace, resourceName, groupKind, labels, annotations, true)
}
// updateLabelsAndAnnotations updates the provided labels and annotations to the specified K8s resource
func (k *K8s) updateLabelsAndAnnotations(resourceNamespace string, resourceName string, groupKind schema.GroupKind, labels map[string]string, annotations map[string]string, isRemove bool) error {
dynamicClient := dynamic.NewForConfigOrDie(k.RestConfig)
discoveryClient := discovery.NewDiscoveryClientForConfigOrDie(k.RestConfig)
groupResources, err := restmapper.GetAPIGroupResources(discoveryClient)
if err != nil {
return err
}
mapper := restmapper.NewDiscoveryRESTMapper(groupResources)
mapping, err := mapper.RESTMapping(groupKind)
if err != nil {
return err
}
deployedResource, err := dynamicClient.Resource(mapping.Resource).Namespace(resourceNamespace).Get(context.TODO(), resourceName, metav1.GetOptions{})
if err != nil {
return err
}
// Pull the existing labels from the rendered resource
deployedLabels := deployedResource.GetLabels()
if deployedLabels == nil {
// Ensure label map exists to avoid nil panic
deployedLabels = make(map[string]string)
}
for key, value := range labels {
if isRemove {
delete(deployedLabels, key)
} else {
deployedLabels[key] = value
}
}
deployedResource.SetLabels(deployedLabels)
// Pull the existing annotations from the rendered resource
deployedAnnotations := deployedResource.GetAnnotations()
if deployedAnnotations == nil {
// Ensure label map exists to avoid nil panic
deployedAnnotations = make(map[string]string)
}
for key, value := range annotations {
if isRemove {
delete(deployedAnnotations, key)
} else {
deployedAnnotations[key] = value
}
}
deployedResource.SetAnnotations(deployedAnnotations)
_, err = dynamicClient.Resource(mapping.Resource).Namespace(resourceNamespace).Update(context.TODO(), deployedResource, metav1.UpdateOptions{})
return err
}