Skip to content

Conversation

@mochizuki875
Copy link
Member

@mochizuki875 mochizuki875 commented Feb 6, 2024

What type of PR is this?

/kind feature
/cleanup

What this PR does / why we need it:

When using --copy-to option with some debugging profile(legacy, restricted, netadmin sysadmin), the probe configuration remains in the copy pod .
Therefore, if the probe condition is not met by debugging operation on the copy pod, the main container will be restarted.
This may prevent debugging.

So in this PR, I've added flags(--keep-liveness,--keep-readiness,--keep-startup) to control the removal of probes from copy pod.
In addition, I've added --keep-labels, --keep-annotations and --keep-init-containers to control the removal of labels, annotations and initContainers too.

Which issue(s) this PR fixes:

Special notes for your reviewer:

Does this PR introduce a user-facing change?

Add `--keep-*` flags to `kubectl debug`, which enables to control the removal of probes, labels, annotations and initContainers from copy pod.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Feb 6, 2024
@mochizuki875
Copy link
Member Author

/sig cli

@k8s-ci-robot k8s-ci-robot added sig/cli Categorizes an issue or PR as relevant to SIG CLI. area/kubectl and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Feb 6, 2024

switch style {
case podCopy:
removeLabelsAndProbes(pod)
Copy link
Member

Choose a reason for hiding this comment

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

I agree with that but I think we need to manage this with on/off flag as we did in https://github.com/openshift/oc/blob/cf7b09cd29f1c0f56b2c6cd8193092c57c7730ff/pkg/cli/debug/debug.go#L252-L255 and default would be to removal of probes. WDYT? @verb

Copy link
Member Author

Choose a reason for hiding this comment

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

@ardaguclu

Thank you for your comment and I agree that.
oc debug seems to be implemented to be able to control the removal of probes, labels, annotations and initContainers.
On the other hands in kubectl debug, labels will be removed in all profiles and probes will be removed in some profiles(general and baseline).

So I'm wondering which should be targeted for removal control(only probes, probes and labels or all?).
How do you think?

Current implementations of kubectl debug are as below.

probes

removeLabelsAndProbes() which remove probes already has been implemented and used in some profiles(general and baseline).

// removeLabelsAndProbes removes labels from the pod and remove probes
// from all containers of the pod.
func removeLabelsAndProbes(p *corev1.Pod) {
p.Labels = nil
for i := range p.Spec.Containers {
p.Spec.Containers[i].LivenessProbe = nil
p.Spec.Containers[i].ReadinessProbe = nil
p.Spec.Containers[i].StartupProbe = nil
}
}

labels

removeLabelsAndProbes() also implemented to remove labels, but copy pod labels will be removed when copy pod is created before removeLabelsAndProbes() will be called.

// generatePodCopyWithDebugContainer takes a Pod and returns a copy and the debug container name of that copy
func (o *DebugOptions) generatePodCopyWithDebugContainer(pod *corev1.Pod) (*corev1.Pod, string, error) {
copied := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: o.CopyTo,
Namespace: pod.Namespace,
Annotations: pod.Annotations,
},
Spec: *pod.Spec.DeepCopy(),
}

annotations and initContainers

There seems to be not implemented the logic to remove annotations and initContainers.

Copy link
Member Author

Choose a reason for hiding this comment

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

For now, I've added --keep-* flags to be able to control the removal of probes, labels, annotations and initContainers like oc debug.

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 19, 2024
@mochizuki875 mochizuki875 force-pushed the remove_probe_from_copy_pod branch 2 times, most recently from 446744b to d252ebc Compare February 19, 2024 07:43
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 5, 2024
@mochizuki875 mochizuki875 force-pushed the remove_probe_from_copy_pod branch from d252ebc to 0dd901d Compare March 25, 2024 04:17
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 25, 2024
@mochizuki875 mochizuki875 force-pushed the remove_probe_from_copy_pod branch 2 times, most recently from 30858a2 to 4b3544e Compare March 25, 2024 06:31
@mochizuki875
Copy link
Member Author

/remove-kind bug
/kind feature

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. and removed kind/bug Categorizes issue or PR as related to a bug. labels Mar 25, 2024
@mochizuki875
Copy link
Member Author

/retest

@mochizuki875 mochizuki875 changed the title kubectl debug: Remove probe from copy pod kubectl debug: Add --keep-* flag to control the removal of probes, labels, annotations, and initContainers from copy pod Mar 25, 2024
@mochizuki875
Copy link
Member Author

PTAL?

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. label May 10, 2024
@mochizuki875
Copy link
Member Author

@ardaguclu
Thank you for your RVs and I've fixed them.
Please check again.

Copy link
Member

@ardaguclu ardaguclu left a comment

Choose a reason for hiding this comment

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

Thanks for spending time and effort on this PR. That would be great, if we can add some integration tests cases also in here https://github.com/kubernetes/kubernetes/blob/master/test/cmd/debug.sh

useHostNamespaces(pod)

case podCopy:
p.keeps.removeLabels(pod)
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a behavioral change for legacyProfile because we were not removing as default and now we are.
I think, we can leave it as is and even keep flags don't change it. As it is a legacy profile.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean copy pod applied legacyProfile should keep annotations, probes and initContainers and doesn’t keep labels(it is not kept now as mentioned here) regardless of --keep-* flags?

Copy link
Member

Choose a reason for hiding this comment

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

Yes that is true. Because it is a legacy profile anyway and we shouldn't change the current behavior, unless it is fully broken, etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!
So should we add additional flags descriptions like "it does not work in legacyProfile" related that?

Copy link
Member

Choose a reason for hiding this comment

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

