Skip to content

Commit

Permalink
Merge pull request #7988 from hiromu-a5a/refactoring-rollout
Browse files Browse the repository at this point in the history
⚠️ Refactor clusterctl alpha rollout
  • Loading branch information
k8s-ci-robot authored Jan 30, 2023
2 parents 94ea776 + efe3728 commit 008e32c
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 98 deletions.
8 changes: 4 additions & 4 deletions cmd/clusterctl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ type Client interface {
// AlphaClient exposes the alpha features in clusterctl high-level client library.
type AlphaClient interface {
// RolloutRestart provides rollout restart of cluster-api resources
RolloutRestart(options RolloutOptions) error
RolloutRestart(options RolloutRestartOptions) error
// RolloutPause provides rollout pause of cluster-api resources
RolloutPause(options RolloutOptions) error
RolloutPause(options RolloutPauseOptions) error
// RolloutResume provides rollout resume of paused cluster-api resources
RolloutResume(options RolloutOptions) error
RolloutResume(options RolloutResumeOptions) error
// RolloutUndo provides rollout rollback of cluster-api resources
RolloutUndo(options RolloutOptions) error
RolloutUndo(options RolloutUndoOptions) error
// TopologyPlan dry runs the topology reconciler
TopologyPlan(options TopologyPlanOptions) (*TopologyPlanOutput, error)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/clusterctl/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,23 @@ func (f fakeClient) ProcessYAML(options ProcessYAMLOptions) (YamlPrinter, error)
return f.internalClient.ProcessYAML(options)
}

func (f fakeClient) RolloutRestart(options RolloutOptions) error {
func (f fakeClient) RolloutRestart(options RolloutRestartOptions) error {
return f.internalClient.RolloutRestart(options)
}

func (f fakeClient) DescribeCluster(options DescribeClusterOptions) (*tree.ObjectTree, error) {
return f.internalClient.DescribeCluster(options)
}

func (f fakeClient) RolloutPause(options RolloutOptions) error {
func (f fakeClient) RolloutPause(options RolloutPauseOptions) error {
return f.internalClient.RolloutPause(options)
}

func (f fakeClient) RolloutResume(options RolloutOptions) error {
func (f fakeClient) RolloutResume(options RolloutResumeOptions) error {
return f.internalClient.RolloutResume(options)
}

func (f fakeClient) RolloutUndo(options RolloutOptions) error {
func (f fakeClient) RolloutUndo(options RolloutUndoOptions) error {
return f.internalClient.RolloutUndo(options)
}

Expand Down
75 changes: 58 additions & 17 deletions cmd/clusterctl/client/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,50 @@ import (
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
)

// RolloutOptions carries the base set of options supported by rollout command.
type RolloutOptions struct {
// RolloutRestartOptions carries the options supported by RolloutRestart.
type RolloutRestartOptions struct {
// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty,
// default rules for kubeconfig discovery will be used.
Kubeconfig Kubeconfig

// Resources for the rollout command
Resources []string

// Namespace where the resource(s) live. If unspecified, the namespace name will be inferred
// from the current configuration.
Namespace string
}

// RolloutPauseOptions carries the options supported by RolloutPause.
type RolloutPauseOptions struct {
// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty,
// default rules for kubeconfig discovery will be used.
Kubeconfig Kubeconfig

// Resources for the rollout command
Resources []string

// Namespace where the resource(s) live. If unspecified, the namespace name will be inferred
// from the current configuration.
Namespace string
}

// RolloutResumeOptions carries the options supported by RolloutResume.
type RolloutResumeOptions struct {
// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty,
// default rules for kubeconfig discovery will be used.
Kubeconfig Kubeconfig

// Resources for the rollout command
Resources []string

// Namespace where the resource(s) live. If unspecified, the namespace name will be inferred
// from the current configuration.
Namespace string
}

// RolloutUndoOptions carries the options supported by RolloutUndo.
type RolloutUndoOptions struct {
// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty,
// default rules for kubeconfig discovery will be used.
Kubeconfig Kubeconfig
Expand All @@ -40,16 +82,15 @@ type RolloutOptions struct {
Namespace string

// Revision number to rollback to when issuing the undo command.
// Revision number of a specific revision when issuing the history command.
ToRevision int64
}

func (c *clusterctlClient) RolloutRestart(options RolloutOptions) error {
func (c *clusterctlClient) RolloutRestart(options RolloutRestartOptions) error {
clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
if err != nil {
return err
}
objRefs, err := getObjectRefs(clusterClient, options)
objRefs, err := getObjectRefs(clusterClient, options.Namespace, options.Resources)
if err != nil {
return err
}
Expand All @@ -61,12 +102,12 @@ func (c *clusterctlClient) RolloutRestart(options RolloutOptions) error {
return nil
}

func (c *clusterctlClient) RolloutPause(options RolloutOptions) error {
func (c *clusterctlClient) RolloutPause(options RolloutPauseOptions) error {
clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
if err != nil {
return err
}
objRefs, err := getObjectRefs(clusterClient, options)
objRefs, err := getObjectRefs(clusterClient, options.Namespace, options.Resources)
if err != nil {
return err
}
Expand All @@ -78,12 +119,12 @@ func (c *clusterctlClient) RolloutPause(options RolloutOptions) error {
return nil
}

func (c *clusterctlClient) RolloutResume(options RolloutOptions) error {
func (c *clusterctlClient) RolloutResume(options RolloutResumeOptions) error {
clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
if err != nil {
return err
}
objRefs, err := getObjectRefs(clusterClient, options)
objRefs, err := getObjectRefs(clusterClient, options.Namespace, options.Resources)
if err != nil {
return err
}
Expand All @@ -95,12 +136,12 @@ func (c *clusterctlClient) RolloutResume(options RolloutOptions) error {
return nil
}

func (c *clusterctlClient) RolloutUndo(options RolloutOptions) error {
func (c *clusterctlClient) RolloutUndo(options RolloutUndoOptions) error {
clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
if err != nil {
return err
}
objRefs, err := getObjectRefs(clusterClient, options)
objRefs, err := getObjectRefs(clusterClient, options.Namespace, options.Resources)
if err != nil {
return err
}
Expand All @@ -112,21 +153,21 @@ func (c *clusterctlClient) RolloutUndo(options RolloutOptions) error {
return nil
}

func getObjectRefs(clusterClient cluster.Client, options RolloutOptions) ([]corev1.ObjectReference, error) {
func getObjectRefs(clusterClient cluster.Client, namespace string, resources []string) ([]corev1.ObjectReference, error) {
// If the option specifying the Namespace is empty, try to detect it.
if options.Namespace == "" {
if namespace == "" {
currentNamespace, err := clusterClient.Proxy().CurrentNamespace()
if err != nil {
return []corev1.ObjectReference{}, err
}
options.Namespace = currentNamespace
namespace = currentNamespace
}

if len(options.Resources) == 0 {
if len(resources) == 0 {
return []corev1.ObjectReference{}, fmt.Errorf("required resource not specified")
}
normalized := normalizeResources(options.Resources)
objRefs, err := util.GetObjectReferences(options.Namespace, normalized...)
normalized := normalizeResources(resources)
objRefs, err := util.GetObjectReferences(namespace, normalized...)
if err != nil {
return []corev1.ObjectReference{}, err
}
Expand Down
Loading

0 comments on commit 008e32c

Please sign in to comment.