|
1 | 1 | package framework
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "testing" |
| 4 | + "errors" |
| 5 | + "strings" |
5 | 6 |
|
| 7 | + y2j "github.com/ghodss/yaml" |
| 8 | + yaml "gopkg.in/yaml.v2" |
| 9 | + apps "k8s.io/api/apps/v1" |
6 | 10 | core "k8s.io/api/core/v1"
|
| 11 | + "k8s.io/api/rbac/v1beta1" |
| 12 | + crd "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" |
| 13 | + extensions_scheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme" |
| 14 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
7 | 15 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
| 16 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 17 | + "k8s.io/apimachinery/pkg/runtime/serializer" |
| 18 | + "k8s.io/client-go/kubernetes/scheme" |
| 19 | + "k8s.io/client-go/rest" |
8 | 20 | )
|
9 | 21 |
|
10 |
| -func (ctx *TestCtx) CreateNamespace(f *Framework, t *testing.T) (string, error) { |
| 22 | +func (ctx *TestCtx) GetNamespace() (string, error) { |
| 23 | + if ctx.Namespace != "" { |
| 24 | + return ctx.Namespace, nil |
| 25 | + } |
11 | 26 | // create namespace
|
12 |
| - namespace := ctx.GetObjID() |
13 |
| - namespaceObj := &core.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}} |
14 |
| - _, err := f.KubeClient.CoreV1().Namespaces().Create(namespaceObj) |
| 27 | + ctx.Namespace = ctx.GetID() |
| 28 | + namespaceObj := &core.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ctx.Namespace}} |
| 29 | + _, err := Global.KubeClient.CoreV1().Namespaces().Create(namespaceObj) |
15 | 30 | if err != nil {
|
16 | 31 | return "", err
|
17 | 32 | }
|
18 |
| - t.Logf("Created namespace: %s", namespace) |
19 |
| - ctx.AddFinalizerFn(func() error { return f.KubeClient.CoreV1().Namespaces().Delete(namespace, metav1.NewDeleteOptions(0)) }) |
20 |
| - return namespace, nil |
| 33 | + ctx.AddFinalizerFn(func() error { |
| 34 | + return Global.KubeClient.CoreV1().Namespaces().Delete(ctx.Namespace, metav1.NewDeleteOptions(0)) |
| 35 | + }) |
| 36 | + return ctx.Namespace, nil |
| 37 | +} |
| 38 | + |
| 39 | +func (ctx *TestCtx) GetCRClient(yamlCR []byte) (*rest.RESTClient, error) { |
| 40 | + if ctx.CRClient != nil { |
| 41 | + return ctx.CRClient, nil |
| 42 | + } |
| 43 | + // get new RESTClient for custom resources |
| 44 | + crConfig := Global.KubeConfig |
| 45 | + yamlMap := make(map[interface{}]interface{}) |
| 46 | + err := yaml.Unmarshal(yamlCR, &yamlMap) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + groupVersion := strings.Split(yamlMap["apiVersion"].(string), "/") |
| 51 | + crGV := schema.GroupVersion{Group: groupVersion[0], Version: groupVersion[1]} |
| 52 | + crConfig.GroupVersion = &crGV |
| 53 | + crConfig.APIPath = "/apis" |
| 54 | + crConfig.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs} |
| 55 | + |
| 56 | + if crConfig.UserAgent == "" { |
| 57 | + crConfig.UserAgent = rest.DefaultKubernetesUserAgent() |
| 58 | + } |
| 59 | + ctx.CRClient, err = rest.RESTClientFor(crConfig) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + return ctx.CRClient, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (ctx *TestCtx) createCRFromYAML(yamlFile []byte, resourceName string) error { |
| 67 | + client, err := ctx.GetCRClient(yamlFile) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + namespace, err := ctx.GetNamespace() |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + jsonDat, err := y2j.YAMLToJSON(yamlFile) |
| 76 | + err = client.Post(). |
| 77 | + Namespace(namespace). |
| 78 | + Resource(resourceName). |
| 79 | + Body(jsonDat). |
| 80 | + Do(). |
| 81 | + Error() |
| 82 | + ctx.AddFinalizerFn(func() error { |
| 83 | + return client.Delete(). |
| 84 | + Namespace(namespace). |
| 85 | + Resource(resourceName). |
| 86 | + Body(metav1.NewDeleteOptions(0)). |
| 87 | + Do(). |
| 88 | + Error() |
| 89 | + }) |
| 90 | + return err |
| 91 | +} |
| 92 | + |
| 93 | +func (ctx *TestCtx) createCRDFromYAML(yamlFile []byte) error { |
| 94 | + decode := extensions_scheme.Codecs.UniversalDeserializer().Decode |
| 95 | + obj, _, err := decode(yamlFile, nil, nil) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + switch o := obj.(type) { |
| 100 | + case *crd.CustomResourceDefinition: |
| 101 | + _, err = Global.ExtensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(o) |
| 102 | + ctx.AddFinalizerFn(func() error { |
| 103 | + err = Global.ExtensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(o.Name, metav1.NewDeleteOptions(0)) |
| 104 | + if err != nil && !apierrors.IsNotFound(err) { |
| 105 | + return err |
| 106 | + } |
| 107 | + return nil |
| 108 | + }) |
| 109 | + if apierrors.IsAlreadyExists(err) { |
| 110 | + return nil |
| 111 | + } |
| 112 | + return err |
| 113 | + default: |
| 114 | + return errors.New("Non-CRD resource in createCRDFromYAML function") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func (ctx *TestCtx) CreateFromYAML(yamlFile []byte) error { |
| 119 | + yamlMap := make(map[interface{}]interface{}) |
| 120 | + err := yaml.Unmarshal(yamlFile, &yamlMap) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + kind := yamlMap["kind"].(string) |
| 125 | + switch kind { |
| 126 | + case "Role": |
| 127 | + case "RoleBinding": |
| 128 | + case "Deployment": |
| 129 | + case "CustomResourceDefinition": |
| 130 | + return ctx.createCRDFromYAML(yamlFile) |
| 131 | + // we assume that all custom resources are from operator-sdk and thus follow |
| 132 | + // a common naming convention |
| 133 | + default: |
| 134 | + return ctx.createCRFromYAML(yamlFile, strings.ToLower(kind)+"s") |
| 135 | + } |
| 136 | + |
| 137 | + decode := scheme.Codecs.UniversalDeserializer().Decode |
| 138 | + obj, _, err := decode(yamlFile, nil, nil) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + namespace, err := ctx.GetNamespace() |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + switch o := obj.(type) { |
| 148 | + case *v1beta1.Role: |
| 149 | + _, err = Global.KubeClient.RbacV1beta1().Roles(namespace).Create(o) |
| 150 | + ctx.AddFinalizerFn(func() error { |
| 151 | + return Global.KubeClient.RbacV1beta1().Roles(namespace).Delete(o.Name, metav1.NewDeleteOptions(0)) |
| 152 | + }) |
| 153 | + return err |
| 154 | + case *v1beta1.RoleBinding: |
| 155 | + _, err = Global.KubeClient.RbacV1beta1().RoleBindings(namespace).Create(o) |
| 156 | + ctx.AddFinalizerFn(func() error { |
| 157 | + return Global.KubeClient.RbacV1beta1().RoleBindings(namespace).Delete(o.Name, metav1.NewDeleteOptions(0)) |
| 158 | + }) |
| 159 | + return err |
| 160 | + case *apps.Deployment: |
| 161 | + _, err = Global.KubeClient.AppsV1().Deployments(namespace).Create(o) |
| 162 | + ctx.AddFinalizerFn(func() error { |
| 163 | + return Global.KubeClient.AppsV1().Deployments(namespace).Delete(o.Name, metav1.NewDeleteOptions(0)) |
| 164 | + }) |
| 165 | + return err |
| 166 | + default: |
| 167 | + return errors.New("Unhandled resource type") |
| 168 | + } |
21 | 169 | }
|
0 commit comments