diff --git a/internal/apis/config/cainjector/v1alpha1/defaults_test.go b/internal/apis/config/cainjector/v1alpha1/defaults_test.go new file mode 100644 index 000000000..4fa0b377e --- /dev/null +++ b/internal/apis/config/cainjector/v1alpha1/defaults_test.go @@ -0,0 +1,64 @@ +/* +Copyright 2021 The cert-manager Authors. + +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 v1alpha1 + +import ( + "encoding/json" + "os" + "testing" + + "github.com/cert-manager/cert-manager/pkg/apis/config/cainjector/v1alpha1" + "github.com/stretchr/testify/require" +) + +func TestCAInjectorConfigurationDefaults(t *testing.T) { + tests := []struct { + name string + config *v1alpha1.CAInjectorConfiguration + jsonFilePath string + }{ + { + "v1alpha1", + &v1alpha1.CAInjectorConfiguration{}, + "testdata/defaults.json", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SetObjectDefaults_CAInjectorConfiguration(tt.config) + + defaultData, err := json.MarshalIndent(tt.config, "", "\t") + if err != nil { + t.Fatal(err) + } + + if os.Getenv("UPDATE_DEFAULTS") == "true" { + if err := os.WriteFile(tt.jsonFilePath, defaultData, 0644); err != nil { + t.Fatal(err) + } + t.Log("cainjector config api defaults updated") + } + + expectedData, err := os.ReadFile(tt.jsonFilePath) + if err != nil { + t.Fatal(err) + } + + require.Equal(t, expectedData, defaultData) + }) + } +} diff --git a/internal/apis/config/cainjector/v1alpha1/testdata/defaults.json b/internal/apis/config/cainjector/v1alpha1/testdata/defaults.json new file mode 100644 index 000000000..ee6066438 --- /dev/null +++ b/internal/apis/config/cainjector/v1alpha1/testdata/defaults.json @@ -0,0 +1,30 @@ +{ + "leaderElectionConfig": { + "enabled": true, + "namespace": "kube-system", + "leaseDuration": 60000000000, + "renewDeadline": 40000000000, + "retryPeriod": 15000000000 + }, + "enableDataSourceConfig": { + "certificates": true + }, + "enableInjectableConfig": { + "validatingWebhookConfigurations": true, + "mutatingWebhookConfigurations": true, + "customResourceDefinitions": true, + "apiServices": true + }, + "enablePprof": false, + "pprofAddress": "localhost:6060", + "logging": { + "format": "text", + "flushFrequency": "5s", + "verbosity": 0, + "options": { + "json": { + "infoBufferSize": "0" + } + } + } +} \ No newline at end of file diff --git a/internal/apis/config/controller/v1alpha1/defaults_test.go b/internal/apis/config/controller/v1alpha1/defaults_test.go new file mode 100644 index 000000000..a6927829f --- /dev/null +++ b/internal/apis/config/controller/v1alpha1/defaults_test.go @@ -0,0 +1,64 @@ +/* +Copyright 2021 The cert-manager Authors. + +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 v1alpha1 + +import ( + "encoding/json" + "os" + "testing" + + "github.com/cert-manager/cert-manager/pkg/apis/config/controller/v1alpha1" + "github.com/stretchr/testify/require" +) + +const TestFileLocation = "testdata/defaults.json" + +func TestControllerConfigurationDefaults(t *testing.T) { + tests := []struct { + name string + config *v1alpha1.ControllerConfiguration + jsonFilePath string + }{ + { + "v1alpha1", + &v1alpha1.ControllerConfiguration{}, + "testdata/defaults.json", + }, + } + for _, tt := range tests { + SetObjectDefaults_ControllerConfiguration(tt.config) + + defaultData, err := json.MarshalIndent(tt.config, "", "\t") + if err != nil { + t.Fatal(err) + } + + if os.Getenv("UPDATE_DEFAULTS") == "true" { + if err := os.WriteFile(tt.jsonFilePath, defaultData, 0644); err != nil { + t.Fatal(err) + } + t.Log("cainjector config api defaults updated") + } + + expectedData, err := os.ReadFile(tt.jsonFilePath) + if err != nil { + t.Fatal(err) + } + + require.Equal(t, expectedData, defaultData) + } +} diff --git a/internal/apis/config/controller/v1alpha1/testdata/defaults.json b/internal/apis/config/controller/v1alpha1/testdata/defaults.json new file mode 100644 index 000000000..1004ab5d9 --- /dev/null +++ b/internal/apis/config/controller/v1alpha1/testdata/defaults.json @@ -0,0 +1,66 @@ +{ + "kubernetesAPIQPS": 20, + "kubernetesAPIBurst": 50, + "clusterResourceNamespace": "kube-system", + "leaderElectionConfig": { + "enabled": true, + "namespace": "kube-system", + "leaseDuration": 60000000000, + "renewDeadline": 40000000000, + "retryPeriod": 15000000000, + "healthzTimeout": 20000000000 + }, + "controllers": [ + "*" + ], + "issuerAmbientCredentials": false, + "clusterIssuerAmbientCredentials": true, + "enableCertificateOwnerRef": false, + "copiedAnnotationPrefixes": [ + "*", + "-kubectl.kubernetes.io/", + "-fluxcd.io/", + "-argocd.argoproj.io/" + ], + "numberOfConcurrentWorkers": 5, + "maxConcurrentChallenges": 60, + "metricsListenAddress": "0.0.0.0:9402", + "metricsTLSConfig": { + "filesystem": {}, + "dynamic": { + "LeafDuration": 0 + } + }, + "healthzListenAddress": "0.0.0.0:9403", + "enablePprof": false, + "pprofAddress": "localhost:6060", + "logging": { + "format": "text", + "flushFrequency": "5s", + "verbosity": 0, + "options": { + "json": { + "infoBufferSize": "0" + } + } + }, + "ingressShimConfig": { + "defaultIssuerKind": "Issuer", + "defaultIssuerGroup": "cert-manager.io", + "defaultAutoCertificateAnnotations": [ + "kubernetes.io/tls-acme" + ] + }, + "acmeHTTP01Config": { + "solverImage": "quay.io/jetstack/cert-manager-acmesolver:canary", + "solverResourceRequestCPU": "10m", + "solverResourceRequestMemory": "64Mi", + "solverResourceLimitsCPU": "100m", + "solverResourceLimitsMemory": "64Mi", + "solverRunAsNonRoot": true + }, + "acmeDNS01Config": { + "recursiveNameserversOnly": false, + "checkRetryPeriod": 10000000000 + } +} \ No newline at end of file diff --git a/internal/apis/config/webhook/v1alpha1/defaults_test.go b/internal/apis/config/webhook/v1alpha1/defaults_test.go new file mode 100644 index 000000000..d25842d34 --- /dev/null +++ b/internal/apis/config/webhook/v1alpha1/defaults_test.go @@ -0,0 +1,64 @@ +/* +Copyright 2021 The cert-manager Authors. + +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 v1alpha1 + +import ( + "encoding/json" + "os" + "testing" + + "github.com/cert-manager/cert-manager/pkg/apis/config/webhook/v1alpha1" + "github.com/stretchr/testify/require" +) + +func TestWebhookConfigurationDefaults(t *testing.T) { + tests := []struct { + name string + config *v1alpha1.WebhookConfiguration + jsonFilePath string + }{ + { + "v1alpha1", + &v1alpha1.WebhookConfiguration{}, + "testdata/defaults.json", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SetObjectDefaults_WebhookConfiguration(tt.config) + + defaultData, err := json.MarshalIndent(tt.config, "", "\t") + if err != nil { + t.Fatal(err) + } + + if os.Getenv("UPDATE_DEFAULTS") == "true" { + if err := os.WriteFile(tt.jsonFilePath, defaultData, 0644); err != nil { + t.Fatal(err) + } + t.Log("cainjector config api defaults updated") + } + + expectedData, err := os.ReadFile(tt.jsonFilePath) + if err != nil { + t.Fatal(err) + } + + require.Equal(t, expectedData, defaultData) + }) + } +} diff --git a/internal/apis/config/webhook/v1alpha1/testdata/defaults.json b/internal/apis/config/webhook/v1alpha1/testdata/defaults.json new file mode 100644 index 000000000..4537a3e93 --- /dev/null +++ b/internal/apis/config/webhook/v1alpha1/testdata/defaults.json @@ -0,0 +1,22 @@ +{ + "securePort": 6443, + "healthzPort": 6080, + "tlsConfig": { + "filesystem": {}, + "dynamic": { + "LeafDuration": 0 + } + }, + "enablePprof": false, + "pprofAddress": "localhost:6060", + "logging": { + "format": "text", + "flushFrequency": "5s", + "verbosity": 0, + "options": { + "json": { + "infoBufferSize": "0" + } + } + } +} \ No newline at end of file diff --git a/make/test.mk b/make/test.mk index 4feb96189..fdf688b27 100644 --- a/make/test.mk +++ b/make/test.mk @@ -87,6 +87,12 @@ unit-test-controller: | $(NEEDS_GOTESTSUM) unit-test-webhook: | $(NEEDS_GOTESTSUM) cd cmd/webhook && $(GOTESTSUM) ./... +.PHONY: update-config-api-defaults +update-config-api-defaults: | $(NEEDS_GO) + cd internal/apis/config/cainjector/v1alpha1/ && UPDATE_DEFAULTS=true $(GO) test . && echo "cainjector config api defaults updated" + cd internal/apis/config/controller/v1alpha1/ && UPDATE_DEFAULTS=true $(GO) test . && echo "controller config api defaults updated" + cd internal/apis/config/webhook/v1alpha1/ && UPDATE_DEFAULTS=true $(GO) test . && echo "webhook config api defaults updated" + .PHONY: setup-integration-tests setup-integration-tests: templated-crds