Skip to content

Commit

Permalink
Merge pull request #921 from tanguofu/fea/registry_auths
Browse files Browse the repository at this point in the history
fea(#915): support custom private registry authorization
  • Loading branch information
ks-ci-bot committed Dec 24, 2021
2 parents 0a56d9e + 3b5ef61 commit d2f96a2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 11 deletions.
15 changes: 9 additions & 6 deletions apis/kubekey/v1alpha2/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package v1alpha2

import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/kubesphere/kubekey/pkg/core/logger"
"github.com/kubesphere/kubekey/pkg/core/util"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
"strconv"
"strings"
"k8s.io/apimachinery/pkg/runtime"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -179,9 +181,10 @@ type ControlPlaneEndpoint struct {

// RegistryConfig defines the configuration information of the image's repository.
type RegistryConfig struct {
RegistryMirrors []string `yaml:"registryMirrors" json:"registryMirrors,omitempty"`
InsecureRegistries []string `yaml:"insecureRegistries" json:"insecureRegistries,omitempty"`
PrivateRegistry string `yaml:"privateRegistry" json:"privateRegistry,omitempty"`
RegistryMirrors []string `yaml:"registryMirrors" json:"registryMirrors,omitempty"`
InsecureRegistries []string `yaml:"insecureRegistries" json:"insecureRegistries,omitempty"`
PrivateRegistry string `yaml:"privateRegistry" json:"privateRegistry,omitempty"`
Auths runtime.RawExtension `yaml:"Auths" json:"Auths,omitempty"`
}

// KubeSphere defines the configuration information of the KubeSphere.
Expand Down
1 change: 1 addition & 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 docs/config-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ spec:
registryMirrors: []
insecureRegistries: []
privateRegistry: ""
auths: # if docker add by `docker login`, if containerd append to `/etc/containerd/config.toml`
"registry-1.docker.io":
username : "xxx"
password : "***"


addons: [] # You can install cloud-native addons (Chart or YAML) by using this field.

---
Expand Down
30 changes: 28 additions & 2 deletions pkg/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package container

import (
"fmt"
"path"
"path/filepath"

"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/container/templates"
"github.com/kubesphere/kubekey/pkg/core/connector"
"github.com/kubesphere/kubekey/pkg/files"
"github.com/kubesphere/kubekey/pkg/utils"
"github.com/pkg/errors"
"path"
"path/filepath"
)

type SyncDockerBinaries struct {
Expand Down Expand Up @@ -74,3 +76,27 @@ func (e *EnableDocker) Execute(runtime connector.Runtime) error {
}
return nil
}

type DockerLoginRegistry struct {
common.KubeAction
}

func (p *DockerLoginRegistry) Execute(runtime connector.Runtime) error {

auths := templates.Auths(p.KubeConf)

for repo, entry := range auths {

cmd := fmt.Sprintf("docker login --username \"%s\" --password \"%s\" %s", entry.Username, entry.Password, repo)
if _, err := runtime.GetRunner().SudoCmd(cmd, false); err != nil {
return errors.Wrapf(err, "login registry failed, cmd: %v, err:%v", cmd, err)
}
}

cmd := "mkdir -p /.docker && cp -f $HOME/.docker/config.json /.docker/ && chmod 0644 /.docker/config.json "
if _, err := runtime.GetRunner().SudoCmd(cmd, false); err != nil {
return errors.Wrapf(err, "copy docker auths failed cmd: %v, err:%v", cmd, err)
}

return nil
}
14 changes: 14 additions & 0 deletions pkg/container/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,26 @@ func InstallDocker(m *InstallContainerModule) []task.Interface {
Parallel: true,
}

dockerLoginRegistry := &task.RemoteTask{
Name: "Login PrivateRegistry",
Desc: "Add auths to container runtime",
Hosts: m.Runtime.GetAllHosts(),
Prepare: &prepare.PrepareCollection{
&kubernetes.NodeInCluster{Not: true},
&DockerExist{},
},
Action: new(DockerLoginRegistry),
Parallel: true,
}

return []task.Interface{
syncBinaries,
generateContainerdService,
enableContainerd,
generateDockerService,
generateDockerConfig,
enableDocker,
dockerLoginRegistry,
}
}

Expand Down Expand Up @@ -212,6 +225,7 @@ func InstallContainerd(m *InstallContainerModule) []task.Interface {
"Mirrors": templates.Mirrors(m.KubeConf),
"InsecureRegistries": templates.InsecureRegistries(m.KubeConf),
"SandBoxImage": images.GetImage(m.Runtime, m.KubeConf, "pause").ImageName(),
"Auths": templates.Auths(m.KubeConf),
},
},
Parallel: true,
Expand Down
13 changes: 12 additions & 1 deletion pkg/container/templates/containerd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package templates

import (
"github.com/lithammer/dedent"
"text/template"

"github.com/lithammer/dedent"
)

var ContainerdConfig = template.Must(template.New("config.toml").Parse(
Expand Down Expand Up @@ -74,4 +75,14 @@ state = "/run/containerd"
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://registry-1.docker.io"]
{{- end}}
{{- if .Auths }}
[plugins."io.containerd.grpc.v1.cri".registry.configs]
{{- range $repo, $entry := .Auths }}
[plugins."io.containerd.grpc.v1.cri".registry.configs."{{$repo}}"]
username = "{{$entry.Username}}"
password = "{{$entry.Password}}"
{{- end}}
{{- end}}
`)))
27 changes: 25 additions & 2 deletions pkg/container/templates/docker_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package templates

import (
"encoding/json"
"fmt"
"github.com/kubesphere/kubekey/pkg/common"
"github.com/lithammer/dedent"
"strings"
"text/template"

"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/core/logger"
"github.com/lithammer/dedent"
)

var DockerConfig = template.Must(template.New("daemon.json").Parse(
Expand Down Expand Up @@ -63,3 +66,23 @@ func InsecureRegistries(kubeConf *common.KubeConf) string {
}
return insecureRegistries
}

type DockerConfigEntry struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}

func Auths(kubeConf *common.KubeConf) (auths map[string]DockerConfigEntry) {

if len(kubeConf.Cluster.Registry.Auths.Raw) == 0 {
return
}

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

return
}

0 comments on commit d2f96a2

Please sign in to comment.