Skip to content

Commit 46fb6e9

Browse files
committed
try to fix tenant webhooks
1 parent c3f17d9 commit 46fb6e9

File tree

7 files changed

+37
-29
lines changed

7 files changed

+37
-29
lines changed

operators/cmd/tenant-operator/main.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,14 @@ func main() {
150150
if *enableWH {
151151
hookServer := mgr.GetWebhookServer()
152152
webhookBypassGroupsList := strings.Split(webhookBypassGroups, ",")
153-
hookServer.Register(ValidatingWebhookPath, tenantwh.MakeTenantValidator(mgr.GetClient(), webhookBypassGroupsList))
154-
hookServer.Register(MutatingWebhookPath, tenantwh.MakeTenantMutator(mgr.GetClient(), webhookBypassGroupsList, targetLabelKey, targetLabelValue, baseWorkspacesList))
153+
hookServer.Register(
154+
ValidatingWebhookPath,
155+
tenantwh.MakeTenantValidator(mgr.GetClient(), webhookBypassGroupsList, mgr.GetScheme()),
156+
)
157+
hookServer.Register(
158+
MutatingWebhookPath,
159+
tenantwh.MakeTenantMutator(mgr.GetClient(), webhookBypassGroupsList, targetLabelKey, targetLabelValue, baseWorkspacesList, mgr.GetScheme()),
160+
)
155161
} else {
156162
log.Info("Webhook set up: operation skipped")
157163
}

operators/pkg/tenantwh/common.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package tenantwh
1616

1717
import (
1818
"context"
19+
"errors"
1920

2021
"k8s.io/apimachinery/pkg/runtime"
2122
"k8s.io/apimachinery/pkg/types"
@@ -38,14 +39,11 @@ func (twh *TenantWebhook) CheckWebhookOverride(req *admission.Request) bool {
3839
return utils.MatchOneInStringSlices(twh.BypassGroups, req.UserInfo.Groups)
3940
}
4041

41-
// InjectDecoder injects the decoder - this method is used by controller runtime.
42-
func (twh *TenantWebhook) InjectDecoder(d *admission.Decoder) error {
43-
twh.decoder = d
44-
return nil
45-
}
46-
4742
// DecodeTenant decodes the tenant from the incoming request.
4843
func (twh *TenantWebhook) DecodeTenant(obj runtime.RawExtension) (tenant *clv1alpha2.Tenant, err error) {
44+
if twh.decoder == nil {
45+
return nil, errors.New("missing decoder")
46+
}
4947
tenant = &clv1alpha2.Tenant{}
5048
err = twh.decoder.DecodeRaw(obj, tenant)
5149
return

operators/pkg/tenantwh/mutating.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
admissionv1 "k8s.io/api/admission/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
2425
ctrl "sigs.k8s.io/controller-runtime"
2526
"sigs.k8s.io/controller-runtime/pkg/client"
2627
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -38,10 +39,14 @@ type TenantMutator struct {
3839
}
3940

4041
// MakeTenantMutator creates a new webhook handler suitable for controller runtime based on TenantMutator.
41-
func MakeTenantMutator(c client.Client, webhookBypassGroups []string, opSelectorKey, opSelectorValue string, baseWorkspaces []string) *webhook.Admission {
42+
func MakeTenantMutator(c client.Client, webhookBypassGroups []string, opSelectorKey, opSelectorValue string, baseWorkspaces []string, scheme *runtime.Scheme) *webhook.Admission {
4243
return &webhook.Admission{Handler: &TenantMutator{
4344
opSelectorKey, opSelectorValue, baseWorkspaces,
44-
TenantWebhook{Client: c, BypassGroups: webhookBypassGroups},
45+
TenantWebhook{
46+
Client: c,
47+
BypassGroups: webhookBypassGroups,
48+
decoder: admission.NewDecoder(scheme),
49+
},
4550
}}
4651
}
4752

operators/pkg/tenantwh/mutating_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package tenantwh_test
15+
package tenantwh
1616

1717
import (
1818
"net/http"
@@ -26,12 +26,11 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2727

2828
clv1alpha2 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha2"
29-
"github.com/netgroup-polito/CrownLabs/operators/pkg/tenantwh"
3029
)
3130

3231
var _ = Describe("Mutating webhook", func() {
3332
var (
34-
mutatingWH *tenantwh.TenantMutator
33+
mutatingWH *TenantMutator
3534
request admission.Request
3635

3736
opSelectorKey = "crownlabs.polito.it/op-sel"
@@ -53,8 +52,8 @@ var _ = Describe("Mutating webhook", func() {
5352

5453
JustBeforeEach(func() {
5554
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
56-
mutatingWH = tenantwh.MakeTenantMutator(fakeClient, bypassGroups, opSelectorKey, opSelectorValue, baseWorkspaces).Handler.(*tenantwh.TenantMutator)
57-
Expect(mutatingWH.InjectDecoder(decoder)).To(Succeed())
55+
mutatingWH = MakeTenantMutator(fakeClient, bypassGroups, opSelectorKey, opSelectorValue, baseWorkspaces, scheme).Handler.(*TenantMutator)
56+
Expect(mutatingWH.decoder).NotTo(BeNil())
5857
})
5958

6059
Describe("The TenantMutator.Handle method", func() {

operators/pkg/tenantwh/tenantwh_suite_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package tenantwh_test
15+
package tenantwh
1616

1717
import (
1818
"context"
@@ -30,9 +30,8 @@ import (
3030
)
3131

3232
var (
33-
scheme *runtime.Scheme
34-
decoder *admission.Decoder
35-
ctx = context.Background()
33+
scheme *runtime.Scheme
34+
ctx = context.Background()
3635

3736
bypassGroups = []string{"admins"}
3837
testTenantName = "test-tenant"
@@ -43,7 +42,6 @@ var _ = BeforeSuite(func() {
4342
scheme = runtime.NewScheme()
4443
Expect(clv1alpha1.AddToScheme(scheme)).To(Succeed())
4544
Expect(clv1alpha2.AddToScheme(scheme)).To(Succeed())
46-
decoder = admission.NewDecoder(scheme)
4745
})
4846

4947
func TestTenantWebHooks(t *testing.T) {

operators/pkg/tenantwh/validating.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
admissionv1 "k8s.io/api/admission/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/runtime"
2829
ctrl "sigs.k8s.io/controller-runtime"
2930
"sigs.k8s.io/controller-runtime/pkg/client"
3031
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -42,10 +43,11 @@ const LastLoginToleration = time.Hour * 24
4243
type TenantValidator struct{ TenantWebhook }
4344

4445
// MakeTenantValidator creates a new webhook handler suitable for controller runtime based on TenantValidator.
45-
func MakeTenantValidator(c client.Client, webhookBypassGroups []string) *webhook.Admission {
46+
func MakeTenantValidator(c client.Client, webhookBypassGroups []string, scheme *runtime.Scheme) *webhook.Admission {
4647
return &webhook.Admission{Handler: &TenantValidator{TenantWebhook{
4748
Client: c,
4849
BypassGroups: webhookBypassGroups,
50+
decoder: admission.NewDecoder(scheme),
4951
}}}
5052
}
5153

operators/pkg/tenantwh/validating_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package tenantwh_test
15+
package tenantwh
1616

1717
import (
1818
"net/http"
@@ -27,7 +27,6 @@ import (
2727

2828
clv1alpha1 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha1"
2929
clv1alpha2 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha2"
30-
"github.com/netgroup-polito/CrownLabs/operators/pkg/tenantwh"
3130
)
3231

3332
var _ = Describe("Validating webhook", func() {
@@ -47,7 +46,7 @@ var _ = Describe("Validating webhook", func() {
4746
}
4847

4948
var (
50-
validatingWH *tenantwh.TenantValidator
49+
validatingWH *TenantValidator
5150
request admission.Request
5251
response admission.Response
5352
manager *clv1alpha2.Tenant
@@ -111,8 +110,9 @@ var _ = Describe("Validating webhook", func() {
111110
workspaceIM,
112111
).Build()
113112

114-
validatingWH = tenantwh.MakeTenantValidator(fakeClient, bypassGroups).Handler.(*tenantwh.TenantValidator)
115-
Expect(validatingWH.InjectDecoder(decoder)).To(Succeed())
113+
validatingWH = MakeTenantValidator(fakeClient, bypassGroups, scheme).Handler.(*TenantValidator)
114+
115+
Expect(validatingWH.decoder).NotTo(BeNil())
116116
})
117117

118118
Describe("The TenantValidator.Handle method", func() {
@@ -185,7 +185,7 @@ var _ = Describe("Validating webhook", func() {
185185
BeforeEach(func() {
186186
newTenant = &clv1alpha2.Tenant{Spec: clv1alpha2.TenantSpec{
187187
LastLogin: metav1.Time{
188-
Time: time.Now().Add(tenantwh.LastLoginToleration / 2),
188+
Time: time.Now().Add(LastLoginToleration / 2),
189189
},
190190
}}
191191
})
@@ -198,7 +198,7 @@ var _ = Describe("Validating webhook", func() {
198198
BeforeEach(func() {
199199
newTenant = &clv1alpha2.Tenant{Spec: clv1alpha2.TenantSpec{
200200
LastLogin: metav1.Time{
201-
Time: time.Now().Add(tenantwh.LastLoginToleration + time.Millisecond),
201+
Time: time.Now().Add(LastLoginToleration + time.Millisecond),
202202
},
203203
}}
204204
})
@@ -372,7 +372,7 @@ var _ = Describe("Validating webhook", func() {
372372
WhenBody := func(cwdc CalcWsDiffCase) func() {
373373
return func() {
374374
var actuals []string
375-
result := tenantwh.CalculateWorkspacesDiff(
375+
result := CalculateWorkspacesDiff(
376376
&clv1alpha2.Tenant{Spec: clv1alpha2.TenantSpec{Workspaces: cwdc.a}},
377377
&clv1alpha2.Tenant{Spec: clv1alpha2.TenantSpec{Workspaces: cwdc.b}},
378378
)

0 commit comments

Comments
 (0)