|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package cluster implements the `restart` command |
| 18 | +package cluster |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/pkg/errors" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + "sigs.k8s.io/kind/pkg/cluster" |
| 27 | +) |
| 28 | + |
| 29 | +type flagpole struct { |
| 30 | + Name string |
| 31 | +} |
| 32 | + |
| 33 | +// NewCommand returns a new cobra.Command for cluster creation |
| 34 | +func NewCommand() *cobra.Command { |
| 35 | + flags := &flagpole{} |
| 36 | + cmd := &cobra.Command{ |
| 37 | + Args: cobra.NoArgs, |
| 38 | + Use: "cluster", |
| 39 | + Short: "Restart a cluster", |
| 40 | + Long: "Restart a cluster", |
| 41 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 42 | + return runE(flags, cmd, args) |
| 43 | + }, |
| 44 | + } |
| 45 | + cmd.Flags().StringVar(&flags.Name, "name", cluster.DefaultName, "the cluster name") |
| 46 | + return cmd |
| 47 | +} |
| 48 | + |
| 49 | +func runE(flags *flagpole, cmd *cobra.Command, args []string) error { |
| 50 | + // Check if the cluster name exists |
| 51 | + known, err := cluster.IsKnown(flags.Name) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + if !known { |
| 56 | + return errors.Errorf("unknown cluster %q", flags.Name) |
| 57 | + } |
| 58 | + // Restart the cluster |
| 59 | + fmt.Printf("Restarting cluster %q ...\n", flags.Name) |
| 60 | + ctx := cluster.NewContext(flags.Name) |
| 61 | + if err := ctx.Restart(); err != nil { |
| 62 | + return errors.Wrap(err, "failed to restart cluster") |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
0 commit comments