Skip to content

Commit

Permalink
Add command and client for clusterctl alpha rollout.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvinderpal committed Oct 23, 2020
1 parent 5ac19dc commit 027e433
Show file tree
Hide file tree
Showing 10 changed files with 560 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/clusterctl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type Client interface {
// ProcessYAML provides a direct way to process a yaml and inspect its
// variables.
ProcessYAML(options ProcessYAMLOptions) (YamlPrinter, error)

// Alpha features in clusterctl

// RolloutRestart provides rollout restart of cluster-api resources
RolloutRestart(options RolloutRestartOptions) error
}

// YamlPrinter exposes methods that prints the processed template and
Expand Down
79 changes: 79 additions & 0 deletions cmd/clusterctl/client/rollout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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"
"strings"

"sigs.k8s.io/cluster-api/cmd/clusterctl/client/rollout"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
)

// 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")
}
normalized := normalizeResources(options.Resources)
tuples, err := util.ResourceTypeAndNameArgs(normalized...)
if err != nil {
return err
}

for _, t := range tuples {
if err := rollout.ObjectRestarter(clusterClient.Proxy(), t, options.Namespace); err != nil {
return err
}
}
return nil
}

func normalizeResources(input []string) []string {
var normalized []string
for _, in := range input {
normalized = append(normalized, strings.ToLower(in))
}
return normalized
}
102 changes: 102 additions & 0 deletions cmd/clusterctl/client/rollout/restarter.go
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
}
35 changes: 35 additions & 0 deletions cmd/clusterctl/cmd/alpha.go
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)
}
56 changes: 56 additions & 0 deletions cmd/clusterctl/cmd/rollout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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))
}
85 changes: 85 additions & 0 deletions cmd/clusterctl/cmd/rollout/restart.go
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
}
Loading

0 comments on commit 027e433

Please sign in to comment.