k3s/vendor/github.com/kubernetes-sigs/cri-tools/cmd/crictl/main.go

300 lines
8.8 KiB
Go
Raw Normal View History

2019-02-08 04:04:22 +00:00
/*
Copyright 2017 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 crictl
import (
"fmt"
"os"
"sort"
"time"
2020-08-10 17:43:49 +00:00
"github.com/pkg/errors"
2019-02-08 04:04:22 +00:00
"github.com/sirupsen/logrus"
2020-03-26 21:07:15 +00:00
"github.com/urfave/cli/v2"
2020-08-10 17:43:49 +00:00
2019-02-08 04:04:22 +00:00
"google.golang.org/grpc"
2020-08-10 17:43:49 +00:00
2019-08-30 18:33:25 +00:00
internalapi "k8s.io/cri-api/pkg/apis"
2020-08-10 17:43:49 +00:00
"k8s.io/kubernetes/pkg/kubelet/cri/remote"
2019-02-08 04:04:22 +00:00
"k8s.io/kubernetes/pkg/kubelet/util"
2020-08-10 17:43:49 +00:00
"github.com/kubernetes-sigs/cri-tools/pkg/common"
2019-02-08 04:04:22 +00:00
"github.com/kubernetes-sigs/cri-tools/pkg/version"
)
const (
2019-07-10 03:51:02 +00:00
defaultTimeout = 2 * time.Second
2019-02-08 04:04:22 +00:00
)
var (
// RuntimeEndpoint is CRI server runtime endpoint
RuntimeEndpoint string
2020-08-10 17:43:49 +00:00
// RuntimeEndpointIsSet is true when RuntimeEndpoint is configured
RuntimeEndpointIsSet bool
2019-02-08 04:04:22 +00:00
// ImageEndpoint is CRI server image endpoint, default same as runtime endpoint
ImageEndpoint string
2020-08-10 17:43:49 +00:00
// ImageEndpointIsSet is true when ImageEndpoint is configured
ImageEndpointIsSet bool
2019-02-08 04:04:22 +00:00
// Timeout of connecting to server (default: 10s)
Timeout time.Duration
// Debug enable debug output
Debug bool
2020-08-10 17:43:49 +00:00
// PullImageOnCreate enables pulling image on create requests
PullImageOnCreate bool
// DisablePullOnRun disable pulling image on run requests
DisablePullOnRun bool
2019-02-08 04:04:22 +00:00
)
func getRuntimeClientConnection(context *cli.Context) (*grpc.ClientConn, error) {
2020-08-10 17:43:49 +00:00
if RuntimeEndpointIsSet && RuntimeEndpoint == "" {
2019-02-08 04:04:22 +00:00
return nil, fmt.Errorf("--runtime-endpoint is not set")
}
2020-08-10 17:43:49 +00:00
logrus.Debug("get runtime connection")
// If no EP set then use the default endpoint types
if !RuntimeEndpointIsSet {
logrus.Warningf("runtime connect using default endpoints: %v. "+
"As the default settings are now deprecated, you should set the "+
"endpoint instead.", defaultRuntimeEndpoints)
logrus.Debug("Note that performance maybe affected as each default " +
"connection attempt takes n-seconds to complete before timing out " +
"and going to the next in sequence.")
return getConnection(defaultRuntimeEndpoints)
2019-02-08 04:04:22 +00:00
}
2020-08-10 17:43:49 +00:00
return getConnection([]string{RuntimeEndpoint})
2019-02-08 04:04:22 +00:00
}
func getImageClientConnection(context *cli.Context) (*grpc.ClientConn, error) {
if ImageEndpoint == "" {
2020-08-10 17:43:49 +00:00
if RuntimeEndpointIsSet && RuntimeEndpoint == "" {
2019-02-08 04:04:22 +00:00
return nil, fmt.Errorf("--image-endpoint is not set")
}
ImageEndpoint = RuntimeEndpoint
2020-08-10 17:43:49 +00:00
ImageEndpointIsSet = RuntimeEndpointIsSet
2019-02-08 04:04:22 +00:00
}
2020-08-10 17:43:49 +00:00
logrus.Debugf("get image connection")
// If no EP set then use the default endpoint types
if !ImageEndpointIsSet {
logrus.Warningf("image connect using default endpoints: %v. "+
"As the default settings are now deprecated, you should set the "+
"endpoint instead.", defaultRuntimeEndpoints)
logrus.Debug("Note that performance maybe affected as each default " +
"connection attempt takes n-seconds to complete before timing out " +
"and going to the next in sequence.")
return getConnection(defaultRuntimeEndpoints)
2019-02-08 04:04:22 +00:00
}
2020-08-10 17:43:49 +00:00
return getConnection([]string{ImageEndpoint})
}
2019-02-08 04:04:22 +00:00
2020-08-10 17:43:49 +00:00
func getConnection(endPoints []string) (*grpc.ClientConn, error) {
if endPoints == nil || len(endPoints) == 0 {
return nil, fmt.Errorf("endpoint is not set")
}
endPointsLen := len(endPoints)
var conn *grpc.ClientConn
for indx, endPoint := range endPoints {
logrus.Debugf("connect using endpoint '%s' with '%s' timeout", endPoint, Timeout)
addr, dialer, err := util.GetAddressAndDialer(endPoint)
if err != nil {
if indx == endPointsLen-1 {
return nil, err
}
logrus.Error(err)
continue
}
conn, err = grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(Timeout), grpc.WithContextDialer(dialer))
if err != nil {
errMsg := errors.Wrapf(err, "connect endpoint '%s', make sure you are running as root and the endpoint has been started", endPoint)
if indx == endPointsLen-1 {
return nil, errMsg
}
logrus.Error(errMsg)
} else {
logrus.Debugf("connected successfully using endpoint: %s", endPoint)
break
}
2019-02-08 04:04:22 +00:00
}
return conn, nil
}
2019-08-30 18:33:25 +00:00
func getRuntimeService(context *cli.Context) (internalapi.RuntimeService, error) {
2019-02-08 04:04:22 +00:00
return remote.NewRemoteRuntimeService(RuntimeEndpoint, Timeout)
}
2020-08-10 17:43:49 +00:00
func getTimeout(timeDuration time.Duration) time.Duration {
if timeDuration.Seconds() > 0 {
return timeDuration
}
return defaultTimeout // use default
}
2019-02-08 04:04:22 +00:00
func Main() {
app := cli.NewApp()
app.Name = "crictl"
app.Usage = "client for CRI"
app.Version = version.Version
2020-03-26 21:07:15 +00:00
app.Commands = []*cli.Command{
2019-02-08 04:04:22 +00:00
runtimeAttachCommand,
createContainerCommand,
runtimeExecCommand,
runtimeVersionCommand,
listImageCommand,
containerStatusCommand,
imageStatusCommand,
imageFsInfoCommand,
podStatusCommand,
logsCommand,
runtimePortForwardCommand,
listContainersCommand,
pullImageCommand,
2019-09-27 21:51:53 +00:00
runContainerCommand,
2019-02-08 04:04:22 +00:00
runPodCommand,
removeContainerCommand,
removeImageCommand,
removePodCommand,
listPodCommand,
startContainerCommand,
runtimeStatusCommand,
stopContainerCommand,
stopPodCommand,
updateContainerCommand,
configCommand,
statsCommand,
completionCommand,
}
2020-08-10 17:43:49 +00:00
runtimeEndpointUsage := fmt.Sprintf("Endpoint of CRI container runtime "+
"service (default: uses in order the first successful one of %v). "+
"Default is now deprecated and the endpoint should be set instead.",
defaultRuntimeEndpoints)
2019-02-08 04:04:22 +00:00
app.Flags = []cli.Flag{
2020-03-26 21:07:15 +00:00
&cli.StringFlag{
2020-08-10 17:43:49 +00:00
Name: "config",
Aliases: []string{"c"},
EnvVars: []string{"CRI_CONFIG_FILE"},
Value: defaultConfigPath,
Usage: "Location of the client config file. If not specified and the default does not exist, the program's directory is searched as well",
2019-02-08 04:04:22 +00:00
},
2020-03-26 21:07:15 +00:00
&cli.StringFlag{
Name: "runtime-endpoint",
Aliases: []string{"r"},
EnvVars: []string{"CONTAINER_RUNTIME_ENDPOINT"},
2020-08-10 17:43:49 +00:00
Usage: runtimeEndpointUsage,
2019-02-08 04:04:22 +00:00
},
2020-03-26 21:07:15 +00:00
&cli.StringFlag{
Name: "image-endpoint",
Aliases: []string{"i"},
EnvVars: []string{"IMAGE_SERVICE_ENDPOINT"},
2020-08-10 17:43:49 +00:00
Usage: "Endpoint of CRI image manager service (default: uses " +
"'runtime-endpoint' setting)",
2019-02-08 04:04:22 +00:00
},
2020-03-26 21:07:15 +00:00
&cli.DurationFlag{
Name: "timeout",
Aliases: []string{"t"},
Value: defaultTimeout,
2020-08-10 17:43:49 +00:00
Usage: "Timeout of connecting to the server in seconds (e.g. 2s, 20s.). " +
"0 or less is set to default",
2019-02-08 04:04:22 +00:00
},
2020-03-26 21:07:15 +00:00
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"D"},
Usage: "Enable debug mode",
2019-02-08 04:04:22 +00:00
},
}
2020-08-10 17:43:49 +00:00
app.Before = func(context *cli.Context) (err error) {
var config *common.ServerConfiguration
var exePath string
if exePath, err = os.Executable(); err != nil {
logrus.Fatal(err)
}
if config, err = common.GetServerConfigFromFile(context.String("config"), exePath); err != nil {
if context.IsSet("config") {
logrus.Fatal(err)
2019-02-08 04:04:22 +00:00
}
}
2020-08-10 17:43:49 +00:00
if config == nil {
2020-03-26 21:07:15 +00:00
RuntimeEndpoint = context.String("runtime-endpoint")
2020-08-10 17:43:49 +00:00
if context.IsSet("runtime-endpoint") {
RuntimeEndpointIsSet = true
}
2020-03-26 21:07:15 +00:00
ImageEndpoint = context.String("image-endpoint")
2020-08-10 17:43:49 +00:00
if context.IsSet("image-endpoint") {
ImageEndpointIsSet = true
}
if context.IsSet("timeout") {
Timeout = getTimeout(context.Duration("timeout"))
} else {
Timeout = context.Duration("timeout")
}
2020-03-26 21:07:15 +00:00
Debug = context.Bool("debug")
2020-08-10 17:43:49 +00:00
DisablePullOnRun = false
2019-02-08 04:04:22 +00:00
} else {
// Command line flags overrides config file.
if context.IsSet("runtime-endpoint") {
RuntimeEndpoint = context.String("runtime-endpoint")
2020-08-10 17:43:49 +00:00
RuntimeEndpointIsSet = true
2019-02-08 04:04:22 +00:00
} else if config.RuntimeEndpoint != "" {
RuntimeEndpoint = config.RuntimeEndpoint
2020-08-10 17:43:49 +00:00
RuntimeEndpointIsSet = true
2019-02-08 04:04:22 +00:00
} else {
2020-03-26 21:07:15 +00:00
RuntimeEndpoint = context.String("runtime-endpoint")
2019-02-08 04:04:22 +00:00
}
if context.IsSet("image-endpoint") {
ImageEndpoint = context.String("image-endpoint")
2020-08-10 17:43:49 +00:00
ImageEndpointIsSet = true
2019-02-08 04:04:22 +00:00
} else if config.ImageEndpoint != "" {
ImageEndpoint = config.ImageEndpoint
2020-08-10 17:43:49 +00:00
ImageEndpointIsSet = true
2019-02-08 04:04:22 +00:00
} else {
2020-03-26 21:07:15 +00:00
ImageEndpoint = context.String("image-endpoint")
2019-02-08 04:04:22 +00:00
}
if context.IsSet("timeout") {
2020-08-10 17:43:49 +00:00
Timeout = getTimeout(context.Duration("timeout"))
} else if config.Timeout > 0 { // 0/neg value set to default timeout
Timeout = config.Timeout
2019-02-08 04:04:22 +00:00
} else {
2020-03-26 21:07:15 +00:00
Timeout = context.Duration("timeout")
2019-02-08 04:04:22 +00:00
}
if context.IsSet("debug") {
2020-03-26 21:07:15 +00:00
Debug = context.Bool("debug")
2019-02-08 04:04:22 +00:00
} else {
Debug = config.Debug
}
2020-08-10 17:43:49 +00:00
PullImageOnCreate = config.PullImageOnCreate
DisablePullOnRun = config.DisablePullOnRun
2019-02-08 04:04:22 +00:00
}
if Debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
// sort all flags
for _, cmd := range app.Commands {
sort.Sort(cli.FlagsByName(cmd.Flags))
}
sort.Sort(cli.FlagsByName(app.Flags))
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}