From f7a4bfd7087b94572e7a248986edd682aa77ab7d Mon Sep 17 00:00:00 2001 From: Luke Kysow <1034429+lkysow@users.noreply.github.com> Date: Mon, 8 Nov 2021 09:36:09 -0800 Subject: [PATCH] Include pod name as tag for connect svcs This will allow Consul DNS tag lookups, e.g. `pod-name.service.dc1.consul`. --- CHANGELOG.md | 1 + .../connect-inject/endpoints_controller.go | 12 ++++------ .../endpoints_controller_ent_test.go | 8 +++---- .../endpoints_controller_test.go | 24 +++++++++---------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f86399f0b..001897fe7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ IMPROVEMENTS: * Control Plane * TLS: Support PKCS1 and PKCS8 private keys for Consul certificate authority. [[GH-843](https://github.com/hashicorp/consul-k8s/pull/843)] + * Connect: Add pod name as tag to service instances and proxies. [[GH-848](https://github.com/hashicorp/consul-k8s/pull/848)] * CLI * Delete jobs, cluster roles, and cluster role bindings on `uninstall`. [[GH-820](https://github.com/hashicorp/consul-k8s/pull/820)] diff --git a/control-plane/connect-inject/endpoints_controller.go b/control-plane/connect-inject/endpoints_controller.go index 4e6a2b73ba..5e4fc3539b 100644 --- a/control-plane/connect-inject/endpoints_controller.go +++ b/control-plane/connect-inject/endpoints_controller.go @@ -416,9 +416,9 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service } } - var tags []string + tags := []string{pod.Name} if raw, ok := pod.Annotations[annotationTags]; ok && raw != "" { - tags = strings.Split(raw, ",") + tags = append(tags, strings.Split(raw, ",")...) } // Get the tags from the deprecated tags annotation and combine. if raw, ok := pod.Annotations[annotationConnectTags]; ok && raw != "" { @@ -432,9 +432,7 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service Address: pod.Status.PodIP, Meta: meta, Namespace: r.consulNamespace(pod.Namespace), - } - if len(tags) > 0 { - service.Tags = tags + Tags: tags, } proxyServiceName := getProxyServiceName(pod, serviceEndpoints) @@ -495,9 +493,7 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service AliasService: serviceID, }, }, - } - if len(tags) > 0 { - proxyService.Tags = tags + Tags: tags, } // A user can enable/disable tproxy for an entire namespace. diff --git a/control-plane/connect-inject/endpoints_controller_ent_test.go b/control-plane/connect-inject/endpoints_controller_ent_test.go index 4b75426346..611bfa4be5 100644 --- a/control-plane/connect-inject/endpoints_controller_ent_test.go +++ b/control-plane/connect-inject/endpoints_controller_ent_test.go @@ -130,7 +130,7 @@ func TestReconcileCreateEndpointWithNamespaces(t *testing.T) { ServiceName: "service-created", ServiceAddress: "1.2.3.4", ServiceMeta: map[string]string{MetaKeyPodName: "pod1", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: test.SourceKubeNS, MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod1"}, Namespace: test.ExpConsulNS, }, { @@ -138,7 +138,7 @@ func TestReconcileCreateEndpointWithNamespaces(t *testing.T) { ServiceName: "service-created", ServiceAddress: "2.2.3.4", ServiceMeta: map[string]string{MetaKeyPodName: "pod2", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: test.SourceKubeNS, MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod2"}, Namespace: test.ExpConsulNS, }, }, @@ -153,7 +153,7 @@ func TestReconcileCreateEndpointWithNamespaces(t *testing.T) { DestinationServiceID: "pod1-service-created", }, ServiceMeta: map[string]string{MetaKeyPodName: "pod1", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: test.SourceKubeNS, MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod1"}, Namespace: test.ExpConsulNS, }, { @@ -166,7 +166,7 @@ func TestReconcileCreateEndpointWithNamespaces(t *testing.T) { DestinationServiceID: "pod2-service-created", }, ServiceMeta: map[string]string{MetaKeyPodName: "pod2", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: test.SourceKubeNS, MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod2"}, Namespace: test.ExpConsulNS, }, }, diff --git a/control-plane/connect-inject/endpoints_controller_test.go b/control-plane/connect-inject/endpoints_controller_test.go index 5f1a0dadd8..7fa340358e 100644 --- a/control-plane/connect-inject/endpoints_controller_test.go +++ b/control-plane/connect-inject/endpoints_controller_test.go @@ -613,7 +613,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { ServiceAddress: "1.2.3.4", ServicePort: 0, ServiceMeta: map[string]string{MetaKeyPodName: "pod1", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod1"}, }, }, expectedProxySvcInstances: []*api.CatalogService{ @@ -629,7 +629,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { LocalServicePort: 0, }, ServiceMeta: map[string]string{MetaKeyPodName: "pod1", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod1"}, }, }, expectedAgentHealthChecks: []*api.AgentCheck{ @@ -691,7 +691,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { ServiceAddress: "1.2.3.4", ServicePort: 0, ServiceMeta: map[string]string{MetaKeyPodName: "pod1", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod1"}, }, { ServiceID: "pod2-service-created", @@ -699,7 +699,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { ServiceAddress: "2.2.3.4", ServicePort: 0, ServiceMeta: map[string]string{MetaKeyPodName: "pod2", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod2"}, }, }, expectedProxySvcInstances: []*api.CatalogService{ @@ -715,7 +715,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { LocalServicePort: 0, }, ServiceMeta: map[string]string{MetaKeyPodName: "pod1", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod1"}, }, { ServiceID: "pod2-service-created-sidecar-proxy", @@ -729,7 +729,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { LocalServicePort: 0, }, ServiceMeta: map[string]string{MetaKeyPodName: "pod2", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod2"}, }, }, expectedAgentHealthChecks: []*api.AgentCheck{ @@ -814,7 +814,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { ServiceAddress: "1.2.3.4", ServicePort: 0, ServiceMeta: map[string]string{MetaKeyPodName: "pod1", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod1"}, }, { ServiceID: "pod2-service-created", @@ -822,7 +822,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { ServiceAddress: "2.2.3.4", ServicePort: 0, ServiceMeta: map[string]string{MetaKeyPodName: "pod2", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod2"}, }, }, expectedProxySvcInstances: []*api.CatalogService{ @@ -838,7 +838,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { LocalServicePort: 0, }, ServiceMeta: map[string]string{MetaKeyPodName: "pod1", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod1"}, }, { ServiceID: "pod2-service-created-sidecar-proxy", @@ -852,7 +852,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { LocalServicePort: 0, }, ServiceMeta: map[string]string{MetaKeyPodName: "pod2", MetaKeyKubeServiceName: "service-created", MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue}, - ServiceTags: []string{}, + ServiceTags: []string{"pod2"}, }, }, expectedAgentHealthChecks: []*api.AgentCheck{ @@ -930,7 +930,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue, }, - ServiceTags: []string{"abc", "123", "def", "456"}, + ServiceTags: []string{"pod1", "abc", "123", "def", "456"}, }, }, expectedProxySvcInstances: []*api.CatalogService{ @@ -963,7 +963,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue, }, - ServiceTags: []string{"abc", "123", "def", "456"}, + ServiceTags: []string{"pod1", "abc", "123", "def", "456"}, }, }, expectedAgentHealthChecks: []*api.AgentCheck{