Skip to content

Commit

Permalink
fix: webhook-controller backport
Browse files Browse the repository at this point in the history
Some changes were needed from the PR in main branch.
- "slices" package is not yet in go 1.19
- The "TypeMeta" needs to be defined in objects used in unit tests.
- Do not use "k8s.io/utils/ptr" package.

Signed-off-by: Andrej Krejcir <akrejcir@redhat.com>
  • Loading branch information
akrejcir committed May 2, 2024
1 parent b588f9c commit 52995a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
12 changes: 10 additions & 2 deletions controllers/webhook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controllers

import (
"context"
"slices"

admissionv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -94,7 +93,7 @@ func updateWebhookConfiguration(webhookConfig *admissionv1.ValidatingWebhookConf
// Check if the webhook reacts to SSP resource.
var hasSspRule bool
for _, rule := range webhook.Rules {
if slices.Contains(rule.APIGroups, sspv1beta2.GroupVersion.Group) {
if containsString(rule.APIGroups, sspv1beta2.GroupVersion.Group) {
hasSspRule = true
break
}
Expand All @@ -108,3 +107,12 @@ func updateWebhookConfiguration(webhookConfig *admissionv1.ValidatingWebhookConf
}
return changed
}

func containsString(slice []string, value string) bool {
for _, s := range slice {
if s == value {
return true
}
}
return false
}
11 changes: 8 additions & 3 deletions controllers/wehook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
admissionv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -28,7 +28,12 @@ var _ = Describe("Webhook controller", func() {
)

BeforeEach(func() {
sideEffectsNone := admissionv1.SideEffectClassNone
webhookConfig = &admissionv1.ValidatingWebhookConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: admissionv1.SchemeGroupVersion.String(),
Kind: "ValidatingWebhookConfiguration",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-webhook",
Labels: map[string]string{
Expand All @@ -41,7 +46,7 @@ var _ = Describe("Webhook controller", func() {
Service: &admissionv1.ServiceReference{
Namespace: "test-namespace",
Name: "test-name",
Path: ptr.To("/webhook"),
Path: pointer.String("/webhook"),
},
},
Rules: []admissionv1.RuleWithOperations{{
Expand All @@ -59,7 +64,7 @@ var _ = Describe("Webhook controller", func() {
"test-namespace-label": "some-value",
},
},
SideEffects: ptr.To(admissionv1.SideEffectClassNone),
SideEffects: &sideEffectsNone,
AdmissionReviewVersions: []string{"v1"},
}},
}
Expand Down
4 changes: 2 additions & 2 deletions tests/webhook_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

admissionv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

sspv1beta2 "kubevirt.io/ssp-operator/api/v1beta2"
Expand All @@ -20,6 +19,7 @@ var _ = Describe("Webhook controller", func() {
var webhook *admissionv1.ValidatingWebhookConfiguration

BeforeEach(func() {
sideEffectsNone := admissionv1.SideEffectClassNone
webhook = &admissionv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "ssp.kubevirt.io-test-webhook-",
Expand Down Expand Up @@ -49,7 +49,7 @@ var _ = Describe("Webhook controller", func() {
"ssp-webhook-test-label": "ssp-webhook-test-label-vale",
},
},
SideEffects: ptr.To(admissionv1.SideEffectClassNone),
SideEffects: &sideEffectsNone,
AdmissionReviewVersions: []string{"v1"},
}},
}
Expand Down

0 comments on commit 52995a8

Please sign in to comment.