forked from kubernetes-sigs/cri-tools
/
main.go
236 lines (212 loc) · 6.5 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
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 (
"fmt"
"os"
"path/filepath"
"sort"
"time"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"google.golang.org/grpc"
internalapi "k8s.io/cri-api/pkg/apis"
"k8s.io/kubernetes/pkg/kubelet/remote"
"k8s.io/kubernetes/pkg/kubelet/util"
"github.com/kubernetes-sigs/cri-tools/pkg/version"
)
const (
defaultTimeout = 2 * time.Second
)
var (
// RuntimeEndpoint is CRI server runtime endpoint
RuntimeEndpoint string
// ImageEndpoint is CRI server image endpoint, default same as runtime endpoint
ImageEndpoint string
// Timeout of connecting to server (default: 10s)
Timeout time.Duration
// Debug enable debug output
Debug bool
)
func getRuntimeClientConnection(context *cli.Context) (*grpc.ClientConn, error) {
if RuntimeEndpoint == "" {
return nil, fmt.Errorf("--runtime-endpoint is not set")
}
addr, dialer, err := util.GetAddressAndDialer(RuntimeEndpoint)
if err != nil {
return nil, err
}
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(Timeout), grpc.WithDialer(dialer))
if err != nil {
return nil, fmt.Errorf("failed to connect, make sure you are running as root and the runtime has been started: %v", err)
}
return conn, nil
}
func getImageClientConnection(context *cli.Context) (*grpc.ClientConn, error) {
if ImageEndpoint == "" {
if RuntimeEndpoint == "" {
return nil, fmt.Errorf("--image-endpoint is not set")
}
ImageEndpoint = RuntimeEndpoint
}
addr, dialer, err := util.GetAddressAndDialer(ImageEndpoint)
if err != nil {
return nil, err
}
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(Timeout), grpc.WithDialer(dialer))
if err != nil {
return nil, fmt.Errorf("failed to connect, make sure you are running as root and the runtime has been started: %v", err)
}
return conn, nil
}
func getRuntimeService(context *cli.Context) (internalapi.RuntimeService, error) {
return remote.NewRemoteRuntimeService(RuntimeEndpoint, Timeout)
}
func main() {
app := cli.NewApp()
app.Name = "crictl"
app.Usage = "client for CRI"
app.Version = version.Version
app.Commands = []cli.Command{
runtimeAttachCommand,
createContainerCommand,
runtimeExecCommand,
runtimeVersionCommand,
listImageCommand,
containerStatusCommand,
imageStatusCommand,
imageFsInfoCommand,
podStatusCommand,
logsCommand,
runtimePortForwardCommand,
listContainersCommand,
pullImageCommand,
runPodCommand,
removeContainerCommand,
removeImageCommand,
removePodCommand,
listPodCommand,
startContainerCommand,
runtimeStatusCommand,
stopContainerCommand,
stopPodCommand,
updateContainerCommand,
configCommand,
statsCommand,
completionCommand,
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
EnvVar: "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",
},
cli.StringFlag{
Name: "runtime-endpoint, r",
EnvVar: "CONTAINER_RUNTIME_ENDPOINT",
Value: defaultRuntimeEndpoint,
Usage: "Endpoint of CRI container runtime service",
},
cli.StringFlag{
Name: "image-endpoint, i",
EnvVar: "IMAGE_SERVICE_ENDPOINT",
Usage: "Endpoint of CRI image manager service",
},
cli.DurationFlag{
Name: "timeout, t",
Value: defaultTimeout,
Usage: "Timeout of connecting to the server",
},
cli.BoolFlag{
Name: "debug, D",
Usage: "Enable debug mode",
},
}
app.Before = func(context *cli.Context) error {
isUseConfig := false
configFile := context.GlobalString("config")
if _, err := os.Stat(configFile); err == nil {
isUseConfig = true
} else {
if context.IsSet("config") || !os.IsNotExist(err) {
// note: the absence of default config file is normal case
// when user have not set it in cli
logrus.Fatalf("Failed to load config file: %v", err)
} else {
// If the default config was not found, and the user didn't
// explicitly specify a config, try looking in the program's
// directory as a fallback. This is a convenience for
// deployments of crictl so they don't have to place a file in a
// global location.
configFile = filepath.Join(filepath.Dir(os.Args[0]), "crictl.yaml")
if _, err := os.Stat(configFile); err == nil {
isUseConfig = true
} else if !os.IsNotExist(err) {
logrus.Fatalf("Failed to load config file: %v", err)
}
}
}
if !isUseConfig {
RuntimeEndpoint = context.GlobalString("runtime-endpoint")
ImageEndpoint = context.GlobalString("image-endpoint")
Timeout = context.GlobalDuration("timeout")
Debug = context.GlobalBool("debug")
} else {
// Get config from file.
config, err := ReadConfig(configFile)
if err != nil {
logrus.Fatalf("Falied to load config file: %v", err)
}
// Command line flags overrides config file.
if context.IsSet("runtime-endpoint") {
RuntimeEndpoint = context.String("runtime-endpoint")
} else if config.RuntimeEndpoint != "" {
RuntimeEndpoint = config.RuntimeEndpoint
} else {
RuntimeEndpoint = context.GlobalString("runtime-endpoint")
}
if context.IsSet("image-endpoint") {
ImageEndpoint = context.String("image-endpoint")
} else if config.ImageEndpoint != "" {
ImageEndpoint = config.ImageEndpoint
} else {
ImageEndpoint = context.GlobalString("image-endpoint")
}
if context.IsSet("timeout") {
Timeout = context.Duration("timeout")
} else if config.Timeout != 0 {
Timeout = time.Duration(config.Timeout) * time.Second
} else {
Timeout = context.GlobalDuration("timeout")
}
if context.IsSet("debug") {
Debug = context.GlobalBool("debug")
} else {
Debug = config.Debug
}
}
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)
}
}