Skip to content

Commit

Permalink
pkg/test: remove unused cr helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNPavel committed Aug 10, 2018
1 parent 7352f48 commit f288683
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 137 deletions.
3 changes: 0 additions & 3 deletions pkg/test/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ import (
"strings"
"testing"
"time"

"k8s.io/client-go/rest"
)

type TestCtx struct {
ID string
CleanUpFns []finalizerFn
Namespace string
CRClient *rest.RESTClient
}

type finalizerFn func() error
Expand Down
135 changes: 1 addition & 134 deletions pkg/test/resource_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,13 @@ package test
import (
"bytes"
goctx "context"
"errors"
"fmt"
"io/ioutil"
"strings"

y2j "github.com/ghodss/yaml"
yaml "gopkg.in/yaml.v2"
core "k8s.io/api/core/v1"
crd "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
extensions_scheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)

func (ctx *TestCtx) GetNamespace() (string, error) {
Expand All @@ -55,119 +45,6 @@ func (ctx *TestCtx) GetNamespace() (string, error) {
return ctx.Namespace, nil
}

func (ctx *TestCtx) GetCRClient(yamlCR []byte) (*rest.RESTClient, error) {
if ctx.CRClient != nil {
return ctx.CRClient, nil
}
// a user may pass nil if they expect the CRClient to already exist
if yamlCR == nil {
return nil, errors.New("CRClient does not exist; yamlCR cannot be nil")
}
// get new RESTClient for custom resources
crConfig := Global.KubeConfig
yamlMap := make(map[interface{}]interface{})
err := yaml.Unmarshal(yamlCR, &yamlMap)
if err != nil {
return nil, err
}
groupVersion := strings.Split(yamlMap["apiVersion"].(string), "/")
crGV := schema.GroupVersion{Group: groupVersion[0], Version: groupVersion[1]}
crConfig.GroupVersion = &crGV
crConfig.APIPath = "/apis"
crConfig.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}

if crConfig.UserAgent == "" {
crConfig.UserAgent = rest.DefaultKubernetesUserAgent()
}
ctx.CRClient, err = rest.RESTClientFor(crConfig)
return ctx.CRClient, err
}

// TODO: Implement a way for a user to add their own scheme to us the dynamic
// client to eliminate the need for the UpdateCR function

// UpdateCR takes the name of a resource, the resource plural name,
// the path of the field that need to be updated (e.g. /spec/size),
// and the new value to that field and patches the resource with
// that change
func (ctx *TestCtx) UpdateCR(name, resourceName, path, value string) error {
crClient, err := ctx.GetCRClient(nil)
if err != nil {
return err
}
namespace, err := ctx.GetNamespace()
if err != nil {
return err
}
return crClient.Patch(types.JSONPatchType).
Namespace(namespace).
Resource(resourceName).
Name(name).
Body([]byte("[{\"op\": \"replace\", \"path\": \"" + path + "\", \"value\": " + value + "}]")).
Do().
Error()
}

func (ctx *TestCtx) createCRFromYAML(yamlFile []byte, resourceName string) error {
client, err := ctx.GetCRClient(yamlFile)
if err != nil {
return err
}
namespace, err := ctx.GetNamespace()
if err != nil {
return err
}
yamlMap := make(map[interface{}]interface{})
err = yaml.Unmarshal(yamlFile, &yamlMap)
if err != nil {
return err
}
// TODO: handle failure of this line without segfault
name := yamlMap["metadata"].(map[interface{}]interface{})["name"].(string)
jsonDat, err := y2j.YAMLToJSON(yamlFile)
err = client.Post().
Namespace(namespace).
Resource(resourceName).
Body(jsonDat).
Do().
Error()
ctx.AddFinalizerFn(func() error {
return client.Delete().
Namespace(namespace).
Resource(resourceName).
Name(name).
Body(metav1.NewDeleteOptions(0)).
Do().
Error()
})
return err
}

func (ctx *TestCtx) createCRDFromYAML(yamlFile []byte) error {
decode := extensions_scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode(yamlFile, nil, nil)
if err != nil {
return err
}
switch o := obj.(type) {
case *crd.CustomResourceDefinition:
_, err = Global.ExtensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(o)
ctx.AddFinalizerFn(func() error {
err = Global.ExtensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(o.Name, metav1.NewDeleteOptions(0))
if err != nil && !apierrors.IsNotFound(err) {
return err
}
return nil
})
if apierrors.IsAlreadyExists(err) {
return nil
}
return err
default:
return errors.New("Non-CRD resource in createCRDFromYAML function")
}
}

func setNamespaceYAML(yamlFile []byte, namespace string) ([]byte, error) {
yamlMap := make(map[interface{}]interface{})
err := yaml.Unmarshal(yamlFile, &yamlMap)
Expand All @@ -192,17 +69,7 @@ func (ctx *TestCtx) CreateFromYAML(yamlFile []byte) error {

obj, _, err := Global.DynamicDecoder.Decode(yamlSpec, nil, nil)
if err != nil {
yamlMap := make(map[interface{}]interface{})
err = yaml.Unmarshal(yamlSpec, &yamlMap)
if err != nil {
return err
}
kind := yamlMap["kind"].(string)
err = ctx.createCRFromYAML(yamlSpec, strings.ToLower(kind)+"s")
if err != nil {
return err
}
continue
return err
}

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

0 comments on commit f288683

Please sign in to comment.