Skip to content

Commit

Permalink
refactor: pull artifacts from remote charts
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 committed Nov 14, 2022
1 parent de82668 commit 3066037
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 53 deletions.
10 changes: 0 additions & 10 deletions charts/charts.go

This file was deleted.

Binary file removed charts/greptimedb-0.1.0.tgz
Binary file not shown.
Binary file removed charts/greptimedb-operator-0.1.0-alpha.2.tgz
Binary file not shown.
30 changes: 18 additions & 12 deletions cmd/app/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type createClusterCliOptions struct {
FrontendImage string
DatanodeImage string
EtcdImage string
Version string
OperatorVersion string

DryRun bool
Timeout int
Expand All @@ -42,10 +44,11 @@ func NewCreateClusterCommand(l log.Logger) *cobra.Command {
}

createOperatorOptions := &manager.CreateOperatorOptions{
OperatorImage: options.OperatorImage,
Namespace: options.OperatorNamespace,
Timeout: time.Duration(options.Timeout) * time.Second,
DryRun: options.DryRun,
OperatorImage: options.OperatorImage,
Namespace: options.OperatorNamespace,
Timeout: time.Duration(options.Timeout) * time.Second,
DryRun: options.DryRun,
OperatorVersion: options.OperatorVersion,
}

var (
Expand All @@ -66,14 +69,15 @@ func NewCreateClusterCommand(l log.Logger) *cobra.Command {

l.Infof("☕️ Start to create GreptimeDB cluster...\n")
createClusterOptions := &manager.CreateClusterOptions{
ClusterName: args[0],
Namespace: options.Namespace,
FrontendImage: options.FrontendImage,
MetaImage: options.MetaImage,
DatanodeImage: options.DatanodeImage,
EtcdImage: options.EtcdImage,
Timeout: time.Duration(options.Timeout) * time.Second,
DryRun: options.DryRun,
ClusterName: args[0],
Namespace: options.Namespace,
FrontendImage: options.FrontendImage,
MetaImage: options.MetaImage,
DatanodeImage: options.DatanodeImage,
EtcdImage: options.EtcdImage,
Timeout: time.Duration(options.Timeout) * time.Second,
DryRun: options.DryRun,
GreptimeDBVersion: options.Version,
}

if err := log.StartSpinning("Creating GreptimeDB cluster", func() error {
Expand All @@ -100,6 +104,8 @@ func NewCreateClusterCommand(l log.Logger) *cobra.Command {
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "default", "Namespace of GreptimeDB cluster.")
cmd.Flags().BoolVar(&options.DryRun, "dry-run", false, "Output the manifests without applying them.")
cmd.Flags().IntVar(&options.Timeout, "timeout", -1, "Timeout in seconds for the command to complete, default is no timeout.")
cmd.Flags().StringVar(&options.Version, "version", "0.1.0", "The GreptimeDB version.")
cmd.Flags().StringVar(&options.OperatorVersion, "operator-version", "0.1.0-alpha.2", "The greptimedb-operator version.")

return cmd
}
28 changes: 16 additions & 12 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"

"github.com/GreptimeTeam/gtctl/charts"
)

type TemplateRender interface {
Expand All @@ -21,19 +21,23 @@ type Render struct{}

var _ TemplateRender = &Render{}

// TODO(zyy17): Support remote charts.

func (r *Render) LoadChartFromLocalDirectory(directory string) (*chart.Chart, error) {
return loader.LoadDir(directory)
}
func (r *Render) LoadChartFromRemoteCharts(downloadURL string) (*chart.Chart, error) {
rsp, err := http.Get(downloadURL)
if err != nil {
return nil, err
}
defer rsp.Body.Close()

func (r *Render) LoadChartFromEmbedCharts(application, version string) (*chart.Chart, error) {
chartName := fmt.Sprintf("%s-%s.tgz", application, version)
content, err := charts.Charts.ReadFile(chartName)
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, fmt.Errorf("chart '%s' not found: %s", chartName, err)
return nil, err
}
return loader.LoadArchive(bytes.NewReader(content))

return loader.LoadArchive(bytes.NewReader(body))
}

func (r *Render) LoadChartFromLocalDirectory(directory string) (*chart.Chart, error) {
return loader.LoadDir(directory)
}

func (r *Render) GenerateManifests(releaseName, namespace string, chart *chart.Chart, values map[string]interface{}) ([]byte, error) {
Expand Down
6 changes: 1 addition & 5 deletions pkg/manager/constants.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package manager

const (
defaultOperatorReleaseName = "greptimedb-operator"

defaultOperatorReleaseName = "greptimedb-operator"
defaultOperatorHelmPackageName = "greptimedb-operator"
defaultGreptimeDBHelmPackageName = "greptimedb"

defaultOperatorHelmPackageVersion = "0.1.0-alpha.2"
defaultGreptimeDBHelmPackageVersion = "0.1.0"
)
42 changes: 28 additions & 14 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ package manager
import (
"context"
"fmt"

"helm.sh/helm/v3/pkg/strvals"
"strings"
"time"

"helm.sh/helm/v3/pkg/strvals"

greptimedbv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/gtctl/pkg/helm"
"github.com/GreptimeTeam/gtctl/pkg/kube"
"github.com/GreptimeTeam/gtctl/pkg/log"
"github.com/GreptimeTeam/gtctl/pkg/utils"
)

const (
defaultChartsURL = "https://github.com/GreptimeTeam/helm-charts/releases/download"
)

// Manager manage the cluster resources.
type Manager interface {
GetCluster(ctx context.Context, options *GetClusterOptions) (*greptimedbv1alpha1.GreptimeDBCluster, error)
Expand All @@ -33,14 +37,15 @@ type GetClusterOptions struct {
type ListClusterOptions struct{}

type CreateClusterOptions struct {
ClusterName string
Namespace string
MetaImage string
FrontendImage string
DatanodeImage string
EtcdImage string
Timeout time.Duration
DryRun bool
ClusterName string
Namespace string
MetaImage string
FrontendImage string
DatanodeImage string
EtcdImage string
Timeout time.Duration
DryRun bool
GreptimeDBVersion string
}

type UpdateClusterOptions struct {
Expand All @@ -57,8 +62,9 @@ type DeleteClusterOption struct {
}

type CreateOperatorOptions struct {
OperatorImage string
Namespace string
OperatorImage string
Namespace string
OperatorVersion string

Timeout time.Duration

Expand Down Expand Up @@ -107,7 +113,11 @@ func (m *manager) CreateCluster(ctx context.Context, options *CreateClusterOptio
return err
}

chart, err := m.render.LoadChartFromEmbedCharts(defaultGreptimeDBHelmPackageName, defaultGreptimeDBHelmPackageVersion)
// The download URL example: https://github.com/GreptimeTeam/helm-charts/releases/download/greptimedb-0.1.0/greptimedb-0.1.0.tgz
chartName := defaultGreptimeDBHelmPackageName + "-" + options.GreptimeDBVersion
downloadURL := fmt.Sprintf("%s/%s/%s.tgz", defaultChartsURL, chartName, chartName)

chart, err := m.render.LoadChartFromRemoteCharts(downloadURL)
if err != nil {
return err
}
Expand Down Expand Up @@ -165,7 +175,11 @@ func (m *manager) CreateOperator(ctx context.Context, options *CreateOperatorOpt
return err
}

chart, err := m.render.LoadChartFromEmbedCharts(defaultOperatorHelmPackageName, defaultOperatorHelmPackageVersion)
// The download URL example: https://github.com/GreptimeTeam/helm-charts/releases/download/greptimedb-operator-0.1.0-alpha.2/greptimedb-operator-0.1.0-alpha.2.tgz
chartName := defaultOperatorHelmPackageName + "-" + options.OperatorVersion
downloadURL := fmt.Sprintf("%s/%s/%s.tgz", defaultChartsURL, chartName, chartName)

chart, err := m.render.LoadChartFromRemoteCharts(downloadURL)
if err != nil {
return err
}
Expand Down

0 comments on commit 3066037

Please sign in to comment.