Skip to content

Commit

Permalink
Merge branch 'kind-setup' of github.com:ShwethaKumbla/e2e-framework i…
Browse files Browse the repository at this point in the history
…nto kind-setup
  • Loading branch information
ShwethaKumbla committed Aug 23, 2021
2 parents edf07b2 + c4181f9 commit 8f30d34
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 80 deletions.
3 changes: 2 additions & 1 deletion examples/k8s/kindsupport.go
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"

"github.com/vladimirvivien/gexe"
Expand All @@ -32,7 +33,7 @@ var (

func createKindCluster(clusterName string) (string, error) {
if e.Prog().Avail("kind") == "" {
fmt.Println(`kind may not be installed, attempting to install...`)
log.Println(`kind may not be installed, attempting to install...`)
p := e.SetEnv("GO111MODULE", "on").RunProc(fmt.Sprintf("go get sigs.k8s.io/kind@%s", kindVersion))
if p.Err() != nil {
return "", fmt.Errorf("install kind failed: %s: %w", p.Result(), p.Err())
Expand Down
12 changes: 6 additions & 6 deletions klient/conf/main_test.go
Expand Up @@ -18,8 +18,8 @@ package conf

import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -47,7 +47,7 @@ func setup() {
if os.IsNotExist(err) {
err = os.MkdirAll(kubeconfigdir, 0777)
if err != nil {
fmt.Println("failed to create .kube dir", err)
log.Println("failed to create .kube dir", err)
return
}

Expand All @@ -56,19 +56,19 @@ func setup() {

err = createFile(kubeconfigpath, data)
if err != nil {
fmt.Println("failed to create config file", err)
log.Println("failed to create config file", err)
return
}
}

fmt.Println("file created successfully", kubeconfigpath)
log.Println("file created successfully", kubeconfigpath)

flag.StringVar(&kubeconfig, "kubeconfig", "", "Paths to a kubeconfig. Only required if out-of-cluster.")

// set --kubeconfig flag
err = flag.Set("kubeconfig", kubeconfigpath)
if err != nil {
fmt.Println("unexpected error while setting flag value", err)
log.Println("unexpected error while setting flag value", err)
return
}

Expand Down Expand Up @@ -120,6 +120,6 @@ func teardown() {
home := homedir.HomeDir()
err := os.RemoveAll(filepath.Join(home, "test"))
if err != nil {
fmt.Println("failed to delete .kube dir", err)
log.Println("failed to delete .kube dir", err)
}
}
20 changes: 10 additions & 10 deletions klient/k8s/resources/main_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -62,7 +63,7 @@ func setup() {
// set up kind cluster
err := setupKindCluster()
if err != nil {
fmt.Println("error while setting up kind cluster", err)
log.Println("error while setting up kind cluster", err)
return
}

Expand All @@ -71,36 +72,35 @@ func setup() {
// set --kubeconfig flag
err = flag.Set("kubeconfig", path)
if err != nil {
fmt.Println("unexpected error while setting flag value", err)
log.Println("unexpected error while setting flag value", err)
return
}

flag.Parse()

cfg, err = conf.New(conf.ResolveKubeConfigFile())
if err != nil {
fmt.Println("error while client connection", err)
log.Println("error while client connection", err)
return
}

clientset, err = kubernetes.NewForConfig(cfg)
if err != nil {
fmt.Println("error while client set connection", err)
log.Println("error while client set connection", err)
return
}
}

// setupKindCluster
func setupKindCluster() error {
kc = kind.NewKindCluster("e2e-test-cluster")
kc = kind.NewCluster("e2e-test-cluster")
if _, err := kc.Create(); err != nil {
return err
}
fmt.Println("kind cluster created")

// stall to wait for kind pods initialization
waitTime := time.Second * 10
fmt.Println("waiting for kind pods to initialize...", waitTime)
log.Println("waiting for kind pods to initialize...", waitTime)
time.Sleep(waitTime)

return nil
Expand All @@ -113,7 +113,7 @@ func teardown() {
// delete kind cluster
err := kc.Destroy()
if err != nil {
fmt.Println("error while deleting the cluster", err)
log.Println("error while deleting the cluster", err)
return
}
}
Expand All @@ -123,7 +123,7 @@ func deleteDeployment(ctx context.Context, dep *appsv1.Deployment, ns string) {
if err == nil {
err = clientset.AppsV1().Deployments(ns).Delete(ctx, dep.Name, metav1.DeleteOptions{})
if err != nil {
fmt.Println("error while deleting deployment", err)
log.Println("error while deleting deployment", err)
}
}
}
Expand All @@ -136,7 +136,7 @@ func deleteNamespace(ctx context.Context, ns *corev1.Namespace) {

err = clientset.CoreV1().Namespaces().Delete(ctx, ns.Name, metav1.DeleteOptions{})
if err != nil {
fmt.Println("error while deleting namespace", err)
log.Println("error while deleting namespace", err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions klient/k8s/resources/resources.go
Expand Up @@ -19,7 +19,7 @@ package resources
import (
"context"
"errors"
"fmt"
"log"
"time"

"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -52,14 +52,14 @@ type Resources struct {
func New(cfg *rest.Config) (*Resources, error) {
if cfg == nil {
// TODO: logging
fmt.Println("must provide rest.Config")
log.Println("must provide rest.Config")
return nil, errors.New("must provide rest.Config")
}

cl, err := cr.New(cfg, cr.Options{Scheme: scheme.Scheme})
if err != nil {
// TODO: log error
fmt.Println("unexpected error creating client using provided config and client options")
log.Println("unexpected error creating client using provided config and client options", err)
return nil, err
}

Expand Down
23 changes: 21 additions & 2 deletions klient/k8s/resources/resources_test.go
Expand Up @@ -19,7 +19,7 @@ package resources
import (
"context"
"encoding/json"
"fmt"
"log"
"testing"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestRes(t *testing.T) {
}

if actual == dep {
fmt.Println("deployment found", dep.Name)
log.Println("deployment found", dep.Name)
}

var depObj appsv1.Deployment
Expand Down Expand Up @@ -217,3 +217,22 @@ func TestPatch(t *testing.T) {
t.Error("resource patch not applied correctly.")
}
}

func TestListAllPods(t *testing.T) {
res, err := New(cfg)
if err != nil {
t.Errorf("config is nill")
}

pods := &corev1.PodList{}
err = res.List(context.TODO(), pods)
if err != nil {
t.Error("error while getting the deployment", err)
}

if pods.Items == nil {
t.Error("error while getting the list of deployments", err)
}

log.Println("pod list contains", len(pods.Items), pods.Items)
}
35 changes: 27 additions & 8 deletions pkg/envsupport/support.go
Expand Up @@ -18,39 +18,58 @@ package envsupport

import (
"context"
"fmt"
"log"
"time"

"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/support/kind"
)

type ContextKey string

func (c ContextKey) String() string {
return string(c)
}

var (
// ContextKeyKubeConfig key for setting kube config value in context
ContextKeyKubeConfig = ContextKey("kubeconfig")
// ContextKeyClusterName key for setting clustername value in context
ContextKeyClusterName = ContextKey("clustername")
)

// GetStringValueFromContext gets the caller value from the context.
// Use this function to retrieve value of a context key
// or can print out value of key by doing,
// fmt.Println("Key is:", ContextKeyKubeConfig.String())
func GetStringValueFromContext(ctx context.Context, key ContextKey) (string, bool) {
value, ok := ctx.Value(key).(string)
return value, ok
}

func CreateCluster(clusterName string) env.Func {
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
k := kind.NewKindCluster(clusterName)
k := kind.NewCluster(clusterName)
kubecfg, err := k.Create()
if err != nil {
return ctx, err
}

fmt.Println("kind cluster created")

// stall to wait for kind pods initialization
waitTime := time.Second * 10
fmt.Println("waiting for kind pods to initialize...", waitTime)
time.Sleep(waitTime)
return context.WithValue(context.WithValue(ctx, 1, kubecfg), 2, clusterName), nil
return context.WithValue(context.WithValue(ctx, ContextKeyKubeConfig, kubecfg), ContextKeyClusterName, clusterName), nil
}
}

func DestroyCluster(clusterName string) {
// delete kind cluster
k := kind.NewKindCluster(clusterName)
k := kind.NewCluster(clusterName)

err := k.Destroy()
if err != nil {
fmt.Println("error while deleting the cluster", err)
log.Println("error while deleting the cluster", err)
return
}
}

0 comments on commit 8f30d34

Please sign in to comment.