feat: add init command.

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
joyceliu 2024-06-11 16:24:12 +08:00
parent 97f4fed170
commit 7247c2733c
11 changed files with 304 additions and 51 deletions

View File

@ -1,10 +1,49 @@
---
- import_playbook: pre_install.yaml
- import_playbook: hook/pre_install.yaml
- import_playbook: precheck.yaml
- import_playbook: init.yaml
- import_playbook: init_os.yaml
- import_playbook: install.yaml
# install
- hosts:
- nfs
gather_facts: true
roles:
- install/nfs
- import_playbook: post_install.yaml
- hosts:
- etcd
gather_facts: true
roles:
- install/etcd
- hosts:
- image_registry
gather_facts: true
roles:
- install/image-registry
- hosts:
- k8s_cluster
gather_facts: true
roles:
- install/cri
- install/kubernetes
- hosts:
- kube_control_plane
roles:
- role: install/certs
when: renew_certs.enabled|default_if_none:false
- hosts:
- k8s_cluster|random
roles:
- addons/cni
- addons/kata
- addons/nfd
- addons/sc
- import_playbook: hook/post_install.yaml

View File

@ -0,0 +1,15 @@
---
- import_playbook: hook/pre_install.yaml
- hosts:
- localhost
roles:
- init/init-artifact
- hosts:
- image_registry
gather_facts: true
roles:
- install/image-registry
- import_playbook: hook/post_install.yaml

View File

@ -1,39 +0,0 @@
---
- hosts:
- nfs
gather_facts: true
roles:
- install/nfs
- hosts:
- etcd
gather_facts: true
roles:
- install/etcd
- hosts:
- image_registry
gather_facts: true
roles:
- install/image-registry
- hosts:
- k8s_cluster
gather_facts: true
roles:
- install/cri
- install/kubernetes
- hosts:
- kube_control_plane
roles:
- role: install/certs
when: renew_certs.enabled|default_if_none:false
- hosts:
- k8s_cluster|random
roles:
- addons/cni
- addons/kata
- addons/nfd
- addons/sc

101
cmd/kk/app/init.go Normal file
View File

