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

Support the ArtifactModule for authorized pull images #1021

Merged
merged 1 commit into from
Jan 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion apis/kubekey/v1alpha2/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ type RegistryConfig struct {
InsecureRegistries []string `yaml:"insecureRegistries" json:"insecureRegistries,omitempty"`
PrivateRegistry string `yaml:"privateRegistry" json:"privateRegistry,omitempty"`
PlainHTTP bool `yaml:"plainHTTP" json:"plainHTTP,omitempty"`
Auths runtime.RawExtension `yaml:"Auths" json:"Auths,omitempty"`
Auths runtime.RawExtension `yaml:"auths" json:"auths,omitempty"`
}

// KubeSphere defines the configuration information of the KubeSphere.
Expand Down
6 changes: 6 additions & 0 deletions apis/kubekey/v1alpha2/manifest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha2

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -94,6 +95,10 @@ type Components struct {
DockerCompose DockerCompose `yaml:"docker-compose" json:"docker-compose"`
}

type ManifestRegistry struct {
Auths runtime.RawExtension `yaml:"auths" json:"auths,omitempty"`
}

// ManifestSpec defines the desired state of Manifest
type ManifestSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Expand All @@ -104,6 +109,7 @@ type ManifestSpec struct {
KubernetesDistributions []KubernetesDistribution `yaml:"kubernetesDistributions" json:"kubernetesDistributions"`
Components Components `yaml:"components" json:"components"`
Images []string `yaml:"images" json:"images"`
ManifestRegistry ManifestRegistry `yaml:"registry" json:"registry"`
}

// ManifestStatus defines the observed state of Manifest
Expand Down
17 changes: 17 additions & 0 deletions apis/kubekey/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/crd/bases/kubekey.kubesphere.io_manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,18 @@ spec:
- version
type: object
type: array
registry:
properties:
auths:
type: object
type: object
required:
- arches
- components
- images
- kubernetesDistributions
- operationSystems
- registry
type: object
status:
description: ManifestStatus defines the observed state of Manifest
Expand Down
35 changes: 34 additions & 1 deletion pkg/artifact/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,33 @@ package artifact
import (
"context"
"crypto/tls"
"encoding/json"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
"github.com/containerd/containerd/remotes/docker/config"
"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/core/logger"
)

// PushTracker returns a new InMemoryTracker which tracks the ref status
var PushTracker = docker.NewInMemoryTracker()

