Skip to content

Commit

Permalink
Creating container images registry is supported
Browse files Browse the repository at this point in the history
Signed-off-by: pixiake <guofeng@yunify.com>
  • Loading branch information
pixiake committed Jan 7, 2022
1 parent 02185cf commit e598c91
Show file tree
Hide file tree
Showing 25 changed files with 1,357 additions and 88 deletions.
71 changes: 53 additions & 18 deletions apis/kubekey/v1alpha2/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,31 @@ type HostCfg struct {
PrivateKeyPath string `yaml:"privateKeyPath,omitempty" json:"privateKeyPath,omitempty"`
Arch string `yaml:"arch,omitempty" json:"arch,omitempty"`

Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`
ID string `yaml:"id,omitempty" json:"id,omitempty"`
Index int `json:"-"`
IsEtcd bool `json:"-"`
IsMaster bool `json:"-"`
IsWorker bool `json:"-"`
Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`
ID string `yaml:"id,omitempty" json:"id,omitempty"`
Index int `json:"-"`
IsEtcd bool `json:"-"`
IsMaster bool `json:"-"`
IsWorker bool `json:"-"`
IsRegistry bool `json:"-"`
}

// RoleGroups defines the grouping of role for hosts (etcd / master / worker).
// RoleGroups defines the grouping of role for hosts (etcd / master / worker / registry).
type RoleGroups struct {
Etcd []string `yaml:"etcd" json:"etcd,omitempty"`
Master []string `yaml:"master" json:"master,omitempty"`
Worker []string `yaml:"worker" json:"worker,omitempty"`
Etcd []string `yaml:"etcd" json:"etcd,omitempty"`
Master []string `yaml:"master" json:"master,omitempty"`
Worker []string `yaml:"worker" json:"worker,omitempty"`
Registry []string `yaml:"registry" json:"registry,omitempty"`
}

// HostGroups defines the grouping of hosts for cluster (all / etcd / master / worker / k8s).
type HostGroups struct {
All []HostCfg
Etcd []HostCfg
Master []HostCfg
Worker []HostCfg
K8s []HostCfg
All []HostCfg
Etcd []HostCfg
Master []HostCfg
Worker []HostCfg
K8s []HostCfg
Registry []HostCfg
}

