Skip to content

Commit

Permalink
feat: kbcli cluster register cmd for cluster create config (#5052)
Browse files Browse the repository at this point in the history
Co-authored-by: 1aal <[email protected]>
  • Loading branch information
1aal and 1aal authored Sep 9, 2023
1 parent 4a9821b commit 4ea3925
Show file tree
Hide file tree
Showing 22 changed files with 724 additions and 229 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ build-kbcli-embed-chart: helmtool create-kbcli-embed-charts-dir \
build-single-kbcli-embed-chart.postgresql-cluster \
build-single-kbcli-embed-chart.kafka-cluster \
build-single-kbcli-embed-chart.mongodb-cluster \
build-single-kbcli-embed-chart.neon-cluster
# build-single-kbcli-embed-chart.neon-cluster
# build-single-kbcli-embed-chart.postgresql-cluster \
# build-single-kbcli-embed-chart.clickhouse-cluster \
# build-single-kbcli-embed-chart.milvus-cluster \
Expand Down
2 changes: 1 addition & 1 deletion deploy/orioledb-cluster/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"title": "Version",
"description": "Cluster version.",
"type": "string",
"default": "postgresql-14.8.0"
"default": "orioledb-beta1"
},
"mode": {
"title": "Mode",
Expand Down
1 change: 1 addition & 0 deletions docs/user_docs/cli/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Cluster command.
* [kbcli cluster list-ops](kbcli_cluster_list-ops.md) - List all opsRequests.
* [kbcli cluster logs](kbcli_cluster_logs.md) - Access cluster log file.
* [kbcli cluster promote](kbcli_cluster_promote.md) - Promote a non-primary or non-leader instance as the new primary or leader of the cluster
* [kbcli cluster register](kbcli_cluster_register.md) - Pull the cluster chart to the local cache and register the type to 'create' sub-command
* [kbcli cluster restart](kbcli_cluster_restart.md) - Restart the specified components in the cluster.
* [kbcli cluster restore](kbcli_cluster_restore.md) - Restore a new cluster from backup.
* [kbcli cluster revoke-role](kbcli_cluster_revoke-role.md) - Revoke role from account
Expand Down
1 change: 1 addition & 0 deletions docs/user_docs/cli/kbcli_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Cluster command.
* [kbcli cluster list-ops](kbcli_cluster_list-ops.md) - List all opsRequests.
* [kbcli cluster logs](kbcli_cluster_logs.md) - Access cluster log file.
* [kbcli cluster promote](kbcli_cluster_promote.md) - Promote a non-primary or non-leader instance as the new primary or leader of the cluster
* [kbcli cluster register](kbcli_cluster_register.md) - Pull the cluster chart to the local cache and register the type to 'create' sub-command
* [kbcli cluster restart](kbcli_cluster_restart.md) - Restart the specified components in the cluster.
* [kbcli cluster restore](kbcli_cluster_restore.md) - Restore a new cluster from backup.
* [kbcli cluster revoke-role](kbcli_cluster_revoke-role.md) - Revoke role from account
Expand Down
1 change: 0 additions & 1 deletion docs/user_docs/cli/kbcli_cluster_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ kbcli cluster create [NAME] [flags]
* [kbcli cluster create kafka](kbcli_cluster_create_kafka.md) - Create a kafka cluster.
* [kbcli cluster create mongodb](kbcli_cluster_create_mongodb.md) - Create a mongodb cluster.
* [kbcli cluster create mysql](kbcli_cluster_create_mysql.md) - Create a mysql cluster.
* [kbcli cluster create neon](kbcli_cluster_create_neon.md) - Create a neon cluster.
* [kbcli cluster create postgresql](kbcli_cluster_create_postgresql.md) - Create a postgresql cluster.
* [kbcli cluster create redis](kbcli_cluster_create_redis.md) - Create a redis cluster.

Expand Down
122 changes: 122 additions & 0 deletions internal/cli/cluster/builtin_charts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package cluster

import (
"embed"
"fmt"
"io"
)

// embedConfig is the interface for the go embed chart
type embedConfig struct {
chartFS embed.FS
// chart file name, include the extension
name string
// chart alias, this alias will be used as the command alias
alias string
}

var _ chartLoader = &embedConfig{}

func (e *embedConfig) register(subcmd ClusterType) error {
if _, ok := ClusterTypeCharts[subcmd]; ok {
return fmt.Errorf("cluster type %s already registered", subcmd)
}
ClusterTypeCharts[subcmd] = e
return nil
}

func (e *embedConfig) getAlias() string {
return e.alias
}

func (e *embedConfig) loadChart() (io.ReadCloser, error) {
return e.chartFS.Open(fmt.Sprintf("charts/%s", e.name))
}

func (e *embedConfig) getChartFileName() string {
return e.name
}

var (
// run `make generate` to generate this embed file
//go:embed charts/apecloud-mysql-cluster.tgz
mysqlChart embed.FS
//go:embed charts/postgresql-cluster.tgz
postgresqlChart embed.FS
//go:embed charts/kafka-cluster.tgz
kafkaChart embed.FS
//go:embed charts/redis-cluster.tgz
redisChart embed.FS
//go:embed charts/mongodb-cluster.tgz
mongodbChart embed.FS
)

// internal_chart registers embed chart

func init() {

mysql := &embedConfig{
chartFS: mysqlChart,
name: "apecloud-mysql-cluster.tgz",
alias: "",
}
if err := mysql.register("mysql"); err != nil {
fmt.Println(err.Error())
}

postgresql := &embedConfig{
chartFS: postgresqlChart,
name: "postgresql-cluster.tgz",
alias: "",
}
if err := postgresql.register("postgresql"); err != nil {
fmt.Println(err.Error())
}

kafka := &embedConfig{
chartFS: kafkaChart,
name: "kafka-cluster.tgz",
alias: "",
}
if err := kafka.register("kafka"); err != nil {
fmt.Println(err.Error())
}

redis := &embedConfig{
chartFS: redisChart,
name: "redis-cluster.tgz",
alias: "",
}
if err := redis.register("redis"); err != nil {
fmt.Println(err.Error())
}

mongodb := &embedConfig{
chartFS: mongodbChart,
name: "mongodb-cluster.tgz",
alias: "",
}
if err := mongodb.register("mongodb"); err != nil {
fmt.Println(err.Error())
}

}
47 changes: 7 additions & 40 deletions internal/cli/cluster/cluster_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package cluster

import (
"compress/gzip"
"embed"
"fmt"
"strings"

Expand Down Expand Up @@ -74,33 +73,6 @@ type ChartInfo struct {
Alias string
}

type (
// ClusterType is the type of the cluster
ClusterType string

// chartConfig is the helm chart config
chartConfig struct {
chartFS embed.FS

// chart file name, include the extension
name string

// chart alias, this alias will be used as the command alias
alias string
}
)

var clusterTypeCharts = map[ClusterType]chartConfig{}

// registerClusterType registers the cluster type, the ClusterType t will be used as
// the command name, the alias will be used as the command alias.
func registerClusterType(t ClusterType, chartFS embed.FS, name string, alias string) {
if _, ok := clusterTypeCharts[t]; ok {
panic(fmt.Sprintf("cluster type %s already registered", t))
}
clusterTypeCharts[t] = chartConfig{chartFS: chartFS, name: name, alias: alias}
}

func BuildChartInfo(t ClusterType) (*ChartInfo, error) {
var err error

Expand Down Expand Up @@ -248,12 +220,11 @@ func ValidateValues(c *ChartInfo, values map[string]interface{}) error {
}

func loadHelmChart(ci *ChartInfo, t ClusterType) error {
cf, ok := clusterTypeCharts[t]
cf, ok := ClusterTypeCharts[t]
if !ok {
return fmt.Errorf("failed to find the helm chart of %s", t)
}

file, err := cf.chartFS.Open(fmt.Sprintf("charts/%s", cf.name))
file, err := cf.loadChart()
if err != nil {
return err
}
Expand All @@ -262,7 +233,7 @@ func loadHelmChart(ci *ChartInfo, t ClusterType) error {
c, err := loader.LoadArchive(file)
if err != nil {
if err == gzip.ErrHeader {
return fmt.Errorf("file '%s' does not appear to be a valid chart file (details: %s)", cf.name, err)
return fmt.Errorf("file '%s' does not appear to be a valid chart file (details: %s)", cf.getChartFileName(), err)
}
}

Expand All @@ -271,22 +242,18 @@ func loadHelmChart(ci *ChartInfo, t ClusterType) error {
}

ci.Chart = c
ci.Alias = cf.alias
ci.Alias = cf.getAlias()
return nil
}

func SupportedTypes() []ClusterType {
types := maps.Keys(clusterTypeCharts)
slices.SortFunc(maps.Keys(clusterTypeCharts), func(i, j ClusterType) bool {
return i < j
types := maps.Keys(ClusterTypeCharts)
slices.SortFunc(types, func(i, j ClusterType) bool {
return i.String() < j.String()
})
return types
}

func (t ClusterType) String() string {
return string(t)
}

func (s SchemaPropName) String() string {
return string(s)
}
Loading

0 comments on commit 4ea3925

Please sign in to comment.