This repository was archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Configure controller to trust registries with self-signed certs #219
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c2f8ce1
Add CustomCerts to enable controller to trust self-signed registry
jcrossley3 9daa2fb
Add a CustomCerts Transformer with unit tests
jcrossley3 7ec6004
Fixed prow bot linter errors
jcrossley3 b32ce7a
Added empty string to CRD enum to fix e2e test
jcrossley3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| Copyright 2019 The Knative 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 common | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| mf "github.com/jcrossley3/manifestival" | ||
| "go.uber.org/zap" | ||
| appsv1 "k8s.io/api/apps/v1" | ||
| v1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| servingv1alpha1 "knative.dev/serving-operator/pkg/apis/serving/v1alpha1" | ||
| ) | ||
|
|
||
| const ( | ||
| customCertsEnvName = "SSL_CERT_DIR" | ||
| customCertsMountPath = "/custom-certs" | ||
| customCertsNamePrefix = "custom-certs-" | ||
| ) | ||
|
|
||
| // CustomCertsTransform configures the controller deployment to trust | ||
| // registries with self-signed certs | ||
| func CustomCertsTransform(instance *servingv1alpha1.KnativeServing, log *zap.SugaredLogger) mf.Transformer { | ||
| empty := servingv1alpha1.CustomCerts{} | ||
| return func(u *unstructured.Unstructured) error { | ||
| if instance.Spec.ControllerCustomCerts == empty { | ||
| return nil | ||
| } | ||
| if u.GetKind() == "Deployment" && u.GetName() == "controller" { | ||
| certs := instance.Spec.ControllerCustomCerts | ||
| deployment := &appsv1.Deployment{} | ||
| if err := scheme.Scheme.Convert(u, deployment, nil); err != nil { | ||
| return err | ||
| } | ||
| if err := configureCustomCerts(deployment, certs); err != nil { | ||
| return err | ||
| } | ||
| if err := scheme.Scheme.Convert(deployment, u, nil); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func configureCustomCerts(deployment *appsv1.Deployment, certs servingv1alpha1.CustomCerts) error { | ||
| source := v1.VolumeSource{} | ||
| switch certs.Type { | ||
| case "ConfigMap": | ||
| source.ConfigMap = &v1.ConfigMapVolumeSource{ | ||
| LocalObjectReference: v1.LocalObjectReference{ | ||
| Name: certs.Name, | ||
| }, | ||
| } | ||
| case "Secret": | ||
| source.Secret = &v1.SecretVolumeSource{ | ||
| SecretName: certs.Name, | ||
| } | ||
| default: | ||
| return fmt.Errorf("Unknown CustomCerts type: %s", certs.Type) | ||
| } | ||
|
|
||
| name := customCertsNamePrefix + certs.Name | ||
| if name == customCertsNamePrefix { | ||
| return fmt.Errorf("CustomCerts name for %s is required", certs.Type) | ||
| } | ||
| deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, v1.Volume{ | ||
| Name: name, | ||
| VolumeSource: source, | ||
| }) | ||
|
|
||
| containers := deployment.Spec.Template.Spec.Containers | ||
| containers[0].VolumeMounts = append(containers[0].VolumeMounts, v1.VolumeMount{ | ||
| Name: name, | ||
| MountPath: customCertsMountPath, | ||
| }) | ||
| containers[0].Env = append(containers[0].Env, v1.EnvVar{ | ||
| Name: customCertsEnvName, | ||
| Value: customCertsMountPath, | ||
| }) | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| Copyright 2019 The Knative 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 common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| servingv1alpha1 "knative.dev/serving-operator/pkg/apis/serving/v1alpha1" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| v1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| ) | ||
|
|
||
| type customCertsTest struct { | ||
| name string | ||
| input servingv1alpha1.CustomCerts | ||
| expectError bool | ||
| expectSource *v1.VolumeSource | ||
| } | ||
|
|
||
| var customCertsTests = []customCertsTest{ | ||
| { | ||
| name: "FromSecret", | ||
| input: servingv1alpha1.CustomCerts{ | ||
| Type: "Secret", | ||
| Name: "my-secret", | ||
| }, | ||
| expectError: false, | ||
| expectSource: &v1.VolumeSource{ | ||
| Secret: &v1.SecretVolumeSource{ | ||
| SecretName: "my-secret", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "FromConfigMap", | ||
| input: servingv1alpha1.CustomCerts{ | ||
| Type: "ConfigMap", | ||
| Name: "my-map", | ||
| }, | ||
| expectError: false, | ||
| expectSource: &v1.VolumeSource{ | ||
| ConfigMap: &v1.ConfigMapVolumeSource{ | ||
| LocalObjectReference: v1.LocalObjectReference{ | ||
| Name: "my-map", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "NoCerts", | ||
| input: servingv1alpha1.CustomCerts{}, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "InvalidType", | ||
| input: servingv1alpha1.CustomCerts{ | ||
| Type: "invalid", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "MissingName", | ||
| input: servingv1alpha1.CustomCerts{ | ||
| Type: "Secret", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| func TestOnlyTransformCustomCertsForController(t *testing.T) { | ||
| before := makeDeployment(t, "not-controller", v1.PodSpec{ | ||
| Containers: []v1.Container{{ | ||
| Name: "definitely-not-controller", | ||
| }}, | ||
| }) | ||
| instance := &servingv1alpha1.KnativeServing{ | ||
| Spec: servingv1alpha1.KnativeServingSpec{ | ||
| ControllerCustomCerts: servingv1alpha1.CustomCerts{ | ||
| Type: "Secret", | ||
| Name: "my-secret", | ||
| }, | ||
| }, | ||
| } | ||
| customCertsTransform := CustomCertsTransform(instance, log) | ||
| unstructured := makeUnstructured(t, before) | ||
| err := customCertsTransform(&unstructured) | ||
| assertEqual(t, err, nil) | ||
| after := &appsv1.Deployment{} | ||
| err = scheme.Scheme.Convert(&unstructured, after, nil) | ||
| assertEqual(t, err, nil) | ||
| assertDeepEqual(t, after.Spec, before.Spec) | ||
| } | ||
|
|
||
| func TestCustomCertsTransform(t *testing.T) { | ||
| for _, tt := range customCertsTests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| runCustomCertsTransformTest(t, &tt) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func runCustomCertsTransformTest(t *testing.T, tt *customCertsTest) { | ||
| unstructured := makeUnstructured(t, makeDeployment(t, "controller", v1.PodSpec{ | ||
| Containers: []v1.Container{{ | ||
| Name: "controller", | ||
| }}, | ||
| })) | ||
| instance := &servingv1alpha1.KnativeServing{ | ||
| Spec: servingv1alpha1.KnativeServingSpec{ | ||
| ControllerCustomCerts: tt.input, | ||
| }, | ||
| } | ||
| customCertsTransform := CustomCertsTransform(instance, log) | ||
| err := customCertsTransform(&unstructured) | ||
| if tt.expectError && err == nil { | ||
| t.Fatal("Transformer should've returned an error and did not") | ||
| } | ||
| validateCustomCertsTransform(t, tt, &unstructured) | ||
| } | ||
|
|
||
| func validateCustomCertsTransform(t *testing.T, tt *customCertsTest, u *unstructured.Unstructured) { | ||
| deployment := &appsv1.Deployment{} | ||
| err := scheme.Scheme.Convert(u, deployment, nil) | ||
| assertEqual(t, err, nil) | ||
| spec := deployment.Spec.Template.Spec | ||
| if tt.expectSource != nil { | ||
| assertEqual(t, spec.Volumes[0].Name, customCertsNamePrefix+tt.input.Name) | ||
| assertDeepEqual(t, &spec.Volumes[0].VolumeSource, tt.expectSource) | ||
| assertDeepEqual(t, spec.Containers[0].Env[0], v1.EnvVar{ | ||
| Name: customCertsEnvName, | ||
| Value: customCertsMountPath, | ||
| }) | ||
| assertDeepEqual(t, spec.Containers[0].VolumeMounts[0], v1.VolumeMount{ | ||
| Name: customCertsNamePrefix + tt.input.Name, | ||
| MountPath: customCertsMountPath, | ||
| }) | ||
| } else { | ||
| assertEqual(t, len(spec.Volumes), 0) | ||
| assertEqual(t, len(spec.Containers[0].Env), 0) | ||
| assertEqual(t, len(spec.Containers[0].VolumeMounts), 0) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.