提交 4a0fb0ee 编写于 作者: P Priya Wadhwa

Add separate makefile target for preloaded images

and convert shell script to go script
上级 85ade71b
......@@ -502,13 +502,19 @@ storage-provisioner-image: out/storage-provisioner-$(GOARCH) ## Build storage-pr
docker build -t $(STORAGE_PROVISIONER_IMAGE) -f deploy/storage-provisioner/Dockerfile --build-arg arch=$(GOARCH) .
.PHONY: kic-base-image
kic-base-image: generate-preloaded-images-tar ## builds the base image used for kic.
kic-base-image: ## builds the base image used for kic.
docker rmi -f $(REGISTRY)/kicbase:v0.0.5-snapshot || true
docker build -f ./hack/images/kicbase.Dockerfile -t $(REGISTRY)/kicbase:v0.0.5-snapshot --build-arg COMMIT_SHA=${VERSION}-$(COMMIT) .
.PHONY: kic-preloaded-base-image
kic-preloaded-base-image: generate-preloaded-images-tar ## builds the base image used for kic.
docker rmi -f $(REGISTRY)/kicbase:v0.0.5-k8s-${KUBERNETES_VERSION} || true
docker build -f ./hack/images/kicbase.Dockerfile -t $(REGISTRY)/kicbase:v0.0.5-k8s-${KUBERNETES_VERSION} --build-arg COMMIT_SHA=${VERSION}-$(COMMIT) --build-arg KUBERNETES_VERSION=${KUBERNETES_VERSION} .
.PHONY: generate-preloaded-images-tar
generate-preloaded-images-tar: out/minikube
KUBERNETES_VERSION=${KUBERNETES_VERSION} ./hack/preload-images/generate-preloaded-images-tar.sh
KUBERNETES_VERSION=${KUBERNETES_VERSION} go run ./hack/preload-images/preload_images.go
.PHONY: push-storage-provisioner-image
......
#!/bin/bash
# Copyright 2016 The Kubernetes Authors All rights reserved.
#
# 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 -ex
PROFILE=generate-preloaded-images-tar
KUBERNETES_VERSION=${KUBERNETES_VERSION:-""}
TARBALL_FILENAME=preloaded-images-k8s-$KUBERNETES_VERSION.tar
function delete_minikube {
out/minikube delete --profile=$PROFILE
}
trap "delete_minikube" ERR
out/minikube start --memory=10000 --profile=$PROFILE --kubernetes-version=$KUBERNETES_VERSION
out/minikube ssh --profile=$PROFILE -- sudo tar cvf $TARBALL_FILENAME /var/lib/docker
scp -o StrictHostKeyChecking=no -i $(out/minikube ssh-key --profile=$PROFILE) docker@$(out/minikube ip --profile=$PROFILE):/home/docker/$TARBALL_FILENAME out/$TARBALL_FILENAME
delete_minikube
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
const (
profile = "generate-preloaded-images-tar"
minikubePath = "out/minikube"
)
var (
kubernetesVersion = ""
tarballFilename = ""
)
func init() {
if kv := os.Getenv("KUBERNETES_VERSION"); kv != "" {
kubernetesVersion = kv
} else {
fmt.Println("Please pass in kubernetes version via the KUBERNETES_VERSION environment variable")
os.Exit(1)
}
tarballFilename = fmt.Sprintf("preloaded-images-k8s-%s.tar", kubernetesVersion)
}
func main() {
if err := executePreloadImages(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func executePreloadImages() error {
defer deleteMinikube()
if err := startMinikube(); err != nil {
return err
}
if err := createImageTarball(); err != nil {
return err
}
return copyTarballToHost()
}
func startMinikube() error {
cmd := exec.Command(minikubePath, "start", "-p", profile, "--memory", "10000", "--kubernetes-version", kubernetesVersion)
cmd.Stdout = os.Stdout
return cmd.Run()
}
func createImageTarball() error {
cmd := exec.Command(minikubePath, "ssh", "-p", profile, "--", "sudo", "tar", "cvf", tarballFilename, "/var/lib/docker")
cmd.Stdout = os.Stdout
return cmd.Run()
}
func copyTarballToHost() error {
sshKey, err := runCmdCaptureStdout([]string{minikubePath, "ssh-key", "-p", profile})
if err != nil {
return errors.Wrap(err, "getting ssh-key")
}
ip, err := runCmdCaptureStdout([]string{minikubePath, "ip", "-p", profile})
if err != nil {
return errors.Wrap(err, "getting ip")
}
dest := filepath.Join("out/", tarballFilename)
args := fmt.Sprintf("scp -o StrictHostKeyChecking=no -i %s docker@%s:/home/docker/%s %s", sshKey, ip, tarballFilename, dest)
_, err = runCmdCaptureStdout(strings.Split(args, " "))
return err
}
func deleteMinikube() error {
cmd := exec.Command(minikubePath, "delete", "-p", profile)
cmd.Stdout = os.Stdout
return cmd.Run()
}
func runCmdCaptureStdout(command []string) (string, error) {
cmd := exec.Command(command[0], command[1:]...)
buf := bytes.NewBuffer([]byte{})
cmd.Stdout = buf
if err := cmd.Run(); err != nil {
return "", err
}
stdout, err := ioutil.ReadAll(buf)
if err != nil {
return "", err
}
return strings.Trim(string(stdout), "\n "), nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册