/
resource_handler.go
386 lines (367 loc) · 13.2 KB
/
resource_handler.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright 2019 The Operator-SDK 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 scorecard
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/operator-framework/operator-sdk/internal/util/yamlutil"
proxyConf "github.com/operator-framework/operator-sdk/pkg/ansible/proxy/kubeconfig"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"github.com/ghodss/yaml"
"github.com/spf13/viper"
appsv1 "k8s.io/api/apps/v1"
v1 "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/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type cleanupFn func() error
// waitUntilCRStatusExists waits until the status block of the CR currently being tested exists. If the timeout
// is reached, it simply continues and assumes there is no status block
func waitUntilCRStatusExists(cr *unstructured.Unstructured) error {
err := wait.Poll(time.Second*1, time.Second*time.Duration(viper.GetInt(InitTimeoutOpt)), func() (bool, error) {
err := runtimeClient.Get(context.TODO(), types.NamespacedName{Namespace: cr.GetNamespace(), Name: cr.GetName()}, cr)
if err != nil {
return false, fmt.Errorf("error getting custom resource: %v", err)
}
if cr.Object["status"] != nil {
return true, nil
}
return false, nil
})
if err != nil && err != wait.ErrWaitTimeout {
return err
}
return nil
}
// yamlToUnstructured decodes a yaml file into an unstructured object
func yamlToUnstructured(yamlPath string) (*unstructured.Unstructured, error) {
yamlFile, err := ioutil.ReadFile(yamlPath)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %v", yamlPath, err)
}
if bytes.Contains(yamlFile, []byte("\n---\n")) {
return nil, fmt.Errorf("custom resource manifest cannot have more than 1 resource")
}
obj := &unstructured.Unstructured{}
jsonSpec, err := yaml.YAMLToJSON(yamlFile)
if err != nil {
return nil, fmt.Errorf("could not convert yaml file to json: %v", err)
}
if err := obj.UnmarshalJSON(jsonSpec); err != nil {
return nil, fmt.Errorf("failed to unmarshal custom resource manifest to unstructured: %s", err)
}
// set the namespace
obj.SetNamespace(viper.GetString(NamespaceOpt))
return obj, nil
}
// createFromYAMLFile will take a path to a YAML file and create the resource. If it finds a
// deployment, it will add the scorecard proxy as a container in the deployments podspec.
func createFromYAMLFile(yamlPath string) error {
yamlSpecs, err := ioutil.ReadFile(yamlPath)
if err != nil {
return fmt.Errorf("failed to read file %s: %v", yamlPath, err)
}
scanner := yamlutil.NewYAMLScanner(yamlSpecs)
for scanner.Scan() {
obj := &unstructured.Unstructured{}
jsonSpec, err := yaml.YAMLToJSON(scanner.Bytes())
if err != nil {
return fmt.Errorf("could not convert yaml file to json: %v", err)
}
if err := obj.UnmarshalJSON(jsonSpec); err != nil {
return fmt.Errorf("could not unmarshal resource spec: %v", err)
}
obj.SetNamespace(viper.GetString(NamespaceOpt))
// dirty hack to merge scorecard proxy into operator deployment; lots of serialization and deserialization
if obj.GetKind() == "Deployment" {
// TODO: support multiple deployments
if deploymentName != "" {
return fmt.Errorf("scorecard currently does not support multiple deployments in the manifests")
}
dep, err := unstructuredToDeployment(obj)
if err != nil {
return fmt.Errorf("failed to convert object to deployment: %v", err)
}
deploymentName = dep.GetName()
err = createKubeconfigSecret()
if err != nil {
return fmt.Errorf("failed to create kubeconfig secret for scorecard-proxy: %v", err)
}
addMountKubeconfigSecret(dep)
addProxyContainer(dep)
// go back to unstructured to create
obj, err = deploymentToUnstructured(dep)
if err != nil {
return fmt.Errorf("failed to convert deployment to unstructured: %v", err)
}
}
err = runtimeClient.Create(context.TODO(), obj)
if err != nil {
_, restErr := 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) {
restMapper.Reset()
_, err := restMapper.RESTMappings(obj.GetObjectKind().GroupVersionKind().GroupKind())
if err != nil {
return false, nil
}
return true, nil
})
err = runtimeClient.Create(context.TODO(), obj)
if err != nil {
return err
}
}
addResourceCleanup(obj, types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()})
if obj.GetKind() == "Deployment" {
proxyPodGlobal, err = getPodFromDeployment(deploymentName, viper.GetString(NamespaceOpt))
if err != nil {
return err
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("failed to scan %s: (%v)", yamlPath, err)
}
return nil
}
// getPodFromDeployment returns a deployment depName's pod in namespace.
func getPodFromDeployment(depName, namespace string) (pod *v1.Pod, err error) {
dep := &appsv1.Deployment{}
err = runtimeClient.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: depName}, dep)
if err != nil {
return nil, fmt.Errorf("failed to get newly created deployment: %v", err)
}
set := labels.Set(dep.Spec.Selector.MatchLabels)
// In some cases, the pod from the old deployment will be picked up
// instead of the new one.
err = wait.PollImmediate(time.Second*1, time.Second*60, func() (bool, error) {
pods := &v1.PodList{}
err = runtimeClient.List(context.TODO(), &client.ListOptions{LabelSelector: set.AsSelector()}, pods)
if err != nil {
return false, fmt.Errorf("failed to get list of pods in deployment: %v", err)
}
// Make sure the pods exist. There should only be 1 pod per deployment.
if len(pods.Items) == 1 {
// If the pod has a deletion timestamp, it is the old pod; wait for
// pod with no deletion timestamp
if pods.Items[0].GetDeletionTimestamp() == nil {
pod = &pods.Items[0]
return true, nil
}
} else {
log.Debug("Operator deployment has more than 1 pod")
}
return false, nil
})
if err != nil {
return nil, fmt.Errorf("failed to get proxyPod: %s", err)
}
return pod, nil
}
// createKubeconfigSecret creates the secret that will be mounted in the operator's container and contains
// the kubeconfig for communicating with the proxy
func createKubeconfigSecret() error {
kubeconfigMap := make(map[string][]byte)
kc, err := proxyConf.Create(metav1.OwnerReference{Name: "scorecard"}, "http://localhost:8889", viper.GetString(NamespaceOpt))
if err != nil {
return err
}
defer func() {
if err := os.Remove(kc.Name()); err != nil {
log.Errorf("Failed to delete generated kubeconfig file: (%v)", err)
}
}()
kc, err = os.Open(kc.Name())
if err != nil {
return err
}
kcBytes, err := ioutil.ReadAll(kc)
if err != nil {
return err
}
kubeconfigMap["kubeconfig"] = kcBytes
kubeconfigSecret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "scorecard-kubeconfig",
Namespace: viper.GetString(NamespaceOpt),
},
Data: kubeconfigMap,
}
err = runtimeClient.Create(context.TODO(), kubeconfigSecret)
if err != nil {
return err
}
addResourceCleanup(kubeconfigSecret, types.NamespacedName{Namespace: kubeconfigSecret.GetNamespace(), Name: kubeconfigSecret.GetName()})
return nil
}
// addMountKubeconfigSecret creates the volume mount for the kubeconfig secret
func addMountKubeconfigSecret(dep *appsv1.Deployment) {
// create mount for secret
dep.Spec.Template.Spec.Volumes = append(dep.Spec.Template.Spec.Volumes, v1.Volume{
Name: "scorecard-kubeconfig",
VolumeSource: v1.VolumeSource{Secret: &v1.SecretVolumeSource{
SecretName: "scorecard-kubeconfig",
Items: []v1.KeyToPath{{
Key: "kubeconfig",
Path: "config",
}},
},
},
})
for index := range dep.Spec.Template.Spec.Containers {
// mount the volume
dep.Spec.Template.Spec.Containers[index].VolumeMounts = append(dep.Spec.Template.Spec.Containers[index].VolumeMounts, v1.VolumeMount{
Name: "scorecard-kubeconfig",
MountPath: "/scorecard-secret",
})
// specify the path via KUBECONFIG env var
dep.Spec.Template.Spec.Containers[index].Env = append(dep.Spec.Template.Spec.Containers[index].Env, v1.EnvVar{
Name: "KUBECONFIG",
Value: "/scorecard-secret/config",
})
}
}
// addProxyContainer adds the container spec for the scorecard-proxy to the deployment's podspec
func addProxyContainer(dep *appsv1.Deployment) {
pullPolicyString := viper.GetString(ProxyPullPolicyOpt)
var pullPolicy v1.PullPolicy
switch pullPolicyString {
case "Always":
pullPolicy = v1.PullAlways
case "Never":
pullPolicy = v1.PullNever
case "PullIfNotPresent":
pullPolicy = v1.PullIfNotPresent
default:
// this case shouldn't happen since we check the values in scorecard.go, but just in case, we'll default to always to prevent errors
pullPolicy = v1.PullAlways
}
dep.Spec.Template.Spec.Containers = append(dep.Spec.Template.Spec.Containers, v1.Container{
Name: scorecardContainerName,
Image: viper.GetString(ProxyImageOpt),
ImagePullPolicy: pullPolicy,
Command: []string{"scorecard-proxy"},
Env: []v1.EnvVar{{
Name: k8sutil.WatchNamespaceEnvVar,
ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"}},
}},
})
}
// unstructuredToDeployment converts an unstructured object to a deployment
func unstructuredToDeployment(obj *unstructured.Unstructured) (*appsv1.Deployment, error) {
jsonByte, err := obj.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("failed to convert deployment to json: %v", err)
}
depObj, _, err := dynamicDecoder.Decode(jsonByte, nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to decode deployment object: %v", err)
}
switch o := depObj.(type) {
case *appsv1.Deployment:
return o, nil
default:
return nil, fmt.Errorf("conversion of runtime object to deployment failed (resulting runtime object not deployment type)")
}
}
// deploymentToUnstructured converts a deployment to an unstructured object
func deploymentToUnstructured(dep *appsv1.Deployment) (*unstructured.Unstructured, error) {
jsonByte, err := json.Marshal(dep)
if err != nil {
return nil, fmt.Errorf("failed to remarshal deployment: %v", err)
}
obj := &unstructured.Unstructured{}
err = obj.UnmarshalJSON(jsonByte)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal updated deployment: %v", err)
}
return obj, nil
}
// cleanupScorecard runs all cleanup functions in reverse order
func cleanupScorecard() error {
failed := false
for i := len(cleanupFns) - 1; i >= 0; i-- {
err := cleanupFns[i]()
if err != nil {
failed = true
log.Printf("a cleanup function failed with error: %v\n", err)
}
}
if failed {
return fmt.Errorf("a cleanup function failed; see stdout for more details")
}
return nil
}
// addResourceCleanup adds a cleanup function for the specified runtime object
func addResourceCleanup(obj runtime.Object, key types.NamespacedName) {
cleanupFns = append(cleanupFns, func() error {
// make a copy of the object because the client changes it
objCopy := obj.DeepCopyObject()
err := runtimeClient.Delete(context.TODO(), obj)
if err != nil && !apierrors.IsNotFound(err) {
return err
}
err = wait.PollImmediate(time.Second*1, time.Second*10, func() (bool, error) {
err = runtimeClient.Get(context.TODO(), key, objCopy)
if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, fmt.Errorf("error encountered during deletion of resource type %v with namespace/name (%+v): %v", objCopy.GetObjectKind().GroupVersionKind().Kind, key, err)
}
return false, nil
})
if err != nil {
return fmt.Errorf("cleanup function failed: %v", err)
}
return nil
})
}
func getProxyLogs(proxyPod *v1.Pod) (string, error) {
// need a standard kubeclient for pod logs
kubeclient, err := kubernetes.NewForConfig(kubeconfig)
if err != nil {
return "", fmt.Errorf("failed to create kubeclient: %v", err)
}
logOpts := &v1.PodLogOptions{Container: scorecardContainerName}
req := kubeclient.CoreV1().Pods(proxyPod.GetNamespace()).GetLogs(proxyPod.GetName(), logOpts)
readCloser, err := req.Stream()
if err != nil {
return "", fmt.Errorf("failed to get logs: %v", err)
}
defer readCloser.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(readCloser)
if err != nil {
return "", fmt.Errorf("test failed and failed to read pod logs: %v", err)
}
return buf.String(), nil
}