Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 25fdf72

Browse files
committed
Add image load.
Signed-off-by: Lantao Liu <[email protected]>
1 parent c6fd18d commit 25fdf72

20 files changed

+1316
-104
lines changed

Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ help:
4848
@echo " * 'test-e2e-node' - Test cri-containerd with Kubernetes node e2e test"
4949
@echo " * 'clean' - Clean artifacts"
5050
@echo " * 'verify' - Execute the source code verification tools"
51+
@echo " * 'proto' - Update protobuf of cri-containerd api"
5152
@echo " * 'install.tools' - Install tools used by verify"
5253
@echo " * 'install.deps' - Install dependencies of cri-containerd (containerd, runc, cni) Note: BUILDTAGS defaults to 'seccomp apparmor' for runc build"
5354
@echo " * 'uninstall' - Remove installed binaries from system locations"
@@ -117,6 +118,9 @@ release: $(BUILD_DIR)/$(TARBALL)
117118
push: $(BUILD_DIR)/$(TARBALL)
118119
@BUILD_DIR=$(BUILD_DIR) TARBALL=$(TARBALL) VERSION=$(VERSION) ./hack/push.sh
119120

121+
proto:
122+
@hack/update-proto.sh
123+
120124
.PHONY: install.deps
121125

122126
install.deps:
@@ -160,4 +164,5 @@ install.tools: .install.gitvalidation .install.gometalinter
160164
test-cri \
161165
test-e2e-node \
162166
uninstall \
163-
version
167+
version \
168+
proto

cmd/cri-containerd/cri_containerd.go

+42-5
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@ package main
1818

1919
import (
2020
"flag"
21+
"fmt"
2122
"os"
23+
"path/filepath"
2224

2325
"github.com/docker/docker/pkg/reexec"
2426
"github.com/golang/glog"
2527
"github.com/opencontainers/selinux/go-selinux"
2628
"github.com/spf13/cobra"
29+
"golang.org/x/net/context"
2730
"k8s.io/kubernetes/pkg/util/interrupt"
2831

2932
"github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options"
33+
api "github.com/kubernetes-incubator/cri-containerd/pkg/api/v1"
34+
"github.com/kubernetes-incubator/cri-containerd/pkg/client"
3035
"github.com/kubernetes-incubator/cri-containerd/pkg/server"
3136
"github.com/kubernetes-incubator/cri-containerd/pkg/version"
3237
)
@@ -72,6 +77,35 @@ func versionCommand() *cobra.Command {
7277
}
7378
}
7479

