generated from kubernetes/kubernetes-template-project
/
resource.go
193 lines (148 loc) · 5.02 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
193
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package res
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
cr "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/e2e-framework/klient/k8s"
)
type Resources struct {
// config is the rest.Config to talk to an apiserver
config *rest.Config
// scheme will be used to map go structs to GroupVersionKinds
scheme *runtime.Scheme
// client is a wrapper for controller runtime client
client cr.Client
}
// Res instantiates the controller runtime client
// object. User can get panic for belopw scenarios.
// 1. if user does not provide k8s config
// 2. if controller runtime client instantiation fails.
func Res(cfg *rest.Config) *Resources {
if cfg == nil {
// TODO: logging
fmt.Println("must provide rest.Config")
panic(errors.New("must provide rest.Config"))
}
cl, err := cr.New(cfg, cr.Options{Scheme: scheme.Scheme})
if err != nil {
// TODO: log error
panic(err)
}
res := &Resources{
config: cfg,
scheme: scheme.Scheme,
client: cl,
}
return res
}
func (r *Resources) Get(ctx context.Context, name, namespace string, obj k8s.Object) error {
return r.client.Get(ctx, cr.ObjectKey{Namespace: namespace, Name: name}, obj)
}
type CreateOption func(*metav1.CreateOptions)
func (r *Resources) Create(ctx context.Context, obj k8s.Object, opts ...CreateOption) error {
createOptions := &metav1.CreateOptions{}
for _, fn := range opts {
fn(createOptions)
}
o := &cr.CreateOptions{Raw: createOptions}
return r.client.Create(ctx, obj, o)
}
type UpdateOption func(*metav1.UpdateOptions)
func (r *Resources) Update(ctx context.Context, obj k8s.Object, opts ...UpdateOption) error {
updateOptions := &metav1.UpdateOptions{}
for _, fn := range opts {
fn(updateOptions)
}
o := &cr.UpdateOptions{Raw: updateOptions}
return r.client.Update(ctx, obj, o)
}
type DeleteOption func(*metav1.DeleteOptions)
func (r *Resources) Delete(ctx context.Context, obj k8s.Object, opts ...DeleteOption) error {
deleteOptions := &metav1.DeleteOptions{}
for _, fn := range opts {
fn(deleteOptions)
}
o := &cr.DeleteOptions{Raw: deleteOptions}
return r.client.Delete(ctx, obj, o)
}
func WithGracePeriod(gpt time.Duration) DeleteOption {
t := gpt.Milliseconds()
return func(do *metav1.DeleteOptions) { do.GracePeriodSeconds = &t }
}
func WithDeletePropagation(prop string) DeleteOption {
p := metav1.DeletionPropagation(prop)
return func(do *metav1.DeleteOptions) { do.PropagationPolicy = &p }
}
type ListOption func(*metav1.ListOptions)
func (r *Resources) List(ctx context.Context, objs k8s.ObjectList, opts ...ListOption) error {
listOptions := &metav1.ListOptions{}
for _, fn := range opts {
fn(listOptions)
}
o := &cr.ListOptions{Raw: listOptions}
return r.client.List(ctx, objs, o)
}
func WithLabelSelector(sel string) ListOption {
return func(lo *metav1.ListOptions) { lo.LabelSelector = sel }
}
func WithFieldSelector(sel string) ListOption {
return func(lo *metav1.ListOptions) { lo.FieldSelector = sel }
}
func WithTimeout(to time.Duration) ListOption {
t := to.Milliseconds()
return func(lo *metav1.ListOptions) { lo.TimeoutSeconds = &t }
}
// Patch is a patch that can be applied to a Kubernetes object.
type Patch struct {
// Type is the PatchType of the patch.
patchType types.PatchType
// Data is the raw data representing the patch.
data []byte
}
// PatchOption is used to provide additional arguments to the Patch call.
type PatchOption func(*metav1.PatchOptions)
// Patch patches portion of object `orig` with data from object `patch`
func (r *Resources) Patch(ctx context.Context, objs k8s.Object, patch Patch, opts ...PatchOption) error {
patchOptions := &metav1.PatchOptions{}
for _, fn := range opts {
fn(patchOptions)
}
p := cr.RawPatch(patch.patchType, patch.data)
o := &cr.PatchOptions{Raw: patchOptions}
return r.client.Patch(ctx, objs, p, o)
}
func (r *Resources) BuildFromJSON(obj k8s.Object, jsonData string) error {
return json.Unmarshal([]byte(jsonData), &obj)
}
func (r *Resources) SetAnnotate(obj k8s.Object, annotation map[string]string) {
obj.SetAnnotations(annotation)
}
func (r *Resources) GetAnnotate(obj k8s.Object) map[string]string {
return obj.GetAnnotations()
}
func (r *Resources) SetLabel(obj k8s.Object, label map[string]string) {
obj.SetLabels(label)
}
func (r *Resources) GetLabel(obj k8s.Object) map[string]string {
return obj.GetLabels()
}