-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command and client for
clusterctl alpha rollout
.
- Loading branch information
1 parent
0008b5b
commit 841edff
Showing
8 changed files
with
537 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package client | ||
|
||
import ( | ||
"fmt" | ||
|
||
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/rollout" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util" | ||
) | ||
|
||
var validResourceTypes = []string{"machinedeployment"} | ||
|
||
// RolloutRestartOptions carries the options supported by rollout restart. | ||
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 to be rollout restarted. | ||
Resources []string | ||
|
||
// Namespace where the resource(s) live. If unspecified, the namespace name will be inferred | ||
// from the current configuration. | ||
Namespace string | ||
} | ||
|
||
func (c *clusterctlClient) RolloutRestart(options RolloutRestartOptions) error { | ||
clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// If the option specifying the Namespace is empty, try to detect it. | ||
if options.Namespace == "" { | ||
currentNamespace, err := clusterClient.Proxy().CurrentNamespace() | ||
if err != nil { | ||
return err | ||
} | ||
options.Namespace = currentNamespace | ||
} | ||
|
||
if len(options.Resources) == 0 { | ||
return fmt.Errorf("required resource not specified") | ||
} | ||
// TODO: normailzie options.Resources to all lowercase. | ||
tuples, err := util.ResourceTypeAndNameArgs(options.Resources...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, t := range tuples { | ||
if err := rollout.ObjectRestarter(clusterClient.Proxy(), t, options.Namespace); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rollout | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var validResourceTypes = []string{"machinedeployment"} | ||
|
||
func ObjectRestarter(proxy cluster.Proxy, t util.ResourceTuple, namespace string) error { | ||
switch t.Resource { | ||
case "machinedeployment": | ||
mdObj := &clusterv1.MachineDeployment{} | ||
if err := getMachineDeployment(proxy, t.Name, namespace, mdObj); err != nil { | ||
return errors.Wrapf(err, "failed to fetch %v/%v", t.Resource, t.Name) | ||
} | ||
if mdObj.Spec.Paused { | ||
fmt.Printf("can't restart paused machinedeployment (run rollout resume first): %v/%v\n", t.Resource, t.Name) | ||
return nil | ||
} | ||
if err := setRestartedAtAnnotation(proxy, t.Name, namespace); err != nil { | ||
return err | ||
} | ||
case "kubeadmcontrolplane": | ||
return fmt.Errorf("restarting is not supported") | ||
default: | ||
return errors.Errorf("Invalid resource type %v. Valid values: %v", t.Resource, validResourceTypes) | ||
} | ||
return nil | ||
} | ||
|
||
// getMachineDeployment retrieves the MachineDeployment object corresponding to the name and namespace specified. | ||
func getMachineDeployment(proxy cluster.Proxy, name, namespace string, mdObj *clusterv1.MachineDeployment) error { | ||
c, err := proxy.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
mdObjKey := client.ObjectKey{ | ||
Namespace: namespace, | ||
Name: name, | ||
} | ||
if err := c.Get(context.TODO(), mdObjKey, mdObj); err != nil { | ||
return errors.Wrapf(err, "error reading %q %s/%s", | ||
mdObj.GroupVersionKind(), mdObj.GetNamespace(), mdObj.GetName()) | ||
} | ||
return nil | ||
} | ||
|
||
// setRestartedAtAnnotation sets the restartedAt annotation in the MachineDeployment's spec.template.objectmeta. | ||
func setRestartedAtAnnotation(proxy cluster.Proxy, name, namespace string) error { | ||
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"cluster.x-k8s.io/restartedAt\":\"%v\"}}}}}", time.Now().Format(time.RFC3339)))) | ||
data, _ := patch.Data(nil) | ||
fmt.Printf("patch: %v\n", string(data)) | ||
return patchMachineDeployemt(proxy, name, namespace, patch) | ||
} | ||
|
||
// patchMachineDeployemt applies a patch to a machinedeployment | ||
func patchMachineDeployemt(proxy cluster.Proxy, name, namespace string, patch client.Patch) error { | ||
cFrom, err := proxy.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
mdObj := &clusterv1.MachineDeployment{} | ||
mdObjKey := client.ObjectKey{ | ||
Namespace: namespace, | ||
Name: name, | ||
} | ||
if err := cFrom.Get(context.TODO(), mdObjKey, mdObj); err != nil { | ||
return errors.Wrapf(err, "error reading %q %s/%s", | ||
mdObj.GroupVersionKind(), mdObj.GetNamespace(), mdObj.GetName()) | ||
} | ||
|
||
if err := cFrom.Patch(context.TODO(), mdObj, patch); err != nil { | ||
return errors.Wrapf(err, "error while patching %q %s/%s", | ||
mdObj.GroupVersionKind(), mdObj.GetNamespace(), mdObj.GetName()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var alphaCmd = &cobra.Command{ | ||
Use: "alpha", | ||
Short: "Commands for features in alpha.", | ||
Long: `These commands correspond to alpha features in clusterctl.`, | ||
} | ||
|
||
func init() { | ||
|
||
// Alpha commands should be added here. | ||
alphaCmd.AddCommand(rolloutCmd) | ||
|
||
RootCmd.AddCommand(alphaCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/lithammer/dedent" | ||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd/rollout" | ||
) | ||
|
||
var ( | ||
rolloutLong = LongDesc(` | ||
Manage the rollout of a cluster-api resource.` + rolloutValidResources) | ||
|
||
rolloutExample = Examples(` | ||
# Force an immediate rollout of machinedeployment | ||
clusterctl alpha rollout restart machinedeployment/my-md-0 | ||
# Rollback to the previous revision of a machinedeployment | ||
clusterctl alpha rollout undo machinedeployment/my-md-0 | ||
# Check the rollout status of a machinedeployment | ||
clusterctl alpha rollout status machinedeployment/my-md-0`) | ||
|
||
rolloutValidResources = dedent.Dedent(` | ||
Valid resource types include: | ||
* machinedeployments | ||
`) | ||
|
||
rolloutCmd = &cobra.Command{ | ||
Use: "rollout SUBCOMMAND", | ||
Short: "Manage the rollout of a cluster-api resource", | ||
Long: rolloutLong, | ||
Example: rolloutExample, | ||
} | ||
) | ||
|
||
func init() { | ||
// subcommands | ||
rolloutCmd.AddCommand(rollout.NewCmdRolloutRestart(cfgFile)) | ||
// rolloutCmd.AddCommand(rolloutHistoryCmd) | ||
// rolloutCmd.AddCommand(rolloutPauseCmd) | ||
// rolloutCmd.AddCommand(rolloutResumeCmd) | ||
// rolloutCmd.AddCommand(rolloutUndoCmd) | ||
// rolloutCmd.AddCommand(rolloutStatusCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rollout | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
"sigs.k8s.io/cluster-api/cmd/clusterctl/client" | ||
) | ||
|
||
// restartOptions is the start of the data required to perform the operation. | ||
type restartOptions struct { | ||
kubeconfig string | ||
kubeconfigContext string | ||
resources []string | ||
namespace string | ||
} | ||
|
||
var ro = &restartOptions{} | ||
|
||
var ( | ||
restartLong = templates.LongDesc(` | ||
Restart of cluser-api resources. | ||
Resources will be rollout restarted.`) | ||
|
||
restartExample = templates.Examples(` | ||
# Restart a machinedeployment | ||
clusterctl alpha rollout restart machinedeployment/my-md-0`) | ||
) | ||
|
||
// NewCmdRolloutRestart returns a Command instance for 'rollout restart' sub command | ||
func NewCmdRolloutRestart(cfgFile string) *cobra.Command { | ||
|
||
cmd := &cobra.Command{ | ||
Use: "restart RESOURCE", | ||
DisableFlagsInUseLine: true, | ||
Short: "Restart a cluster-api resource", | ||
Long: restartLong, | ||
Example: restartExample, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runRestart(cfgFile, cmd, args) | ||
}, | ||
} | ||
cmd.Flags().StringVar(&ro.kubeconfig, "kubeconfig", "", | ||
"Path to the kubeconfig file to use for accessing the management cluster. If unspecified, default discovery rules apply.") | ||
cmd.Flags().StringVar(&ro.kubeconfigContext, "kubeconfig-context", "", | ||
"Context to be used within the kubeconfig file. If empty, current context will be used.") | ||
cmd.Flags().StringVar(&ro.namespace, "namespace", "n", | ||
"Namespace where the resource(s) reside. If unspecified, the defult namespace will be used.") | ||
|
||
return cmd | ||
} | ||
|
||
func runRestart(cfgFile string, cmd *cobra.Command, args []string) error { | ||
ro.resources = args | ||
|
||
c, err := client.New(cfgFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := c.RolloutRestart(client.RolloutRestartOptions{ | ||
Kubeconfig: client.Kubeconfig{Path: ro.kubeconfig, Context: ro.kubeconfigContext}, | ||
Namespace: ro.namespace, | ||
Resources: ro.resources, | ||
}); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.