generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 105
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
k8s-ci-robot
merged 14 commits into
kubernetes-sigs:main
from
reetasingh:reetas_kwok_feature_support
Jul 2, 2023
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5a6be4e
Add support for kwok cluster simulator
reetasingh 8c89030
use go install to install kwok and kwokctl
reetasingh 94e4e83
remove kwok binary installation steps
reetasingh 4c3a112
create node and verify pod status in example test for kwok
reetasingh 1c60bdc
fix test
reetasingh 0417e26
fix formatting
reetasingh a3e23d0
refactor tests
reetasingh 7607037
pass --wait argument to create cluster
reetasingh 80c77ce
change wait duration default
reetasingh a75ec03
remove sleep and use wait.For
reetasingh e43c58a
remove check for dep is not nil
reetasingh 201c4f4
update kwok file
reetasingh 2904a6e
gofumpt main_test
reetasingh a3234c7
update kwok version
reetasingh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.kwok
/kind
or any other one in the futurecc @vladimirvivien @reetasingh Does this make sense ?
Define an interface like above and make the
ClusterProvider
as an argument to functions in theenvfuncs
helper so that we can build a reusable set of envfunc ?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xref: #245
There was a problem hiding this comment.
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?