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

kubeadm: refactor the dry-run logic #126776

Merged

Conversation

neolit123
Copy link
Member

@neolit123 neolit123 commented Aug 19, 2024

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

The current dryrun client implemnetation is suboptimal
and sparse. It has the following problems:

  • When an object CREATE or UPDATE reaches the default dryrun client
    the operation is a NO-OP, which means subsequent GET calls must
    fully emulate the object that exists in the store.
  • There are multiple implmentations of a DryRunGetter interface
    such the one in init_dryrun.go but there are no implementations
    for reset, upgrade, join.
  • There is a specific DryRunGetter that is backed by a real
    client in clientbacked_dryrun.go, but this is used for upgrade
    and does not work in conjuction with a fake client.

This commit does the following changes:

  • Removes all existing dryrun.go implementations.
  • Add a new DryRun implementation in dryrun.go that implements
    3 clients - fake clientset, real clientset, real dynamic client.
  • The DryRun object uses the method chaining pattern.
  • Allows the user opt-in into real clients only if needed, by passing
    a real kubeconfig. By default only constructs a fake client.
  • The default reactor chain for the fake client, always logs the
    object action, then for GET or LIST actions attempts to use the
    real dynamic client to get the object. If a real object does not
    exist it attempts to get the object from the fake object store.
  • The user can prepend or append reactors to the chain.
  • All known needed reactors for operations during init, join,
    reset, upgrade are added as methods of the DryRun struct.
  • Adds detailed unit test for the DryRun struct and its methods
    including reactors.

Additional changes:

  • Use the new DryRun implementation in all command workflows -
    init, join, reset, upgrade.
  • Ensure that --dry-run works even if there is no active cluster
    by returning faked objects. For join, a faked cluster-info
    with a fake bootstrap token and CA are used.

Which issue(s) this PR fixes:

Fixes kubernetes/kubeadm#2653
Fixes kubernetes/kubeadm#1932

Special notes for your reviewer:

NONE

Does this PR introduce a user-facing change?

kubeadm: increased the verbosity of API client dry-run actions during the subcommands "init", "join", "upgrade" and "reset". Allowed dry-run on 'kubeadm join' even if there is no existing cluster by utilizing a faked, in-memory cluster-info ConfigMap.

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. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. 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 Aug 19, 2024
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubeadm sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Aug 19, 2024
@neolit123
Copy link
Member Author

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 19, 2024
@neolit123
Copy link
Member Author

neolit123 commented Aug 19, 2024

/triage accepted
/priority backlog

@k8s-ci-robot k8s-ci-robot added priority/backlog Higher priority than priority/awaiting-more-evidence. triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Aug 19, 2024
@neolit123 neolit123 force-pushed the 1.31-improve-dry-run-logic branch 4 times, most recently from 174edd6 to 451fa49 Compare August 20, 2024 15:57
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 3, 2024
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 3, 2024
@neolit123
Copy link
Member Author

/retitle kubeadm: refactor the dry-run logic

this is ready for review

@@ -176,7 +176,7 @@ func resetConfigDir(configPathDir string, dirsToClean []string, isDryRun bool) {
}
}
} else {
fmt.Printf("[reset] Would delete files: %v\n", filesToClean)
fmt.Printf("[dryrun] Would delete files: %v\n", filesToClean)
Copy link
Member

Choose a reason for hiding this comment

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

Should file path be quoted using %q?

Copy link
Member Author

Choose a reason for hiding this comment

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

filesToClean is a slice of strings, so that's why here is %v

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 1, 2024
@neolit123
Copy link
Member Author

updated

@carlory
Copy link
Member

carlory commented Oct 7, 2024

/lgtm

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

LGTM label has been added.

Git tree hash: bc73c16edff62f82ad6ed983f48d13f07018621a

@pacoxu
Copy link
Member

pacoxu commented Oct 8, 2024

/lgtm

objBytes, err := d.marshalFunc(obj, gv)
if err == nil {
fmt.Fprintln(d.writer, "[dryrun] Attached object:")
fmt.Fprintf(d.writer, "%s", objBytes)
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to use fmt.Fprinln() here?

Copy link
Member Author

@neolit123 neolit123 Oct 10, 2024

Choose a reason for hiding this comment

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

changed to:

		fmt.Fprintln(d.writer, string(objBytes))

cmd/kubeadm/app/util/apiclient/dryrun.go Outdated Show resolved Hide resolved
@neolit123
Copy link
Member Author

neolit123 commented Oct 10, 2024

@SataQiu add comments if you have too. x
i will try to merge this early next week and update the dry run e2e test.

NVM, i missed your review above.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 10, 2024
@neolit123
Copy link
Member Author

@SataQiu updated.

The current dryrun client implemnetation is suboptimal
and sparse. It has the following problems:

- When an object CREATE or UPDATE reaches the default dryrun client
the operation is a NO-OP, which means subsequent GET calls must
fully emulate the object that exists in the store.
- There are multiple implmentations of a DryRunGetter interface
such the one in init_dryrun.go but there are no implementations
for reset, upgrade, join.
- There is a specific DryRunGetter that is backed by a real
client in clientbacked_dryrun.go, but this is used for upgrade
and does not work in conjuction with a fake client.

This commit does the following changes:

- Removes all existing *dryrun*.go implementations.
- Add a new DryRun implementation in dryrun.go that implements
3 clients - fake clientset, real clientset, real dynamic client.
- The DryRun object uses the method chaining pattern.
- Allows the user opt-in into real clients only if needed, by passing
a real kubeconfig. By default only constructs a fake client.
- The default reactor chain for the fake client, always logs the
object action, then for GET or LIST actions attempts to use the
real dynamic client to get the object. If a real object does not
exist it attempts to get the object from the fake object store.
- The user can prepend or append reactors to the chain.
- All known needed reactors for operations during init, join,
reset, upgrade are added as methods of the DryRun struct.
- Adds detailed unit test for the DryRun struct and its methods
including reactors.

Additional changes:
- Use the new DryRun implementation in all command workflows -
init, join, reset, upgrade.
- Ensure that --dry-run works even if there is no active cluster
by returning faked objects. For join, a faked cluster-info
with a fake bootstrap token and CA are used.
@neolit123
Copy link
Member Author

@SataQiu @pacoxu @carlory
looking for LGTM so that i can merge this and update e2e tests.

@pacoxu
Copy link
Member

pacoxu commented Oct 14, 2024

/lgtm
/approve
(The only update after my last LGTM is a message format update according to Sata's comments. )

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

LGTM label has been added.

Git tree hash: 9f3c027fa029ac074dfd473646137e1ee4e369c6

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: neolit123, pacoxu

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

@neolit123
Copy link
Member Author

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 14, 2024
@k8s-ci-robot k8s-ci-robot merged commit 769695a into kubernetes:master Oct 14, 2024
14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.32 milestone Oct 14, 2024
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/kubeadm cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. 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/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. 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
None yet
Development

Successfully merging this pull request may close these issues.

add dry run e2e tests the dynamic dryrun client in kubeadm only supports the core/v1 GroupVersion
5 participants