80+
func loadImageCommand() *cobra.Command {
81+
c := &cobra.Command{
82+
Use: "load TAR",
83+
Short: "Load an image from a tar archive.",
84+
Args: cobra.ExactArgs(1),
85+
}
86+
endpoint, timeout := options.AddGRPCFlags(c.Flags())
87+
c.RunE = func(cmd *cobra.Command, args []string) error {
88+
cl, err := client.NewCRIContainerdClient(*endpoint, *timeout)
89+
if err != nil {
90+
return fmt.Errorf("failed to create grpc client: %v", err)
91+
}
92+
path, err := filepath.Abs(args[0])
93+
if err != nil {
94+
return fmt.Errorf("failed to get absolute path: %v", err)
95+
}
96+
res, err := cl.LoadImage(context.Background(), &api.LoadImageRequest{FilePath: path})
97+
if err != nil {
98+
return fmt.Errorf("failed to load image: %v", err)
99+
}
100+
images := res.GetImages()
101+
for _, image := range images {
102+
fmt.Println("Loaded image:", image)
103+
}
104+
return nil
105+
}
106+
return c
107+
}
108+
75109
func main() {
76110
if reexec.Init() {
77111
return
@@ -81,10 +115,11 @@ func main() {
81115
o.AddFlags(cmd.Flags())
82116
cmd.AddCommand(defaultConfigCommand())
83117
cmd.AddCommand(versionCommand())
118+
cmd.AddCommand(loadImageCommand())
84119

85-
cmd.Run = func(cmd *cobra.Command, args []string) {
120+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
86121
if err := o.InitFlags(cmd.Flags()); err != nil {
87-
glog.Exitf("Failed to init CRI containerd flags: %v", err)
122+
return fmt.Errorf("failed to init CRI containerd flags: %v", err)
88123
}
89124
validateConfig(o)
90125

@@ -93,19 +128,21 @@ func main() {
93128
glog.V(2).Infof("Run cri-containerd grpc server on socket %q", o.SocketPath)
94129
s, err := server.NewCRIContainerdService(o.Config)
95130
if err != nil {
96-
glog.Exitf("Failed to create CRI containerd service: %v", err)
131+
return fmt.Errorf("failed to create CRI containerd service: %v", err)
97132
}
98133
// Use interrupt handler to make sure the server is stopped properly.
99134
// Pass in non-empty final function to avoid os.Exit(1). We expect `Run`
100135
// to return itself.
101136
h := interrupt.New(func(os.Signal) {}, s.Stop)
102137
if err := h.Run(func() error { return s.Run() }); err != nil {
103-
glog.Exitf("Failed to run cri-containerd grpc server: %v", err)
138+
return fmt.Errorf("failed to run cri-containerd grpc server: %v", err)
104139
}
140+
return nil
105141
}
106142

107143
if err := cmd.Execute(); err != nil {
108-
glog.Exitf("Failed to execute cri-containerd: %v", err)
144+
// Error should have been reported.
145+
os.Exit(1)
109146
}
110147
}
111148

cmd/cri-containerd/options/options.go

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package options
1919
import (
2020
"fmt"
2121
"os"
22+
"time"
2223

2324
"github.com/BurntSushi/toml"
2425
"github.com/containerd/containerd"
@@ -30,6 +31,8 @@ const (
3031
configFilePathArgName = "config"
3132
// defaultConfigFilePath is the default config file path.
3233
defaultConfigFilePath = "/etc/cri-containerd/config.toml"
34+
// connectionTimeout is the grpc connection timeout.
35+
connectionTimeout = 10 * time.Second
3336
)
3437

3538
// ContainerdConfig contains config related to containerd
@@ -178,6 +181,13 @@ func PrintDefaultTomlConfig() {
178181
}
179182
}
180183

184+
// AddGRPCFlags add flags for grpc connection.
185+
func AddGRPCFlags(fs *pflag.FlagSet) (*string, *time.Duration) {
186+
endpoint := fs.String("endpoint", defaultConfig().SocketPath, "cri-containerd endpoint.")
187+
timeout := fs.Duration("timeout", connectionTimeout, "cri-containerd connection timeout.")
188+
return endpoint, timeout
189+
}
190+
181191
// defaultConfig returns default configurations of cri-containerd.
182192
func defaultConfig() Config {
183193
return Config{

hack/update-proto.sh

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
# Copyright 2017 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
22+
API_ROOT="${ROOT}/pkg/api/v1"
23+
24+
go get k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo
25+
if ! which protoc-gen-gogo >/dev/null; then
26+
echo "GOPATH is not in PATH"
27+
exit 1
28+
fi
29+
30+
function cleanup {
31+
rm -f ${API_ROOT}/api.pb.go.bak
32+
}
33+
34+
trap cleanup EXIT
35+
36+
protoc \
37+
--proto_path="${API_ROOT}" \
38+
--proto_path="${ROOT}/vendor" \
39+
--gogo_out=plugins=grpc:${API_ROOT} ${API_ROOT}/api.proto
40+
41+
# Update boilerplate for the generated file.
42+
echo "$(cat hack/boilerplate/boilerplate.go.txt ${API_ROOT}/api.pb.go)" > ${API_ROOT}/api.pb.go
43+
sed -i".bak" "s/Copyright YEAR/Copyright $(date '+%Y')/g" ${API_ROOT}/api.pb.go
44+
45+
gofmt -l -s -w ${API_ROOT}/api.pb.go

hack/verify-lint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ set -o errexit
1717
set -o nounset
1818
set -o pipefail
1919

20-
for d in $(find . -type d -a \( -iwholename './pkg*' -o -iwholename './cmd*' \)); do
20+
for d in $(find . -type d -a \( -iwholename './pkg*' -o -iwholename './cmd*' \) -not -iwholename './pkg/api*'); do
2121
echo for directory ${d} ...
2222
gometalinter \
2323
--exclude='error return value not checked.*(Close|Log|Print).*\(errcheck\)$' \

0 commit comments

Comments
 (0)