Skip to content

Commit

Permalink
refactor: pull artifacts from remote charts (#27)
Browse files Browse the repository at this point in the history
* refactor: pull artifacts from remote charts

* refactor: modify default operator version
  • Loading branch information
zyy17 committed Nov 15, 2022
1 parent aec3dbc commit 3b8816f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 38 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.
14 changes: 10 additions & 4 deletions cmd/app/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type createClusterCliOptions struct {
StorageClassName string
StorageSize string
StorageRetainPolicy string
Version string
OperatorVersion string

DryRun bool
Timeout int
Expand All @@ -45,10 +47,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 Down Expand Up @@ -80,6 +83,7 @@ func NewCreateClusterCommand(l log.Logger) *cobra.Command {
StorageRetainPolicy: options.StorageRetainPolicy,
Timeout: time.Duration(options.Timeout) * time.Second,
DryRun: options.DryRun,
GreptimeDBVersion: options.Version,
}

if err := log.StartSpinning("Creating GreptimeDB cluster", func() error {
Expand Down Expand Up @@ -110,6 +114,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.4", "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"
)
30 changes: 23 additions & 7 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +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 Down Expand Up @@ -41,8 +46,10 @@ type CreateClusterOptions struct {
StorageClassName string
StorageSize string
StorageRetainPolicy string
Timeout time.Duration
DryRun bool
GreptimeDBVersion string

Timeout time.Duration
DryRun bool
}

type UpdateClusterOptions struct {
Expand All @@ -59,8 +66,9 @@ type DeleteClusterOption struct {
}

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

Timeout time.Duration

Expand Down Expand Up @@ -109,7 +117,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 @@ -167,7 +179,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 3b8816f

Please sign in to comment.