/
context.go
162 lines (149 loc) · 5.95 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2022, Nadun De Silva. All Rights Reserved.
* 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 cleanup
import (
"context"
"fmt"
"testing"
"github.com/nadundesilva/k8s-replicator/test/utils/common"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/e2e-framework/klient/k8s"
)
type testObjectsContextKey struct{}
type testControllerObjectsContextKey struct{}
type testObjects struct {
managedObjects []k8s.Object
namespaces corev1.NamespaceList
clusterRoles rbacv1.ClusterRoleList
clusterRoleBindings rbacv1.ClusterRoleBindingList
}
func AddTestObjectToContext(ctx context.Context, t *testing.T, object k8s.Object) context.Context {
return addObjectToContext(ctx, t, object, testObjectsContextKey{}, "test")
}
func AddControllerObjectToContext(ctx context.Context, t *testing.T, object k8s.Object) context.Context {
return addObjectToContext(ctx, t, object, testControllerObjectsContextKey{}, "controller")
}
func addObjectToContext(ctx context.Context, t *testing.T, object k8s.Object, contextKey any, usage string) context.Context {
ctxValue := ctx.Value(contextKey)
var objects *testObjects
if ctxValue == nil {
objects = &testObjects{}
} else {
objects = ctxValue.(*testObjects)
}
var objectType string
if namespace, ok := object.(*corev1.Namespace); ok {
objects.namespaces.Items = append(objects.namespaces.Items, *namespace)
objectType = "namespace"
} else if clusterrole, ok := object.(*rbacv1.ClusterRole); ok {
objects.clusterRoles.Items = append(objects.clusterRoles.Items, *clusterrole)
objectType = "cluster role"
} else if clusterrolebinding, ok := object.(*rbacv1.ClusterRoleBinding); ok {
objects.clusterRoleBindings.Items = append(objects.clusterRoleBindings.Items, *clusterrolebinding)
objectType = "cluster role binding"
} else {
objectType, objectTypeOk := object.GetLabels()[common.ObjectTypeLabelKey]
if objectTypeOk && objectType == common.ObjectTypeLabelValueReplicated {
objects.managedObjects = append(objects.managedObjects, object)
version := object.GetObjectKind().GroupVersionKind()
objectType = fmt.Sprintf("%s/%s/%s", version.Group, version.Kind, version.Version)
} else {
objectKind := object.GetObjectKind().GroupVersionKind()
t.Fatalf(
"cannot add unknown object %s/%s of type %s/%s/%s as %s object",
object.GetNamespace(),
object.GetName(),
objectKind.Group,
objectKind.Kind,
objectKind.Version,
usage,
)
}
}
t.Logf("added %s %s object %s to context", usage, objectType, object.GetName())
return context.WithValue(ctx, contextKey, objects)
}
func RemoveTestObjectFromContext(ctx context.Context, t *testing.T, object k8s.Object) context.Context {
return removeObjectFromContext(ctx, t, object, testObjectsContextKey{}, "test")
}
func RemoveControllerObjectFromContext(ctx context.Context, t *testing.T, object k8s.Object) context.Context {
return removeObjectFromContext(ctx, t, object, testControllerObjectsContextKey{}, "controller")
}
func removeObjectFromContext(ctx context.Context, t *testing.T, object k8s.Object, contextKey any, usage string) context.Context {
ctxValue := ctx.Value(contextKey)
var objects *testObjects
if ctxValue == nil {
objects = &testObjects{}
t.Log("initialized new test objects struct in context")
} else {
objects = ctxValue.(*testObjects)
}
removeItemFromContext := func(testObjList []runtime.Object, obj runtime.Object) []runtime.Object {
var index *int
for i, testObject := range testObjList {
if testObject.(metav1.Object).GetName() == obj.(metav1.Object).GetName() {
index = &i
break
}
}
if index == nil {
t.Fatalf("test object to be removed from context not present")
} else {
testObjList = append(testObjList[:*index], testObjList[*index+1:]...)
}
return testObjList
}
var objectType string
if namespace, ok := object.(*corev1.Namespace); ok {
objList, err := meta.ExtractList(&objects.namespaces)
if err != nil {
t.Fatalf("failed to extract namespaces list: %v", err)
}
objList = removeItemFromContext(objList, namespace)
err = meta.SetList(&objects.namespaces, objList)
if err != nil {
t.Fatalf("failed to set the new reduced list: %v", err)
}
objectType = "namespace"
} else if clusterrole, ok := object.(*rbacv1.ClusterRole); ok {
objList, err := meta.ExtractList(&objects.clusterRoles)
if err != nil {
t.Fatalf("failed to extract cluster roles list: %v", err)
}
objList = removeItemFromContext(objList, clusterrole)
err = meta.SetList(&objects.clusterRoles, objList)
if err != nil {
t.Fatalf("failed to set the new reduced list: %v", err)
}
objectType = "cluster role"
} else if clusterrolebinding, ok := object.(*rbacv1.ClusterRoleBinding); ok {
objList, err := meta.ExtractList(&objects.clusterRoleBindings)
if err != nil {
t.Fatalf("failed to extract cluster role bindings list: %v", err)
}
objList = removeItemFromContext(objList, clusterrolebinding)
err = meta.SetList(&objects.clusterRoleBindings, objList)
if err != nil {
t.Fatalf("failed to set the new reduced list: %v", err)
}
objectType = "cluster role binding"
} else {
t.Fatalf("cannot remove unknown object type %s as %s object", object.GetObjectKind().GroupVersionKind().String(), usage)
}
t.Logf("removed %s %s object %s from context", usage, objectType, object.GetName())
return context.WithValue(ctx, contextKey, objects)
}