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"
|
|
|
|
"net/url"
|
|
|
|
|
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"
|
2019-02-08 04:04:22 +00:00
|
|
|
"golang.org/x/net/context"
|
2019-08-30 18:33:25 +00:00
|
|
|
pb "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
|
2019-02-08 04:04:22 +00:00
|
|
|
)
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
var runtimeAttachCommand = &cli.Command{
|
2019-02-08 04:04:22 +00:00
|
|
|
Name: "attach",
|
|
|
|
Usage: "Attach to a running container",
|
|
|
|
ArgsUsage: "CONTAINER-ID",
|
|
|
|
UseShortOptionHandling: true,
|
|
|
|
Flags: []cli.Flag{
|
2020-03-26 21:07:15 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "tty",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Usage: "Allocate a pseudo-TTY",
|
2019-02-08 04:04:22 +00:00
|
|
|
},
|
2020-03-26 21:07:15 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "stdin",
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
Usage: "Keep STDIN open",
|
2019-02-08 04:04:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
|
|
|
return cli.ShowSubcommandHelp(context)
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
runtimeClient, conn, err := getRuntimeClient(context)
|
|
|
|
if err != nil {
|
2019-02-08 04:04:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
defer closeConnection(context, conn)
|
2019-02-08 04:04:22 +00:00
|
|
|
|
|
|
|
var opts = attachOptions{
|
|
|
|
id: id,
|
|
|
|
tty: context.Bool("tty"),
|
|
|
|
stdin: context.Bool("stdin"),
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
err = Attach(runtimeClient, opts)
|
2019-02-08 04:04:22 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return errors.Wrap(err, "attaching running container failed")
|
2019-02-08 04:04:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach sends an AttachRequest to server, and parses the returned AttachResponse
|
|
|
|
func Attach(client pb.RuntimeServiceClient, opts attachOptions) error {
|
|
|
|
if opts.id == "" {
|
|
|
|
return fmt.Errorf("ID cannot be empty")
|
|
|
|
|
|
|
|
}
|
|
|
|
request := &pb.AttachRequest{
|
|
|
|
ContainerId: opts.id,
|
|
|
|
Tty: opts.tty,
|
|
|
|
Stdin: opts.stdin,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: !opts.tty,
|
|
|
|
}
|
|
|
|
logrus.Debugf("AttachRequest: %v", request)
|
|
|
|
r, err := client.Attach(context.Background(), request)
|
|
|
|
logrus.Debugf("AttachResponse: %v", r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attachURL := r.Url
|
|
|
|
|
|
|
|
URL, err := url.Parse(attachURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if URL.Host == "" {
|
|
|
|
URL.Host = kubeletURLHost
|
|
|
|
}
|
|
|
|
if URL.Scheme == "" {
|
|
|
|
URL.Scheme = kubeletURLSchema
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Attach URL: %v", URL)
|
|
|
|
return stream(opts.stdin, opts.tty, URL)
|
|
|
|
}
|