diff --git a/app/cmd/cmd.go b/app/cmd/cmd.go new file mode 100755 index 0000000000000000000000000000000000000000..8afb5a58629098a47ed732128652420549a8b3a3 --- /dev/null +++ b/app/cmd/cmd.go @@ -0,0 +1,37 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 cmd + +import ( + "io" + + "github.com/spf13/cobra" +) + +func NewNkdCommand(in io.Reader, out, err io.Writer) *cobra.Command { + cmds := &cobra.Command{ + Use: "nkd", + Short: "nkd: easily bootstrap a secure Kubernetes cluster", + } + + cmds.ResetFlags() + // TODO: 修改名称 + // TODO: 添加DeployCommand, DestroyCommand, ExtendCommand + cmds.AddCommand(NewConfigCommand()) + cmds.AddCommand(NewInitCommand()) + + return cmds +} diff --git a/app/cmd/config.go b/app/cmd/config.go new file mode 100755 index 0000000000000000000000000000000000000000..efafe09ef81122dbe8666ec2e6c4fd3897cac332 --- /dev/null +++ b/app/cmd/config.go @@ -0,0 +1,37 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 cmd + +import ( + phases "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/config" + "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/workflow" + + "github.com/spf13/cobra" +) + +func NewConfigCommand() *cobra.Command { + configRunner := workflow.NewRunner() + cmd := &cobra.Command{ + Use: "config", + Short: "Manage a k8s cluster", + RunE: func(cmd *cobra.Command, args []string) error { + return configRunner.Run() + }, + } + configRunner.AppendPhase(phases.NewPrintDefaultNkdConfigCmd()) + + return cmd +} diff --git a/app/cmd/deploy.go b/app/cmd/deploy.go new file mode 100755 index 0000000000000000000000000000000000000000..3814ce6539b59bacbb3910acfe77d69e73c6b8c4 --- /dev/null +++ b/app/cmd/deploy.go @@ -0,0 +1,16 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 cmd diff --git a/app/cmd/extend.go b/app/cmd/extend.go new file mode 100755 index 0000000000000000000000000000000000000000..3814ce6539b59bacbb3910acfe77d69e73c6b8c4 --- /dev/null +++ b/app/cmd/extend.go @@ -0,0 +1,16 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 cmd diff --git a/app/cmd/init.go b/app/cmd/init.go new file mode 100755 index 0000000000000000000000000000000000000000..929717c3f89f6d896bc2bd15e65669db2522a0a1 --- /dev/null +++ b/app/cmd/init.go @@ -0,0 +1,38 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 cmd + +import ( + phases "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/init" + "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/workflow" + "github.com/spf13/cobra" +) + +func NewInitCommand() *cobra.Command { + initRunner := workflow.NewRunner() + cmd := &cobra.Command{ + Use: "init", + Short: "Use this command to init insert or config", + RunE: func(cmd *cobra.Command, args []string) error { + return initRunner.Run() + }, + } + + phases.NewGenerateCertsCmd() + initRunner.AppendPhase(phases.NewGenerateCertsCmd()) + initRunner.AppendPhase(phases.NewGenerateIgnCmd()) + return cmd +} diff --git a/app/cmd/phases/config/print.go b/app/cmd/phases/config/print.go new file mode 100644 index 0000000000000000000000000000000000000000..4ee27be1eb7e4fa89b3e3a62ae12cdc72301639a --- /dev/null +++ b/app/cmd/phases/config/print.go @@ -0,0 +1,32 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 phases + +import ( + "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/workflow" +) + +func NewPrintDefaultNkdConfigCmd() workflow.Phase { + return workflow.Phase{ + Name: "print", + Short: "run print phase to pring default nkd config", + Run: runPrintDefaultConfig, + } +} + +func runPrintDefaultConfig() error { + return nil +} diff --git a/app/cmd/phases/init/cert.go b/app/cmd/phases/init/cert.go new file mode 100644 index 0000000000000000000000000000000000000000..542a86fd200064b3d731ddfe8141b59a10a49c69 --- /dev/null +++ b/app/cmd/phases/init/cert.go @@ -0,0 +1,30 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 phases + +import "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/workflow" + +func NewGenerateCertsCmd() workflow.Phase { + return workflow.Phase{ + Name: "cert", + Short: "Run certs to generate certs", + Run: runGenerateCertsConfig, + } +} + +func runGenerateCertsConfig() error { + return nil +} diff --git a/app/cmd/phases/init/ign.go b/app/cmd/phases/init/ign.go new file mode 100644 index 0000000000000000000000000000000000000000..74cf5a85087fd6adebebe12eca4ddb630e4086d2 --- /dev/null +++ b/app/cmd/phases/init/ign.go @@ -0,0 +1,30 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 phases + +import "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd/phases/workflow" + +func NewGenerateIgnCmd() workflow.Phase { + return workflow.Phase{ + Name: "ign", + Short: "run ign to genertate ignition file", + Run: runGenerateIgnConfig, + } +} + +func runGenerateIgnConfig() error { + return nil +} diff --git a/app/cmd/phases/workflow/phase.go b/app/cmd/phases/workflow/phase.go new file mode 100644 index 0000000000000000000000000000000000000000..be993ea483baf9a75a4496a088fc7222c6d95096 --- /dev/null +++ b/app/cmd/phases/workflow/phase.go @@ -0,0 +1,23 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 workflow + +type Phase struct { + Name string + Short string + Phases []Phase + Run func() error +} diff --git a/app/cmd/phases/workflow/runner.go b/app/cmd/phases/workflow/runner.go new file mode 100644 index 0000000000000000000000000000000000000000..99e040e22db3318c941df6cb9f2c432223dbab10 --- /dev/null +++ b/app/cmd/phases/workflow/runner.go @@ -0,0 +1,99 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 workflow + +import ( + "strings" +) + +type Runner struct { + Phases []Phase + phaseRunners []*phaseRunner +} + +type phaseRunner struct { + Phase + parent *phaseRunner + level int + selfPath []string + generatedName string + use string +} + +func NewRunner() *Runner { + return &Runner{ + Phases: []Phase{}, + } +} + +func (r *Runner) Run() error { + r.prepareForExcution() + + err := r.VisitAll(func(p *phaseRunner) error { + if p.Run != nil { + if err := p.Run(); err != nil { + return err + } + } + return nil + }) + return err +} + +func (r *Runner) AppendPhase(t Phase) { + r.Phases = append(r.Phases, t) +} + +func (r *Runner) VisitAll(fn func(*phaseRunner) error) error { + for _, currentRunner := range r.phaseRunners { + if err := fn(currentRunner); err != nil { + return err + } + } + return nil +} +func (r *Runner) prepareForExcution() { + r.phaseRunners = []*phaseRunner{} + var parentRunner *phaseRunner + for _, phase := range r.Phases { + addPhaseRunner(r, parentRunner, phase) + } +} + +func addPhaseRunner(e *Runner, parentRunner *phaseRunner, phase Phase) { + use := cleanName(phase.Name) + generatedName := use + selfPath := []string{generatedName} + currentRunner := &phaseRunner{ + Phase: phase, + parent: parentRunner, + level: len(selfPath) - 1, + selfPath: selfPath, + generatedName: generatedName, + use: use, + } + e.phaseRunners = append(e.phaseRunners, currentRunner) + + for _, childPhase := range phase.Phases { + addPhaseRunner(e, currentRunner, childPhase) + } + +} + +func cleanName(name string) string { + ret := strings.ToLower(name) + return ret +} diff --git a/app/cmd/upgrade.go b/app/cmd/upgrade.go new file mode 100755 index 0000000000000000000000000000000000000000..3814ce6539b59bacbb3910acfe77d69e73c6b8c4 --- /dev/null +++ b/app/cmd/upgrade.go @@ -0,0 +1,16 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 cmd diff --git a/app/nkd.go b/app/nkd.go new file mode 100644 index 0000000000000000000000000000000000000000..b3e90f01e5fb94b2655e84097f1fe1bc32fd097c --- /dev/null +++ b/app/nkd.go @@ -0,0 +1,28 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 ( + "os" + + "gitee.com/openeuler/nestos-kubernetes-deployer/app/cmd" +) + +func Run() error { + cmd := cmd.NewNkdCommand(os.Stdin, os.Stdout, os.Stderr) + // pflag.CommandLine.MarkHidden("version") + return cmd.Execute() +} diff --git a/pkg/apimonitor.go b/app/phases/apimonitor.go similarity index 100% rename from pkg/apimonitor.go rename to app/phases/apimonitor.go diff --git a/pkg/bootconfig/ignitioncreator.go b/app/phases/bootconfig/ignitioncreator.go similarity index 100% rename from pkg/bootconfig/ignitioncreator.go rename to app/phases/bootconfig/ignitioncreator.go diff --git a/pkg/bootconfig/initconfig.go b/app/phases/bootconfig/initconfig.go similarity index 100% rename from pkg/bootconfig/initconfig.go rename to app/phases/bootconfig/initconfig.go diff --git a/pkg/cert/cacert.go b/app/phases/cert/cacert.go similarity index 100% rename from pkg/cert/cacert.go rename to app/phases/cert/cacert.go diff --git a/pkg/cert/certapi.go b/app/phases/cert/certapi.go similarity index 100% rename from pkg/cert/certapi.go rename to app/phases/cert/certapi.go diff --git a/pkg/cert/etcd.go b/app/phases/cert/etcd.go similarity index 100% rename from pkg/cert/etcd.go rename to app/phases/cert/etcd.go diff --git a/pkg/cert/selfsignedcert.go b/app/phases/cert/selfsignedcert.go similarity index 100% rename from pkg/cert/selfsignedcert.go rename to app/phases/cert/selfsignedcert.go diff --git a/pkg/cert/signedcerts.go b/app/phases/cert/signedcerts.go similarity index 100% rename from pkg/cert/signedcerts.go rename to app/phases/cert/signedcerts.go diff --git a/pkg/cert/tools.go b/app/phases/cert/tools.go similarity index 100% rename from pkg/cert/tools.go rename to app/phases/cert/tools.go diff --git a/pkg/deployer/clusterInfo.go b/app/phases/deployer/clusterInfo.go similarity index 100% rename from pkg/deployer/clusterInfo.go rename to app/phases/deployer/clusterInfo.go diff --git a/pkg/deployer/phase/base.go b/app/phases/deployer/phase/base.go similarity index 100% rename from pkg/deployer/phase/base.go rename to app/phases/deployer/phase/base.go diff --git a/pkg/deployer/phase/generate.go b/app/phases/deployer/phase/generate.go similarity index 100% rename from pkg/deployer/phase/generate.go rename to app/phases/deployer/phase/generate.go diff --git a/pkg/deployer/phase/init.go b/app/phases/deployer/phase/init.go similarity index 100% rename from pkg/deployer/phase/init.go rename to app/phases/deployer/phase/init.go diff --git a/pkg/deployer/phase/join.go b/app/phases/deployer/phase/join.go similarity index 100% rename from pkg/deployer/phase/join.go rename to app/phases/deployer/phase/join.go diff --git a/pkg/deployer/playbook/base.go b/app/phases/deployer/playbook/base.go similarity index 100% rename from pkg/deployer/playbook/base.go rename to app/phases/deployer/playbook/base.go diff --git a/pkg/deployer/playbook/oneshot.go b/app/phases/deployer/playbook/oneshot.go similarity index 100% rename from pkg/deployer/playbook/oneshot.go rename to app/phases/deployer/playbook/oneshot.go diff --git a/pkg/deployer/playbook/upgrade.go b/app/phases/deployer/playbook/upgrade.go similarity index 100% rename from pkg/deployer/playbook/upgrade.go rename to app/phases/deployer/playbook/upgrade.go diff --git a/pkg/deployer/state.go b/app/phases/deployer/state.go similarity index 100% rename from pkg/deployer/state.go rename to app/phases/deployer/state.go diff --git a/pkg/deployer/template.go b/app/phases/deployer/template.go similarity index 100% rename from pkg/deployer/template.go rename to app/phases/deployer/template.go diff --git a/pkg/deployer/utils.go b/app/phases/deployer/utils.go similarity index 100% rename from pkg/deployer/utils.go rename to app/phases/deployer/utils.go diff --git a/pkg/deployer/workflow.go b/app/phases/deployer/workflow.go similarity index 100% rename from pkg/deployer/workflow.go rename to app/phases/deployer/workflow.go diff --git a/pkg/infra/assets/assets.go b/app/phases/infra/assets/assets.go similarity index 100% rename from pkg/infra/assets/assets.go rename to app/phases/infra/assets/assets.go diff --git a/pkg/infra/assets/cluster/cluster.go b/app/phases/infra/assets/cluster/cluster.go similarity index 100% rename from pkg/infra/assets/cluster/cluster.go rename to app/phases/infra/assets/cluster/cluster.go diff --git a/pkg/infra/assets/cluster/tfvars.go b/app/phases/infra/assets/cluster/tfvars.go similarity index 100% rename from pkg/infra/assets/cluster/tfvars.go rename to app/phases/infra/assets/cluster/tfvars.go diff --git a/pkg/infra/terraform/init.go b/app/phases/infra/terraform/init.go similarity index 100% rename from pkg/infra/terraform/init.go rename to app/phases/infra/terraform/init.go diff --git a/pkg/infra/terraform/logger.go b/app/phases/infra/terraform/logger.go similarity index 100% rename from pkg/infra/terraform/logger.go rename to app/phases/infra/terraform/logger.go diff --git a/pkg/infra/terraform/providers/providers.go b/app/phases/infra/terraform/providers/providers.go similarity index 100% rename from pkg/infra/terraform/providers/providers.go rename to app/phases/infra/terraform/providers/providers.go diff --git a/pkg/infra/terraform/stage.go b/app/phases/infra/terraform/stage.go similarity index 100% rename from pkg/infra/terraform/stage.go rename to app/phases/infra/terraform/stage.go diff --git a/pkg/infra/terraform/stages/openstack/stages.go b/app/phases/infra/terraform/stages/openstack/stages.go similarity index 100% rename from pkg/infra/terraform/stages/openstack/stages.go rename to app/phases/infra/terraform/stages/openstack/stages.go diff --git a/pkg/infra/terraform/stages/platform/stages.go b/app/phases/infra/terraform/stages/platform/stages.go similarity index 100% rename from pkg/infra/terraform/stages/platform/stages.go rename to app/phases/infra/terraform/stages/platform/stages.go diff --git a/pkg/infra/terraform/stages/split.go b/app/phases/infra/terraform/stages/split.go similarity index 100% rename from pkg/infra/terraform/stages/split.go rename to app/phases/infra/terraform/stages/split.go diff --git a/pkg/infra/terraform/state.go b/app/phases/infra/terraform/state.go similarity index 100% rename from pkg/infra/terraform/state.go rename to app/phases/infra/terraform/state.go diff --git a/pkg/infra/terraform/terraform.go b/app/phases/infra/terraform/terraform.go similarity index 100% rename from pkg/infra/terraform/terraform.go rename to app/phases/infra/terraform/terraform.go diff --git a/pkg/infra/tfvars/openstack/tfvars.go b/app/phases/infra/tfvars/openstack/tfvars.go similarity index 100% rename from pkg/infra/tfvars/openstack/tfvars.go rename to app/phases/infra/tfvars/openstack/tfvars.go diff --git a/pkg/manager/manager.go b/app/phases/manager/manager.go similarity index 100% rename from pkg/manager/manager.go rename to app/phases/manager/manager.go diff --git a/pkg/osimage/osimage.go b/app/phases/osimage/osimage.go similarity index 100% rename from pkg/osimage/osimage.go rename to app/phases/osimage/osimage.go diff --git a/pkg/registry/registry.go b/app/phases/registry/registry.go similarity index 100% rename from pkg/registry/registry.go rename to app/phases/registry/registry.go diff --git a/cmd/nkd/deploy.go b/cmd/nkd/deploy.go deleted file mode 100755 index 0caebe63c239f85b536a71899b642aef9bf6cd3a..0000000000000000000000000000000000000000 --- a/cmd/nkd/deploy.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "github.com/spf13/cobra" -) - -var ( - deployCommand = &cobra.Command{ - Use: "deploy [nodes]", - Short: "deploy masters ", - Long: "deploy cluster bootconfig for a new cluster", - RunE: deploy, - Args: cobra.MaximumNArgs(1), - } -) - - -func deploy(command *cobra.Command, args []string) error{ - println("deploy") - return nil -} diff --git a/cmd/nkd/generate.go b/cmd/nkd/generate.go deleted file mode 100755 index 961909c41ad63d1f001a19facd4c589fbd3eb0cf..0000000000000000000000000000000000000000 --- a/cmd/nkd/generate.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import "github.com/spf13/cobra" - -var ( - generateCommand = &cobra.Command{ - Use: "generate [options] [REGISTRY]", - Short: "generate cluster bootconfig", - Long: "generate cluster bootconfig for a new cluster", - RunE: generate, - Args: cobra.MaximumNArgs(1), - } -) - - -func generate(command *cobra.Command, args []string) error{ - println("generate") - return nil -} - diff --git a/cmd/nkd/join.go b/cmd/nkd/join.go deleted file mode 100755 index 1a7abf920bdab998b10fce4a43b515075cce4586..0000000000000000000000000000000000000000 --- a/cmd/nkd/join.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "github.com/spf13/cobra" -) - -var ( - joinCommand = &cobra.Command{ - Use: "join [nodes]", - Short: "join nodes ", - Long: "join nodes to cluster", - RunE: join, - Args: cobra.MaximumNArgs(1), - } -) - - -func join(command *cobra.Command, args []string) error{ - println("join") - return nil -} diff --git a/cmd/nkd/main.go b/cmd/nkd/main.go deleted file mode 100755 index e4eaf8e639a46a1472b8c24b3d6fbf71075151a1..0000000000000000000000000000000000000000 --- a/cmd/nkd/main.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "os" - "path/filepath" - - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" -) - -func newRootCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: filepath.Base(os.Args[0]), - Short: "Create kubernetes cluster", - Long: "", - } - return cmd -} - -func main() { - rootCmd := newRootCmd() - - for _, subCmd := range []*cobra.Command{ - /*todo: - newDeployCmd() - newGenerateCmd() - newInitCmd() - newJoinCommand() - */ - newUpgradeCmd(), - } { - rootCmd.AddCommand(subCmd) - } - if err := rootCmd.Execute(); err != nil { - logrus.Errorf("Error executing nkd: %v", err) - } -} diff --git a/cmd/nkd/root.go b/cmd/nkd/root.go deleted file mode 100755 index d8a4995be3b602ac0e31747bfeb3e5985552280c..0000000000000000000000000000000000000000 --- a/cmd/nkd/root.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "github.com/spf13/cobra" -) - -var ( - rootCmd = &cobra.Command{ - Use: "nkd [options]", - Long: "deploy k8s clusters", - SilenceUsage: true, - SilenceErrors: true, - TraverseChildren: true, - //PersistentPreRunE: persistentPreRunE, - //RunE: validate.SubCommandExists, - //PersistentPostRunE: persistentPostRunE, - //Version: version.Version.String(), - DisableFlagsInUseLine: true, - } - -) diff --git a/cmd/nkd/upgrade.go b/cmd/nkd/upgrade.go deleted file mode 100755 index 8493cacd6081e8c44339ac2c29a8393e03ce885e..0000000000000000000000000000000000000000 --- a/cmd/nkd/upgrade.go +++ /dev/null @@ -1,118 +0,0 @@ -package main - -import ( - "context" - "fmt" - "time" - - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/dynamic" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" - "sigs.k8s.io/yaml" - - wait "k8s.io/apimachinery/pkg/util/wait" -) - -func newUpgradeCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "upgrade", - Short: "Upgrade your cluster to a newer version", - Long: "", - RunE: runUpgradeCmd, - } - return cmd -} - -func runUpgradeCmd(command *cobra.Command, args []string) error { - var ( - osVersion = "" - osImageURL = "" - kubeVersion = "" - evictPodForce = false - maxUnavailable = 2 - loopTimeout = 2 * time.Minute - kubeconfig = "" - ) - // Get the kubeconfig configuration - config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) - if err != nil { - config, err = rest.InClusterConfig() - if err != nil { - logrus.Errorf("Error getting Kubernetes client config: %v\n", err) - return err - } - } - - // Create dynamic client - dynamicClient, err := dynamic.NewForConfig(config) - if err != nil { - logrus.Errorf("Error creating Dynamic client: %v\n", err) - return err - } - - // Define the YAML data for the Custom Resource (CR) - yamlData := fmt.Sprintf(` -apiVersion: housekeeper.io/v1alpha1 -kind: Update -metadata: - name: update-sample - namespace: housekeeper-system -spec: - osVersion: %s - osImageURL: %s - kubeVersion: %s - evictPodForce: %t - maxUnavailable: %d -`, osVersion, osImageURL, kubeVersion, evictPodForce, maxUnavailable) - - var unstructuredObj unstructured.Unstructured - err = yaml.Unmarshal([]byte(yamlData), &unstructuredObj) - if err != nil { - logrus.Errorf("Error unmarshalling YAML: %v\n", err) - return err - } - - // Create or Update CR - resource := schema.GroupVersionResource{ - Group: "housekeeper.io", - Version: "v1alpha1", - Resource: "updates", // Pluralized resource name - } - - // The loop attempts to create or update a CR until it succeeds or times out - if err := wait.PollImmediate(2*time.Second, loopTimeout, func() (bool, error) { - gvk := unstructuredObj.GroupVersionKind() - dynamicResource := dynamicClient.Resource(gvk.GroupVersion().WithResource(resource.Resource)).Namespace(unstructuredObj.GetNamespace()) - - //Attempts to get the specified Custom Resource from the Kubernetes API Server. - obj, err := dynamicResource.Get(context.Background(), unstructuredObj.GetName(), metav1.GetOptions{}) - if err != nil { - // Not found, create the resource - _, err = dynamicResource.Create(context.Background(), &unstructuredObj, metav1.CreateOptions{}) - if err == nil { - logrus.Infof("Custom Resource created successfully!") - return true, nil - } - } else { - // Found, update the resource - unstructuredObj.SetResourceVersion(obj.GetResourceVersion()) - _, err = dynamicResource.Update(context.Background(), &unstructuredObj, metav1.UpdateOptions{}) - if err == nil { - logrus.Infof("Custom Resource updated successfully!") - return true, nil - } - } - logrus.Errorf("Error creating or updating CR: %v\n", err) - return false, nil - }); err != nil { - logrus.Errorf("Timeout while waiting for Custom Resource to be created or updated.") - } - - return nil -} diff --git a/nkd.go b/nkd.go new file mode 100644 index 0000000000000000000000000000000000000000..eebffb28bada7540ee65fabe9101e78b33db6ef9 --- /dev/null +++ b/nkd.go @@ -0,0 +1,22 @@ +/* +Copyright 2023 KylinSoft Co., Ltd. + +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 main + +import "gitee.com/openeuler/nestos-kubernetes-deployer/app" + +func main() { + app.Run() +} diff --git a/data/templates/openstack/main.tf.template b/resource/templates/terraform/main.tf.template similarity index 100% rename from data/templates/openstack/main.tf.template rename to resource/templates/terraform/main.tf.template