-
Notifications
You must be signed in to change notification settings - Fork 27
/
resource.go
218 lines (199 loc) · 5.92 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
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
package framework
import (
"bufio"
"bytes"
goctx "context"
"fmt"
"io"
"io/ioutil"
"os"
"time"
"golang.org/x/net/context"
core "k8s.io/api/core/v1"
apierrors "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/util/wait"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/yaml"
)
const maxExecutiveEmpties = 100
// Scanner scans a yaml manifest file for manifest tokens delimited by "---".
// See bufio.Scanner for semantics.
type Scanner struct {
reader *k8syaml.YAMLReader
token []byte // Last token returned by split.
err error // Sticky error.
empties int // Count of successive empty tokens.
done bool // Scan has finished.
}
func NewYAMLScanner(r io.Reader) *Scanner {
return &Scanner{reader: k8syaml.NewYAMLReader(bufio.NewReader(r))}
}
func (s *Scanner) Err() error {
if s.err == io.EOF {
return nil
}
return s.err
}
func (s *Scanner) Scan() bool {
if s.done {
return false
}
var (
tok []byte
err error
)
for {
tok, err = s.reader.Read()
if err != nil {
if err == io.EOF {
s.done = true
}
s.err = err
return false
}
if len(bytes.TrimSpace(tok)) == 0 {
s.empties++
if s.empties > maxExecutiveEmpties {
panic("yaml.Scan: too many empty tokens without progressing")
}
continue
}
s.empties = 0
s.token = tok
return true
}
}
func (s *Scanner) Text() string {
return string(s.token)
}
func (s *Scanner) Bytes() []byte {
return s.token
}
// TODO: remove before 1.0.0
// Deprecated: GetNamespace() exists for historical compatibility.
// Use GetOperatorNamespace() or GetWatchNamespace() instead
func (ctx *Context) GetNamespace() (string, error) {
var err error
ctx.namespace, err = ctx.getNamespace(ctx.namespace)
return ctx.namespace, err
}
// GetOperatorNamespace will return an Operator Namespace,
// if the flag --operator-namespace not be used (TestOpeatorNamespaceEnv not set)
// then it will create a new namespace with randon name and return that namespace
func (ctx *Context) GetOperatorNamespace() (string, error) {
var err error
ctx.operatorNamespace, err = ctx.getNamespace(ctx.operatorNamespace)
return ctx.operatorNamespace, err
}
func (ctx *Context) getNamespace(ns string) (string, error) {
if ns != "" {
return ns, nil
}
// create namespace
ns = ctx.GetID()
namespaceObj := &core.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
Labels: map[string]string{
"pod-security.kubernetes.io/enforce": "privileged",
"security.openshift.io/scc.podSecurityLabelSync": "false",
},
},
}
fmt.Println(namespaceObj)
_, err := ctx.kubeclient.CoreV1().Namespaces().Create(context.TODO(), namespaceObj, metav1.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
return "", fmt.Errorf("namespace %s already exists: %w", ns, err)
} else if err != nil {
return "", err
}
ctx.AddCleanupFn(func() error {
gracePeriodSeconds := int64(0)
opts := metav1.DeleteOptions{GracePeriodSeconds: &gracePeriodSeconds}
return ctx.kubeclient.CoreV1().Namespaces().Delete(context.TODO(), ns, opts)
})
return ns, nil
}
// GetWatchNamespace will return the namespaces to operator
// watch for changes, if the flag --watch-namespaced not be used
// then it will return the Operator Namespace.
func (ctx *Context) GetWatchNamespace() (string, error) {
// if ctx.watchNamespace is already set and not "";
// then return ctx.watchnamespace
if ctx.watchNamespace != "" {
return ctx.watchNamespace, nil
}
// if ctx.watchNamespace == "";
// ensure it was set explicitly using TestWatchNamespaceEnv
if ns, ok := os.LookupEnv(TestWatchNamespaceEnv); ok {
return ns, nil
}
// get ctx.operatorNamespace (use ctx.GetOperatorNamespace()
// to make sure ctx.operatorNamespace is not "")
operatorNamespace, err := ctx.GetOperatorNamespace()
if err != nil {
return "", nil
}
ctx.watchNamespace = operatorNamespace
return ctx.watchNamespace, nil
}
func (ctx *Context) createFromYAML(yamlFile []byte, skipIfExists bool, cleanupOptions *CleanupOptions) error {
operatorNamespace, err := ctx.GetOperatorNamespace()
if err != nil {
return err
}
scanner := NewYAMLScanner(bytes.NewBuffer(yamlFile))
for scanner.Scan() {
yamlSpec := scanner.Bytes()
obj := &unstructured.Unstructured{}
jsonSpec, err := yaml.YAMLToJSON(yamlSpec)
if err != nil {
return fmt.Errorf("could not convert yaml file to json: %w", err)
}
if err := obj.UnmarshalJSON(jsonSpec); err != nil {
return fmt.Errorf("failed to unmarshal object spec: %w", err)
}
obj.SetNamespace(operatorNamespace)
err = ctx.client.Create(goctx.TODO(), obj, cleanupOptions)
if skipIfExists && apierrors.IsAlreadyExists(err) {
continue
}
if err != nil {
_, restErr := ctx.restMapper.RESTMappings(obj.GetObjectKind().GroupVersionKind().GroupKind())
if restErr == nil {
return err
}
// don't store error, as only error will be timeout. Error from runtime client will be easier for
// the user to understand than the timeout error, so just use that if we fail
_ = wait.PollImmediate(time.Second*1, time.Second*10, func() (bool, error) {
ctx.restMapper.Reset()
_, err := ctx.restMapper.RESTMappings(obj.GetObjectKind().GroupVersionKind().GroupKind())
if err != nil {
return false, nil
}
return true, nil
})
err = ctx.client.Create(goctx.TODO(), obj, cleanupOptions)
if skipIfExists && apierrors.IsAlreadyExists(err) {
continue
}
if err != nil {
return err
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("failed to scan manifest: %w", err)
}
return nil
}
func (ctx *Context) InitializeClusterResources(cleanupOptions *CleanupOptions) error {
// create namespaced resources
namespacedYAML, err := ioutil.ReadFile(ctx.namespacedManPath)
if err != nil {
return fmt.Errorf("failed to read namespaced manifest: %w", err)
}
return ctx.createFromYAML(namespacedYAML, false, cleanupOptions)
}