mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 07:27:21 +00:00
Add a kubectl stop command
This commit is contained in:
@@ -64,6 +64,8 @@ type Factory struct {
|
||||
Printer func(cmd *cobra.Command, mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error)
|
||||
// Returns a Resizer for changing the size of the specified RESTMapping type or an error
|
||||
Resizer func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Resizer, error)
|
||||
// Returns a Reaper for gracefully shutting down resources.
|
||||
Reaper func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Reaper, error)
|
||||
// Returns a schema that can validate objects stored on disk.
|
||||
Validator func(*cobra.Command) (validation.Schema, error)
|
||||
// Returns the default namespace to use in cases where no other namespace is specified
|
||||
@@ -131,11 +133,14 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resizer, ok := kubectl.ResizerFor(mapping.Kind, client)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no resizer has been implemented for %q", mapping.Kind)
|
||||
return kubectl.ResizerFor(mapping.Kind, client)
|
||||
},
|
||||
Reaper: func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Reaper, error) {
|
||||
client, err := clients.ClientForVersion(mapping.APIVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resizer, nil
|
||||
return kubectl.ReaperFor(mapping.Kind, client)
|
||||
},
|
||||
Validator: func(cmd *cobra.Command) (validation.Schema, error) {
|
||||
if GetFlagBool(cmd, "validate") {
|
||||
@@ -206,6 +211,7 @@ Find more information at https://github.com/GoogleCloudPlatform/kubernetes.`,
|
||||
cmds.AddCommand(f.NewCmdResize(out))
|
||||
|
||||
cmds.AddCommand(f.NewCmdRunContainer(out))
|
||||
cmds.AddCommand(f.NewCmdStop(out))
|
||||
|
||||
return cmds
|
||||
}
|
||||
|
||||
56
pkg/kubectl/cmd/stop.go
Normal file
56
pkg/kubectl/cmd/stop.go
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
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 (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func (f *Factory) NewCmdStop(out io.Writer) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "stop <resource> <id>",
|
||||
Short: "Gracefully shutdown a resource",
|
||||
Long: `Gracefully shutdown a resource
|
||||
|
||||
Attempts to shutdown and delete a resource that supports graceful termination.
|
||||
If the resource is resizable it will be resized to 0 before deletion.
|
||||
|
||||
Examples:
|
||||
$ kubectl stop replicationcontroller foo
|
||||
foo stopped
|
||||
`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) != 2 {
|
||||
usageError(cmd, "<resource> <id>")
|
||||
}
|
||||
cmdNamespace, err := f.DefaultNamespace(cmd)
|
||||
mapper, _ := f.Object(cmd)
|
||||
mapping, namespace, name := ResourceFromArgs(cmd, args, mapper, cmdNamespace)
|
||||
|
||||
reaper, err := f.Reaper(cmd, mapping)
|
||||
checkErr(err)
|
||||
|
||||
s, err := reaper.Stop(namespace, name)
|
||||
checkErr(err)
|
||||
fmt.Fprintf(out, "%s\n", s)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
Reference in New Issue
Block a user