Skip to content

Commit

Permalink
test(webhooks): add unit tests for new validation webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed Sep 16, 2024
1 parent ff7f74c commit d0d5fc7
Show file tree
Hide file tree
Showing 16 changed files with 718 additions and 437 deletions.
83 changes: 77 additions & 6 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
# Note: The kustomize configs are not used in production, but they are currently still used when setting up the unit
# test environment, like in internal/dash0/webhooks/webhook_suite_test.go -> &envtest.Environment#WebhookInstallOptions.
# Thus, they need to be kept in sync with the webhook resources defined in
# helm-chart/dash0-operator/templates/operator/deployment-and-webhooks.yaml.
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: mutating-webhook-configuration
name: dash0-operator-injector
labels:
app.kubernetes.io/name: dash0-operator
app.kubernetes.io/component: injector
app.kubernetes.io/instance: mutating-webhook
app.kubernetes.io/part-of: dash0-operator
webhooks:
- admissionReviewVersions:
- name: inject.dash0.com
admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
name: dash0-operator-webhook-service
namespace: dash0-system
path: /v1alpha1/inject/dash0
failurePolicy: Ignore
name: inject.dash0.kb.io
rules:
- apiGroups:
- apps
Expand Down Expand Up @@ -55,4 +64,66 @@ webhooks:
resources:
- pods
sideEffects: None
timeoutSeconds: 5
timeoutSeconds: 5
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: dash0-operator-operator-configuration-validator
labels:
app.kubernetes.io/name: dash0-operator
app.kubernetes.io/component: validator
app.kubernetes.io/instance: operator-configuration-validator-webhook
app.kubernetes.io/part-of: dash0-operator
webhooks:
- name: validate-operator-configuration.dash0.com
clientConfig:
service:
name: dash0-operator-webhook-service
namespace: namespace
path: /v1alpha1/validate/operator-configuration
admissionReviewVersions:
- v1
rules:
- apiGroups:
- operator.dash0.com
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- dash0operatorconfigurations
sideEffects: None
timeoutSeconds: 5
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: dash0-operator-monitoring-validator
labels:
app.kubernetes.io/name: dash0-operator
app.kubernetes.io/component: validator
app.kubernetes.io/instance: monitoring-validator-webhook
app.kubernetes.io/part-of: dash0-operator
webhooks:
- name: validate-monitoring.dash0.com
clientConfig:
service:
name: dash0-operator-webhook-service
namespace: namespace
path: /v1alpha1/validate/monitoring
admissionReviewVersions:
- v1
rules:
- apiGroups:
- operator.dash0.com
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- dash0monitorings
sideEffects: None
timeoutSeconds: 5
26 changes: 7 additions & 19 deletions internal/backendconnection/backendconnection_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,9 @@ var _ = Describe("The backend connection manager", Ordered, func() {
})