func GetResolver(ctx context.Context) remotes.Resolver {
func GetResolver(ctx context.Context, auth registryAuth) remotes.Resolver {
username := auth.Username
secret := auth.Password

options := docker.ResolverOptions{
Tracker: PushTracker,
}

hostOptions := config.HostOptions{}
hostOptions.Credentials = func(host string) (string, string, error) {
return username, secret, nil
}

if auth.PlainHTTP {
hostOptions.DefaultScheme = "http"
}

defaultConfig := &tls.Config{
InsecureSkipVerify: true,
Expand All @@ -43,3 +56,23 @@ func GetResolver(ctx context.Context) remotes.Resolver {
options.Hosts = config.ConfigureHosts(ctx, hostOptions)
return docker.NewResolver(options)
}

type registryAuth struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
PlainHTTP bool `json:"plainHTTP,omitempty"`
}

func Auths(manifest *common.ArtifactManifest) (auths map[string]registryAuth) {
if len(manifest.Spec.ManifestRegistry.Auths.Raw) == 0 {
return
}

err := json.Unmarshal(manifest.Spec.ManifestRegistry.Auths.Raw, &auths)
if err != nil {
logger.Log.Fatal("Failed to Parse Registry Auths configuration: %v", manifest.Spec.ManifestRegistry.Auths.Raw)
return
}

return
}
19 changes: 16 additions & 3 deletions pkg/artifact/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes"
"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/core/connector"
"github.com/kubesphere/kubekey/pkg/core/logger"
Expand Down Expand Up @@ -62,9 +63,17 @@ func (p *PullImages) Execute(_ connector.Runtime) error {
if !ok {
return errors.New("get containerd client failed by pipeline client")
}
client := c.(*containerd.Client)

client := c.(*containerd.Client)
ctx := namespaces.WithNamespace(context.Background(), "kubekey")

// k: registry name "registry-1.docker.io", v: registryAuth
auths := Auths(p.Manifest)
resolvers := make(map[string]remotes.Resolver)
for repo, auth := range auths {
resolvers[repo] = GetResolver(ctx, auth)
}

for _, image := range p.Manifest.Spec.Images {
progress := make(chan struct{})
lctx, done, err := client.WithLease(ctx)
Expand All @@ -89,11 +98,16 @@ func (p *PullImages) Execute(_ connector.Runtime) error {
})

opts := []containerd.RemoteOpt{
containerd.WithResolver(GetResolver(ctx)),
containerd.WithImageHandler(h),
containerd.WithSchema1Conversion,
}

if resolver, ok := resolvers[strings.Split(image, "/")[0]]; ok {
opts = append(opts, containerd.WithResolver(resolver))
} else {
opts = append(opts, containerd.WithResolver(GetResolver(ctx, registryAuth{})))
}

for _, arch := range p.Manifest.Spec.Arches {
opts = append(opts, containerd.WithPlatform(arch))
}
Expand All @@ -106,7 +120,6 @@ func (p *PullImages) Execute(_ connector.Runtime) error {
}

<-progress
//todo: Whether it need to be unpack?
}

return nil
Expand Down
16 changes: 9 additions & 7 deletions pkg/artifact/templates/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ spec:
- arch: {{ $v.Arch }}
type: {{ $v.Type }}
id: {{ $v.Id }}
version: {{ $v.Version }}
version: "{{ $v.Version }}"
osImage: {{ $v.OsImage }}
repository:
iso:
Expand Down Expand Up @@ -69,16 +69,18 @@ spec:
crictl:
version: {{ .Options.Components.Crictl.Version }}
##
#docker-registry:
# version: 2
#harbor:
# version: v2.4.1
#docker-compose:
# version: v2.2.2
# docker-registry:
# version: "2"
# harbor:
# version: v2.4.1
# docker-compose:
# version: v2.2.2
images:
{{- range .Options.Images }}
- {{ . }}
{{- end }}
registry:
auths: {}

`)))

Expand Down
12 changes: 9 additions & 3 deletions pkg/common/artifact_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package common

import (
"encoding/json"
kubekeyv1alpha2 "github.com/kubesphere/kubekey/apis/kubekey/v1alpha2"
"github.com/kubesphere/kubekey/pkg/core/connector"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"io/ioutil"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"path/filepath"
)

Expand Down Expand Up @@ -56,9 +57,14 @@ func NewArtifactRuntime(arg ArtifactArgument) (*ArtifactRuntime, error) {
return nil, errors.Wrapf(err, "Failed to read file %s", fp)
}

contentToJson, err := k8syaml.ToJSON(fileByte)
if err != nil {
return nil, errors.Wrap(err, "Unable to convert configuration to json")
}

manifest := &kubekeyv1alpha2.Manifest{}
if err := yaml.Unmarshal(fileByte, manifest); err != nil {
return nil, errors.Wrapf(err, "Failed to yaml unmarshal")
if err := json.Unmarshal(contentToJson, manifest); err != nil {
return nil, errors.Wrapf(err, "Failed to json unmarshal")
}

r := &ArtifactRuntime{
Expand Down
3 changes: 2 additions & 1 deletion pkg/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ func CmdPush(fileName string, prePath string, kubeConf *common.KubeConf, arches
pushCmd = fmt.Sprintf("%s --user %s:%s", pushCmd, auth.Username, auth.Password)
}

logger.Log.Debugf("push command: %s", pushCmd)
if out, err := exec.Command("/bin/bash", "-c", pushCmd).CombinedOutput(); err != nil {
return errors.Wrapf(err, "push image %s failed: %s", oldName, out)
}

logger.Log.Messagef(common.LocalHost, "Push %s success", image.ImageName())
fmt.Printf("push %s success \n", image.ImageName())
return nil
}