Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for kwok cluster simulator #239

Merged
5 changes: 5 additions & 0 deletions examples/kwok/kwok_with_config/kwok-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kwok.x-k8s.io/v1alpha1
kind: KwokctlConfiguration
options:
runtime: docker
kubeVersion: v1.26.0
136 changes: 136 additions & 0 deletions examples/kwok/kwok_with_config/kwok_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kwok

import (
"context"
"testing"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

func TestKwokCluster(t *testing.T) {
deploymentFeature := features.New("appsv1/deployment").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
// setup node
node := newNode("kwok-node")
if err := cfg.Client().Resources().Create(ctx, node); err != nil {
t.Fatal(err)
}
// start a deployment
deployment := newDeployment(cfg.Namespace(), "test-deployment", 1)
if err := cfg.Client().Resources().Create(ctx, deployment); err != nil {
t.Fatal(err)
}
return ctx
}).
Assess("deployment creation", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
var dep appsv1.Deployment
if err := cfg.Client().Resources().Get(ctx, "test-deployment", cfg.Namespace(), &dep); err != nil {
t.Fatal(err)
}
// wait for the deployment to finish becoming available
err := wait.For(conditions.New(cfg.Client().Resources()).DeploymentConditionMatch(&dep, appsv1.DeploymentAvailable, corev1.ConditionTrue), wait.WithTimeout(time.Minute*1))
if err != nil {
t.Fatal(err)
}
return context.WithValue(ctx, "test-deployment", &dep)
}).
Assess("pod ready", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
pods := &corev1.PodList{}
err := cfg.Client().Resources(cfg.Namespace()).List(context.TODO(), pods)
if err != nil || pods.Items == nil {
t.Error("error while getting pods", err)
}
if len(pods.Items) != 1 {
t.Fatal("could not find any pod in the namespace")
}
pod := pods.Items[0]
// wait for the pod to be ready
err = wait.For(conditions.New(cfg.Client().Resources()).PodConditionMatch(&pod, corev1.PodReady, corev1.ConditionTrue), wait.WithTimeout(time.Minute*1))
if err != nil {
t.Fatal(err)
}
return ctx
}).
Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
dep := ctx.Value("test-deployment").(*appsv1.Deployment)
if err := cfg.Client().Resources().Delete(ctx, dep); err != nil {
t.Fatal(err)
}
return ctx
}).Feature()
testenv.Test(t, deploymentFeature)
}

func newDeployment(namespace string, name string, replicaCount int32) *appsv1.Deployment {
podSpec := corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "my-container",
Image: "nginx",
},
},
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: "type",
Operator: corev1.NodeSelectorOpIn,
Values: []string{"kwok"},
},
},
},
},
},
},
},
}
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace, Labels: map[string]string{"app": "test-app"}},
Spec: appsv1.DeploymentSpec{
Replicas: &replicaCount,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "test-app"},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "test-app"}},
Spec: podSpec,
},
},
}
}

func newNode(nodeName string) *corev1.Node {
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
Labels: map[string]string{"beta.kubernetes.io/arch": "amd64", "beta.kubernetes.io/os": "linux", "type": "kwok"},
},
Spec: corev1.NodeSpec{},
}
}
45 changes: 45 additions & 0 deletions examples/kwok/kwok_with_config/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kwok