It("should fall back to the operator configuration export settings if the monitoring resource has no export", func() {
CreateOperatorConfigurationResource(
CreateDefaultOperatorConfigurationResource(
ctx,
k8sClient,
"operator-configuration-resource",
dash0v1alpha1.Dash0OperatorConfigurationSpec{
Export: &dash0v1alpha1.Export{
Dash0: &dash0v1alpha1.Dash0Configuration{
Endpoint: EndpointDash0Test,
Authorization: dash0v1alpha1.Authorization{
Token: &AuthorizationTokenTest,
},
},
},
},
)
err := manager.EnsureOpenTelemetryCollectorIsDeployedInOperatorNamespace(
ctx,
Expand Down Expand Up @@ -190,10 +179,9 @@ var _ = Describe("The backend connection manager", Ordered, func() {

It("should fail if the monitoring resource has no export and the existing operator configuration "+
"resource has no export either", func() {
CreateOperatorConfigurationResource(
CreateOperatorConfigurationResourceWithSpec(
ctx,
k8sClient,
"operator-configuration-resource",
dash0v1alpha1.Dash0OperatorConfigurationSpec{},
)
err := manager.EnsureOpenTelemetryCollectorIsDeployedInOperatorNamespace(
Expand Down Expand Up @@ -253,15 +241,15 @@ var _ = Describe("The backend connection manager", Ordered, func() {
It("should not delete the collector if there are still Dash0 monitoring resources", func() {
// create multiple Dash0 monitoring resources
firstName := types.NamespacedName{Namespace: TestNamespaceName, Name: "das0-monitoring-test-resource-1"}
firstDash0MonitoringResource := CreateDash0MonitoringResource(ctx, k8sClient, firstName)
firstDash0MonitoringResource := CreateDefaultMonitoringResource(ctx, k8sClient, firstName)
createdObjects = append(createdObjects, firstDash0MonitoringResource)

secondName := types.NamespacedName{Namespace: TestNamespaceName, Name: "das0-monitoring-test-resource-2"}
secondDash0MonitoringResource := CreateDash0MonitoringResource(ctx, k8sClient, secondName)
secondDash0MonitoringResource := CreateDefaultMonitoringResource(ctx, k8sClient, secondName)
createdObjects = append(createdObjects, secondDash0MonitoringResource)

thirdName := types.NamespacedName{Namespace: TestNamespaceName, Name: "das0-monitoring-test-resource-3"}
thirdDash0MonitoringResource := CreateDash0MonitoringResource(ctx, k8sClient, thirdName)
thirdDash0MonitoringResource := CreateDefaultMonitoringResource(ctx, k8sClient, thirdName)
createdObjects = append(createdObjects, thirdDash0MonitoringResource)

// Let the manager create the collector so there is something to delete.
Expand All @@ -287,7 +275,7 @@ var _ = Describe("The backend connection manager", Ordered, func() {

It("should not delete the collector if there is only one Dash0 monitoring resource left but it is not the one being deleted", func() {
resourceName := types.NamespacedName{Namespace: TestNamespaceName, Name: "das0-monitoring-test-resource-1"}
existingDash0MonitoringResource := CreateDash0MonitoringResource(ctx, k8sClient, resourceName)
existingDash0MonitoringResource := CreateDefaultMonitoringResource(ctx, k8sClient, resourceName)
createdObjects = append(createdObjects, existingDash0MonitoringResource)

// Let the manager create the collector so there is something to delete.
Expand Down Expand Up @@ -332,7 +320,7 @@ var _ = Describe("The backend connection manager", Ordered, func() {
It("should delete the collector if the Dash0 monitoring resource that is being deleted is the only one left", func() {
// create multiple Dash0 monitoring resources
resourceName := types.NamespacedName{Namespace: TestNamespaceName, Name: "das0-monitoring-test-resource-1"}
dash0MonitoringResource := CreateDash0MonitoringResource(ctx, k8sClient, resourceName)
dash0MonitoringResource := CreateDefaultMonitoringResource(ctx, k8sClient, resourceName)
createdObjects = append(createdObjects, dash0MonitoringResource)

// Let the manager create the collector so there is something to delete.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var _ = Describe("The OpenTelemetry Collector resource manager", Ordered, func()
BeforeAll(func() {
EnsureDash0OperatorNamespaceExists(ctx, k8sClient)
EnsureTestNamespaceExists(ctx, k8sClient)
dash0MonitoringResource = EnsureDash0MonitoringResourceExists(ctx, k8sClient)
dash0MonitoringResource = EnsureMonitoringResourceExists(ctx, k8sClient)
})

BeforeEach(func() {
Expand Down Expand Up @@ -137,20 +137,9 @@ var _ = Describe("The OpenTelemetry Collector resource manager", Ordered, func()
})

It("should fall back to the operator configuration export settings if the monitoring resource has no export", func() {
CreateOperatorConfigurationResource(
CreateDefaultOperatorConfigurationResource(
ctx,
k8sClient,
"operator-configuration-resource",
dash0v1alpha1.Dash0OperatorConfigurationSpec{
Export: &dash0v1alpha1.Export{
Dash0: &dash0v1alpha1.Dash0Configuration{
Endpoint: EndpointDash0Test,
Authorization: dash0v1alpha1.Authorization{
Token: &AuthorizationTokenTest,
},
},
},
},
)
resourcesHaveBeenCreated, resourcesHaveBeenUpdated, err := oTelColResourceManager.CreateOrUpdateOpenTelemetryCollectorResources(
ctx,
Expand Down Expand Up @@ -186,10 +175,9 @@ var _ = Describe("The OpenTelemetry Collector resource manager", Ordered, func()

It("should fail if the monitoring resource has no export and the existing operator configuration "+
"resource has no export either", func() {
CreateOperatorConfigurationResource(
CreateOperatorConfigurationResourceWithSpec(
ctx,
k8sClient,
"operator-configuration-resource",
dash0v1alpha1.Dash0OperatorConfigurationSpec{},
)
_, _, err := oTelColResourceManager.CreateOrUpdateOpenTelemetryCollectorResources(
Expand Down
Loading

0 comments on commit d0d5fc7

Please sign in to comment.