Skip to content

Commit

Permalink
Merge pull request #2267 from will4j/feature/addons-enhance
Browse files Browse the repository at this point in the history
Feature/cluster addons enhancement
  • Loading branch information
ks-ci-bot authored Jun 3, 2024
2 parents 6beb293 + 9452a28 commit 9a79dc7
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 10 deletions.
12 changes: 7 additions & 5 deletions cmd/kk/apis/kubekey/v1alpha2/addons_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package v1alpha2

type Addon struct {
Name string `yaml:"name" json:"name,omitempty"`
Namespace string `yaml:"namespace" json:"namespace,omitempty"`
Sources Sources `yaml:"sources" json:"sources,omitempty"`
Retries int `yaml:"retries" json:"retries,omitempty"`
Delay int `yaml:"delay" json:"delay,omitempty"`
Name string `yaml:"name" json:"name,omitempty"`
Namespace string `yaml:"namespace" json:"namespace,omitempty"`
PreInstall []CustomScripts `yaml:"preInstall" json:"preInstall,omitempty"`
Sources Sources `yaml:"sources" json:"sources,omitempty"`
PostInstall []CustomScripts `yaml:"postInstall" json:"postInstall,omitempty"`
Retries int `yaml:"retries" json:"retries,omitempty"`
Delay int `yaml:"delay" json:"delay,omitempty"`
}

type Sources struct {
Expand Down
3 changes: 3 additions & 0 deletions cmd/kk/cmd/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type CreateClusterOptions struct {
EnableKubeSphere bool
KubeSphere string
LocalStorage bool
SkipInstallAddons bool
SkipPullImages bool
SkipPushImages bool
SecurityEnhancement bool
Expand Down Expand Up @@ -113,6 +114,7 @@ func (o *CreateClusterOptions) Run() error {
KubernetesVersion: o.Kubernetes,
KsEnable: o.EnableKubeSphere,
KsVersion: o.KubeSphere,
SkipInstallAddons: o.SkipInstallAddons,
SkipPullImages: o.SkipPullImages,
SkipPushImages: o.SkipPushImages,
SecurityEnhancement: o.SecurityEnhancement,
Expand All @@ -139,6 +141,7 @@ func (o *CreateClusterOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Kubernetes, "with-kubernetes", "", "", "Specify a supported version of kubernetes")
cmd.Flags().BoolVarP(&o.LocalStorage, "with-local-storage", "", false, "Deploy a local PV provisioner")
cmd.Flags().BoolVarP(&o.EnableKubeSphere, "with-kubesphere", "", false, fmt.Sprintf("Deploy a specific version of kubesphere (default %s)", kubesphere.Latest().Version))
cmd.Flags().BoolVarP(&o.SkipInstallAddons, "skip-install-addons", "", false, "Skip install addons")
cmd.Flags().BoolVarP(&o.SkipPullImages, "skip-pull-images", "", false, "Skip pre pull images")
cmd.Flags().BoolVarP(&o.SkipPushImages, "skip-push-images", "", false, "Skip pre push images")
cmd.Flags().BoolVarP(&o.SecurityEnhancement, "with-security-enhancement", "", false, "Security enhancement")
Expand Down
49 changes: 49 additions & 0 deletions cmd/kk/cmd/create/phase/addons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package phase

import (
"github.com/spf13/cobra"

"github.com/kubesphere/kubekey/v3/cmd/kk/cmd/options"
"github.com/kubesphere/kubekey/v3/cmd/kk/cmd/util"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/phase/addons"
)

type ApplyAddonsOptions struct {
CommonOptions *options.CommonOptions
ClusterCfgFile string
}

func NewApplyAddonsOptions() *ApplyAddonsOptions {
return &ApplyAddonsOptions{
CommonOptions: options.NewCommonOptions(),
}
}

func NewCmdApplyAddons() *cobra.Command {
o := NewApplyAddonsOptions()
cmd := &cobra.Command{
Use: "addons",
Short: "Apply cluster addons",
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.Run(args))
},
}

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

func (o *ApplyAddonsOptions) Run(args []string) error {
arg := common.Argument{
FilePath: o.ClusterCfgFile,
Debug: o.CommonOptions.Verbose,
EnabledAddons: args,
}
return addons.ApplyClusterAddons(arg)
}

func (o *ApplyAddonsOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.ClusterCfgFile, "filename", "f", "", "Path to a configuration file")
}
1 change: 1 addition & 0 deletions cmd/kk/cmd/create/phase/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewPhaseCommand() *cobra.Command {
cmds.AddCommand(NewCmdCreateJoinNodes())
cmds.AddCommand(NewCmdCreateConfigureKubernetes())
cmds.AddCommand(NewCmdCreateKubeSphere())
cmds.AddCommand(NewCmdApplyAddons())

return cmds
}
23 changes: 23 additions & 0 deletions cmd/kk/pkg/addons/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package addons

import (
"fmt"

kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/v3/cmd/kk/apis/kubekey/v1alpha2"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/task"
)
Expand Down Expand Up @@ -44,3 +47,23 @@ func (a *AddonsModule) Init() {
install,
}
}

