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

Add automountServiceAccountToken option to alertmanager spec #5474

Conversation

stgrace
Copy link
Contributor

@stgrace stgrace commented Mar 31, 2023

Description

Add automountServiceAccountToken option to alertmanager spec. As a best practice it's best to disable mounting a service account token for pods that do not need to interact with the Kubernetes API spec, hence this pull request.

Type of change

  • CHANGE (fix or feature that would cause existing functionality to not work as expected)
  • FEATURE (non-breaking change which adds functionality)
  • BUGFIX (non-breaking change which fixes an issue)
  • ENHANCEMENT (non-breaking change which improves existing functionality)
  • NONE (if none of the other choices apply. Example, tooling, build system, CI, docs, etc.)

Changelog entry

Add automountServiceAccountToken option to alertmanager spec

Signed-off-by: stgrace <stefgraces@hotmail.com>
@stgrace stgrace requested a review from a team as a code owner March 31, 2023 15:09
@ArthurSens
Copy link
Member

I'd rather have it disabled by default and not have the option to turn it back on 🤔. Do you see a use-case where alertmanager would need the serviceaccount mounted?

@simonpasquier
Copy link
Contributor

IIUC the PR doesn't change the current behavior: the SA token will not be automatically mounted until the user sets the field to true.

Do you see a use-case where alertmanager would need the serviceaccount mounted?

One example is when using kube-rbac-proxy in front of Alertmanager. One could probably use SA token volume projection but it's more involved that just setting automountServiceAccountToken.

We might want to have a similar change for the ThanosRuler CRD but it doesn't have to be in this PR.

@stgrace
Copy link
Contributor Author

stgrace commented Apr 4, 2023

Hi @simonpasquier

IIUC the PR doesn't change the current behavior: the SA token will not be automatically mounted until the user sets the field to true.

Not sure what you mean? By default, all pods mount their serviceaccounttoken in kubernetes. Adding this option to alertmanager and setting it to false allows users to opt out of this default behavior.

@stgrace
Copy link
Contributor Author

stgrace commented Apr 4, 2023

I'd rather have it disabled by default and not have the option to turn it back on 🤔. Do you see a use-case where alertmanager would need the serviceaccount mounted?

I'd also think that disabling it and not having the option to turn it back on is a good idea if there is no reason for this component to have access to the kubernetes API

@simonpasquier
Copy link
Contributor

By default, all pods mount their serviceaccounttoken in kubernetes.

Yes sorry I wasn't clear enough: I meant to say that for people that have already opted out for API credential automounting at the SA level (https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#opt-out-of-api-credential-automounting), it won't change anything. Similarly for people that rely on automounting at the SA level, the token will still be mounted unless they explicitly set spec.automountServiceAccountToken: false

As a follow-up, this snippet achieves the same goal:

apiVersion: monitoring.rhobs/v1
kind: Alertmanager
metadata:
  name: example
spec:
   ...
  volumeMounts:
  - mountPath: /var/run/secrets/tokens
    name: sa-token
  volumes:
  - name: sa-token
    projected:
      sources:
      - serviceAccountToken:
          path: sa-token

@simonpasquier
Copy link
Contributor

if there is no reason for this component to have access to the kubernetes API

this isn't a valid assumption (see my comment earlier about kube-rbac-proxy).

@stgrace
Copy link
Contributor Author

stgrace commented Apr 4, 2023

Yes sorry I wasn't clear enough: I meant to say that for people that have already opted out for API credential automounting at the SA level (https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#opt-out-of-api-credential-automounting), it won't change anything. Similarly for people that rely on automounting at the SA level, the token will still be mounted unless they explicitly set spec.automountServiceAccountToken: false

I understand that if you disable it at the SA level it doesn't change anything, but I still think it's a good idea to have it as an option to also disable it at the pod level.

@simonpasquier
Copy link
Contributor

I understand that if you disable it at the SA level it doesn't change anything, but I still think it's a good idea to have it as an option to also disable it at the pod level.

Yes I agree. But to be clear, I don't think that we should default to automountServiceAccountToken: false.

@stgrace
Copy link
Contributor Author

stgrace commented Apr 11, 2023

@simonpasquier Is there anything that needs to be changed for this PR then?

@vimalk78
Copy link

vimalk78 commented May 8, 2023

@stgrace any updates planned for this PR?

@stgrace
Copy link
Contributor Author

stgrace commented May 8, 2023

@stgrace any updates planned for this PR?

What updates are exactly required? For me this is ready to be merged?

Copy link
Member

@ArthurSens ArthurSens left a comment

Choose a reason for hiding this comment

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

Sorry for taking this long to review, just have some small nits.

Comment on lines 238 to 240
// If you don't want the kubelet to automatically mount a ServiceAccount's API credentials, you can opt out of the default behavior.
// You can opt out of automounting API credentials on /var/run/secrets/kubernetes.io/serviceaccount/token for a pod by setting automountServiceAccountToken: false on the pod spec.
AutomountServiceAccountToken *bool `json:"automountServiceAccountToken,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// If you don't want the kubelet to automatically mount a ServiceAccount's API credentials, you can opt out of the default behavior.
// You can opt out of automounting API credentials on /var/run/secrets/kubernetes.io/serviceaccount/token for a pod by setting automountServiceAccountToken: false on the pod spec.
AutomountServiceAccountToken *bool `json:"automountServiceAccountToken,omitempty"`
// AutomountServiceAccountToken defines if the ServiceAccount token is mounted into Alertmanager's pod or not.
// +optional
AutomountServiceAccountToken *bool `json:"automountServiceAccountToken,omitempty"`

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that the original comment is useful because otherwise it might be unclear when this field is useful.

Suggested change
// If you don't want the kubelet to automatically mount a ServiceAccount's API credentials, you can opt out of the default behavior.
// You can opt out of automounting API credentials on /var/run/secrets/kubernetes.io/serviceaccount/token for a pod by setting automountServiceAccountToken: false on the pod spec.
AutomountServiceAccountToken *bool `json:"automountServiceAccountToken,omitempty"`
// AutomountServiceAccountToken indicates whether a service account token should be automatically mounted in the pod.
// If the service account has `automountServiceAccountToken: true`, set the field to `false` to opt out of automounting API credentials.
AutomountServiceAccountToken *bool `json:"automountServiceAccountToken,omitempty"`

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me, let's not forget about the extra comment // +optional 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the late response,
Added the latest comment from @simonpasquier, with the // +optional

@vimalk78
Copy link

vimalk78 commented Jun 1, 2023

hi @stgrace , can you please incorporate the comment.
If you don't have time, i am happy to open another PR which includes your commit.

Signed-off-by: stgrace <stefgraces@hotmail.com>
Signed-off-by: stgrace <stefgraces@hotmail.com>
Signed-off-by: stgrace <stefgraces@hotmail.com>
@simonpasquier simonpasquier merged commit ab29c1b into prometheus-operator:main Jun 5, 2023
16 checks passed
@simonpasquier
Copy link
Contributor

Thanks!

mcbenjemaa pushed a commit to mcbenjemaa/prometheus-operator that referenced this pull request Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants