-
Notifications
You must be signed in to change notification settings - Fork 175
/
scyllacluster_webhooks.go
157 lines (141 loc) · 5.93 KB
/
scyllacluster_webhooks.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
// Copyright (c) 2021 ScyllaDB
package scyllacluster
import (
"context"
"fmt"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
scyllav1 "github.com/scylladb/scylla-operator/pkg/api/scylla/v1"
scyllaclusterfixture "github.com/scylladb/scylla-operator/test/e2e/fixture/scyllacluster"
"github.com/scylladb/scylla-operator/test/e2e/framework"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
var _ = g.Describe("ScyllaCluster webhook", func() {
defer g.GinkgoRecover()
f := framework.NewFramework("scyllacluster")
g.It("should forbid invalid requests", func() {
ctx, cancel := context.WithTimeout(context.Background(), testTimout)
defer cancel()
validSC := scyllaclusterfixture.BasicScyllaCluster.ReadOrFail()
err := framework.SetupScyllaClusterSA(ctx, f.KubeClient().CoreV1(), f.KubeClient().RbacV1(), f.Namespace(), validSC.Name)
o.Expect(err).NotTo(o.HaveOccurred())
framework.By("rejecting a creation of ScyllaCluster with duplicated racks")
duplicateRacksSC := validSC.DeepCopy()
duplicateRacksSC.Spec.Datacenter.Racks = append(duplicateRacksSC.Spec.Datacenter.Racks, *duplicateRacksSC.Spec.Datacenter.Racks[0].DeepCopy())
_, err = f.ScyllaClient().ScyllaV1().ScyllaClusters(f.Namespace()).Create(ctx, duplicateRacksSC, metav1.CreateOptions{})
o.Expect(err).To(o.Equal(&errors.StatusError{ErrStatus: metav1.Status{
Status: "Failure",
Message: `admission webhook "webhook.scylla.scylladb.com" denied the request: ScyllaCluster.scylla.scylladb.com "basic" is invalid: spec.datacenter.racks[1].name: Duplicate value: "us-east-1a"`,
Reason: "Invalid",
Details: &metav1.StatusDetails{
Name: "basic",
Group: "scylla.scylladb.com",
Kind: "ScyllaCluster",
UID: "",
Causes: []metav1.StatusCause{
{
Type: "FieldValueDuplicate",
Message: `Duplicate value: "us-east-1a"`,
Field: "spec.datacenter.racks[1].name",
},
},
},
Code: 422,
}}))
framework.By("rejecting a creation of ScyllaCluster with invalid intensity in repair task spec")
invalidIntensitySC := validSC.DeepCopy()
invalidIntensitySC.Spec.Repairs = append(invalidIntensitySC.Spec.Repairs, scyllav1.RepairTaskSpec{
Intensity: "100Mib",
})
_, err = f.ScyllaClient().ScyllaV1().ScyllaClusters(f.Namespace()).Create(ctx, invalidIntensitySC, metav1.CreateOptions{})
o.Expect(err).To(o.Equal(&errors.StatusError{ErrStatus: metav1.Status{
Status: "Failure",
Message: `admission webhook "webhook.scylla.scylladb.com" denied the request: ScyllaCluster.scylla.scylladb.com "basic" is invalid: spec.repairs[0].intensity: Invalid value: "100Mib": invalid intensity, it must be a float value`,
Reason: "Invalid",
Details: &metav1.StatusDetails{
Name: "basic",
Group: "scylla.scylladb.com",
Kind: "ScyllaCluster",
UID: "",
Causes: []metav1.StatusCause{
{
Type: "FieldValueInvalid",
Message: `Invalid value: "100Mib": invalid intensity, it must be a float value`,
Field: "spec.repairs[0].intensity",
},
},
},
Code: 422,
}}))
framework.By("accepting a creation of valid ScyllaCluster")
validSC, err = f.ScyllaClient().ScyllaV1().ScyllaClusters(f.Namespace()).Create(ctx, validSC, metav1.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
framework.By("waiting for the ScyllaCluster to deploy")
waitCtx1, waitCtx1Cancel := contextForRollout(ctx, validSC)
defer waitCtx1Cancel()
validSC, err = waitForScyllaClusterState(waitCtx1, f.ScyllaClient().ScyllaV1(), validSC.Namespace, validSC.Name, scyllaClusterRolledOut)
o.Expect(err).NotTo(o.HaveOccurred())
di, err := NewDataInserter(ctx, f.KubeClient().CoreV1(), validSC, getMemberCount(validSC))
o.Expect(err).NotTo(o.HaveOccurred())
defer di.Close()
err = di.Insert()
o.Expect(err).NotTo(o.HaveOccurred())
verifyScyllaCluster(ctx, f.KubeClient(), validSC, di)
framework.By("rejecting an update of ScyllaCluster's repo")
_, err = f.ScyllaClient().ScyllaV1().ScyllaClusters(validSC.Namespace).Patch(
ctx,
validSC.Name,
types.MergePatchType,
[]byte(fmt.Sprintf(`{"spec": {"datacenter": {"name": "%s-updated"}}}`, validSC.Spec.Datacenter.Name)),
metav1.PatchOptions{},
)
o.Expect(err).To(o.Equal(&errors.StatusError{ErrStatus: metav1.Status{
Status: "Failure",
Message: `admission webhook "webhook.scylla.scylladb.com" denied the request: ScyllaCluster.scylla.scylladb.com "basic" is invalid: spec.datacenter.name: Forbidden: change of datacenter name is currently not supported`,
Reason: "Invalid",
Details: &metav1.StatusDetails{
Name: "basic",
Group: "scylla.scylladb.com",
Kind: "ScyllaCluster",
UID: "",
Causes: []metav1.StatusCause{
{
Type: "FieldValueForbidden",
Message: "Forbidden: change of datacenter name is currently not supported",
Field: "spec.datacenter.name",
},
},
},
Code: 422,
}}))
framework.By("rejecting an update of ScyllaCluster's dcName")
_, err = f.ScyllaClient().ScyllaV1().ScyllaClusters(validSC.Namespace).Patch(
ctx,
validSC.Name,
types.MergePatchType,
[]byte(fmt.Sprintf(`{"spec":{"datacenter": {"name": "%s-updated"}}}`, validSC.Spec.Datacenter.Name)),
metav1.PatchOptions{},
)
o.Expect(err).To(o.Equal(&errors.StatusError{ErrStatus: metav1.Status{
Status: "Failure",
Message: `admission webhook "webhook.scylla.scylladb.com" denied the request: ScyllaCluster.scylla.scylladb.com "basic" is invalid: spec.datacenter.name: Forbidden: change of datacenter name is currently not supported`,
Reason: "Invalid",
Details: &metav1.StatusDetails{
Name: "basic",
Group: "scylla.scylladb.com",
Kind: "ScyllaCluster",
UID: "",
Causes: []metav1.StatusCause{
{
Type: "FieldValueForbidden",
Message: "Forbidden: change of datacenter name is currently not supported",
Field: "spec.datacenter.name",
},
},
},
Code: 422,
}}))
})
})