Skip to content

Commit

Permalink
优化镜像推送机器人
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz committed Jun 15, 2024
1 parent 095d611 commit a360a4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
16 changes: 13 additions & 3 deletions practise/image-practise/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type KubeadmImage struct {

type Image struct {
KubernetesVersion string
ImageRepository string

Harbor string
ImageRepository string

User string
Password string
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions practise/image-practise/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package main

import (
"flag"

"github.com/caoyingjunz/pixiulib/config"
"k8s.io/klog/v2"

"go-learning/practise/image-practise/image"
)

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")
Expand All @@ -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 {
Expand Down

0 comments on commit a360a4c

Please sign in to comment.