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 for kube-api-access volume mount #348

Merged
merged 5 commits into from
Aug 30, 2023

Conversation

wondywang
Copy link
Contributor

What this PR does / why we need it:
This PR continue to improve the feature gated support for VirtualCluster to work with any Kubernetes version 1.21+ and when the RootCACertConfigMap feature is enabled in 1.20 clusters. To achieve this, before creating the pPod, the content in the kube-api-access volume is changed. Change it to directly mount the tenant cluster secret instead of applying the token of the super cluster.

Before:

  volumes:
  - name: kube-api-access-sjz6l
    projected:
      defaultMode: 420
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
            path: namespace

After:

  volumes:
  - name: kube-api-access-f7ww9
    projected:
      defaultMode: 420
      sources:
      - secret:
          name: default-token-v2wlt

Here is the reference to the implementation in the Kubernetes: https://github.com/kubernetes/kubernetes/blob/master/plugin/pkg/admission/serviceaccount/admission.go

Follow-up needs to be considered: After Kubernetes version 1.24+, KCM will no longer distribute ServiceAccount tokens. I plan to add a token manager in the ServiceAccount sync controller to create ServiceAccount tokens and keep them in the validity period.

Which issue(s) this PR fixes:

#282

/kind feature

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 3, 2023
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 3, 2023
@christopherhein
Copy link
Member

This is looking great so far, this doesn’t do the TokenManagement aspects but I see your note that this can come later.

@Fei-Guo
Copy link

Fei-Guo commented Aug 4, 2023

@wondywang.

  • Please fix lint/test failures.
  • Please add UTs for the change.
  • Please add a feature gate to guard your change entirely.

@wondywang
Copy link
Contributor Author

@wondywang.

  • Please fix lint/test failures.
  • Please add UTs for the change.
  • Please add a feature gate to guard your change entirely.

Ok, I will do it. If adding a new gate, I will prefer separate out a new file. I actually thought about doing that.

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Aug 8, 2023
@wondywang
Copy link
Contributor Author

@Fei-Guo @christopherhein

PTAL, thanks.

Copy link

@Fei-Guo Fei-Guo left a comment

Choose a reason for hiding this comment

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

Overall LG. Some nits.

}
for _, secret := range secrets.Items {
if secret.Type != corev1.SecretTypeOpaque {
klog.Infof("type")
Copy link

Choose a reason for hiding this comment

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

Do you need the log?

for i, container := range pod.Spec.Containers {
for j := 0; j < len(container.VolumeMounts); j++ {
if container.VolumeMounts[j].Name == old {
klog.V(6).Infof("mutate containers %s volumeMount.name from %s to %s", container.Name, old, new)
Copy link

Choose a reason for hiding this comment

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

V(6) -> V(4).

@Fei-Guo
Copy link

Fei-Guo commented Aug 10, 2023

LGTM. @christopherhein, take another look?

Copy link

@ravisantoshgudimetla ravisantoshgudimetla left a comment

Choose a reason for hiding this comment

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

Mostly LGTM. Thanks for working on this @wondywang

}

// Could not find in cache, attempt to look up directly
numAttempts := 1
Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thx @ravisantoshgudimetla. I suddenly found that the retry logic here is unnecessary. So it was removed directly.

return fmt.Errorf("error looking up secret of serviceAccount %s/%s: %v", targetNamespace, p.PPod.Spec.ServiceAccountName, err)
}

if shouldAutomount(serviceAccount, p.PPod) {

Choose a reason for hiding this comment

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

Can we use automountServiceAccountToken on the pod spec?

Copy link
Member

Choose a reason for hiding this comment

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

We explicitly override this from vc-syncer so the value in the pPod should always be false, seems like if we have the vPod object we could get that value.

tests := []struct {
name string
pPod *corev1.Pod
existingObjectInSuper []runtime.Object

Choose a reason for hiding this comment

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

It'd be easier if you add an expected Pod after mutation as a field in this struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi, @ravisantoshgudimetla. I thought about it. This is not easy to implement, because the names kube-api-access-xxxx here are randomly generated by names.SimpleNameGenerator.GenerateName.

Choose a reason for hiding this comment

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

I see. No issues then. I was actually referring to the contents not the name.

func TestPodKubeAPIAccessMutatorPlugin_Mutator(t *testing.T) {
defer util.SetFeatureGateDuringTest(t, featuregate.DefaultFeatureGate, featuregate.KubeAPIAccessSupport, true)()

tests := []struct {

Choose a reason for hiding this comment

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

Can you add 2 more test cases with auto mount enable and disabled and if possible can we check the content of this secret(token matches what we expect it to be?)

Copy link
Member

Choose a reason for hiding this comment

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

@christopherhein
Copy link
Member

Overall looks great, @Fei-Guo & @ravisantoshgudimetla added the same feedback I would. Thank you @wondywang for taking the lead on this.

@wondywang
Copy link
Contributor Author

Overall looks great, @Fei-Guo & @ravisantoshgudimetla added the same feedback I would. Thank you @wondywang for taking the lead on this.

@christopherhein @ravisantoshgudimetla That's great, thanks! I will make some change later.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 18, 2023
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 19, 2023
@ravisantoshgudimetla
Copy link

@wondywang - go-imports are failing. Can you fix and push the commit. LGTM from code standpoint.

@wondywang
Copy link
Contributor Author

@wondywang - go-imports are failing. Can you fix and push the commit. LGTM from code standpoint.

Sorry, I've been a little busy lately. I will get it done as soon as possible. @ravisantoshgudimetla

@wondywang
Copy link
Contributor Author

wondywang commented Aug 29, 2023

@Fei-Guo @christopherhein PTAL.

And I also update serviceAccount token manager in this commit. This part of the logic is an idea of mine (maybe not complete). Can be used as a reference. I plan not to mention PR for now. Wait for @ravisantoshgudimetla to finish it. What do you think?

wondywang@ad8545a

Copy link
Member

@christopherhein christopherhein left a comment

Choose a reason for hiding this comment

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

/lgtm

@Fei-Guo I’ll give you the chance to approve this.

cc @ravisantoshgudimetla as well, thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 30, 2023
@Fei-Guo
Copy link

Fei-Guo commented Aug 30, 2023

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Fei-Guo, wondywang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 30, 2023
@k8s-ci-robot k8s-ci-robot merged commit 1e31f2d into kubernetes-sigs:main Aug 30, 2023
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants