Skip to content

Commit 9cace89

Browse files
authored
ci: scope scenario parameters instead of whole job (#2578)
* scenario vars * refined scenario scope * linux agnhost * add delete resource * lint
1 parent 6e66efd commit 9cace89

File tree

11 files changed

+596
-149
lines changed

11 files changed

+596
-149
lines changed

test/e2e/framework/kubernetes/create-agnhost-statefulset.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ func (c *CreateAgnhostStatefulSet) getAgnhostDeployment() *appsv1.StatefulSet {
120120
},
121121
},
122122
},
123+
NodeSelector: map[string]string{
124+
"kubernetes.io/os": "linux",
125+
},
123126
Containers: []v1.Container{
124127
{
125128
Name: c.AgnhostName,

test/e2e/framework/kubernetes/create-resource.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ func CreateResource(ctx context.Context, obj runtime.Object, clientset *kubernet
202202
return fmt.Errorf("failed to create/update NetworkPolicy \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
203203
}
204204

205+
case *v1.Secret:
206+
log.Printf("Creating/Updating Secret \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
207+
client := clientset.CoreV1().Secrets(o.Namespace)
208+
_, err := client.Get(ctx, o.Name, metaV1.GetOptions{})
209+
if errors.IsNotFound(err) {
210+
_, err = client.Create(ctx, o, metaV1.CreateOptions{})
211+
if err != nil {
212+
return fmt.Errorf("failed to create Secret \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
213+
}
214+
return nil
215+
}
216+
_, err = client.Update(ctx, o, metaV1.UpdateOptions{})
217+
if err != nil {
218+
return fmt.Errorf("failed to create/update Secret \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
219+
}
220+
205221
default:
206222
return fmt.Errorf("unknown object type: %T, err: %w", obj, ErrUnknownResourceType)
207223
}

test/e2e/framework/kubernetes/delete-resource.go

Lines changed: 201 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"time"
78

89
appsv1 "k8s.io/api/apps/v1"
910
v1 "k8s.io/api/core/v1"
@@ -13,10 +14,188 @@ import (
1314
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
"k8s.io/apimachinery/pkg/runtime"
1516
"k8s.io/client-go/kubernetes"
17+
"k8s.io/client-go/tools/clientcmd"
1618
)
1719

1820
var ErrDeleteNilResource = fmt.Errorf("cannot create nil resource")
1921

22+
type ResourceType string
23+
24+
const (
25+
DaemonSet ResourceType = "DaemonSet"
26+
Deployment ResourceType = "Deployment"
27+
StatefulSet ResourceType = "StatefulSet"
28+
Service ResourceType = "Service"
29+
ServiceAccount ResourceType = "ServiceAccount"
30+
Role ResourceType = "Role"
31+
RoleBinding ResourceType = "RoleBinding"
32+
ClusterRole ResourceType = "ClusterRole"
33+
ClusterRoleBinding ResourceType = "ClusterRoleBinding"
34+
ConfigMap ResourceType = "ConfigMap"
35+
NetworkPolicy ResourceType = "NetworkPolicy"
36+
Secret ResourceType = "Secret"
37+
Unknown ResourceType = "Unknown"
38+
)
39+
40+
// Parameters can only be strings, heres to help add guardrails
41+
func TypeString(resourceType ResourceType) string {
42+
ResourceTypes := map[ResourceType]string{
43+
DaemonSet: "DaemonSet",
44+
Deployment: "Deployment",
45+
StatefulSet: "StatefulSet",
46+
Service: "Service",
47+
ServiceAccount: "ServiceAccount",
48+
Role: "Role",
49+
RoleBinding: "RoleBinding",
50+
ClusterRole: "ClusterRole",
51+
ClusterRoleBinding: "ClusterRoleBinding",
52+
ConfigMap: "ConfigMap",
53+
NetworkPolicy: "NetworkPolicy",
54+
Secret: "Secret",
55+
Unknown: "Unknown",
56+
}
57+
str, ok := ResourceTypes[resourceType]
58+
if !ok {
59+
return ResourceTypes[Unknown]
60+
}
61+
return str
62+
}
63+
64+
type DeleteKubernetesResource struct {
65+
ResourceType string // can't use enum, breaks parameter parsing, all must be strings
66+
ResourceName string
67+
ResourceNamespace string
68+
KubeConfigFilePath string
69+
}
70+
71+
func (d *DeleteKubernetesResource) Run() error {
72+
config, err := clientcmd.BuildConfigFromFlags("", d.KubeConfigFilePath)
73+
if err != nil {
74+
return fmt.Errorf("error building kubeconfig: %w", err)
75+
}
76+
77+
clientset, err := kubernetes.NewForConfig(config)
78+
if err != nil {
79+
return fmt.Errorf("error creating Kubernetes client: %w", err)
80+
}
81+
82+
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second)
83+
defer cancel()
84+
85+
res := ResourceType(d.ResourceType)
86+
87+
var resource runtime.Object
88+
89+
switch res {
90+
case DaemonSet:
91+
resource = &appsv1.DaemonSet{
92+
ObjectMeta: metaV1.ObjectMeta{
93+
Name: d.ResourceName,
94+
Namespace: d.ResourceNamespace,
95+
},
96+
}
97+
case Deployment:
98+
resource = &appsv1.Deployment{
99+
ObjectMeta: metaV1.ObjectMeta{
100+
Name: d.ResourceName,
101+
Namespace: d.ResourceNamespace,
102+
},
103+
}
104+
case StatefulSet:
105+
resource = &appsv1.StatefulSet{
106+
ObjectMeta: metaV1.ObjectMeta{
107+
Name: d.ResourceName,
108+
Namespace: d.ResourceNamespace,
109+
},
110+
}
111+
case Service:
112+
resource = &v1.Service{
113+
ObjectMeta: metaV1.ObjectMeta{
114+
Name: d.ResourceName,
115+
Namespace: d.ResourceNamespace,
116+
},
117+
}
118+
case ServiceAccount:
119+
resource = &v1.ServiceAccount{
120+
ObjectMeta: metaV1.ObjectMeta{
121+
Name: d.ResourceName,
122+
Namespace: d.ResourceNamespace,
123+
},
124+
}
125+
case Role:
126+
resource = &rbacv1.Role{
127+
ObjectMeta: metaV1.ObjectMeta{
128+
Name: d.ResourceName,
129+
Namespace: d.ResourceNamespace,
130+
},
131+
}
132+
case RoleBinding:
133+
resource = &rbacv1.RoleBinding{
134+
ObjectMeta: metaV1.ObjectMeta{
135+
Name: d.ResourceName,
136+
Namespace: d.ResourceNamespace,
137+
},
138+
}
139+
case ClusterRole:
140+
resource = &rbacv1.ClusterRole{
141+
ObjectMeta: metaV1.ObjectMeta{
142+
Name: d.ResourceName,
143+
},
144+
}
145+
case ClusterRoleBinding:
146+
resource = &rbacv1.ClusterRoleBinding{
147+
ObjectMeta: metaV1.ObjectMeta{
148+
Name: d.ResourceName,
149+
},
150+
}
151+
case ConfigMap:
152+
resource = &v1.ConfigMap{
153+
ObjectMeta: metaV1.ObjectMeta{
154+
Name: d.ResourceName,
155+
Namespace: d.ResourceNamespace,
156+
},
157+
}
158+
case NetworkPolicy:
159+
resource = &networkingv1.NetworkPolicy{
160+
ObjectMeta: metaV1.ObjectMeta{
161+
Name: d.ResourceName,
162+
Namespace: d.ResourceNamespace,
163+
},
164+
}
165+
case Secret:
166+
resource = &v1.Secret{
167+
ObjectMeta: metaV1.ObjectMeta{
168+
Name: d.ResourceName,
169+
Namespace: d.ResourceNamespace,
170+
},
171+
}
172+
case Unknown:
173+
return fmt.Errorf("unknown resource type: %s: %w", d.ResourceType, ErrUnknownResourceType)
174+
default:
175+
return ErrUnknownResourceType
176+
}
177+
178+
err = DeleteResource(ctx, resource, clientset)
179+
if err != nil {
180+
return fmt.Errorf("error deleting resource: %w", err)
181+
}
182+
183+
return nil
184+
}
185+
186+
func (d *DeleteKubernetesResource) Stop() error {
187+
return nil
188+
}
189+
190+
func (d *DeleteKubernetesResource) Prevalidate() error {
191+
restype := ResourceType(d.ResourceType)
192+
if restype == Unknown {
193+
return ErrUnknownResourceType
194+
}
195+
196+
return nil
197+
}
198+
20199
func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernetes.Clientset) error { //nolint:gocyclo //this is just boilerplate code
21200
if obj == nil {
22201
return ErrCreateNilResource
@@ -36,7 +215,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
36215
}
37216

38217
case *appsv1.Deployment:
39-
log.Printf("Creating/Updating Deployment \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
218+
log.Printf("Deleting Deployment \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
40219
client := clientset.AppsV1().Deployments(o.Namespace)
41220
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
42221
if err != nil {
@@ -48,7 +227,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
48227
}
49228

50229
case *appsv1.StatefulSet:
51-
log.Printf("Creating/Updating StatefulSet \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
230+
log.Printf("Deleting StatefulSet \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
52231
client := clientset.AppsV1().StatefulSets(o.Namespace)
53232
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
54233
if err != nil {
@@ -60,7 +239,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
60239
}
61240

62241
case *v1.Service:
63-
log.Printf("Creating/Updating Service \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
242+
log.Printf("Deleting Service \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
64243
client := clientset.CoreV1().Services(o.Namespace)
65244
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
66245
if err != nil {
@@ -72,7 +251,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
72251
}
73252

74253
case *v1.ServiceAccount:
75-
log.Printf("Creating/Updating ServiceAccount \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
254+
log.Printf("Deleting ServiceAccount \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
76255
client := clientset.CoreV1().ServiceAccounts(o.Namespace)
77256
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
78257
if err != nil {
@@ -84,7 +263,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
84263
}
85264

86265
case *rbacv1.Role:
87-
log.Printf("Creating/Updating Role \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
266+
log.Printf("Deleting Role \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
88267
client := clientset.RbacV1().Roles(o.Namespace)
89268
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
90269
if err != nil {
@@ -96,7 +275,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
96275
}
97276

98277
case *rbacv1.RoleBinding:
99-
log.Printf("Creating/Updating RoleBinding \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
278+
log.Printf("Deleting RoleBinding \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
100279
client := clientset.RbacV1().RoleBindings(o.Namespace)
101280
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
102281
if err != nil {
@@ -108,7 +287,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
108287
}
109288

110289
case *rbacv1.ClusterRole:
111-
log.Printf("Creating/Updating ClusterRole \"%s\"...\n", o.Name)
290+
log.Printf("Deleting ClusterRole \"%s\"...\n", o.Name)
112291
client := clientset.RbacV1().ClusterRoles()
113292
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
114293
if err != nil {
@@ -120,7 +299,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
120299
}
121300

122301
case *rbacv1.ClusterRoleBinding:
123-
log.Printf("Creating/Updating ClusterRoleBinding \"%s\"...\n", o.Name)
302+
log.Printf("Deleting ClusterRoleBinding \"%s\"...\n", o.Name)
124303
client := clientset.RbacV1().ClusterRoleBindings()
125304
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
126305
if err != nil {
@@ -132,7 +311,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
132311
}
133312

134313
case *v1.ConfigMap:
135-
log.Printf("Creating/Updating ConfigMap \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
314+
log.Printf("Deleting ConfigMap \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
136315
client := clientset.CoreV1().ConfigMaps(o.Namespace)
137316
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
138317
if err != nil {
@@ -144,7 +323,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
144323
}
145324

146325
case *networkingv1.NetworkPolicy:
147-
log.Printf("Creating/Updating NetworkPolicy \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
326+
log.Printf("Deleting NetworkPolicy \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
148327
client := clientset.NetworkingV1().NetworkPolicies(o.Namespace)
149328
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
150329
if err != nil {
@@ -155,6 +334,18 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
155334
return fmt.Errorf("failed to delete NetworkPolicy \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
156335
}
157336

337+
case *v1.Secret:
338+
log.Printf("Deleting Secret \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
339+
client := clientset.CoreV1().Secrets(o.Namespace)
340+
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
341+
if err != nil {
342+
if errors.IsNotFound(err) {
343+
log.Printf("Secret \"%s\" in namespace \"%s\" does not exist\n", o.Name, o.Namespace)
344+
return nil
345+
}
346+
return fmt.Errorf("failed to delete Secret \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
347+
}
348+
158349
default:
159350
return fmt.Errorf("unknown object type: %T, err: %w", obj, ErrUnknownResourceType)
160351
}

test/e2e/framework/kubernetes/port-forward.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ func (p *PortForward) Run() error {
7272
}
7373

7474
portForwardFn := func() error {
75-
7675
// if we have a pod name (likely from affinity above), use it, otherwise use label selector
7776
opts := k8s.PortForwardingOpts{
7877
Namespace: p.Namespace,

test/e2e/framework/types/examples/background_test.go renamed to test/e2e/framework/types/background_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@ import (
66
"sync"
77
"testing"
88
"time"
9-
10-
"github.com/Azure/azure-container-networking/test/e2e/framework/types"
119
)
1210

1311
func TestFramework(t *testing.T) {
14-
job := types.NewJob("Validate that drop metrics are present in the prometheus endpoint")
15-
runner := types.NewRunner(t, job)
12+
job := NewJob("Validate that drop metrics are present in the prometheus endpoint")
13+
runner := NewRunner(t, job)
1614
defer runner.Run()
1715

1816
job.AddStep(&TestBackground{
1917
CounterName: "Example Counter",
20-
}, &types.StepOptions{
18+
}, &StepOptions{
2119
ExpectError: false,
2220
RunInBackgroundWithID: "TestStep",
2321
})
2422

25-
job.AddStep(&types.Sleep{
23+
job.AddStep(&Sleep{
2624
Duration: 1 * time.Second,
2725
}, nil)
2826

29-
job.AddStep(&types.Stop{
27+
job.AddStep(&Stop{
3028
BackgroundID: "TestStep",
3129
}, nil)
3230
}

0 commit comments

Comments
 (0)