Skip to content

Commit

Permalink
Include pod name as tag for connect svcs
Browse files Browse the repository at this point in the history
This will allow Consul DNS tag lookups, e.g.
`pod-name.service.dc1.consul`.
  • Loading branch information
lkysow committed Nov 8, 2021
1 parent 26b49ed commit f7a4bfd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down
12 changes: 4 additions & 8 deletions control-plane/connect-inject/endpoints_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions control-plane/connect-inject/endpoints_controller_ent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ 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,
},
{
ServiceID: "pod2-service-created",
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,
},
},
Expand All @@ -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,
},
{
Expand All @@ -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,
},
},
Expand Down
24 changes: 12 additions & 12 deletions control-plane/connect-inject/endpoints_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -691,15 +691,15 @@ 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",
ServiceName: "service-created",
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{
Expand All @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -814,15 +814,15 @@ 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",
ServiceName: "service-created",
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{
Expand All @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit f7a4bfd

Please sign in to comment.