Skip to content

Commit

Permalink
feat: cli basic framework
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Oct 10, 2023
1 parent 17133d6 commit 78e1ffc
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 1 deletion.
47 changes: 47 additions & 0 deletions cmd/core/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2023 The Kubebb 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 main

import (
"github.com/spf13/cobra"

"github.com/kubebb/core/pkg/cmd"
)

var longUsage = `Quickly deploy the kubebb platform in the existing k8s environment.
The platform mainly includes four components: core, cluster-component, u4a, and component-store.
For more details, please refer to https://kubebb.github.io/website
Examples:
# install kubebb to a cluster
corectl install --context ~/.kube/devconfig [--skip-component-store|--skip-u4a-component|--skip-cluster-component]
`

func main() {
rootCmd := cobra.Command{
Use: "corectl",
Short: "Quickly deploy the kubebb platform in the existing k8s environment",
Long: longUsage,
}

rootCmd.AddCommand(cmd.NewInstallCmd())
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/goharbor/go-client v0.26.2
github.com/google/go-github/v54 v54.0.1-0.20230830144129-e3cda7864bce
github.com/kubeagi/arcadia v0.0.0-20230830103051-8e48dde112e2
github.com/spf13/cobra v1.6.1
github.com/tektoncd/pipeline v0.40.2
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783
golang.org/x/sync v0.2.0
Expand Down Expand Up @@ -191,7 +192,6 @@ require (
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
Expand Down
118 changes: 118 additions & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Copyright 2023 The Kubebb 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 cmd

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/client-go/rest"
)

type config struct {
// {"core": []string{"webhook.enalbe=true"}}
installers map[string][]string
// maybe for helm client
cfg *rest.Config // nolint
// If necessary, define other parameters here
}

type InstallerFunc func(*config) Installer

var (
store map[string]InstallerFunc
)

const (
CORE = "core"
CLUSTERCOMPONENT = "cluster-component"
U4A = "u4a"
COMPONENTSTORE = "component-store"
)

// Enroll Only name conflicts will return an error
func Enroll(name string, instance InstallerFunc) {
store[name] = instance
}

func GetInstaller(name string) InstallerFunc {
return store[name]
}

type Installer interface {
Description() string
Install() error
Uninstall()
}

func NewInstallCmd() *cobra.Command {
var (
installClusterComponent, installU4A, installComponentStore bool
)
cmd := &cobra.Command{
Use: "install",
Long: "install core, cluster-component",
Run: func(cmd *cobra.Command, args []string) {
initConf := config{
// Must install core
installers: map[string][]string{
CORE: {},
CLUSTERCOMPONENT: {},
U4A: {},
COMPONENTSTORE: {},
},
}
if installClusterComponent { // nolint
// if install cluster-component, we will upgrade core
// TODO
}
if installU4A { // nolint
// make sure cluster-component is set
// TODO
}
if installComponentStore { // nolint
// TODO
}

// get complete config
installTasks := []string{CORE, CLUSTERCOMPONENT, U4A, COMPONENTSTORE}
tasks := make([]Installer, 0)
for _, task := range installTasks {
if _, ok := initConf.installers[task]; ok {
if fn := GetInstaller(task); fn != nil {
tasks = append(tasks, fn(&initConf))
}
}
}

for idx, installer := range tasks {
fmt.Printf("Step %d: %s\n", idx+1, installer.Description())
if err := installer.Install(); err != nil {
// uninstall
for i := idx; i >= 0; i-- {
tasks[i].Uninstall()
}
}
}
},
}

cmd.Flags().BoolVar(&installClusterComponent, "cluster-component", false, "install cluster-component?")
cmd.Flags().BoolVar(&installU4A, "u4a", false, "install u4a?")
cmd.Flags().BoolVar(&installComponentStore, "component-store", false, "install component-store?")
return cmd
}
38 changes: 38 additions & 0 deletions pkg/cmd/create_core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2023 The Kubebb 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 cmd

type core struct {
args []string
}

func NewCoreInstaller(conf *config) Installer {
return &core{args: conf.installers[CORE]}
}

func (c *core) Description() string {
return "Installing core"
}

func (c *core) Install() error {
return nil
}

func (c *core) Uninstall() {}
func init() {
Enroll(CORE, NewCoreInstaller)
}

0 comments on commit 78e1ffc

Please sign in to comment.