Skip to content

Commit

Permalink
Warden admission control for labelled namespaces only as per webhook …
Browse files Browse the repository at this point in the history
…configuration (#192)

* Warden admission should act only in labelled namespaces as per webhook configuration

* give time to k8s in integration test

* give time to k8s in integration test
  • Loading branch information
kwiatekus authored Feb 12, 2024
1 parent 68cc849 commit 9c3c3bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
11 changes: 9 additions & 2 deletions charts/warden/charts/warden-admission/templates/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ webhooks:
failurePolicy: Ignore
sideEffects: None
matchPolicy: Exact
timeoutSeconds: 15
timeoutSeconds: 1
admissionReviewVersions: [ "v1beta1", "v1" ]
name: validation.webhook.warden.kyma-project.io
namespaceSelector:
matchLabels:
namespaces.warden.kyma-project.io/validate: enabled
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
Expand All @@ -31,6 +34,10 @@ webhooks:
failurePolicy: Ignore
sideEffects: None
matchPolicy: Exact
timeoutSeconds: 15
timeoutSeconds: 10
admissionReviewVersions: [ "v1beta1", "v1" ]
name: defaulting.webhook.warden.kyma-project.io
namespaceSelector:
matchLabels:
namespaces.warden.kyma-project.io/validate: enabled

21 changes: 17 additions & 4 deletions internal/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package webhook

import (
"context"
"k8s.io/utils/ptr"
"reflect"

"k8s.io/utils/ptr"

"github.com/kyma-project/warden/internal/admission"

"github.com/kyma-project/warden/pkg"
"github.com/pkg/errors"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -25,7 +27,8 @@ const (
DefaultingWebhookName = "defaulting.webhook.warden.kyma-project.io"
ValidationWebhookName = "validation.webhook.warden.kyma-project.io"

WebhookTimeout = 15
ValidationWebhookTimeout = 1
MutationWebhookTimeout = 10

PodValidationPath = "/validation/pods"
)
Expand Down Expand Up @@ -123,7 +126,12 @@ func getFunctionMutatingWebhookCfg(config WebhookConfig) admissionregistrationv1
},
},
SideEffects: &sideEffects,
TimeoutSeconds: ptr.To[int32](WebhookTimeout),
TimeoutSeconds: ptr.To[int32](MutationWebhookTimeout),
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
pkg.NamespaceValidationLabel: pkg.NamespaceValidationEnabled,
},
},
}
}

Expand Down Expand Up @@ -172,7 +180,12 @@ func createValidatingWebhookConfiguration(config WebhookConfig) *admissionregist
},

SideEffects: &sideEffects,
TimeoutSeconds: ptr.To[int32](WebhookTimeout),
TimeoutSeconds: ptr.To[int32](ValidationWebhookTimeout),
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
pkg.NamespaceValidationLabel: pkg.NamespaceValidationEnabled,
},
},
},
},
}
Expand Down
14 changes: 9 additions & 5 deletions tests/helpers/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package helpers
import (
"context"
"fmt"
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"testing"
"time"
)

type testContext struct {
Expand All @@ -19,7 +19,7 @@ type testContext struct {
validationEnabled bool
namePrefix string
namespaceName string
namespace *corev1.Namespace
namespace *v1.Namespace
}

func NewTestContext(t *testing.T, namePrefix string) *testContext {
Expand All @@ -42,6 +42,10 @@ func (tc *testContext) Initialize() *testContext {
tc.client, err = ctrlclient.New(ctrl.GetConfigOrDie(), ctrlclient.Options{})
require.NoError(tc.test, err)
tc.CreateNamespace()
if tc.validationEnabled {
//give some time for k8s to reconcile webhook selectors
time.Sleep(1 * time.Second)
}
return tc
}

Expand Down

0 comments on commit 9c3c3bf

Please sign in to comment.