This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathdynamic.go
More file actions
234 lines (202 loc) · 6.89 KB
/
dynamic.go
File metadata and controls
234 lines (202 loc) · 6.89 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package kclient
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/klog/v2"
)
// PatchDynamicResource patches a dynamic custom resource and returns true
// if the generation of the resource increased or the resource is created
func (c *Client) PatchDynamicResource(resource unstructured.Unstructured) (bool, error) {
klog.V(5).Infoln("Applying resource via server-side apply:")
klog.V(5).Infoln(resourceAsJson(resource.Object))
unversionedResource := resource.DeepCopy()
unversionedResource.SetResourceVersion("")
data, err := json.Marshal(unversionedResource.Object)
if err != nil {
return false, fmt.Errorf("unable to marshal resource: %w", err)
}
gvr, err := c.GetRestMappingFromUnstructured(*unversionedResource)
if err != nil {
return false, err
}
var previousGeneration int64 = -1
// Get the generation of the current resource
previous, err := c.DynamicClient.Resource(gvr.Resource).Namespace(c.Namespace).Get(context.TODO(), unversionedResource.GetName(), metav1.GetOptions{})
if err != nil {
if !kerrors.IsNotFound(err) {
return false, err
}
} else {
previousGeneration = previous.GetGeneration()
}
// Patch the dynamic resource
current, err := c.DynamicClient.Resource(gvr.Resource).Namespace(c.Namespace).Patch(context.TODO(), unversionedResource.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{FieldManager: FieldManager, Force: Bool(true)})
if err != nil {
return false, err
}
newGeneration := current.GetGeneration()
return newGeneration > previousGeneration, nil
}
// ListDynamicResources returns an unstructured list of instances of a Custom
// Resource currently deployed in the specified namespace of the cluster. The current namespace is used if the namespace is not specified.
// If a selector is passed, then it will be used as a label selector to list the resources.
func (c *Client) ListDynamicResources(namespace string, gvr schema.GroupVersionResource, selector string) (*unstructured.UnstructuredList, error) {
if c.DynamicClient == nil {
return nil, nil
}
ns := namespace
if ns == "" {
ns = c.Namespace
}
listOptions := metav1.ListOptions{}
if selector != "" {
listOptions.LabelSelector = selector
}
list, err := c.DynamicClient.Resource(gvr).Namespace(ns).List(context.TODO(), listOptions)
if err != nil {
if kerrors.IsNotFound(err) {
// Assume this is a cluster scoped resource (not namespace scoped) and skip it
return &unstructured.UnstructuredList{}, nil
}
return nil, err
}
return list, nil
}
// ListClusterWideDynamicResources returns an unstructured list of instances of a Custom
// Resource currently deployed cluster-wide in the cluster.
// If a selector is passed, then it will be used as a label selector to list the resources.
func (c *Client) ListClusterWideDynamicResources(gvr schema.GroupVersionResource, selector string) (*unstructured.UnstructuredList, error) {
if c.DynamicClient == nil {
return nil, nil
}
listOptions := metav1.ListOptions{}
if selector != "" {
listOptions.LabelSelector = selector
}
list, err := c.DynamicClient.Resource(gvr).List(context.TODO(), listOptions)
if err != nil {
if kerrors.IsNotFound(err) {
return &unstructured.UnstructuredList{}, nil
}
return nil, err
}
return list, nil
}
// GetDynamicResource returns an unstructured instance of a Custom Resource currently deployed in the active namespace
func (c *Client) GetDynamicResource(gvr schema.GroupVersionResource, name string) (*unstructured.Unstructured, error) {
res, err := c.DynamicClient.Resource(gvr).Namespace(c.Namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return res, nil
}
// UpdateDynamicResource updates a dynamic resource
func (c *Client) UpdateDynamicResource(gvr schema.GroupVersionResource, name string, u *unstructured.Unstructured) error {
_, err := c.DynamicClient.Resource(gvr).Namespace(c.Namespace).Update(context.TODO(), u, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}
type GVRN struct {
gvr schema.GroupVersionResource
name string
}
// DeleteDynamicResource deletes an instance, specified by name, of a Custom Resource
// if wait is true, it will set the PropagationPolicy to DeletePropagationForeground
// to wait for owned resources to be deleted (only for resources with a BlockOwnerDeletion set to true)
func (c *Client) DeleteDynamicResource(name string, gvr schema.GroupVersionResource, wait bool) error {
doDeleteResource := func() error {
return c.DynamicClient.Resource(gvr).Namespace(c.Namespace).Delete(context.TODO(), name, metav1.DeleteOptions{
PropagationPolicy: func(f metav1.DeletionPropagation) *metav1.DeletionPropagation {
if wait {
return &f
}
return nil
}(metav1.DeletePropagationForeground),
})
}
if !wait {
return doDeleteResource()
}
// Search resources referencing this resource without BlockOwnerDeletion, to handle waiting their deletion here
thisRes, err := c.GetDynamicResource(gvr, name)
if err != nil {
return err
}
all, err := c.GetAllResourcesFromSelector("", c.Namespace)
if err != nil {
return err
}
var toWait []GVRN
for _, res := range all {
ownerRefs := res.GetOwnerReferences()
for _, ownerRef := range ownerRefs {
if ownerRef.UID == thisRes.GetUID() {
if ownerRef.BlockOwnerDeletion == nil || !*ownerRef.BlockOwnerDeletion {
mapping, err2 := c.GetRestMappingFromUnstructured(res)
if err2 != nil {
return err2
}
toWait = append(toWait, GVRN{
gvr: mapping.Resource,
name: res.GetName(),
})
}
}
}
}
err = doDeleteResource()
if err != nil {
return err
}
err = c.WaitDynamicResourceDeleted(gvr, name)
if err != nil {
return err
}
for _, wait := range toWait {
err = c.WaitDynamicResourceDeleted(wait.gvr, wait.name)
if err != nil {
return err
}
}
return nil
}
// WaitDynamicResourceDeleted waits for the given resource to be deleted, with a timeout
func (c *Client) WaitDynamicResourceDeleted(gvr schema.GroupVersionResource, name string) error {
watcher, err := c.DynamicClient.Resource(gvr).Namespace(c.Namespace).Watch(context.TODO(), metav1.ListOptions{FieldSelector: "metadata.name=" + name})
if err != nil {
return err
}
defer watcher.Stop()
_, err = c.GetDynamicResource(gvr, name)
if err != nil {
// deletion is done if the resource does not exist
if kerrors.IsNotFound(err) {
return nil
}
return err
}
for {
select {
case <-time.After(time.Minute):
return fmt.Errorf("timeout while waiting for %q resource to be deleted", name)
case val, ok := <-watcher.ResultChan():
if !ok {
return errors.New("error getting value from resultchan")
}
if val.Type == watch.Deleted {
return nil
}
}
}
}