forked from kubernetes-sigs/cri-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattach.go
119 lines (102 loc) · 2.81 KB
/
attach.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
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 main
import (
"context"
"fmt"
"net/url"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
internalapi "k8s.io/cri-api/pkg/apis"
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
)
var runtimeAttachCommand = &cli.Command{
Name: "attach",
Usage: "Attach to a running container",
ArgsUsage: "CONTAINER-ID",
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "tty",
Aliases: []string{"t"},
Usage: "Allocate a pseudo-TTY",
},
&cli.BoolFlag{
Name: "stdin",
Aliases: []string{"i"},
Usage: "Keep STDIN open",
},
&cli.StringFlag{
Name: transportFlag,
Aliases: []string{"r"},
Value: transportSpdy,
Usage: fmt.Sprintf("Transport protocol to be used, must be one of: %s, %s", transportSpdy, transportWebsocket),
},
},
Action: func(c *cli.Context) error {
id := c.Args().First()
if id == "" {
return fmt.Errorf("ID cannot be empty")
}
if c.NArg() != 1 {
return cli.ShowSubcommandHelp(c)
}
runtimeClient, err := getRuntimeService(c, 0)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(c.Context)
defer cancel()
var opts = attachOptions{
id: id,
tty: c.Bool("tty"),
stdin: c.Bool("stdin"),
}
if err = Attach(ctx, runtimeClient, opts); err != nil {
return fmt.Errorf("attaching running container failed: %w", err)
}
return nil
},
}
// Attach sends an AttachRequest to server, and parses the returned AttachResponse
func Attach(ctx context.Context, client internalapi.RuntimeService, 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(ctx, 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(ctx, opts.stdin, opts.tty, opts.transport, URL)
}