From a360a4cbb7bb8017369acb38442a2a67f6a1c480 Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Sat, 15 Jun 2024 09:03:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=95=9C=E5=83=8F=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E6=9C=BA=E5=99=A8=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practise/image-practise/image/image.go | 16 +++++++++++++--- practise/image-practise/main.go | 15 +++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/practise/image-practise/image/image.go b/practise/image-practise/image/image.go index eeacc8d..859068a 100644 --- a/practise/image-practise/image/image.go +++ b/practise/image-practise/image/image.go @@ -36,7 +36,9 @@ type KubeadmImage struct { type Image struct { KubernetesVersion string - ImageRepository string + + Harbor string + ImageRepository string User string Password string @@ -97,7 +99,9 @@ func (img *Image) Complete() error { img.exec = exec.New() if img.Cfg.Default.PushKubernetes { - cmd := []string{"sudo", "apt-get", "install", "-y", fmt.Sprintf("kubeadm=%s-00", img.Cfg.Kubernetes.Version[1:])} + //cmd := []string{"sudo", "apt-get", "install", "-y", fmt.Sprintf("kubeadm=%s-00", img.Cfg.Kubernetes.Version[1:])} + cmd := []string{"sudo", "curl", "-LO", fmt.Sprintf("https://dl.k8s.io/release/%s/bin/linux/amd64/kubeadmt", img.Cfg.Kubernetes.Version)} + klog.Infof("Starting install kubeadm", cmd) out, err := img.exec.Command(cmd[0], cmd[1:]...).CombinedOutput() if err != nil { return fmt.Errorf("failed to install kubeadm %v %v", string(out), err) @@ -176,7 +180,10 @@ func (img *Image) parseTargetImage(imageToPush string) (string, error) { return "", fmt.Errorf("invaild image format: %s", imageToPush) } - return img.ImageRepository + "/" + parts[len(parts)-1], nil + if img.Harbor == "" { + return img.ImageRepository + "/" + parts[len(parts)-1], nil + } + return img.Harbor + "/" + img.ImageRepository + "/" + parts[len(parts)-1], nil } func (img *Image) doPushImage(imageToPush string) error { @@ -253,6 +260,9 @@ func (img *Image) PushImages() error { // 登陆 cmd := []string{"docker", "login", "-u", img.User, "-p", img.Password} + if img.Harbor != "" { + cmd = append(cmd, img.Harbor) + } out, err := img.exec.Command(cmd[0], cmd[1:]...).CombinedOutput() if err != nil { return fmt.Errorf("failed to login in image %v %v", string(out), err) diff --git a/practise/image-practise/main.go b/practise/image-practise/main.go index b8d0fb3..66ad8b6 100644 --- a/practise/image-practise/main.go +++ b/practise/image-practise/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "github.com/caoyingjunz/pixiulib/config" "k8s.io/klog/v2" @@ -10,8 +9,8 @@ import ( ) var ( - kubernetesVersion = flag.String("kubernetes-version", "", "Choose a specific Kubernetes version for the control plane") - imageRepository = flag.String("image-repository", "pixiuio", "Choose a container registry to push (default pixiuio") + harbor = flag.String("harbor", "harbor.cloud.pixiuio.com", "Choose a harbor to push (default harbor.cloud.pixiuio.com") + imageRepository = flag.String("image-repository", "pixiuio", "Choose a container registry to push (default pixiuio") user = flag.String("user", "", "docker register user") password = flag.String("password", "", "docker register password") @@ -33,11 +32,11 @@ func main() { } img := image.Image{ - ImageRepository: *imageRepository, - KubernetesVersion: *kubernetesVersion, - User: *user, - Password: *password, - Cfg: cfg, + Harbor: *harbor, + ImageRepository: *imageRepository, + User: *user, + Password: *password, + Cfg: cfg, } if err := img.Complete(); err != nil {