Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化镜像推送机器人 #107

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 7 additions & 7 deletions practise/image-practise/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,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")
Expand All @@ -33,11 +33,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
Loading