Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the value $POD_NAME for the annotation #982

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ IMPROVEMENTS:
* Allow customization of `terminationGracePeriodSeconds` on the ingress gateways. [[GH-947](https://github.com/hashicorp/consul-k8s/pull/947)]
* Support `ui.dashboardURLTemplates.service` value for setting [dashboard URL templates](https://www.consul.io/docs/agent/options#ui_config_dashboard_url_templates_service). [[GH-937](https://github.com/hashicorp/consul-k8s/pull/937)]
* Allow using dash-separated names for config entries when using `kubectl`. [[GH-965](https://github.com/hashicorp/consul-k8s/pull/965)]
* Control Plane
* Support the value `$POD_NAME` for the annotation `consul.hashicorp.com/service-meta-pod_name` that will now be interpolated and set to the pod name in the service's metadata. [[GH-982](https://github.com/hashicorp/consul-k8s/pull/982)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thisisnotashwin it's supported for any meta annotation right? Not just service-meta-pod_name but also service-meta-foo for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really. I asked @david-yu and the ask from the customer is to only support $POD_NAME.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thisisnotashwin Similar question that @lkysow asked. Should it support any meta annotation? I'm looking to also grab the pod-namespace. I can make a similar PR but then we get into different territory of adding every single metadata that we might need and doesn't seem sustainable

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or would it make sense to add another value for $POD_NAMESPACE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jeenx !! Currently we have only added support for the value $POD_NAME. I agree that adding a separate PR per env var is not sustainable, but in this case, these env vars are not actually set in the runtime env. We are actually replacing it with the value pod.Name(). Hence we would need to this do on a per env basis as it is hard to determine whether the desired variable does exist in the env or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also important to note that this isn't actually an env var. We're doing this in the endpoints controller that doesn't have access to the actual pod's environment variables. So we can only support data we actually have when looking at the pod yaml.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, would it be possible to add all that data that is available on the pod (e.g, namespace)?


BUG FIXES:
* Helm
Expand Down
6 changes: 5 additions & 1 deletion control-plane/connect-inject/endpoints_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,11 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service
}
for k, v := range pod.Annotations {
if strings.HasPrefix(k, annotationMeta) && strings.TrimPrefix(k, annotationMeta) != "" {
meta[strings.TrimPrefix(k, annotationMeta)] = v
if v == "$POD_NAME" {
meta[strings.TrimPrefix(k, annotationMeta)] = pod.Name
} else {
meta[strings.TrimPrefix(k, annotationMeta)] = v
}
}
}
tags := consulTags(pod)
Expand Down
3 changes: 3 additions & 0 deletions control-plane/connect-inject/endpoints_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ 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[fmt.Sprintf("%spod_name", annotationMeta)] = "$POD_NAME"
pod1.Annotations[annotationTags] = "abc,123,$POD_NAME"
thisisnotashwin marked this conversation as resolved.
Show resolved Hide resolved
pod1.Annotations[annotationConnectTags] = "def,456,$POD_NAME"
pod1.Annotations[annotationUpstreams] = "upstream1:1234"
Expand Down Expand Up @@ -925,6 +926,7 @@ func TestReconcileCreateEndpoint(t *testing.T) {
ServiceMeta: map[string]string{
"name": "abc",
"version": "2",
"pod_name": "pod1",
MetaKeyPodName: "pod1",
MetaKeyKubeServiceName: "service-created",
MetaKeyKubeNS: "default",
Expand Down Expand Up @@ -958,6 +960,7 @@ func TestReconcileCreateEndpoint(t *testing.T) {
ServiceMeta: map[string]string{
"name": "abc",
"version": "2",
"pod_name": "pod1",
MetaKeyPodName: "pod1",
MetaKeyKubeServiceName: "service-created",
MetaKeyKubeNS: "default",
Expand Down