Let's don't add it for now. I think, adding more text just for a a legacy profile seems to be unnecessary. Isn't the recommended path is using the other profiles other than legacy?

Copy link
Member

Choose a reason for hiding this comment

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

We can say that if you want to manage these resources, please use the other recommended paths.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's don't add it for now.

Ok, I've got it.

Isn't the recommended path is using the other profiles other than legacy?

By the way, how many users recognize it recommended to use other profiles than legacy profile and the existence of debugging profile?

  • The official document doesn't seem to mention debugging profiles.(It's mentioned only in KEP now as I know.)
  • "kubectl debug --help" doesn't seem to show any recommended paths regarding profiles and show default profile is legacy.

So IMO, I think many users don't know the existence of debugging profile and recommendation.
Shouldn't we add any description about debugging profile somewhere?

In addition, it is said that in KEP:

Default Profile and Automation Selection
In order to provide a seamless experience and encourage use of PodSecurity, the "auto" profile will automatically choose a profile that's compatible with the current security profile by examining the pod-security.kubernetes.io/enforce annotation on the namespace and selecting the most permissive of "general", "baseline", and "restricted" that the controller will allow.

This will become the default behavior, but in order to maintain backwards compatibility the "legacy" profile will be the default profile until the 1.25 release. When --profile is not specified kubectl debug will print a warning about the upcoming change in behavior.

Though auto profile is WIP(#120687) now, shouldn't we print any warning or recommendation about profile as a first step?

These are outside the scope of this PR, but I was a little curious.

Copy link
Member

Choose a reason for hiding this comment

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

I assume that legacy name is self explanatory but I see your comments and it all makes sense. We should reconsider what is the path for legacy profile.

@k8s-ci-robot k8s-ci-robot added area/test sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels May 15, 2024
@mochizuki875
Copy link
Member Author

@ardaguclu
Ok, I've fixed.

Thanks for spending time and effort on this PR. That would be great, if we can add some integration tests cases also in here https://github.com/kubernetes/kubernetes/blob/master/test/cmd/debug.sh

I've added test pod manifest(hack/testdata/pod-with-metadata-and-probes.yaml) for added test cases.
If there is some good idea instead of adding the manifest, please comment that.

@mochizuki875
Copy link
Member Author

/retest

Copy link
Member

@ardaguclu ardaguclu left a comment

Choose a reason for hiding this comment

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

tests look great. Thanks.

@mochizuki875
Copy link
Member Author

@ardaguclu
I've fixed.
In addition, I've renamed KeepFields to keepFlags because previous name seems to be not intuitive.

Copy link
Member

@ardaguclu ardaguclu left a comment

Choose a reason for hiding this comment

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

One last comment and after that I think we are good to go


if o.Applier == nil {
applier, err := NewProfileApplier(o.Profile)
kflags := keepFlags{
Copy link
Member

Choose a reason for hiding this comment

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

Agree that keepFlags is better naming and sorry, if I wasn't clear enough: Could you please export this, it's functions and it's fields?.

Copy link
Member Author

Choose a reason for hiding this comment

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

Could you please export this, it's functions and it's fields?.

It's ok.
Could you tell me the reason?
As you said here, they are not used by anywhere so I thought we need not to export...

Copy link
Member

Choose a reason for hiding this comment

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

Could you please export this, it's functions and it's fields?.

It's ok. Could you tell me the reason? As you said here, they are not used by anywhere so I thought we need not to export...

Basically, in case some other tools depend on kubectl debug functionality, they would be able to customize the behavior. Otherwise, keep flag functionality will be black box. We can also may want to intentionally unexport them. However, they would fully be relying on the flags and it may better to export them in my opinion. Hope that it makes sense.

Copy link
Member

Choose a reason for hiding this comment

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

Another reason would be ProfileApplier is exported function and can be customizable. On the other hand, it only gets an unexported type which drastically hurts the usability of ProfileApplier.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for your explanation.
I've understood.

@mochizuki875 mochizuki875 force-pushed the remove_probe_from_copy_pod branch from 301dc1b to e56ce82 Compare May 16, 2024 17:10
@mochizuki875
Copy link
Member Author

@ardaguclu
I've fixed and rebased.

return fmt.Errorf("the %s profile doesn't support objects of type %T", ProfileLegacy, target)
// KeepFlags holds the flag set that determine which fields to keep in the copy pod.
type KeepFlags struct {
labels bool
Copy link
Member

Choose a reason for hiding this comment

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

Lastly fields to be exported?

Copy link
Member Author

@mochizuki875 mochizuki875 May 16, 2024

Choose a reason for hiding this comment

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

Sorry, I overlooked it...
I've fixed.

@mochizuki875 mochizuki875 force-pushed the remove_probe_from_copy_pod branch from e56ce82 to b63fa13 Compare May 16, 2024 17:26
@ardaguclu
Copy link
Member

Great, thanks!
/approve
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 16, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 0a826e71ead3de72941f4581360fe22684c131a5

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ardaguclu, mochizuki875

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 May 16, 2024
@mochizuki875
Copy link
Member Author

@ardaguclu
I'm very appreciate your many comments!

@k8s-triage-robot
Copy link

The Kubernetes project has merge-blocking tests that are currently too flaky to consistently pass.

This bot retests PRs for certain kubernetes repos according to the following rules:

  • The PR does have any do-not-merge/* labels
  • The PR does not have the needs-ok-to-test label
  • The PR is mergeable (does not have a needs-rebase label)
  • The PR is approved (has cncf-cla: yes, lgtm, approved labels)
  • The PR is failing tests required for merge

You can:

/retest

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. area/kubectl area/test 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. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants