Skip to content

Commit 851e600

Browse files
committed
pkg/test: remove unused cr helper functions
1 parent 34f82f9 commit 851e600

File tree

2 files changed

+1
-137
lines changed

2 files changed

+1
-137
lines changed

pkg/test/context.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ import (
2020
"strings"
2121
"testing"
2222
"time"
23-
24-
"k8s.io/client-go/rest"
2523
)
2624

2725
type TestCtx struct {
2826
ID string
2927
CleanUpFns []finalizerFn
3028
Namespace string
31-
CRClient *rest.RESTClient
3229
}
3330

3431
type finalizerFn func() error

pkg/test/resource_creator.go

Lines changed: 1 addition & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,13 @@ package test
1717
import (
1818
"bytes"
1919
goctx "context"
20-
"errors"
2120
"fmt"
2221
"io/ioutil"
23-
"strings"
2422

25-
y2j "github.com/ghodss/yaml"
2623
yaml "gopkg.in/yaml.v2"
2724
core "k8s.io/api/core/v1"
28-
crd "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
29-
extensions_scheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
3025
apierrors "k8s.io/apimachinery/pkg/api/errors"
3126
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32-
"k8s.io/apimachinery/pkg/runtime/schema"
33-
"k8s.io/apimachinery/pkg/runtime/serializer"
34-
"k8s.io/apimachinery/pkg/types"
35-
"k8s.io/client-go/kubernetes/scheme"
36-
"k8s.io/client-go/rest"
3727
)
3828

3929
func (ctx *TestCtx) GetNamespace() (string, error) {
@@ -55,119 +45,6 @@ func (ctx *TestCtx) GetNamespace() (string, error) {
5545
return ctx.Namespace, nil
5646
}
5747

58-
func (ctx *TestCtx) GetCRClient(yamlCR []byte) (*rest.RESTClient, error) {
59-
if ctx.CRClient != nil {
60-
return ctx.CRClient, nil
61-
}
62-
// a user may pass nil if they expect the CRClient to already exist
63-
if yamlCR == nil {
64-
return nil, errors.New("ctx.CRClient does not exist; yamlCR cannot be nil")
65-
}
66-
// get new RESTClient for custom resources
67-
crConfig := Global.KubeConfig
68-
yamlMap := make(map[interface{}]interface{})
69-
err := yaml.Unmarshal(yamlCR, &yamlMap)
70-
if err != nil {
71-
return nil, err
72-
}
73-
groupVersion := strings.Split(yamlMap["apiVersion"].(string), "/")
74-
crGV := schema.GroupVersion{Group: groupVersion[0], Version: groupVersion[1]}
75-
crConfig.GroupVersion = &crGV
76-
crConfig.APIPath = "/apis"
77-
crConfig.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
78-
79-
if crConfig.UserAgent == "" {
80-
crConfig.UserAgent = rest.DefaultKubernetesUserAgent()
81-
}
82-
ctx.CRClient, err = rest.RESTClientFor(crConfig)
83-
return ctx.CRClient, err
84-
}
85-
86-
// TODO: Implement a way for a user to add their own scheme to us the dynamic
87-
// client to eliminate the need for the UpdateCR function
88-
89-
// UpdateCR takes the name of a resource, the resource plural name,
90-
// the path of the field that need to be updated (e.g. /spec/size),
91-
// and the new value to that field and patches the resource with
92-
// that change
93-
func (ctx *TestCtx) UpdateCR(name, resourceName, path, value string) error {
94-
crClient, err := ctx.GetCRClient(nil)
95-
if err != nil {
96-
return err
97-
}
98-
namespace, err := ctx.GetNamespace()
99-
if err != nil {
100-
return err
101-
}
102-
return crClient.Patch(types.JSONPatchType).
103-
Namespace(namespace).
104-
Resource(resourceName).
105-
Name(name).
106-
Body([]byte("[{\"op\": \"replace\", \"path\": \"" + path + "\", \"value\": " + value + "}]")).
107-
Do().
108-
Error()
109-
}
110-
111-
func (ctx *TestCtx) createCRFromYAML(yamlFile []byte, resourceName string) error {
112-
client, err := ctx.GetCRClient(yamlFile)
113-
if err != nil {
114-
return err
115-
}
116-
namespace, err := ctx.GetNamespace()
117-
if err != nil {
118-
return err
119-
}
120-
yamlMap := make(map[interface{}]interface{})
121-
err = yaml.Unmarshal(yamlFile, &yamlMap)
122-
if err != nil {
123-
return err
124-
}
125-
// TODO: handle failure of this line without segfault
126-
name := yamlMap["metadata"].(map[interface{}]interface{})["name"].(string)
127-
jsonDat, err := y2j.YAMLToJSON(yamlFile)
128-
err = client.Post().
129-
Namespace(namespace).
130-
Resource(resourceName).
131-
Body(jsonDat).
132-
Do().
133-
Error()
134-
ctx.AddFinalizerFn(func() error {
135-
return client.Delete().
136-
Namespace(namespace).
137-
Resource(resourceName).
138-
Name(name).
139-
Body(metav1.NewDeleteOptions(0)).
140-
Do().
141-
Error()
142-
})
143-
return err
144-
}
145-
146-
func (ctx *TestCtx) createCRDFromYAML(yamlFile []byte) error {
147-
decode := extensions_scheme.Codecs.UniversalDeserializer().Decode
148-
obj, _, err := decode(yamlFile, nil, nil)
149-
if err != nil {
150-
return err
151-
}
152-
switch o := obj.(type) {
153-
case *crd.CustomResourceDefinition:
154-
_, err = Global.ExtensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(o)
155-
ctx.AddFinalizerFn(func() error {
156-
err = Global.ExtensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(o.Name, metav1.NewDeleteOptions(0))
157-
if err != nil && !apierrors.IsNotFound(err) {
158-
return err
159-
}
160-
return nil
161-
})
162-
if apierrors.IsAlreadyExists(err) {
163-
return nil
164-
}
165-
return err
166-
default:
167-
return errors.New("non-CRD resource in createCRDFromYAML function")
168-
}
169-
}
170-
17148
func setNamespaceYAML(yamlFile []byte, namespace string) ([]byte, error) {
17249
yamlMap := make(map[interface{}]interface{})
17350
err := yaml.Unmarshal(yamlFile, &yamlMap)
@@ -192,17 +69,7 @@ func (ctx *TestCtx) CreateFromYAML(yamlFile []byte) error {
19269

19370
obj, _, err := Global.DynamicDecoder.Decode(yamlSpec, nil, nil)
19471
if err != nil {
195-
yamlMap := make(map[interface{}]interface{})
196-
err = yaml.Unmarshal(yamlSpec, &yamlMap)
197-
if err != nil {
198-
return err
199-
}
200-
kind := yamlMap["kind"].(string)
201-
err = ctx.createCRFromYAML(yamlSpec, strings.ToLower(kind)+"s")
202-
if err != nil {
203-
return err
204-
}
205-
continue
72+
return err
20673
}
20774

20875
err = Global.DynamicClient.Create(goctx.TODO(), obj)

0 commit comments

Comments
 (0)