diff --git a/pkg/controller/configuration/configuration_test.go b/pkg/controller/configuration/configuration_test.go index dd3e8353292d..20bc511f3583 100644 --- a/pkg/controller/configuration/configuration_test.go +++ b/pkg/controller/configuration/configuration_test.go @@ -73,6 +73,10 @@ func getTestConfiguration() *v1alpha1.Configuration { "test-label": "test", "example.com/namespaced-label": "test", }, + Annotations: map[string]string{ + "test-annotation-1": "foo", + "test-annotation-2": "bar", + }, }, Spec: v1alpha1.RevisionSpec{ // corev1.Container has a lot of setting. We try to pass many @@ -220,6 +224,12 @@ func TestCreateConfigurationsCreatesRevision(t *testing.T) { } } + for k, v := range config.Spec.RevisionTemplate.ObjectMeta.Annotations { + if rev.Annotations[k] != v { + t.Errorf("revisionTemplate annotation %s=%s not passed to revision", k, v) + } + } + if len(rev.OwnerReferences) != 1 || config.Name != rev.OwnerReferences[0].Name { t.Errorf("expected owner references to have 1 ref with name %s", config.Name) } diff --git a/pkg/controller/revision/ela_autoscaler.go b/pkg/controller/revision/ela_autoscaler.go index 36053701bf4a..ed4d7adbf05b 100644 --- a/pkg/controller/revision/ela_autoscaler.go +++ b/pkg/controller/revision/ela_autoscaler.go @@ -50,13 +50,16 @@ func MakeElaAutoscalerDeployment(u *v1alpha1.Revision) *v1beta1.Deployment { labels := MakeElaResourceLabels(u) labels[ela.AutoscalerLabelKey] = controller.GetRevisionAutoscalerName(u) + annotations := MakeElaResourceAnnotations(u) + annotations[sidecarIstioInjectAnnotation] = "false" replicas := int32(1) return &v1beta1.Deployment{ ObjectMeta: meta_v1.ObjectMeta{ - Name: controller.GetRevisionAutoscalerName(u), - Namespace: AutoscalerNamespace, - Labels: MakeElaResourceLabels(u), + Name: controller.GetRevisionAutoscalerName(u), + Namespace: AutoscalerNamespace, + Labels: MakeElaResourceLabels(u), + Annotations: MakeElaResourceAnnotations(u), }, Spec: v1beta1.DeploymentSpec{ Replicas: &replicas, @@ -66,10 +69,8 @@ func MakeElaAutoscalerDeployment(u *v1alpha1.Revision) *v1beta1.Deployment { }, Template: corev1.PodTemplateSpec{ ObjectMeta: meta_v1.ObjectMeta{ - Labels: labels, - Annotations: map[string]string{ - "sidecar.istio.io/inject": "false", - }, + Labels: labels, + Annotations: annotations, }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ @@ -115,9 +116,10 @@ func MakeElaAutoscalerDeployment(u *v1alpha1.Revision) *v1beta1.Deployment { func MakeElaAutoscalerService(u *v1alpha1.Revision) *corev1.Service { return &corev1.Service{ ObjectMeta: meta_v1.ObjectMeta{ - Name: controller.GetRevisionAutoscalerName(u), - Namespace: AutoscalerNamespace, - Labels: MakeElaResourceLabels(u), + Name: controller.GetRevisionAutoscalerName(u), + Namespace: AutoscalerNamespace, + Labels: MakeElaResourceLabels(u), + Annotations: MakeElaResourceAnnotations(u), }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ diff --git a/pkg/controller/revision/ela_pod.go b/pkg/controller/revision/ela_pod.go index d2c4a327709e..9308c7e89fad 100644 --- a/pkg/controller/revision/ela_pod.go +++ b/pkg/controller/revision/ela_pod.go @@ -111,11 +111,15 @@ func MakeElaDeployment(u *v1alpha1.Revision, namespace string) *v1beta1.Deployme MaxSurge: &elaPodMaxSurge, } + podTemplateAnnotations := MakeElaResourceAnnotations(u) + podTemplateAnnotations[sidecarIstioInjectAnnotation] = "true" + return &v1beta1.Deployment{ ObjectMeta: meta_v1.ObjectMeta{ - Name: controller.GetRevisionDeploymentName(u), - Namespace: namespace, - Labels: MakeElaResourceLabels(u), + Name: controller.GetRevisionDeploymentName(u), + Namespace: namespace, + Labels: MakeElaResourceLabels(u), + Annotations: MakeElaResourceAnnotations(u), }, Spec: v1beta1.DeploymentSpec{ Replicas: &elaPodReplicaCount, @@ -125,10 +129,8 @@ func MakeElaDeployment(u *v1alpha1.Revision, namespace string) *v1beta1.Deployme }, Template: corev1.PodTemplateSpec{ ObjectMeta: meta_v1.ObjectMeta{ - Labels: MakeElaResourceLabels(u), - Annotations: map[string]string{ - "sidecar.istio.io/inject": "true", - }, + Labels: MakeElaResourceLabels(u), + Annotations: podTemplateAnnotations, }, }, }, diff --git a/pkg/controller/revision/ela_resource.go b/pkg/controller/revision/ela_resource.go index 120c7f031cad..442f7fd9e3bc 100644 --- a/pkg/controller/revision/ela_resource.go +++ b/pkg/controller/revision/ela_resource.go @@ -15,3 +15,11 @@ func MakeElaResourceLabels(u *v1alpha1.Revision) map[string]string { } return labels } + +func MakeElaResourceAnnotations(u *v1alpha1.Revision) map[string]string { + annotations := make(map[string]string, len(u.ObjectMeta.Annotations)+1) + for k, v := range u.ObjectMeta.Annotations { + annotations[k] = v + } + return annotations +} diff --git a/pkg/controller/revision/ela_service.go b/pkg/controller/revision/ela_service.go index 45820c1c7162..67ebe1a1a06c 100644 --- a/pkg/controller/revision/ela_service.go +++ b/pkg/controller/revision/ela_service.go @@ -34,9 +34,10 @@ var servicePort = 80 func MakeRevisionK8sService(u *v1alpha1.Revision, ns string) *corev1.Service { return &corev1.Service{ ObjectMeta: meta_v1.ObjectMeta{ - Name: controller.GetElaK8SServiceNameForRevision(u), - Namespace: ns, - Labels: MakeElaResourceLabels(u), + Name: controller.GetElaK8SServiceNameForRevision(u), + Namespace: ns, + Labels: MakeElaResourceLabels(u), + Annotations: MakeElaResourceAnnotations(u), }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ diff --git a/pkg/controller/revision/revision.go b/pkg/controller/revision/revision.go index f882151f222b..4b5aee1ce01f 100644 --- a/pkg/controller/revision/revision.go +++ b/pkg/controller/revision/revision.go @@ -75,6 +75,8 @@ const ( autoscalerPort = 8080 serviceTimeoutDuration = 5 * time.Minute + + sidecarIstioInjectAnnotation = "sidecar.istio.io/inject" ) var ( diff --git a/pkg/controller/revision/revision_test.go b/pkg/controller/revision/revision_test.go index eb86c830a428..78e94ef94d4c 100644 --- a/pkg/controller/revision/revision_test.go +++ b/pkg/controller/revision/revision_test.go @@ -60,6 +60,9 @@ func getTestRevision() *v1alpha1.Revision { "testLabel2": "bar", ela.RouteLabelKey: "test-route", }, + Annotations: map[string]string{ + "testAnnotation": "test", + }, }, Spec: v1alpha1.RevisionSpec{ // corev1.Container has a lot of setting. We try to pass many @@ -146,6 +149,17 @@ func getTestNotReadyEndpoints(revName string) *corev1.Endpoints { } } +func sumMaps(a map[string]string, b map[string]string) map[string]string { + summedMap := make(map[string]string, len(a)+len(b)+2) + for k, v := range a { + summedMap[k] = v + } + for k, v := range b { + summedMap[k] = v + } + return summedMap +} + func newTestController(t *testing.T, elaObjects ...runtime.Object) ( kubeClient *fakekubeclientset.Clientset, elaClient *fakeclientset.Clientset, @@ -263,12 +277,16 @@ func TestCreateRevCreatesStuff(t *testing.T) { t.Error("Missing queue-proxy container") } - expectedLabels := map[string]string{ - "testLabel1": "foo", - "testLabel2": "bar", - ela.RouteLabelKey: "test-route", - ela.RevisionLabelKey: rev.Name, - } + expectedLabels := sumMaps( + rev.Labels, + map[string]string{ela.RevisionLabelKey: rev.Name}, + ) + expectedAnnotations := rev.Annotations + expectedPodSpecAnnotations := sumMaps( + rev.Annotations, + map[string]string{"sidecar.istio.io/inject": "true"}, + ) + if labels := deployment.ObjectMeta.Labels; !reflect.DeepEqual(labels, expectedLabels) { t.Errorf("Labels not set correctly on deployment: expected %v got %v.", expectedLabels, labels) @@ -277,6 +295,14 @@ func TestCreateRevCreatesStuff(t *testing.T) { t.Errorf("Label not set correctly in pod template: expected %v got %v.", expectedLabels, labels) } + if annotations := deployment.ObjectMeta.Annotations; !reflect.DeepEqual(annotations, expectedAnnotations) { + t.Errorf("Annotations not set correctly on deployment: expected %v got %v.", + expectedAnnotations, annotations) + } + if annotations := deployment.Spec.Template.ObjectMeta.Annotations; !reflect.DeepEqual(annotations, expectedPodSpecAnnotations) { + t.Errorf("Annotations not set correctly in pod template: expected %v got %v.", + expectedPodSpecAnnotations, annotations) + } // Look for the revision service. expectedServiceName := fmt.Sprintf("%s-service", rev.Name) @@ -300,16 +326,21 @@ func TestCreateRevCreatesStuff(t *testing.T) { t.Errorf("Label not set correctly for revision service: expected %v got %v.", expectedLabels, labels) } + if annotations := service.ObjectMeta.Annotations; !reflect.DeepEqual(annotations, expectedAnnotations) { + t.Errorf("Annotations not set correctly for revision service: expected %v got %v.", + expectedAnnotations, annotations) + } // Look for the autoscaler deployment. expectedAutoscalerName := fmt.Sprintf("%s-autoscaler", rev.Name) - expectedAutoscalerLabels := map[string]string{ - "testLabel1": "foo", - "testLabel2": "bar", - ela.RouteLabelKey: "test-route", - ela.RevisionLabelKey: rev.Name, - ela.AutoscalerLabelKey: expectedAutoscalerName, - } + expectedAutoscalerLabels := sumMaps( + expectedLabels, + map[string]string{ela.AutoscalerLabelKey: expectedAutoscalerName}, + ) + expectedAutoscalerPodSpecAnnotations := sumMaps( + rev.Annotations, + map[string]string{"sidecar.istio.io/inject": "false"}, + ) asDeployment, err := kubeClient.ExtensionsV1beta1().Deployments(AutoscalerNamespace).Get(expectedAutoscalerName, metav1.GetOptions{}) if err != nil { @@ -319,6 +350,10 @@ func TestCreateRevCreatesStuff(t *testing.T) { t.Errorf("Label not set correctly in autoscaler pod template: expected %v got %v.", expectedAutoscalerLabels, labels) } + if annotations := asDeployment.Spec.Template.ObjectMeta.Annotations; !reflect.DeepEqual(annotations, expectedAutoscalerPodSpecAnnotations) { + t.Errorf("Annotations not set correctly in autoscaler pod template: expected %v got %v.", + expectedAutoscalerPodSpecAnnotations, annotations) + } // Check the autoscaler deployment environment variables foundAutoscaler := false for _, container := range asDeployment.Spec.Template.Spec.Containers { @@ -346,6 +381,10 @@ func TestCreateRevCreatesStuff(t *testing.T) { t.Errorf("Label not set correctly autoscaler service: expected %v got %v.", expectedLabels, labels) } + if annotations := asService.ObjectMeta.Annotations; !reflect.DeepEqual(annotations, expectedAnnotations) { + t.Errorf("Annotations not set correctly autoscaler service: expected %v got %v.", + expectedAnnotations, annotations) + } rev, err = elaClient.ElafrosV1alpha1().Revisions(testNamespace).Get(rev.Name, metav1.GetOptions{}) if err != nil {