diff --git a/CHANGELOG.md b/CHANGELOG.md index bbade74d26..4a287c4bf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ FEATURES: IMPROVEMENTS: * Control Plane - * Bump `consul-k8s-control-plane` UBI images for OpenShift to use base image `ubi-minimal:8.5`: [[GH-922](https://github.com/hashicorp/consul-k8s/pull/922)] + * Bump `consul-k8s-control-plane` UBI images for OpenShift to use base image `ubi-minimal:8.5`. [[GH-922](https://github.com/hashicorp/consul-k8s/pull/922)] + * Support the value `$POD_NAME` for the annotation `consul.hashicorp.com/service-tags` that will now be interpolated and set to the pod name. [[GH-931](https://github.com/hashicorp/consul-k8s/pull/931)] ## 0.38.0 (December 08, 2021) diff --git a/control-plane/connect-inject/endpoints_controller.go b/control-plane/connect-inject/endpoints_controller.go index 78b1748d4b..1712967308 100644 --- a/control-plane/connect-inject/endpoints_controller.go +++ b/control-plane/connect-inject/endpoints_controller.go @@ -416,15 +416,7 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service meta[strings.TrimPrefix(k, annotationMeta)] = v } } - - var tags []string - if raw, ok := pod.Annotations[annotationTags]; ok && raw != "" { - tags = strings.Split(raw, ",") - } - // Get the tags from the deprecated tags annotation and combine. - if raw, ok := pod.Annotations[annotationConnectTags]; ok && raw != "" { - tags = append(tags, strings.Split(raw, ",")...) - } + tags := consulTags(pod) service := &api.AgentServiceRegistration{ ID: serviceID, @@ -433,9 +425,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) @@ -496,9 +486,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. @@ -1034,3 +1022,29 @@ func isLabeledIgnore(labels map[string]string) bool { return shouldIgnore && labelExists && err == nil } + +// consulTags returns tags that should be added to the Consul service and proxy registrations. +func consulTags(pod corev1.Pod) []string { + var tags []string + if raw, ok := pod.Annotations[annotationTags]; ok && raw != "" { + tags = strings.Split(raw, ",") + } + // Get the tags from the deprecated tags annotation and combine. + if raw, ok := pod.Annotations[annotationConnectTags]; ok && raw != "" { + tags = append(tags, strings.Split(raw, ",")...) + } + + var interpolatedTags []string + for _, t := range tags { + // Support light interpolation to preserve backwards compatibility where tags could + // be environment variables. + // Right now the only string we interpolate is $POD_NAME since that's all + // users have asked for as of now. More can be added here in the future. + if t == "$POD_NAME" { + t = pod.Name + } + interpolatedTags = append(interpolatedTags, t) + } + + return interpolatedTags +} diff --git a/control-plane/connect-inject/endpoints_controller_test.go b/control-plane/connect-inject/endpoints_controller_test.go index de021ec198..846c1c434e 100644 --- a/control-plane/connect-inject/endpoints_controller_test.go +++ b/control-plane/connect-inject/endpoints_controller_test.go @@ -886,8 +886,8 @@ func TestReconcileCreateEndpoint(t *testing.T) { pod1.Annotations[annotationService] = "different-consul-svc-name" pod1.Annotations[fmt.Sprintf("%sname", annotationMeta)] = "abc" pod1.Annotations[fmt.Sprintf("%sversion", annotationMeta)] = "2" - pod1.Annotations[annotationTags] = "abc,123" - pod1.Annotations[annotationConnectTags] = "def,456" + pod1.Annotations[annotationTags] = "abc,123,$POD_NAME" + pod1.Annotations[annotationConnectTags] = "def,456,$POD_NAME" pod1.Annotations[annotationUpstreams] = "upstream1:1234" pod1.Annotations[annotationEnableMetrics] = "true" pod1.Annotations[annotationPrometheusScrapePort] = "12345" @@ -930,7 +930,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue, }, - ServiceTags: []string{"abc", "123", "def", "456"}, + ServiceTags: []string{"abc", "123", "pod1", "def", "456", "pod1"}, }, }, expectedProxySvcInstances: []*api.CatalogService{ @@ -963,7 +963,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { MetaKeyKubeNS: "default", MetaKeyManagedBy: managedByValue, }, - ServiceTags: []string{"abc", "123", "def", "456"}, + ServiceTags: []string{"abc", "123", "pod1", "def", "456", "pod1"}, }, }, expectedAgentHealthChecks: []*api.AgentCheck{