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

Commit

Permalink
Add image load.
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Liu <lantaol@google.com>
  • Loading branch information
Random-Liu committed Oct 27, 2017
1 parent c6fd18d commit 25fdf72
Show file tree
Hide file tree
Showing 20 changed files with 1,316 additions and 104 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ help:
@echo " * 'test-e2e-node' - Test cri-containerd with Kubernetes node e2e test"
@echo " * 'clean' - Clean artifacts"
@echo " * 'verify' - Execute the source code verification tools"
@echo " * 'proto' - Update protobuf of cri-containerd api"
@echo " * 'install.tools' - Install tools used by verify"
@echo " * 'install.deps' - Install dependencies of cri-containerd (containerd, runc, cni) Note: BUILDTAGS defaults to 'seccomp apparmor' for runc build"
@echo " * 'uninstall' - Remove installed binaries from system locations"
Expand Down Expand Up @@ -117,6 +118,9 @@ release: $(BUILD_DIR)/$(TARBALL)
push: $(BUILD_DIR)/$(TARBALL)
@BUILD_DIR=$(BUILD_DIR) TARBALL=$(TARBALL) VERSION=$(VERSION) ./hack/push.sh

proto:
@hack/update-proto.sh

.PHONY: install.deps

install.deps:
Expand Down Expand Up @@ -160,4 +164,5 @@ install.tools: .install.gitvalidation .install.gometalinter
test-cri \
test-e2e-node \
uninstall \
version
version \
proto
47 changes: 42 additions & 5 deletions cmd/cri-containerd/cri_containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ package main

import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/docker/docker/pkg/reexec"
"github.com/golang/glog"
"github.com/opencontainers/selinux/go-selinux"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"k8s.io/kubernetes/pkg/util/interrupt"

"github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options"
api "github.com/kubernetes-incubator/cri-containerd/pkg/api/v1"
"github.com/kubernetes-incubator/cri-containerd/pkg/client"
"github.com/kubernetes-incubator/cri-containerd/pkg/server"
"github.com/kubernetes-incubator/cri-containerd/pkg/version"
)
Expand Down Expand Up @@ -72,6 +77,35 @@ func versionCommand() *cobra.Command {
}
}

func loadImageCommand() *cobra.Command {
c := &cobra.Command{
Use: "load TAR",
Short: "Load an image from a tar archive.",
Args: cobra.ExactArgs(1),
}
endpoint, timeout := options.AddGRPCFlags(c.Flags())
c.RunE = func(cmd *cobra.Command, args []string) error {
cl, err := client.NewCRIContainerdClient(*endpoint, *timeout)
if err != nil {
return fmt.Errorf("failed to create grpc client: %v", err)
}
path, err := filepath.Abs(args[0])
if err != nil {
return fmt.Errorf("failed to get absolute path: %v", err)
}
res, err := cl.LoadImage(context.Background(), &api.LoadImageRequest{FilePath: path})
if err != nil {
return fmt.Errorf("failed to load image: %v", err)
}
images := res.GetImages()
for _, image := range images {
fmt.Println("Loaded image:", image)
}
return nil
}
return c
}

func main() {
if reexec.Init() {
return
Expand All @@ -81,10 +115,11 @@ func main() {
o.AddFlags(cmd.Flags())
cmd.AddCommand(defaultConfigCommand())
cmd.AddCommand(versionCommand())
cmd.AddCommand(loadImageCommand())

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

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

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

Expand Down
10 changes: 10 additions & 0 deletions cmd/cri-containerd/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package options
import (
"fmt"
"os"
"time"

"github.com/BurntSushi/toml"
"github.com/containerd/containerd"
Expand All @@ -30,6 +31,8 @@ const (
configFilePathArgName = "config"
// defaultConfigFilePath is the default config file path.
defaultConfigFilePath = "/etc/cri-containerd/config.toml"
// connectionTimeout is the grpc connection timeout.
connectionTimeout = 10 * time.Second
)

// ContainerdConfig contains config related to containerd
Expand Down Expand Up @@ -178,6 +181,13 @@ func PrintDefaultTomlConfig() {
}
}

// AddGRPCFlags add flags for grpc connection.
func AddGRPCFlags(fs *pflag.FlagSet) (*string, *time.Duration) {
endpoint := fs.String("endpoint", defaultConfig().SocketPath, "cri-containerd endpoint.")
timeout := fs.Duration("timeout", connectionTimeout, "cri-containerd connection timeout.")
return endpoint, timeout
}

// defaultConfig returns default configurations of cri-containerd.
func defaultConfig() Config {
return Config{
Expand Down
45 changes: 45 additions & 0 deletions hack/update-proto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# 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.

set -o errexit
set -o nounset
set -o pipefail

ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
API_ROOT="${ROOT}/pkg/api/v1"

go get k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo
if ! which protoc-gen-gogo >/dev/null; then
echo "GOPATH is not in PATH"
exit 1
fi

function cleanup {
rm -f ${API_ROOT}/api.pb.go.bak
}

trap cleanup EXIT

protoc \
--proto_path="${API_ROOT}" \
--proto_path="${ROOT}/vendor" \
--gogo_out=plugins=grpc:${API_ROOT} ${API_ROOT}/api.proto

# Update boilerplate for the generated file.
echo "$(cat hack/boilerplate/boilerplate.go.txt ${API_ROOT}/api.pb.go)" > ${API_ROOT}/api.pb.go
sed -i".bak" "s/Copyright YEAR/Copyright $(date '+%Y')/g" ${API_ROOT}/api.pb.go

gofmt -l -s -w ${API_ROOT}/api.pb.go
2 changes: 1 addition & 1 deletion hack/verify-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set -o errexit
set -o nounset
set -o pipefail

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

0 comments on commit 25fdf72

Please sign in to comment.