// ControlPlaneEndpoint defines the control plane endpoint information for cluster.
Expand All @@ -181,6 +184,7 @@ type ControlPlaneEndpoint struct {

// RegistryConfig defines the configuration information of the image's repository.
type RegistryConfig struct {
Type string `yaml:"type" json:"type,omitempty"`
RegistryMirrors []string `yaml:"registryMirrors" json:"registryMirrors,omitempty"`
InsecureRegistries []string `yaml:"insecureRegistries" json:"insecureRegistries,omitempty"`
PrivateRegistry string `yaml:"privateRegistry" json:"privateRegistry,omitempty"`
Expand Down Expand Up @@ -242,7 +246,7 @@ func (cfg *ClusterSpec) GroupHosts() (*HostGroups, error) {
hostList[host.Name] = host.Name
}

etcdGroup, masterGroup, workerGroup, err := cfg.ParseRolesList(hostList)
etcdGroup, masterGroup, workerGroup, registryGroup, err := cfg.ParseRolesList(hostList)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -275,6 +279,14 @@ func (cfg *ClusterSpec) GroupHosts() (*HostGroups, error) {
}
}

if len(registryGroup) > 0 {
for _, hostName := range registryGroup {
if hostName != "" && host.Name == hostName {
host.IsRegistry = true
}
}
}

if host.IsEtcd {
clusterHostsGroups.Etcd = append(clusterHostsGroups.Etcd, host)
}
Expand All @@ -287,6 +299,10 @@ func (cfg *ClusterSpec) GroupHosts() (*HostGroups, error) {
if host.IsMaster || host.IsWorker {
clusterHostsGroups.K8s = append(clusterHostsGroups.K8s, host)
}
if host.IsRegistry {
clusterHostsGroups.Registry = append(clusterHostsGroups.Registry, host)
}

clusterHostsGroups.All = append(clusterHostsGroups.All, host)
}

Expand All @@ -297,6 +313,9 @@ func (cfg *ClusterSpec) GroupHosts() (*HostGroups, error) {
if len(etcdGroup) == 0 {
logger.Log.Fatal(errors.New("The number of etcd cannot be 0"))
}
if len(registryGroup) > 1 {
logger.Log.Fatal(errors.New("The number of registry node cannot be greater than 1."))
}

if len(masterGroup) != len(clusterHostsGroups.Master) {
return nil, errors.New("Incorrect nodeName under roleGroups/master in the configuration file")
Expand All @@ -307,6 +326,9 @@ func (cfg *ClusterSpec) GroupHosts() (*HostGroups, error) {
if len(workerGroup) != len(clusterHostsGroups.Worker) {
return nil, errors.New("Incorrect nodeName under roleGroups/work in the configuration file")
}
if len(registryGroup) != len(clusterHostsGroups.Registry) {
return nil, errors.New("Incorrect nodeName under roleGroups/registry in the configuration file")
}

return &clusterHostsGroups, nil
}
Expand All @@ -331,10 +353,11 @@ func (cfg *ClusterSpec) ClusterDNS() string {
}

// ParseRolesList is used to parse the host grouping list.
func (cfg *ClusterSpec) ParseRolesList(hostList map[string]string) ([]string, []string, []string, error) {
func (cfg *ClusterSpec) ParseRolesList(hostList map[string]string) ([]string, []string, []string, []string, error) {
etcdGroupList := make([]string, 0)
masterGroupList := make([]string, 0)
workerGroupList := make([]string, 0)
registryGroupList := make([]string, 0)

for _, host := range cfg.RoleGroups.Etcd {
if strings.Contains(host, "[") && strings.Contains(host, "]") && strings.Contains(host, ":") {
Expand Down Expand Up @@ -368,7 +391,19 @@ func (cfg *ClusterSpec) ParseRolesList(hostList map[string]string) ([]string, []
workerGroupList = append(workerGroupList, host)
}
}
return etcdGroupList, masterGroupList, workerGroupList, nil

for _, host := range cfg.RoleGroups.Registry {
if strings.Contains(host, "[") && strings.Contains(host, "]") && strings.Contains(host, ":") {
registryGroupList = append(registryGroupList, getHostsRange(host, hostList, "registry")...)
} else {
if err := hostVerify(hostList, host, "registry"); err != nil {
logger.Log.Fatal(err)
}
registryGroupList = append(registryGroupList, host)
}
}

return etcdGroupList, masterGroupList, workerGroupList, registryGroupList, nil
}

func getHostsRange(rangeStr string, hostList map[string]string, group string) []string {
Expand Down
97 changes: 50 additions & 47 deletions apis/kubekey/v1alpha2/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,53 +24,56 @@ import (
)

const (
DefaultPreDir = "kubekey"
DefaultTmpDir = "/tmp/kubekey"
DefaultSSHPort = 22
DefaultLBPort = 6443
DefaultLBDomain = "lb.kubesphere.local"
DefaultNetworkPlugin = "calico"
DefaultPodsCIDR = "10.233.64.0/18"
DefaultServiceCIDR = "10.233.0.0/18"
DefaultKubeImageNamespace = "kubesphere"
DefaultClusterName = "cluster.local"
DefaultArch = "amd64"
DefaultEtcdVersion = "v3.4.13"
DefaultEtcdPort = "2379"
DefaultDockerVersion = "20.10.8"
DefaultCrictlVersion = "v1.22.0"
DefaultKubeVersion = "v1.21.5"
DefaultCalicoVersion = "v3.20.0"
DefaultFlannelVersion = "v0.12.0"
DefaultCniVersion = "v0.9.1"
DefaultCiliumVersion = "v1.8.3"
DefaultKubeovnVersion = "v1.5.0"
DefalutMultusVersion = "v3.8"
DefaultHelmVersion = "v3.6.3"
DefaultMaxPods = 110
DefaultNodeCidrMaskSize = 24
DefaultIPIPMode = "Always"
DefaultVXLANMode = "Never"
DefaultVethMTU = 1440
DefaultBackendMode = "vxlan"
DefaultProxyMode = "ipvs"
DefaultCrioEndpoint = "unix:///var/run/crio/crio.sock"
DefaultContainerdEndpoint = "unix:///run/containerd/containerd.sock"
DefaultIsulaEndpoint = "unix:///var/run/isulad.sock"
Etcd = "etcd"
Master = "master"
Worker = "worker"
K8s = "k8s"
DefaultEtcdBackupDir = "/var/backups/kube_etcd"
DefaultEtcdBackupPeriod = 30
DefaultKeepBackNumber = 5
DefaultEtcdBackupScriptDir = "/usr/local/bin/kube-scripts"
DefaultJoinCIDR = "100.64.0.0/16"
DefaultNetworkType = "geneve"
DefaultVlanID = "100"
DefaultOvnLabel = "node-role.kubernetes.io/master"
DefaultDPDKVersion = "19.11"
DefaultDNSAddress = "114.114.114.114"
DefaultPreDir = "kubekey"
DefaultTmpDir = "/tmp/kubekey"
DefaultSSHPort = 22
DefaultLBPort = 6443
DefaultLBDomain = "lb.kubesphere.local"
DefaultNetworkPlugin = "calico"
DefaultPodsCIDR = "10.233.64.0/18"
DefaultServiceCIDR = "10.233.0.0/18"
DefaultKubeImageNamespace = "kubesphere"
DefaultClusterName = "cluster.local"
DefaultArch = "amd64"
DefaultEtcdVersion = "v3.4.13"
DefaultEtcdPort = "2379"
DefaultDockerVersion = "20.10.8"
DefaultCrictlVersion = "v1.22.0"
DefaultKubeVersion = "v1.21.5"
DefaultCalicoVersion = "v3.20.0"
DefaultFlannelVersion = "v0.12.0"
DefaultCniVersion = "v0.9.1"
DefaultCiliumVersion = "v1.8.3"
DefaultKubeovnVersion = "v1.5.0"
DefalutMultusVersion = "v3.8"
DefaultHelmVersion = "v3.6.3"
DefaultDockerComposeVersion = "v2.2.2"
DefaultRegistryVersion = "2"
DefaultHarborVersion = "v2.4.1"
DefaultMaxPods = 110
DefaultNodeCidrMaskSize = 24
DefaultIPIPMode = "Always"
DefaultVXLANMode = "Never"
DefaultVethMTU = 1440
DefaultBackendMode = "vxlan"
DefaultProxyMode = "ipvs"
DefaultCrioEndpoint = "unix:///var/run/crio/crio.sock"
DefaultContainerdEndpoint = "unix:///run/containerd/containerd.sock"
DefaultIsulaEndpoint = "unix:///var/run/isulad.sock"
Etcd = "etcd"
Master = "master"
Worker = "worker"
K8s = "k8s"
DefaultEtcdBackupDir = "/var/backups/kube_etcd"
DefaultEtcdBackupPeriod = 30
DefaultKeepBackNumber = 5
DefaultEtcdBackupScriptDir = "/usr/local/bin/kube-scripts"
DefaultJoinCIDR = "100.64.0.0/16"
DefaultNetworkType = "geneve"
DefaultVlanID = "100"
DefaultOvnLabel = "node-role.kubernetes.io/master"
DefaultDPDKVersion = "19.11"
DefaultDNSAddress = "114.114.114.114"

Docker = "docker"
Conatinerd = "containerd"
Expand Down
12 changes: 12 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.

1 change: 1 addition & 0 deletions cmd/ctl/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ func NewCmdInit() *cobra.Command {
}
o.CommonOptions.AddCommonFlag(cmd)
cmd.AddCommand(NewCmdInitOs())
cmd.AddCommand(NewCmdInitRegistry())
return cmd
}
67 changes: 67 additions & 0 deletions cmd/ctl/init/init_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2022 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package init

import (
"github.com/kubesphere/kubekey/cmd/ctl/options"
"github.com/kubesphere/kubekey/cmd/ctl/util"
"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/pipelines"
"github.com/spf13/cobra"
)

type InitRegistryOptions struct {
CommonOptions *options.CommonOptions
ClusterCfgFile string
DownloadCmd string
}

func NewInitRegistryOptions() *InitRegistryOptions {
return &InitRegistryOptions{
CommonOptions: options.NewCommonOptions(),
}
}

// NewCmdInitRegistry creates a new init os command
func NewCmdInitRegistry() *cobra.Command {
o := NewInitRegistryOptions()
cmd := &cobra.Command{
Use: "registry",
Short: "Init a local image registry",
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.Run())
},
}

o.CommonOptions.AddCommonFlag(cmd)
o.AddFlags(cmd)
return cmd
}

func (o *InitRegistryOptions) Run() error {
arg := common.Argument{
FilePath: o.ClusterCfgFile,
Debug: o.CommonOptions.Verbose,
}
return pipelines.InitRegistry(arg, o.DownloadCmd)
}

func (o *InitRegistryOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.ClusterCfgFile, "filename", "f", "", "Path to a configuration file")
cmd.Flags().StringVarP(&o.DownloadCmd, "download-cmd", "", "curl -L -o %s %s",
`The user defined command to download the necessary files. The first param '%s' is output path, the second param '%s', is the URL`)
}
8 changes: 7 additions & 1 deletion config/crd/bases/kubekey.kubesphere.io_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,12 @@ spec:
items:
type: string
type: array
type:
type: string
type: object
roleGroups:
description: RoleGroups defines the grouping of role for hosts (etcd
/ master / worker).
/ master / worker / registry).
properties:
etcd:
items:
Expand All @@ -621,6 +623,10 @@ spec:
items:
type: string
type: array
registry:
items:
type: string
type: array
worker:
items:
type: string
Expand Down

0 comments on commit e598c91

Please sign in to comment.