Skip to content

Commit

Permalink
selectedNamespace contains a single namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmoisey committed Jun 29, 2024
1 parent 7a1aea1 commit e14db24
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion vertical-pod-autoscaler/pkg/admission-controller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ build-binary: clean
$(ENVVAR) GOOS=$(GOOS) go build -o ${COMPONENT}

test-unit: clean build
$(TEST_ENVVAR) go test --test.short -race ./... $(FLAGS)
$(TEST_ENVVAR) go test -count=1 --test.short -race ./... $(FLAGS)

.PHONY: docker-build
docker-build: $(addprefix docker-build-,$(ALL_ARCHITECTURES))
Expand Down
7 changes: 3 additions & 4 deletions vertical-pod-autoscaler/pkg/admission-controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func configTLS(cfg certsConfig, minTlsVersion, ciphers string, stop <-chan struc

// register this webhook admission controller with the kube-apiserver
// by creating MutatingWebhookConfiguration.
func selfRegistration(clientset kubernetes.Interface, caCert []byte, namespace, serviceName, url string, registerByURL bool, timeoutSeconds int32, selectedNamespaces string, ignoredNamespaces string) {
func selfRegistration(clientset kubernetes.Interface, caCert []byte, namespace, serviceName, url string, registerByURL bool, timeoutSeconds int32, selectedNamespace string, ignoredNamespaces string) {
time.Sleep(10 * time.Second)
client := clientset.AdmissionregistrationV1().MutatingWebhookConfigurations()
_, err := client.Get(context.TODO(), webhookConfigName, metav1.GetOptions{})
Expand All @@ -112,7 +112,6 @@ func selfRegistration(clientset kubernetes.Interface, caCert []byte, namespace,
failurePolicy := admissionregistration.Ignore
RegisterClientConfig.CABundle = caCert

selectedNamespacesList := strings.Split(selectedNamespaces, ",")
ignoredNamespacesList := strings.Split(ignoredNamespaces, ",")

var namespaceSelector metav1.LabelSelector
Expand All @@ -126,13 +125,13 @@ func selfRegistration(clientset kubernetes.Interface, caCert []byte, namespace,
},
},
}
} else if len(selectedNamespaces) > 0 {
} else if len(selectedNamespace) > 0 {
namespaceSelector = metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "kubernetes.io/metadata.name",
Operator: metav1.LabelSelectorOpIn,
Values: selectedNamespacesList,
Values: []string{selectedNamespace},
},
},
}
Expand Down
22 changes: 11 additions & 11 deletions vertical-pod-autoscaler/pkg/admission-controller/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func TestSelfRegistrationBase(t *testing.T) {
url := "http://example.com/"
registerByURL := true
timeoutSeconds := int32(32)
selectedNamespaces := ""
selectedNamespace := ""
ignoredNamespaces := ""

selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespaces, ignoredNamespaces)
selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespace, ignoredNamespaces)

webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations()
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{})
Expand Down Expand Up @@ -78,10 +78,10 @@ func TestSelfRegistrationWithURL(t *testing.T) {
url := "http://example.com/"
registerByURL := true
timeoutSeconds := int32(32)
selectedNamespaces := ""
selectedNamespace := ""
ignoredNamespaces := ""

selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespaces, ignoredNamespaces)
selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespace, ignoredNamespaces)

webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations()
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{})
Expand All @@ -105,10 +105,10 @@ func TestSelfRegistrationWithOutURL(t *testing.T) {
url := "http://example.com/"
registerByURL := false
timeoutSeconds := int32(32)
selectedNamespaces := ""
selectedNamespace := ""
ignoredNamespaces := ""

selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespaces, ignoredNamespaces)
selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespace, ignoredNamespaces)

webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations()
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{})
Expand All @@ -134,10 +134,10 @@ func TestSelfRegistrationWithIgnoredNamespaces(t *testing.T) {
url := "http://example.com/"
registerByURL := false
timeoutSeconds := int32(32)
selectedNamespaces := ""
selectedNamespace := ""
ignoredNamespaces := "test"

selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespaces, ignoredNamespaces)
selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespace, ignoredNamespaces)

webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations()
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{})
Expand All @@ -164,10 +164,10 @@ func TestSelfRegistrationWithSelectedNamespaces(t *testing.T) {
url := "http://example.com/"
registerByURL := false
timeoutSeconds := int32(32)
selectedNamespaces := "test,namespaces"
selectedNamespace := "test"
ignoredNamespaces := ""

selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespaces, ignoredNamespaces)
selfRegistration(testClientSet, caCert, namespace, serviceName, url, registerByURL, timeoutSeconds, selectedNamespace, ignoredNamespaces)

webhookConfigInterface := testClientSet.AdmissionregistrationV1().MutatingWebhookConfigurations()
webhookConfig, err := webhookConfigInterface.Get(context.TODO(), webhookConfigName, metav1.GetOptions{})
Expand All @@ -182,5 +182,5 @@ func TestSelfRegistrationWithSelectedNamespaces(t *testing.T) {

matchExpression := webhook.NamespaceSelector.MatchExpressions[0]
assert.Equal(t, metav1.LabelSelectorOpIn, matchExpression.Operator, "expected namespace operator to be OpIn")
assert.Equal(t, strings.Split(selectedNamespaces, ","), matchExpression.Values, "expected namespace selector match expression to be equal")
assert.Equal(t, strings.Split(selectedNamespace, ","), matchExpression.Values, "expected namespace selector match expression to be equal")
}

0 comments on commit e14db24

Please sign in to comment.