-
Notifications
You must be signed in to change notification settings - Fork 74
Fix issue #1668 about auto renew certificate on mutating / validating #1669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // | ||
| // Copyright (c) 2019-2026 Red Hat, Inc. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
|
|
||
| package infrastructure | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCertManagerDetectedReturnsTrueWhenCertManagerInstalled(t *testing.T) { | ||
| InitializeForTestingWithCertManager(Kubernetes) | ||
| assert.True(t, CertManagerDetected()) | ||
| } | ||
|
|
||
| func TestCertManagerDetectedReturnsFalseWhenCertManagerNotInstalled(t *testing.T) { | ||
| InitializeForTesting(Kubernetes) | ||
| assert.False(t, CertManagerDetected()) | ||
| } | ||
|
|
||
| func TestCertManagerDetectedWithOpenShift(t *testing.T) { | ||
| InitializeForTestingWithCertManager(OpenShiftv4) | ||
| assert.True(t, CertManagerDetected()) | ||
| } | ||
|
|
||
| func TestCertManagerDetectedPanicsWhenNotInitialized(t *testing.T) { | ||
| initialized = false | ||
| defer func() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using |
||
| InitializeForTesting(Kubernetes) | ||
| }() | ||
| assert.Panics(t, func() { | ||
| CertManagerDetected() | ||
| }) | ||
| } | ||
|
|
||
| func TestInitializeForTestingSetsCertManagerDetectedToFalse(t *testing.T) { | ||
| InitializeForTesting(OpenShiftv4) | ||
| assert.False(t, CertManagerDetected()) | ||
| assert.True(t, IsOpenShift()) | ||
| } | ||
|
|
||
| func TestInitializeForTestingWithCertManagerSetsDetectedToTrue(t *testing.T) { | ||
| InitializeForTestingWithCertManager(Kubernetes) | ||
| assert.True(t, CertManagerDetected()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // | ||
| // Copyright (c) 2019-2026 Red Hat, Inc. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
|
|
||
| package workspace | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/devfile/devworkspace-operator/pkg/infrastructure" | ||
| ) | ||
|
|
||
| func getWebhookAnnotations(namespace string) map[string]string { | ||
| annotations := map[string]string{} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a brief comment here explaining why cert-manager takes priority over the OpenShift Service CA annotation. The ordering is intentional and correct, but without a comment a future maintainer may read the |
||
| if infrastructure.CertManagerDetected() { | ||
| annotations["cert-manager.io/inject-ca-from"] = fmt.Sprintf("%s/devworkspace-controller-serving-cert", namespace) | ||
| } else if infrastructure.IsOpenShift() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting |
||
| annotations["service.beta.openshift.io/inject-cabundle"] = "true" | ||
| } | ||
| return annotations | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // | ||
| // Copyright (c) 2019-2026 Red Hat, Inc. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
|
|
||
| package workspace | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/devfile/devworkspace-operator/pkg/infrastructure" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGetWebhookAnnotationsWithCertManager(t *testing.T) { | ||
| infrastructure.InitializeForTestingWithCertManager(infrastructure.Kubernetes) | ||
| annotations := getWebhookAnnotations("test-namespace") | ||
| assert.Equal(t, map[string]string{ | ||
| "cert-manager.io/inject-ca-from": "test-namespace/devworkspace-controller-serving-cert", | ||
| }, annotations) | ||
| } | ||
|
|
||
| func TestGetWebhookAnnotationsWithOpenShift(t *testing.T) { | ||
| infrastructure.InitializeForTesting(infrastructure.OpenShiftv4) | ||
| annotations := getWebhookAnnotations("test-namespace") | ||
| assert.Equal(t, map[string]string{ | ||
| "service.beta.openshift.io/inject-cabundle": "true", | ||
| }, annotations) | ||
| } | ||
|
|
||
| func TestGetWebhookAnnotationsWithKubernetes(t *testing.T) { | ||
| infrastructure.InitializeForTesting(infrastructure.Kubernetes) | ||
| annotations := getWebhookAnnotations("test-namespace") | ||
| assert.Empty(t, annotations) | ||
| } | ||
|
|
||
| func TestGetWebhookAnnotationsWithCertManagerOnOpenShift(t *testing.T) { | ||
| infrastructure.InitializeForTestingWithCertManager(infrastructure.OpenShiftv4) | ||
| annotations := getWebhookAnnotations("test-namespace") | ||
| assert.Equal(t, map[string]string{ | ||
| "cert-manager.io/inject-ca-from": "test-namespace/devworkspace-controller-serving-cert", | ||
| }, annotations) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // | ||
| // Copyright (c) 2019-2025 Red Hat, Inc. | ||
| // Copyright (c) 2019-2026 Red Hat, Inc. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
|
|
@@ -153,8 +153,9 @@ func BuildMutateWebhookCfg(namespace string) *admregv1.MutatingWebhookConfigurat | |
|
|
||
| return &admregv1.MutatingWebhookConfiguration{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: MutateWebhookCfgName, | ||
| Labels: server.WebhookServerAppLabels(), | ||
| Name: MutateWebhookCfgName, | ||
| Labels: server.WebhookServerAppLabels(), | ||
| Annotations: getWebhookAnnotations(namespace), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When cert-manager or the OpenShift Service CA operator is active, the webhook config will have both an explicit |
||
| }, | ||
| Webhooks: []admregv1.MutatingWebhook{ | ||
| workspaceMutateWebhook, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to move the
certManagerDetectedassignment intoInitialize()alongside thedetect()call, rather than insidedetect()as a side effect? The function's name and(Type, error)return type suggest it only determines the cluster type, so the hidden side effect is a bit surprising. It also makesdetect()harder to test in isolation and is architecturally inconsistent with how OpenShift detection works (returned as a value, not a side effect). One option: return a richer struct fromdetect()(e.g.,DetectionResult{InfraType, CertManagerAvailable}) or call a separate function fromInitialize().