|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package watch_resources |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + appsv1 "k8s.io/api/apps/v1" |
| 24 | + v1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/labels" |
| 27 | + "k8s.io/klog/v2" |
| 28 | + "sigs.k8s.io/e2e-framework/klient/k8s/resources" |
| 29 | + "sigs.k8s.io/e2e-framework/klient/k8s/watcher" |
| 30 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 31 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 32 | +) |
| 33 | + |
| 34 | +func TestWatchForResources(t *testing.T) { |
| 35 | + watchFeature := features.New("test watcher").WithLabel("env", "dev"). |
| 36 | + Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 37 | + cl, err := cfg.NewClient() |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + |
| 42 | + dep := appsv1.Deployment{ |
| 43 | + ObjectMeta: metav1.ObjectMeta{Name: "watch-dep", Namespace: cfg.Namespace()}, |
| 44 | + } |
| 45 | + |
| 46 | + // watch for the deployment and triger action based on the event recieved. |
| 47 | + go cl.Resources().Watch(&appsv1.DeploymentList{}, resources.WithFieldSelector(labels.FormatLabels(map[string]string{"metadata.name": dep.Name}))). |
| 48 | + WithAddFunc(onAdd).WithDeleteFunc(onDelete).Start(ctx) |
| 49 | + |
| 50 | + return ctx |
| 51 | + }). |
| 52 | + Assess("create watch deployment", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 53 | + // create a deployment |
| 54 | + deployment := newDeployment(cfg.Namespace(), "watch-dep", 1) |
| 55 | + client, err := cfg.NewClient() |
| 56 | + if err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + if err := client.Resources().Create(ctx, deployment); err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + return context.WithValue(ctx, "test-dep", deployment) |
| 63 | + }). |
| 64 | + Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 65 | + client, err := cfg.NewClient() |
| 66 | + if err != nil { |
| 67 | + t.Fatal(err) |
| 68 | + } |
| 69 | + depl := ctx.Value("test-dep").(*appsv1.Deployment) |
| 70 | + if err := client.Resources().Delete(ctx, depl); err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + return ctx |
| 74 | + }).Feature() |
| 75 | + |
| 76 | + testenv.Test(t, watchFeature) |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +// TestWatchForResourcesWithStop() demonstartes how to start and stop the watcher |
| 81 | +var w *watcher.EventHandlerFuncs |
| 82 | + |
| 83 | +func TestWatchForResourcesWithStop(t *testing.T) { |
| 84 | + watchFeature := features.New("test watcher with stop").WithLabel("env", "prod"). |
| 85 | + Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 86 | + cl, err := cfg.NewClient() |
| 87 | + if err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + |
| 91 | + dep := appsv1.Deployment{ |
| 92 | + ObjectMeta: metav1.ObjectMeta{Name: "watchnstop-dep", Namespace: cfg.Namespace()}, |
| 93 | + } |
| 94 | + |
| 95 | + // watch for the deployment and triger action based on the event recieved. |
| 96 | + w = cl.Resources().Watch(&appsv1.DeploymentList{}, resources.WithFieldSelector(labels.FormatLabels(map[string]string{"metadata.name": dep.Name}))). |
| 97 | + WithAddFunc(onAdd).WithDeleteFunc(onDelete) |
| 98 | + |
| 99 | + go func() { |
| 100 | + err := w.Start(ctx) |
| 101 | + if err != nil { |
| 102 | + t.Error(err) |
| 103 | + } |
| 104 | + }() |
| 105 | + |
| 106 | + return ctx |
| 107 | + }). |
| 108 | + Assess("create watch deployment", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 109 | + // create a deployment |
| 110 | + deployment := newDeployment(cfg.Namespace(), "watchnstop-dep", 1) |
| 111 | + client, err := cfg.NewClient() |
| 112 | + if err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + if err := client.Resources().Create(ctx, deployment); err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + return context.WithValue(ctx, "stop-dep", deployment) |
| 119 | + }). |
| 120 | + Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 121 | + client, err := cfg.NewClient() |
| 122 | + if err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + depl := ctx.Value("stop-dep").(*appsv1.Deployment) |
| 126 | + if err := client.Resources().Delete(ctx, depl); err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + |
| 130 | + w.Stop() |
| 131 | + |
| 132 | + return ctx |
| 133 | + }).Feature() |
| 134 | + |
| 135 | + testenv.Test(t, watchFeature) |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +func newDeployment(namespace string, name string, replicas int32) *appsv1.Deployment { |
| 140 | + return &appsv1.Deployment{ |
| 141 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace, Labels: map[string]string{"app": "watch-for-resources"}}, |
| 142 | + Spec: appsv1.DeploymentSpec{ |
| 143 | + Replicas: &replicas, |
| 144 | + Selector: &metav1.LabelSelector{ |
| 145 | + MatchLabels: map[string]string{"app": "watch-for-resources"}, |
| 146 | + }, |
| 147 | + Template: v1.PodTemplateSpec{ |
| 148 | + ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "watch-for-resources"}}, |
| 149 | + Spec: v1.PodSpec{Containers: []v1.Container{{Name: "nginx", Image: "nginx"}}}, |
| 150 | + }, |
| 151 | + }, |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// onAdd is the function executed when the kubernetes watch notifies the |
| 156 | +// presence of a new kubernetes deployment in the cluster |
| 157 | +func onAdd(obj interface{}) { |
| 158 | + dep := obj.(*appsv1.Deployment) |
| 159 | + depName := dep.GetName() |
| 160 | + if depName == "watch-dep" || depName == "watchnstop-dep" { |
| 161 | + klog.InfoS("Deployment name matches with actual name!") |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// onDelete is the function executed when the kubernetes watch notifies |
| 166 | +// delete event on deployment |
| 167 | +func onDelete(obj interface{}) { |
| 168 | + dep := obj.(*appsv1.Deployment) |
| 169 | + depName := dep.GetName() |
| 170 | + if depName == "watch-dep" || depName == "watchnstop-dep" { |
| 171 | + klog.InfoS("Deployment deleted successfully!") |
| 172 | + } |
| 173 | +} |
0 commit comments