generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 17
/
resource.go
192 lines (162 loc) · 5.4 KB
/
resource.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
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
package resources
import (
"context"
"github.com/kyma-project/serverless/tests/serverless/internal/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
)
type Resource struct {
ResCli dynamic.ResourceInterface
namespace string
kind string
verbose bool
log *logrus.Entry
}
func New(dynamicCli dynamic.Interface, s schema.GroupVersionResource, namespace string, log *logrus.Entry, verbose bool) *Resource {
resCli := dynamicCli.Resource(s).Namespace(namespace)
return &Resource{ResCli: resCli, namespace: namespace, kind: s.Resource, log: log, verbose: verbose}
}
func (r *Resource) Create(res interface{}) (string, error) {
var resourceVersion string
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(res)
if err != nil {
return resourceVersion, errors.Wrapf(err, "while converting resource %s %+v to unstructured", r.kind, res)
}
unstructuredObj := &unstructured.Unstructured{
Object: u,
}
err = utils.OnTimeout(utils.DefaultBackoff, func() error {
var resource *unstructured.Unstructured
resource, err = r.ResCli.Create(context.Background(), unstructuredObj, metav1.CreateOptions{})
if err != nil {
return err
}
resourceVersion = resource.GetResourceVersion()
return nil
}, r.log)
if err != nil {
return resourceVersion, errors.Wrapf(err, "while creating resource %s", unstructuredObj.GetKind())
}
if r.verbose {
r.log.Infof("[CREATE]: name %s, namespace %s, resource %v", unstructuredObj.GetName(), unstructuredObj.GetNamespace(), unstructuredObj)
}
return resourceVersion, nil
}
func (r *Resource) List(set map[string]string) (*unstructured.UnstructuredList, error) {
var result *unstructured.UnstructuredList
selector := labels.SelectorFromSet(set).String()
err := utils.OnTimeout(utils.DefaultBackoff, func() error {
var err error
result, err = r.ResCli.List(context.Background(), metav1.ListOptions{
LabelSelector: selector,
})
return err
}, r.log)
if err != nil {
return nil, errors.Wrapf(err, "while listing resource %s in namespace %s", r.kind, r.namespace)
}
namespace := "-"
if r.namespace != "" {
namespace = r.namespace
}
if r.verbose {
r.log.Infof("LIST %s: namespace:%s kind:%s\n%v", selector, namespace, r.kind, result)
}
return result, nil
}
func (r *Resource) Get(name string) (*unstructured.Unstructured, error) {
var result *unstructured.Unstructured
err := utils.OnTimeout(utils.DefaultBackoff, func() error {
var err error
result, err = r.ResCli.Get(context.Background(), name, metav1.GetOptions{})
return err
}, r.log)
if err != nil {
return nil, errors.Wrapf(err, "while getting resource %s '%s'", r.kind, name)
}
namespace := "-"
if r.namespace != "" {
namespace = r.namespace
}
if r.verbose {
r.log.Infof("GET name:%s: namespace:%s kind:%s\n%v", name, namespace, r.kind, result)
}
return result, nil
}
func WaitUntilConditionSatisfied(ctx context.Context, resCli dynamic.ResourceInterface, isReady func(event watch.Event) (bool, error)) error {
watcher, err := resCli.Watch(ctx, metav1.ListOptions{})
defer func() {
if watcher != nil {
watcher.Stop()
}
}()
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case result := <-watcher.ResultChan():
ready, err := isReady(result)
if err != nil {
return err
}
if ready {
return nil
}
}
}
}
func (r *Resource) Delete(name string) error {
return utils.WithIgnoreOnNotFound(utils.DefaultBackoff, func() error {
namespace := "-"
if r.namespace != "" {
namespace = r.namespace
}
if r.verbose {
r.log.Infof("DELETE %s: namespace:%s name:%s", r.kind, namespace, name)
}
// if the DeletePropagationForeground is not enough then we'll need to somehow watch specified resource
// and make sure that it was deleted manually
// in the moment of writing this comment we do not have such a case in those tests
// that's why we'll just hope DeletePropagationForeground is enough
deletePropagationPolicy := metav1.DeletePropagationForeground
return r.ResCli.Delete(context.Background(), name, metav1.DeleteOptions{
PropagationPolicy: &deletePropagationPolicy,
})
}, r.log)
}
func (r *Resource) Update(res interface{}) (*unstructured.Unstructured, error) {
// https://github.com/kubernetes/client-go/blob/kubernetes-1.17.4/examples/dynamic-create-update-delete-deployment/main.go#L119-L166
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(res)
if err != nil {
return nil, errors.Wrapf(err, "while converting resource %s %s to unstructured", r.kind, res)
}
unstructuredObj := &unstructured.Unstructured{
Object: u,
}
var result *unstructured.Unstructured
result, err = r.ResCli.Update(context.Background(), unstructuredObj, metav1.UpdateOptions{})
if err != nil {
// upstream caller RetryOnConflict doesn't work with wrapped errors
// https://github.com/kubernetes/client-go/blob/9927afa2880713c4332723b7f0865adee5e63a7b/util/retry/util.go#L89-L93
return nil, err
}
namespace := "-"
if r.namespace != "" {
namespace = r.namespace
}
r.log.Infof("UPDATE %s: namespace:%s kind:%s", result.GetName(), namespace, r.kind)
if r.verbose {
r.log.Infof("%+v", result)
}
return result, nil
}