type AddonModule struct {
common.KubeModule
addon *kubekeyapiv1alpha2.Addon
}

func (s *AddonModule) Init() {
s.Name = "AddonModule"
s.Desc = fmt.Sprintf("Install addon %s", s.addon.Name)

install := &task.LocalTask{
Name: "InstallAddon",
Desc: fmt.Sprintf("Install addon %s", s.addon.Name),
Action: &InstallAddon{addon: s.addon},
}

s.Tasks = []task.Interface{
install,
}
}
79 changes: 78 additions & 1 deletion cmd/kk/pkg/addons/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ package addons
import (
"fmt"
"path/filepath"
"strings"

"github.com/pkg/errors"

kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/v3/cmd/kk/apis/kubekey/v1alpha2"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/bootstrap/customscripts"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/logger"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/module"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/pipeline"
)

type Install struct {
Expand All @@ -31,11 +38,81 @@ type Install struct {

func (i *Install) Execute(runtime connector.Runtime) error {
nums := len(i.KubeConf.Cluster.Addons)

enabledAddons, err := i.enabledAddons()
if err != nil {
return err
}

logger.Log.Messagef(runtime.RemoteHost().GetName(), "[%v/%v] enabled addons", len(enabledAddons), nums)

for index, addon := range i.KubeConf.Cluster.Addons {
if _, ok := enabledAddons[addon.Name]; !ok {
continue
}
logger.Log.Messagef(runtime.RemoteHost().GetName(), "Install addon [%v-%v]: %s", nums, index, addon.Name)
if err := InstallAddons(i.KubeConf, &addon, filepath.Join(runtime.GetWorkDir(), fmt.Sprintf("config-%s", runtime.GetObjName()))); err != nil {

m := []module.Module{
&customscripts.CustomScriptsModule{Phase: "PreInstall", Scripts: addon.PreInstall},
&AddonModule{addon: &addon},
&customscripts.CustomScriptsModule{Phase: "PostInstall", Scripts: addon.PostInstall},
}
p := pipeline.Pipeline{
Name: "InstallAddonsPipeline",
Modules: m,
Runtime: runtime,
SkipPrintLogo: true,
}
if err := p.Start(); err != nil {
return err
}
}
return nil
}

func (i *Install) enabledAddons() (map[string]struct{}, error) {
enabledAddons := make(map[string]struct{}, len(i.KubeConf.Cluster.Addons))
for _, addon := range i.KubeConf.Cluster.Addons {
enabledAddons[addon.Name] = struct{}{}
}

if len(i.KubeConf.Arg.EnabledAddons) == 0 {
return enabledAddons, nil
}

enabledAddonsConfig := make(map[string]struct{}, len(i.KubeConf.Arg.EnabledAddons))
for _, config := range i.KubeConf.Arg.EnabledAddons {
enabledAddonsConfig[config] = struct{}{}
}

for addon := range enabledAddons {
if _, ok := enabledAddonsConfig[addon]; !ok {
// drop addons not in input args
delete(enabledAddons, addon)
} else {
// drop input args validated
delete(enabledAddonsConfig, addon)
}
}
if len(enabledAddonsConfig) > 0 {
// exists invalid input args
keys := make([]string, 0, len(enabledAddonsConfig))
for key := range enabledAddonsConfig {
keys = append(keys, key)
}
return nil, errors.New(fmt.Sprintf("Addons not exists: %s", strings.Join(keys, ",")))
}
return enabledAddons, nil
}

type InstallAddon struct {
common.KubeAction
addon *kubekeyapiv1alpha2.Addon
}

func (i *InstallAddon) Execute(runtime connector.Runtime) error {
if err := InstallAddons(i.KubeConf, i.addon, filepath.Join(runtime.GetWorkDir(), fmt.Sprintf("config-%s", runtime.GetObjName()))); err != nil {
return err
}
return nil
}
2 changes: 2 additions & 0 deletions cmd/kk/pkg/common/kube_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Argument struct {
KsVersion string
Debug bool
IgnoreErr bool
SkipInstallAddons bool
EnabledAddons []string
SkipPullImages bool
SkipPushImages bool
SkipDependencyCheck bool
Expand Down
5 changes: 4 additions & 1 deletion cmd/kk/pkg/core/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ type Pipeline struct {
PipelineCache *cache.Cache
ModuleCachePool sync.Pool
ModulePostHooks []module.PostHookInterface
SkipPrintLogo bool
}

func (p *Pipeline) Init() error {
fmt.Print(logo)
if !p.SkipPrintLogo {
fmt.Print(logo)
}
p.PipelineCache = cache.NewCache()
p.SpecHosts = len(p.Runtime.GetAllHosts())
//if err := p.Runtime.GenerateWorkDir(); err != nil {
Expand Down
57 changes: 57 additions & 0 deletions cmd/kk/pkg/phase/addons/addons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package addons

import (
"github.com/pkg/errors"

"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/addons"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/bootstrap/precheck"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/module"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/pipeline"
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/kubernetes"
)

func ApplyClusterAddons(args common.Argument) error {

var loaderType string
if args.FilePath != "" {
loaderType = common.File
} else {
loaderType = common.AllInOne
}

runtime, err := common.NewKubeRuntime(loaderType, args)
if err != nil {
return err
}

switch runtime.Cluster.Kubernetes.Type {
case common.Kubernetes:
if err := ApplyClusterAddonsPipeline(runtime); err != nil {
return err
}
default:
return errors.New("unsupported cluster kubernetes type")
}

return nil
}

func ApplyClusterAddonsPipeline(runtime *common.KubeRuntime) error {
m := []module.Module{
&precheck.GreetingsModule{},
&precheck.NodePreCheckModule{},
&kubernetes.StatusModule{},
&addons.AddonsModule{},
}

p := pipeline.Pipeline{
Name: "ApplyClusterAddonsPipeline",
Modules: m,
Runtime: runtime,
}
if err := p.Start(); err != nil {
return err
}
return nil
}
6 changes: 3 additions & 3 deletions cmd/kk/pkg/pipelines/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewCreateClusterPipeline(runtime *common.KubeRuntime) error {
&kubernetes.SaveKubeConfigModule{},
&plugins.DeployPluginsModule{},
&customscripts.CustomScriptsModule{Phase: "PostClusterInstall", Scripts: runtime.Cluster.System.PostClusterInstall},
&addons.AddonsModule{},
&addons.AddonsModule{Skip: runtime.Arg.SkipInstallAddons},
&storage.DeployLocalVolumeModule{Skip: skipLocalStorage},
&kubesphere.DeployModule{Skip: !runtime.Cluster.KubeSphere.Enabled},
&kubesphere.CheckResultModule{Skip: !runtime.Cluster.KubeSphere.Enabled},
Expand Down Expand Up @@ -168,7 +168,7 @@ func NewK3sCreateClusterPipeline(runtime *common.KubeRuntime) error {
&certs.AutoRenewCertsModule{Skip: !runtime.Cluster.Kubernetes.EnableAutoRenewCerts()},
&k3s.SaveKubeConfigModule{},
&customscripts.CustomScriptsModule{Phase: "PostClusterInstall", Scripts: runtime.Cluster.System.PostClusterInstall},
&addons.AddonsModule{},
&addons.AddonsModule{Skip: runtime.Arg.SkipInstallAddons},
&storage.DeployLocalVolumeModule{Skip: skipLocalStorage},
&kubesphere.DeployModule{Skip: !runtime.Cluster.KubeSphere.Enabled},
&kubesphere.CheckResultModule{Skip: !runtime.Cluster.KubeSphere.Enabled},
Expand Down Expand Up @@ -243,7 +243,7 @@ func NewK8eCreateClusterPipeline(runtime *common.KubeRuntime) error {
&certs.AutoRenewCertsModule{Skip: !runtime.Cluster.Kubernetes.EnableAutoRenewCerts()},
&k8e.SaveKubeConfigModule{},
&customscripts.CustomScriptsModule{Phase: "PostClusterInstall", Scripts: runtime.Cluster.System.PostClusterInstall},
&addons.AddonsModule{},
&addons.AddonsModule{Skip: runtime.Arg.SkipInstallAddons},
&storage.DeployLocalVolumeModule{Skip: skipLocalStorage},
&kubesphere.DeployModule{Skip: !runtime.Cluster.KubeSphere.Enabled},
&kubesphere.CheckResultModule{Skip: !runtime.Cluster.KubeSphere.Enabled},
Expand Down

0 comments on commit 9a79dc7

Please sign in to comment.