Skip to content

Commit af64afa

Browse files
add restart cluster command.
Signed-off-by: Jintao Zhang <zhangjintao9020@gmail.com>
1 parent d4ff13e commit af64afa

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

cmd/kind/kind.go

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"sigs.k8s.io/kind/cmd/kind/export"
3232
"sigs.k8s.io/kind/cmd/kind/get"
3333
"sigs.k8s.io/kind/cmd/kind/load"
34+
"sigs.k8s.io/kind/cmd/kind/restart"
3435
"sigs.k8s.io/kind/cmd/kind/version"
3536
logutil "sigs.k8s.io/kind/pkg/log"
3637
)
@@ -70,6 +71,7 @@ func NewCommand() *cobra.Command {
7071
cmd.AddCommand(get.NewCommand())
7172
cmd.AddCommand(version.NewCommand())
7273
cmd.AddCommand(load.NewCommand())
74+
cmd.AddCommand(restart.NewCommand())
7375
return cmd
7476
}
7577

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

cmd/kind/restart/restart.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 restart implements the `restart` command
18+
package restart
19+
20+
import (
21+
"github.com/spf13/cobra"
22+
23+
restartcluster "sigs.k8s.io/kind/cmd/kind/restart/cluster"
24+
)
25+
26+
// NewCommand returns a new cobra.Command for cluster creation
27+
func NewCommand() *cobra.Command {
28+
cmd := &cobra.Command{
29+
Args: cobra.NoArgs,
30+
Use: "restart",
31+
Short: "restarts one of [cluster]",
32+
Long: "restarts one of local Kubernetes cluster (cluster)",
33+
}
34+
cmd.AddCommand(restartcluster.NewCommand())
35+
return cmd
36+
}

pkg/cluster/context.go

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
internalcontext "sigs.k8s.io/kind/pkg/cluster/internal/context"
2424
internalcreate "sigs.k8s.io/kind/pkg/cluster/internal/create"
2525
internaldelete "sigs.k8s.io/kind/pkg/cluster/internal/delete"
26+
internalrestart "sigs.k8s.io/kind/pkg/cluster/internal/restart"
2627
"sigs.k8s.io/kind/pkg/cluster/logs"
2728
"sigs.k8s.io/kind/pkg/cluster/nodes"
2829
)
@@ -99,3 +100,8 @@ func (c *Context) CollectLogs(dir string) error {
99100
}
100101
return logs.Collect(nodes, dir)
101102
}
103+
104+
// Restart a kubernetes-in-docker cluster
105+
func (c *Context) Restart() error {
106+
return internalrestart.Cluster(c.ic)
107+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 restart
18+
19+
import (
20+
"github.com/pkg/errors"
21+
22+
"sigs.k8s.io/kind/pkg/cluster/internal/context"
23+
"sigs.k8s.io/kind/pkg/cluster/nodes"
24+
)
25+
26+
// Cluster restarts the cluster identified by ctx
27+
func Cluster(c *context.Context) error {
28+
n, err := c.ListNodes()
29+
if err != nil {
30+
return errors.Wrap(err, "error listing nodes")
31+
}
32+
33+
return nodes.Restart(n...)
34+
}

pkg/cluster/nodes/nodes.go

+46
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,49 @@ func WaitForReady(node *Node, until time.Time) bool {
134134
return true
135135
})
136136
}
137+
138+
// Restart restarts nodes by name / ID (see Node.String())
139+
func Restart(nodes ...Node) error {
140+
if len(nodes) == 0 {
141+
return nil
142+
}
143+
ids := []string{}
144+
for _, node := range nodes {
145+
ids = append(ids, node.name)
146+
}
147+
cmd := exec.Command(
148+
"docker",
149+
append(
150+
[]string{
151+
"restart",
152+
// the default wait time is 10s
153+
"--time=1",
154+
},
155+
ids...,
156+
)...,
157+
)
158+
159+
if err := cmd.Run(); err != nil {
160+
// TODO: logging here
161+
return err
162+
}
163+
164+
for _, node := range nodes {
165+
166+
if err := node.FixMounts(); err != nil {
167+
// TODO: logging here
168+
return err
169+
}
170+
171+
if err := node.SignalStart(); err != nil {
172+
// TODO: logging here
173+
return err
174+
}
175+
176+
if !node.WaitForDocker(time.Now().Add(time.Second * 30)) {
177+
return errors.Errorf("timed out waiting for docker to be ready on node %s", node.Name())
178+
}
179+
}
180+
181+
return nil
182+
}

0 commit comments

Comments
 (0)