From b2b2aea237a80a755b6c4e398e287258d9c8ddf1 Mon Sep 17 00:00:00 2001
From: cuisongliu <cuisongliu@qq.com>
Date: Wed, 27 Apr 2022 01:14:00 +0800
Subject: [PATCH] feature(main): support auth build (#954)

* feature(main): sealos run repeat exec

* feature(main): support auth build

* feature(main): support auth build

* feature(main): support auth build
---
 cmd/sealos/cmd/prune.go       |  2 +-
 cmd/sealos/cmd/tag.go         |  2 +-
 pkg/apply/processor/create.go |  2 +-
 pkg/image/binary/image.go     |  7 +++++--
 pkg/registry/save.go          |  2 +-
 pkg/registry/util.go          | 21 +++++++++++++++++++++
 6 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/cmd/sealos/cmd/prune.go b/cmd/sealos/cmd/prune.go
index a1054dcd..e064667f 100644
--- a/cmd/sealos/cmd/prune.go
+++ b/cmd/sealos/cmd/prune.go
@@ -24,7 +24,7 @@ func newPruneCmd() *cobra.Command {
 		Use:     "prune",
 		Short:   "prune  image ",
 		Example: `sealos prune`,
-		Args:    cobra.ExactArgs(2),
+		Args:    cobra.NoArgs,
 		RunE: func(cmd *cobra.Command, args []string) error {
 			registrySvc, err := image.NewImageService()
 			if err != nil {
diff --git a/cmd/sealos/cmd/tag.go b/cmd/sealos/cmd/tag.go
index 1e68dc1d..800028fb 100644
--- a/cmd/sealos/cmd/tag.go
+++ b/cmd/sealos/cmd/tag.go
@@ -24,7 +24,7 @@ func newTagCmd() *cobra.Command {
 		Use:     "tag",
 		Short:   "tag a image as a new one",
 		Example: `sealos tag localhost/oci-kubernetes:1.22.8 registry.cn-hongkong.aliyuncs.com/sealyun/oci-kubernetes:1.22.8`,
-		Args:    cobra.NoArgs,
+		Args:    cobra.ExactArgs(2),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			registrySvc, err := image.NewImageService()
 			if err != nil {
diff --git a/pkg/apply/processor/create.go b/pkg/apply/processor/create.go
index cb533778..8b4389ea 100644
--- a/pkg/apply/processor/create.go
+++ b/pkg/apply/processor/create.go
@@ -135,7 +135,7 @@ func (c *CreateProcessor) Join(cluster *v2.Cluster) error {
 }
 
 func (c *CreateProcessor) RunGuest(cluster *v2.Cluster) error {
-	return c.Guest.Apply(cluster, nil)
+	return c.Guest.Apply(cluster, cluster.Spec.Image)
 }
 
 func NewCreateProcessor(clusterFile clusterfile.Interface) (Interface, error) {
diff --git a/pkg/image/binary/image.go b/pkg/image/binary/image.go
index 44089d71..943af8bf 100644
--- a/pkg/image/binary/image.go
+++ b/pkg/image/binary/image.go
@@ -122,8 +122,11 @@ func (d *ImageService) Build(options *types.BuildOptions, contextDir, imageName
 		return errors.Wrap(err, "load images list failed in this context")
 	}
 	images = append(images, yamlImages...)
-	//TODO add auth
-	is := registry.NewImageSaver(context.Background(), nil)
+	auths, err := registry.GetAuthInfo()
+	if err != nil {
+		return err
+	}
+	is := registry.NewImageSaver(context.Background(), auths)
 	platform := strings.Split(options.Platform, "/")
 	var platformVar v1.Platform
 	if len(platform) > 2 {
diff --git a/pkg/registry/save.go b/pkg/registry/save.go
index 2c20243e..8d0c7a72 100644
--- a/pkg/registry/save.go
+++ b/pkg/registry/save.go
@@ -228,7 +228,7 @@ func (is *DefaultImageSaver) saveManifestAndGetDigest(nameds []Named, repo distr
 
 			desc, err := repo.Tags(is.ctx).Get(is.ctx, tmpnamed.tag)
 			if err != nil {
-				return fmt.Errorf("get %s tag descriptor error: %v, try \"docker login\" if you are using a private registry", tmpnamed.repo, err)
+				return fmt.Errorf("get %s tag descriptor error: %v, try \"sealos login\" if you are using a private registry", tmpnamed.repo, err)
 			}
 			imageDigest, err := is.handleManifest(manifest, desc.Digest, platform)
 			if err != nil {
diff --git a/pkg/registry/util.go b/pkg/registry/util.go
index 4946dd9d..44d55c56 100644
--- a/pkg/registry/util.go
+++ b/pkg/registry/util.go
@@ -17,6 +17,10 @@ package registry
 import (
 	"fmt"
 	"strings"
+
+	"github.com/docker/docker/api/types"
+	fileutil "github.com/fanux/sealos/pkg/utils/file"
+	"k8s.io/apimachinery/pkg/util/json"
 )
 
 //this package contains some utils to handle docker image name
@@ -87,3 +91,20 @@ func ParseNormalizedNamed(s string) (Named, error) {
 	}
 	return named, nil
 }
+
+func GetAuthInfo() (map[string]types.AuthConfig, error) {
+	authFile := "/run/user/0/containers/auth.json"
+	type auths struct {
+		Auths map[string]types.AuthConfig `json:"auths"`
+	}
+	aus := &auths{}
+	data, err := fileutil.ReadAll(authFile)
+	if err != nil {
+		return nil, err
+	}
+	err = json.Unmarshal(data, aus)
+	if err != nil {
+		return nil, err
+	}
+	return aus.Auths, nil
+}
-- 
GitLab