@ -0,0 +1,101 @@
/*
Copyright 2024 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 app
import (
"io/fs"
"os"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"github.com/kubesphere/kubekey/v4/cmd/kk/app/options"
_const "github.com/kubesphere/kubekey/v4/pkg/const"
)
func newInitCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initializes the installation environment",
}
cmd.AddCommand(newInitOSCommand())
cmd.AddCommand(newInitRegistryCommand())
return cmd
}
func newInitOSCommand() *cobra.Command {
o := options.NewInitOSOptions()
cmd := &cobra.Command{
Use: "os",
Short: "Init operating system",
RunE: func(cmd *cobra.Command, args []string) error {
pipeline, config, inventory, err := o.Complete(cmd, []string{"playbooks/init_os.yaml"})
if err != nil {
return err
}
// set workdir
_const.SetWorkDir(o.WorkDir)
// create workdir directory,if not exists
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
return err
}
}
return run(signals.SetupSignalHandler(), pipeline, config, inventory)
},
}
flags := cmd.Flags()
for _, f := range o.Flags().FlagSets {
flags.AddFlagSet(f)
}
return cmd
}
func newInitRegistryCommand() *cobra.Command {
o := options.NewInitRegistryOptions()
cmd := &cobra.Command{
Use: "registry",
Short: "Init a local image registry",
RunE: func(cmd *cobra.Command, args []string) error {
pipeline, config, inventory, err := o.Complete(cmd, []string{"playbooks/init_registry.yaml"})
if err != nil {
return err
}
// set workdir
_const.SetWorkDir(o.WorkDir)
// create workdir directory,if not exists
if _, err := os.Stat(o.WorkDir); os.IsNotExist(err) {
if err := os.MkdirAll(o.WorkDir, fs.ModePerm); err != nil {
return err
}
}
return run(signals.SetupSignalHandler(), pipeline, config, inventory)
},
}
flags := cmd.Flags()
for _, f := range o.Flags().FlagSets {
flags.AddFlagSet(f)
}
return cmd
}
func init() {
registerInternalCommand(newInitCommand())
}

View File

@ -43,7 +43,7 @@ type CreateClusterOptions struct {
func (o *CreateClusterOptions) Flags() cliflag.NamedFlagSets {
fss := o.CommonOptions.Flags()
kfs := fss.FlagSet("kubernetes")
kfs := fss.FlagSet("config")
kfs.StringVar(&o.Kubernetes, "with-kubernetes", "", "Specify a supported version of kubernetes")
kfs.StringVar(&o.ContainerManager, "container-manager", "", "Container runtime: docker, crio, containerd and isula.")
kfs.StringVarP(&o.Artifact, "artifact", "a", "", "Path to a KubeKey artifact")

143
cmd/kk/app/options/init.go Normal file
View File

@ -0,0 +1,143 @@
/*
Copyright 2024 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 options
import (
"fmt"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliflag "k8s.io/component-base/cli/flag"
kubekeyv1 "github.com/kubesphere/kubekey/v4/pkg/apis/kubekey/v1"
)
// ======================================================================================
// init os
// ======================================================================================
type InitOSOptions struct {
CommonOptions
// Artifact container all binaries which used to install kubernetes.
Artifact string
}
func (o *InitOSOptions) Flags() cliflag.NamedFlagSets {
fss := o.CommonOptions.Flags()
kfs := fss.FlagSet("config")
kfs.StringVarP(&o.Artifact, "artifact", "a", "", "Path to a KubeKey artifact")
return fss
}
func (o InitOSOptions) Complete(cmd *cobra.Command, args []string) (*kubekeyv1.Pipeline, *kubekeyv1.Config, *kubekeyv1.Inventory, error) {
pipeline := &kubekeyv1.Pipeline{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "init-os-",
Namespace: o.Namespace,
Annotations: map[string]string{
kubekeyv1.BuiltinsProjectAnnotation: "",
},
},
}
// complete playbook. now only support one playbook
if len(args) == 1 {
o.Playbook = args[0]
} else {
return nil, nil, nil, fmt.Errorf("%s\nSee '%s -h' for help and examples", cmd.Use, cmd.CommandPath())
}
pipeline.Spec = kubekeyv1.PipelineSpec{
Playbook: o.Playbook,
Debug: o.Debug,
}
config, inventory, err := o.completeRef(pipeline)
if err != nil {
return nil, nil, nil, err
}
if o.Artifact != "" {
// override artifact_file in config
if err := config.SetValue("artifact_file", o.Artifact); err != nil {
return nil, nil, nil, err
}
}
return pipeline, config, inventory, nil
}
func NewInitOSOptions() *InitOSOptions {
// set default value
return &InitOSOptions{CommonOptions: newCommonOptions()}
}
// ======================================================================================
// init registry
// ======================================================================================
type InitRegistryOptions struct {
CommonOptions
// Artifact container all binaries which used to install kubernetes.
Artifact string
}
func (o *InitRegistryOptions) Flags() cliflag.NamedFlagSets {
fss := o.CommonOptions.Flags()
kfs := fss.FlagSet("config")
kfs.StringVarP(&o.Artifact, "artifact", "a", "", "Path to a KubeKey artifact")
return fss
}
func (o InitRegistryOptions) Complete(cmd *cobra.Command, args []string) (*kubekeyv1.Pipeline, *kubekeyv1.Config, *kubekeyv1.Inventory, error) {
pipeline := &kubekeyv1.Pipeline{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "init-registry-",
Namespace: o.Namespace,
Annotations: map[string]string{
kubekeyv1.BuiltinsProjectAnnotation: "",
},
},
}
// complete playbook. now only support one playbook
if len(args) == 1 {
o.Playbook = args[0]
} else {
return nil, nil, nil, fmt.Errorf("%s\nSee '%s -h' for help and examples", cmd.Use, cmd.CommandPath())
}
pipeline.Spec = kubekeyv1.PipelineSpec{
Playbook: o.Playbook,
Debug: o.Debug,
}
config, inventory, err := o.completeRef(pipeline)
if err != nil {
return nil, nil, nil, err
}
if o.Artifact != "" {
// override artifact_file in config
if err := config.SetValue("artifact_file", o.Artifact); err != nil {
return nil, nil, nil, err
}
}
return pipeline, config, inventory, nil
}
func NewInitRegistryOptions() *InitRegistryOptions {
// set default value
return &InitRegistryOptions{CommonOptions: newCommonOptions()}
}

View File

@ -1,7 +1,6 @@
package options
import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliflag "k8s.io/component-base/cli/flag"
)
@ -26,9 +25,4 @@ func (o *PipelineOptions) Flags() cliflag.NamedFlagSets {
pfs.StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "namespace of pipeline")
pfs.StringVar(&o.WorkDir, "work-dir", o.WorkDir, "the base Dir for kubekey. Default current dir. ")
return fss
}
func (o *PipelineOptions) Complete(cmd *cobra.Command, args []string) {
// do nothing
}

View File

@ -74,7 +74,7 @@ func loadPlaybook(baseFS fs.FS, pbPath string, pb *kkcorev1.Playbook) error {
if p.ImportPlaybook != "" {
importPlaybook := getPlaybookBaseFromPlaybook(baseFS, pbPath, p.ImportPlaybook)
if importPlaybook == "" {
return fmt.Errorf("cannot found import playbook %s", importPlaybook)
return fmt.Errorf("cannot found import playbook %s", p.ImportPlaybook)
}
if err := loadPlaybook(baseFS, importPlaybook, pb); err != nil {
return err