forked from open-cluster-management-io/ocm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork_webhook_test.go
More file actions
208 lines (181 loc) · 7.82 KB
/
work_webhook_test.go
File metadata and controls
208 lines (181 loc) · 7.82 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package e2e
import (
"context"
"fmt"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/retry"
workclientset "open-cluster-management.io/api/client/work/clientset/versioned"
workapiv1 "open-cluster-management.io/api/work/v1"
"open-cluster-management.io/ocm/test/integration/util"
)
// Test cases with lable "sanity-check" could be ran on an existing environment with validating webhook installed
// and well configured as sanity check. Resource leftovers should be cleaned up on both hub and managed cluster.
var _ = ginkgo.Describe("ManifestWork admission webhook", ginkgo.Label("validating-webhook", "sanity-check"), func() {
var nameSuffix string
var workName string
ginkgo.BeforeEach(func() {
nameSuffix = rand.String(5)
workName = fmt.Sprintf("w1-%s", nameSuffix)
})
ginkgo.AfterEach(func() {
ginkgo.By(fmt.Sprintf("delete manifestwork %v/%v", universalClusterName, workName))
gomega.Expect(hub.CleanManifestWorks(universalClusterName, workName)).To(gomega.BeNil())
})
ginkgo.Context("Creating a manifestwork", func() {
ginkgo.It("Should respond bad request when creating a manifestwork with no manifests", func() {
work := newManifestWork(universalClusterName, workName)
_, err := hub.WorkClient.WorkV1().ManifestWorks(universalClusterName).Create(context.Background(), work, metav1.CreateOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(errors.IsBadRequest(err)).Should(gomega.BeTrue())
})
ginkgo.It("Should respond bad request when creating a manifest with no name", func() {
work := newManifestWork(universalClusterName, workName, []runtime.Object{util.NewConfigmap("default", "", nil, nil)}...)
_, err := hub.WorkClient.WorkV1().ManifestWorks(universalClusterName).Create(context.Background(), work, metav1.CreateOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(errors.IsBadRequest(err)).Should(gomega.BeTrue())
})
ginkgo.Context("executor", func() {
var hubUser string
var roleName string
ginkgo.BeforeEach(func() {
hubUser = fmt.Sprintf("sa-%s", nameSuffix)
roleName = fmt.Sprintf("role-%s", nameSuffix)
if !nilExecutorValidating {
ginkgo.Skip(fmt.Sprintf("feature gate %q is disabled", "NilExecutorValidating"))
}
// create a temporary role
_, err := hub.KubeClient.RbacV1().Roles(universalClusterName).Create(
context.TODO(), &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Namespace: universalClusterName,
Name: roleName,
},
Rules: []rbacv1.PolicyRule{
{
Verbs: []string{"create", "update", "patch", "get", "list", "delete"},
APIGroups: []string{"work.open-cluster-management.io"},
Resources: []string{"manifestworks"},
},
},
}, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
// create a temporary rolebinding
_, err = hub.KubeClient.RbacV1().RoleBindings(universalClusterName).Create(
context.TODO(), &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Namespace: universalClusterName,
Name: roleName,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Namespace: universalClusterName,
Name: hubUser,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: roleName,
},
}, metav1.CreateOptions{})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
})
ginkgo.AfterEach(func() {
// delete the temporary role
err := hub.KubeClient.RbacV1().Roles(universalClusterName).Delete(context.TODO(), roleName, metav1.DeleteOptions{})
if !errors.IsNotFound(err) {
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}
// delete the temporary rolebinding
err = hub.KubeClient.RbacV1().RoleBindings(universalClusterName).Delete(context.TODO(), roleName, metav1.DeleteOptions{})
if !errors.IsNotFound(err) {
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}
})
ginkgo.It("Should respond bad request when no permission for nil executor", func() {
work := newManifestWork(universalClusterName, workName, []runtime.Object{util.NewConfigmap("default", "cm1", nil, nil)}...)
// impersonate as a hub user without execute-as permission
hubClusterCfg, err := clientcmd.BuildConfigFromFlags("", hubKubeconfig)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
impersonatedConfig := *hubClusterCfg
impersonatedConfig.Impersonate.UserName = fmt.Sprintf("system:serviceaccount:%s:%s", universalClusterName, hubUser)
impersonatedHubWorkClient, err := workclientset.NewForConfig(&impersonatedConfig)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
_, err = impersonatedHubWorkClient.WorkV1().ManifestWorks(universalClusterName).Create(
context.Background(), work, metav1.CreateOptions{})
gomega.Expect(err).To(gomega.HaveOccurred())
if !errors.IsBadRequest(err) {
// not bad request, assert true=false to show the error message
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}
})
})
})
ginkgo.Context("Updating a manifestwork", func() {
var err error
ginkgo.BeforeEach(func() {
work := newManifestWork(universalClusterName, workName, []runtime.Object{util.NewConfigmap("default", "cm1", nil, nil)}...)
_, err = hub.WorkClient.WorkV1().ManifestWorks(universalClusterName).Create(context.Background(), work, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
ginkgo.It("Should respond bad request when a manifestwork with invalid manifests", func() {
manifest := workapiv1.Manifest{}
manifest.Object = util.NewConfigmap("default", "", nil, nil)
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
work, err := hub.WorkClient.WorkV1().ManifestWorks(universalClusterName).Get(context.Background(), workName, metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
work.Spec.Workload.Manifests = append(work.Spec.Workload.Manifests, manifest)
_, err = hub.WorkClient.WorkV1().ManifestWorks(universalClusterName).Update(context.Background(), work, metav1.UpdateOptions{})
return err
})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(errors.IsBadRequest(err)).Should(gomega.BeTrue())
})
ginkgo.It("Should respond bad request when the size of manifests is more than the limit", func() {
manifests := []workapiv1.Manifest{
{
RawExtension: runtime.RawExtension{
Object: newSecretBySize("default", "test1", 100*1024),
},
},
{
RawExtension: runtime.RawExtension{
Object: newSecretBySize("default", "test2", 100*1024),
},
},
{
RawExtension: runtime.RawExtension{
Object: newSecretBySize("default", "test3", 100*1024),
},
},
{
RawExtension: runtime.RawExtension{
Object: newSecretBySize("default", "test4", 100*1024),
},
},
{
RawExtension: runtime.RawExtension{
Object: newSecretBySize("default", "test5", 100*1024),
},
},
}
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
work, err := hub.WorkClient.WorkV1().ManifestWorks(universalClusterName).Get(context.Background(), workName, metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
work.Spec.Workload.Manifests = append(work.Spec.Workload.Manifests, manifests...)
_, err = hub.WorkClient.WorkV1().ManifestWorks(universalClusterName).Update(context.Background(), work, metav1.UpdateOptions{})
return err
})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(errors.IsBadRequest(err)).Should(gomega.BeTrue())
})
})
})