Skip to content

Commit

Permalink
pkg/test: add dynamic client using controller-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNPavel committed Aug 7, 2018
1 parent 46300e7 commit 145e6a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 49 deletions.
4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@
[[constraint]]
name = "github.com/sergi/go-diff"
version = "1.0.0"

[[constraint]]
name = "github.com/kubernetes-sigs/controller-runtime"
revision = "60bb251ad86f9b313653618aad0c2c53f41a6625"
11 changes: 7 additions & 4 deletions pkg/test/main_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ func MainEntry(m *testing.M) {
}
// setup context to use when setting up crd
ctx := Global.NewTestCtx(nil)
// use out-of-test cleanup function
defer ctx.CleanupNoT()
// os.Exit stops the program before the deferred functions run
// to fix this, we put the exit in the defer as well
defer func() {
exitCode := m.Run()
ctx.CleanupNoT()
os.Exit(exitCode)
}()
// create crd
crdYAML, err := ioutil.ReadFile(*Global.CrdManPath)
if err != nil {
Expand All @@ -58,6 +63,4 @@ func MainEntry(m *testing.M) {
if err != nil {
log.Fatalf("failed to create crd resource: %v", err)
}

os.Exit(m.Run())
}
66 changes: 21 additions & 45 deletions pkg/test/resource_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ package test

import (
"bytes"
goctx "context"
"errors"
"fmt"
"io/ioutil"
"strings"

y2j "github.com/ghodss/yaml"
yaml "gopkg.in/yaml.v2"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
"k8s.io/api/rbac/v1beta1"
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"
Expand Down Expand Up @@ -166,61 +165,38 @@ func (ctx *TestCtx) createCRDFromYAML(yamlFile []byte) error {
}
}

func (ctx *TestCtx) CreateFromYAML(yamlFile []byte) error {
func setNamespaceYAML(yamlFile []byte, namespace string) ([]byte, error) {
yamlMap := make(map[interface{}]interface{})
err := yaml.Unmarshal(yamlFile, &yamlMap)
if err != nil {
return err
}
kind := yamlMap["kind"].(string)
// TODO: use a dynamic client for standard and extensions kubernetes resources.
// We should consider both the SDK's wrapper for the kubernetes dynamic client
// (which at time of typing needs updating) or using the controller-runtime
// dynamic client in the test framework (and the SDK as well).
switch kind {
case "Role":
case "RoleBinding":
case "Deployment":
case "CustomResourceDefinition":
return ctx.createCRDFromYAML(yamlFile)
// we assume that all custom resources are from operator-sdk and thus follow
// a common naming convention
default:
return ctx.createCRFromYAML(yamlFile, strings.ToLower(kind)+"s")
return nil, err
}
yamlMap["metadata"].(map[interface{}]interface{})["namespace"] = namespace
return yaml.Marshal(yamlMap)
}

decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode(yamlFile, nil, nil)
func (ctx *TestCtx) CreateFromYAML(yamlFile []byte) error {
namespace, err := ctx.GetNamespace()
if err != nil {
return err
}

namespace, err := ctx.GetNamespace()
yamlFile, err = setNamespaceYAML(yamlFile, namespace)
if err != nil {
return err
}
switch o := obj.(type) {
case *v1beta1.Role:
_, err = Global.KubeClient.RbacV1beta1().Roles(namespace).Create(o)
ctx.AddFinalizerFn(func() error {
return Global.KubeClient.RbacV1beta1().Roles(namespace).Delete(o.Name, metav1.NewDeleteOptions(0))
})
return err
case *v1beta1.RoleBinding:
_, err = Global.KubeClient.RbacV1beta1().RoleBindings(namespace).Create(o)
ctx.AddFinalizerFn(func() error {
return Global.KubeClient.RbacV1beta1().RoleBindings(namespace).Delete(o.Name, metav1.NewDeleteOptions(0))
})
return err
case *apps.Deployment:
_, err = Global.KubeClient.AppsV1().Deployments(namespace).Create(o)
ctx.AddFinalizerFn(func() error {
return Global.KubeClient.AppsV1().Deployments(namespace).Delete(o.Name, metav1.NewDeleteOptions(0))
})
return err
default:
return errors.New("Unhandled resource type")

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

return Global.DynamicClient.Create(goctx.TODO(), obj)
}

func (ctx *TestCtx) InitializeClusterResources() error {
Expand Down

0 comments on commit 145e6a2

Please sign in to comment.