This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
/
client.go
115 lines (99 loc) · 3.33 KB
/
client.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
package e2e
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/gingersnap-project/operator/api/v1alpha1"
"github.com/gingersnap-project/operator/pkg/kubernetes/client"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
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/client-go/rest"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
)
type TestClient struct {
*client.Runtime
Ctx context.Context
Rest rest.Interface
}
func (c *TestClient) WriteAllResourcesToFile(dir string) {
// Operator Pod logs
printErr(os.MkdirAll(dir, os.ModePerm))
c.WriteKindToFile(dir, OperatorNamespace, "Pod", &corev1.PodList{}, map[string]string{"app.kubernetes.io/name": "gingersnap-operator"})
c.WriteKindToFile(dir, OperatorNamespace, "Cache", &v1alpha1.CacheList{}, nil)
c.WriteKindToFile(dir, OperatorNamespace, "EagerCacheRule", &v1alpha1.EagerCacheRuleList{}, nil)
c.WriteKindToFile(dir, OperatorNamespace, "LazyCacheRule", &v1alpha1.LazyCacheRuleList{}, nil)
}
func (c *TestClient) WriteKindToFile(dir, namespace, suffix string, list runtimeClient.ObjectList, set labels.Set) {
err := c.WithNamespace(namespace).List(set, list)
printErr(err)
if podList, ok := list.(*corev1.PodList); ok {
for _, pod := range podList.Items {
for _, container := range pod.Spec.Containers {
log, err := c.Logs(pod.Name, container.Name, namespace)
printErr(err)
fileName := fmt.Sprintf("%s/%s_%s.log", dir, pod.Name, container.Name)
err = ioutil.WriteFile(fileName, []byte(log), 0666)
printErr(err)
}
}
}
unstructuredResource, err := runtime.DefaultUnstructuredConverter.ToUnstructured(list)
printErr(err)
unstructuredResourceList := unstructured.UnstructuredList{}
unstructuredResourceList.SetUnstructuredContent(unstructuredResource)
for _, item := range unstructuredResourceList.Items {
yaml_, err := yaml.Marshal(item)
printErr(err)
fileName := fmt.Sprintf("%s/%s_%s.yaml", dir, item.GetName(), suffix)
err = ioutil.WriteFile(fileName, yaml_, 0666)
printErr(err)
}
}
func (c *TestClient) Logs(pod, container, namespace string) (string, error) {
req := c.Rest.Get().Namespace(namespace).Resource("pods").Name(pod).SubResource("log")
if container != "" {
req.Param("container", container)
}
readCloser, err := req.Stream(c.Ctx)
if err != nil {
return "", err
}
defer func() {
cerr := readCloser.Close()
if err == nil {
err = cerr
}
}()
body, err := ioutil.ReadAll(readCloser)
if err != nil {
return "", err
}
return string(body), err
}
func (c *TestClient) DeleteAllForeground(set map[string]string, obj runtimeClient.Object) error {
propagationPolicy := metav1.DeletePropagationForeground
err := runtimeClient.IgnoreNotFound(
c.Runtime.Client.DeleteAllOf(c.Ctx, obj, &runtimeClient.DeleteAllOfOptions{
ListOptions: runtimeClient.ListOptions{
Namespace: c.Namespace,
LabelSelector: labels.SelectorFromSet(set),
},
DeleteOptions: runtimeClient.DeleteOptions{
PropagationPolicy: &propagationPolicy,
},
}),
)
return err
}
// printErr if the error is not nil, printErr to stdout. Should only be used by test cleanup operations that shouldn't result
// in a test failing
func printErr(err error) {
if err != nil {
fmt.Printf("Encountered error: %v", err)
}
}