import (
"os"
"testing"

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

var testenv env.Environment

func TestMain(m *testing.M) {
testenv = env.New()
kwokClusterName := envconf.RandomName("kwok-cluster", 16)
namespace := envconf.RandomName("kwok-ns", 16)

testenv.Setup(
envfuncs.CreateKwokClusterWithConfig(kwokClusterName, "kwok-config.yaml"),
envfuncs.CreateNamespace(namespace),
)

testenv.Finish(
envfuncs.DeleteNamespace(namespace),
envfuncs.DestroyKwokCluster(kwokClusterName),
)
os.Exit(testenv.Run(m))
}
106 changes: 106 additions & 0 deletions pkg/envfuncs/kwok_funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package envfuncs

import (
"context"
"fmt"

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

type kwokContextKey string

// GetKwokClusterFromContext helps extract the kwok.Cluster object from the context.
func GetKwokClusterFromContext(ctx context.Context, clusterName string) (*kwok.Cluster, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this and kind_funcs.go share more of less the same set of features/workflows, I am wondering if we can really simplify this better ? It might lead to an interface change but, that might not be a bad idea considering we are still in early phases.

  1. Create a interface type that indicates the cluster providers that can be used to implement kwok/kind or any other one in the future
  2. This env funcs use the interface type with certain args so that these things don't need to be changed frequently

cc @vladimirvivien @reetasingh Does this make sense ?

type clusterOpts func(c ClusterProvider)

type ClusterProvider interface {
	WithVersion(version string) ClusterProvider
	WithOpts(opts ...clusterOpts) ClusterProvider
	Create(args ...string) (string, error)
	GetKubeConfig() string
	GetKubectlContext() string
	ExportLogs(dest string) error
	Destroy() error
	LoadDockerImages(image string) error
	LoadImageArchive(archivePath string) error
}

Define an interface like above and make the ClusterProvider as an argument to functions in the envfuncs helper so that we can build a reusable set of envfunc ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harshanarayana I like this refactor a lot. First impression is that it is doable and makes sense. Let's spend some times discussing to make sure we're not trapping future development work into a corner by being too specific.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 I agree. And I think we should do that as a pre-work before we can get this one in and not really use this PR to do that change. What do you think ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xref: #245

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harshanarayana do you plan to address the comments in your PR #239 soon? I am thinking when do we plan to proceed and close this one out?

kwokCluster := ctx.Value(kwokContextKey(clusterName))
if kwokCluster == nil {
return nil, false
}
cluster, ok := kwokCluster.(*kwok.Cluster)
return cluster, ok
}

// CreateKwokCluster returns an env.Func that is used to
// create a kwok cluster that is then injected in the context
// using the name as a key.
//
// NOTE: the returned function will update its env config with the
// kubeconfig file for the config client.
func CreateKwokCluster(clusterName string) env.Func {
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
k := kwok.NewCluster(clusterName)
kubecfg, err := k.Create()
if err != nil {
return ctx, err
}

// update envconfig with kubeconfig
cfg.WithKubeconfigFile(kubecfg)

// store entire cluster value in ctx for future access using the cluster name
return context.WithValue(ctx, kwokContextKey(clusterName), k), nil
}
}

// CreateKwokClusterWithConfig returns an env.Func that is used to
// create a kwok cluster that is then injected in the context
// using the name as a key.
//
// NOTE: the returned function will update its env config with the
// kubeconfig file for the config client.
func CreateKwokClusterWithConfig(clusterName, configFilePath string) env.Func {
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
k := kwok.NewCluster(clusterName)
kubecfg, err := k.CreateWithConfig(configFilePath)
if err != nil {
return ctx, err
}

// update envconfig with kubeconfig
cfg.WithKubeconfigFile(kubecfg)

// store entire cluster value in ctx for future access using the cluster name
return context.WithValue(ctx, kwokContextKey(clusterName), k), nil
}
}

// DestroyKwokCluster returns an EnvFunc that
// retrieves a previously saved kwok Cluster in the context (using the name), then deletes it.
//
// NOTE: this should be used in a Environment.Finish step.
func DestroyKwokCluster(name string) env.Func {
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
clusterVal := ctx.Value(kwokContextKey(name))
if clusterVal == nil {
return ctx, fmt.Errorf("destroy kwok cluster func: context cluster is nil")
}

cluster, ok := clusterVal.(*kwok.Cluster)
if !ok {
return ctx, fmt.Errorf("destroy kwok cluster func: unexpected type for cluster value")
}

if err := cluster.Destroy(); err != nil {
return ctx, fmt.Errorf("destroy kwok cluster: %w", err)
}

return ctx, nil
}
}
Loading