Skip to content

Commit

Permalink
feat: add new cli option '--use-greptime-cn-artifacts'" (#143)
Browse files Browse the repository at this point in the history
Signed-off-by: zyy17 <[email protected]>
  • Loading branch information
zyy17 authored Aug 28, 2023
1 parent fbf59e3 commit 6a86dbd
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 29 deletions.
20 changes: 14 additions & 6 deletions pkg/cmd/gtctl/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type ClusterCliOptions struct {
Timeout int
DryRun bool
Set configValues

// If UseGreptimeCNArtifacts is true, the creation will download the artifacts(charts and binaries) from 'downloads.greptime.cn'.
// Also, it will use ACR registry for charts images.
UseGreptimeCNArtifacts bool
}

func NewCreateClusterCommand(l logger.Logger) *cobra.Command {
Expand Down Expand Up @@ -102,6 +106,7 @@ func NewCreateClusterCommand(l logger.Logger) *cobra.Command {
cmd.Flags().StringVar(&options.Config, "config", "", "Configuration to deploy the greptimedb cluster on bare-metal environment.")
cmd.Flags().BoolVar(&options.AlwaysDownload, "always-download", false, "If true, always download the binary.")
cmd.Flags().BoolVar(&options.RetainLogs, "retain-logs", true, "If true, always retain the logs of binary.")
cmd.Flags().BoolVar(&options.UseGreptimeCNArtifacts, "use-greptime-cn-artifacts", false, "If true, use greptime-cn artifacts(charts and binaries).")

return cmd
}
Expand Down Expand Up @@ -234,6 +239,7 @@ func deployGreptimeDBOperator(ctx context.Context, l logger.Logger, options *Clu
GreptimeDBOperatorChartVersion: options.GreptimeDBOperatorChartVersion,
ImageRegistry: options.ImageRegistry,
ConfigValues: options.Set.operatorConfig,
UseGreptimeCNArtifacts: options.UseGreptimeCNArtifacts,
}

name := types.NamespacedName{Namespace: options.OperatorNamespace, Name: "greptimedb-operator"}.String()
Expand All @@ -257,12 +263,13 @@ func deployEtcdCluster(ctx context.Context, l logger.Logger, options *ClusterCli
}

createEtcdClusterOptions := &deployer.CreateEtcdClusterOptions{
ImageRegistry: options.ImageRegistry,
EtcdChartVersion: options.EtcdChartVersion,
EtcdStorageClassName: options.EtcdStorageClassName,
EtcdStorageSize: options.EtcdStorageSize,
EtcdClusterSize: options.EtcdClusterSize,
ConfigValues: options.Set.etcdConfig,
ImageRegistry: options.ImageRegistry,
EtcdChartVersion: options.EtcdChartVersion,
EtcdStorageClassName: options.EtcdStorageClassName,
EtcdStorageSize: options.EtcdStorageSize,
EtcdClusterSize: options.EtcdClusterSize,
ConfigValues: options.Set.etcdConfig,
UseGreptimeCNArtifacts: options.UseGreptimeCNArtifacts,
}

var name string
Expand Down Expand Up @@ -300,6 +307,7 @@ func deployGreptimeDBCluster(ctx context.Context, l logger.Logger, options *Clus
DatanodeStorageRetainPolicy: options.StorageRetainPolicy,
EtcdEndPoint: fmt.Sprintf("%s.%s:2379", common.EtcdClusterName(clusterName), options.EtcdNamespace),
ConfigValues: options.Set.clusterConfig,
UseGreptimeCNArtifacts: options.UseGreptimeCNArtifacts,
}

var name string
Expand Down
24 changes: 20 additions & 4 deletions pkg/deployer/k8s/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type deployer struct {
dryRun bool
}

const (
AliCloudRegistry = "greptime-registry.cn-hangzhou.cr.aliyuncs.com"
)

var _ Interface = &deployer{}

type Option func(*deployer)
Expand Down Expand Up @@ -118,7 +122,11 @@ func (d *deployer) CreateGreptimeDBCluster(ctx context.Context, name string, opt
return err
}

manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.GreptimeDBChartName, options.GreptimeDBChartVersion, *options)
if options.UseGreptimeCNArtifacts && options.ImageRegistry == "" {
options.ConfigValues += fmt.Sprintf("image.registry=%s,initializer.registry=%s,", AliCloudRegistry, AliCloudRegistry)
}

manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.GreptimeDBChartName, options.GreptimeDBChartVersion, options.UseGreptimeCNArtifacts, *options)
if err != nil {
return err
}
Expand Down Expand Up @@ -169,11 +177,15 @@ func (d *deployer) CreateEtcdCluster(ctx context.Context, name string, options *

// TODO(zyy17): Maybe we can set this in the top level configures.
const (
disableRBACConfig = "auth.rbac.create=false,auth.rbac.token.enabled=false"
disableRBACConfig = "auth.rbac.create=false,auth.rbac.token.enabled=false,"
)
options.ConfigValues += disableRBACConfig

manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.EtcdBitnamiOCIRegistry, helm.DefaultEtcdChartVersion, *options)
if options.UseGreptimeCNArtifacts && options.ImageRegistry == "" {
options.ConfigValues += fmt.Sprintf("image.registry=%s,", AliCloudRegistry)
}

manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.EtcdBitnamiOCIRegistry, helm.DefaultEtcdChartVersion, options.UseGreptimeCNArtifacts, *options)
if err != nil {
return fmt.Errorf("error while loading helm chart: %v", err)
}
Expand Down Expand Up @@ -205,7 +217,11 @@ func (d *deployer) CreateGreptimeDBOperator(ctx context.Context, name string, op
return err
}

manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.GreptimeDBOperatorChartName, options.GreptimeDBOperatorChartVersion, *options)
if options.UseGreptimeCNArtifacts && options.ImageRegistry == "" {
options.ConfigValues += fmt.Sprintf("image.registry=%s,", AliCloudRegistry)
}

manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.GreptimeDBOperatorChartName, options.GreptimeDBOperatorChartVersion, options.UseGreptimeCNArtifacts, *options)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/deployer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type ListGreptimeDBClustersOptions struct{}
// CreateGreptimeDBClusterOptions is the options to create a GreptimeDB cluster.
type CreateGreptimeDBClusterOptions struct {
GreptimeDBChartVersion string
UseGreptimeCNArtifacts bool

ImageRegistry string `helm:"image.registry"`
InitializerImageRegistry string `helm:"initializer.registry"`
Expand All @@ -89,7 +90,8 @@ type DeleteGreptimeDBClusterOption struct{}

// CreateEtcdClusterOptions is the options to create an etcd cluster.
type CreateEtcdClusterOptions struct {
EtcdChartVersion string
EtcdChartVersion string
UseGreptimeCNArtifacts bool

// The parameters reference: https://artifacthub.io/packages/helm/bitnami/etcd.
EtcdClusterSize string `helm:"replicaCount"`
Expand All @@ -105,6 +107,7 @@ type DeleteEtcdClusterOption struct{}
// CreateGreptimeDBOperatorOptions is the options to create a GreptimeDB operator.
type CreateGreptimeDBOperatorOptions struct {
GreptimeDBOperatorChartVersion string
UseGreptimeCNArtifacts bool

ImageRegistry string `helm:"image.registry"`
ConfigValues string `helm:"*"`
Expand Down
1 change: 1 addition & 0 deletions pkg/helm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (

GreptimeChartIndexURL = "https://raw.githubusercontent.com/GreptimeTeam/helm-charts/gh-pages/index.yaml"
GreptimeChartReleaseDownloadURL = "https://github.com/GreptimeTeam/helm-charts/releases/download"
GreptimeCNCharts = "https://downloads.greptime.cn/releases/charts"

GreptimeDBChartName = "greptimedb"
GreptimeDBOperatorChartName = "greptimedb-operator"
Expand Down
53 changes: 36 additions & 17 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func WithChartsCacheDir(chartsCacheDir string) func(*Manager) {
}

// LoadAndRenderChart loads the chart from the remote charts and render the manifests with the values.
func (r *Manager) LoadAndRenderChart(ctx context.Context, name, namespace, chartName, chartVersion string, options interface{}) ([]byte, error) {
func (r *Manager) LoadAndRenderChart(ctx context.Context, name, namespace, chartName, chartVersion string, useGreptimeCNArtifacts bool, options interface{}) ([]byte, error) {
values, err := r.generateHelmValues(options)
if err != nil {
return nil, err
Expand All @@ -104,11 +104,17 @@ func (r *Manager) LoadAndRenderChart(ctx context.Context, name, namespace, chart
return nil, err
}
} else {
downloadURL, err := r.getChartDownloadURL(ctx, chartName, chartVersion)
downloadURL, err := r.getChartDownloadURL(ctx, chartName, chartVersion, useGreptimeCNArtifacts)
if err != nil {
return nil, err
}
helmChart, err = r.loadChartFromRemoteCharts(ctx, downloadURL)

alwaysDownload := false
if chartVersion == "" { // always download the latest version.
alwaysDownload = true
}

helmChart, err = r.loadChartFromRemoteCharts(ctx, downloadURL, alwaysDownload)
if err != nil {
return nil, err
}
Expand All @@ -123,7 +129,7 @@ func (r *Manager) LoadAndRenderChart(ctx context.Context, name, namespace, chart
return manifests, nil
}

func (r *Manager) loadChartFromRemoteCharts(ctx context.Context, downloadURL string) (*chart.Chart, error) {
func (r *Manager) loadChartFromRemoteCharts(ctx context.Context, downloadURL string, alwaysDownload bool) (*chart.Chart, error) {
parsedURL, err := url.Parse(downloadURL)
if err != nil {
return nil, err
Expand All @@ -134,7 +140,7 @@ func (r *Manager) loadChartFromRemoteCharts(ctx context.Context, downloadURL str
cachePath = filepath.Join(r.chartsCacheDir, packageName)
)

if r.isInChartsCache(packageName) {
if !alwaysDownload && r.isInChartsCache(packageName) {
data, err := os.ReadFile(cachePath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -323,29 +329,42 @@ func (r *Manager) newHelmClient(releaseName, namespace string) (*action.Install,
return helmClient, nil
}

func (r *Manager) getChartDownloadURL(ctx context.Context, chartName, version string) (string, error) {
indexFile, err := r.getIndexFile(ctx, GreptimeChartIndexURL)
if err != nil {
return "", err
}
func (r *Manager) getChartDownloadURL(ctx context.Context, chartName, version string, useGreptimeCNArtifacts bool) (string, error) {
// Get the latest version from index file of GitHub repo.
if !useGreptimeCNArtifacts && version == "" {
indexFile, err := r.getIndexFile(ctx, GreptimeChartIndexURL)
if err != nil {
return "", err
}

var downloadURL string
if version == "" {
chartVersion, err := r.getLatestChart(indexFile, chartName)
if err != nil {
return "", err
}
downloadURL = chartVersion.URLs[0]

downloadURL := chartVersion.URLs[0]
r.logger.V(3).Infof("get latest chart '%s', version '%s', url: '%s'",
chartName, chartVersion.Version, downloadURL)
} else {
// The download URL example: 'https://github.com/GreptimeTeam/helm-charts/releases/download/greptimedb-0.1.1-alpha.3/greptimedb-0.1.1-alpha.3.tgz'.
chartName := chartName + "-" + version
downloadURL = fmt.Sprintf("%s/%s/%s.tgz", GreptimeChartReleaseDownloadURL, chartName, chartName)
return downloadURL, nil
}

if useGreptimeCNArtifacts {
if version == "" {
version = "latest"
}

// The download URL example: 'https://downloads.greptime.cn/releases/charts/etcd/9.2.0/etcd-9.2.0.tgz'.
downloadURL := fmt.Sprintf("%s/%s/%s/%s.tgz", GreptimeCNCharts, chartName, version, chartName+"-"+version)
r.logger.V(3).Infof("get given version chart '%s', version '%s', url: '%s'",
chartName, version, downloadURL)
return downloadURL, nil
}

// The download URL example: 'https://github.com/GreptimeTeam/helm-charts/releases/download/greptimedb-0.1.1-alpha.3/greptimedb-0.1.1-alpha.3.tgz'.
downloadURL := fmt.Sprintf("%s/%s/%s.tgz", GreptimeChartReleaseDownloadURL, chartName, chartName+"-"+version)
r.logger.V(3).Infof("get given version chart '%s', version '%s', url: '%s'",
chartName, version, downloadURL)

return downloadURL, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestLoadAndRenderChart(t *testing.T) {
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
manifests, err := r.LoadAndRenderChart(ctx, tt.name, tt.namespace, tt.chartName, tt.chartVersion, tt.values)
manifests, err := r.LoadAndRenderChart(ctx, tt.name, tt.namespace, tt.chartName, tt.chartVersion, false, tt.values)
if err != nil {
t.Errorf("failed to load and render chart: %v", err)
}
Expand Down

0 comments on commit 6a86dbd

Please sign in to comment.