Skip to content

Commit

Permalink
refactor: add 'HelmManager' and simplify the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 committed Aug 23, 2023
1 parent c5cfb1a commit 6f2baf2
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 134 deletions.
94 changes: 11 additions & 83 deletions pkg/deployer/k8s/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ import (
)

type deployer struct {
render *helm.Render
client *kube.Client
timeout time.Duration
logger logger.Logger
dryRun bool
helmManager *helm.Manager
client *kube.Client
timeout time.Duration
logger logger.Logger
dryRun bool
}

var _ Interface = &deployer{}

type Option func(*deployer)

func NewDeployer(l logger.Logger, opts ...Option) (Interface, error) {
r, err := helm.NewRender()
hm, err := helm.NewManager(l)
if err != nil {
return nil, err
}

d := &deployer{
render: r,
logger: l,
helmManager: hm,
logger: l,
}

for _, opt := range opts {
Expand Down Expand Up @@ -118,27 +118,10 @@ func (d *deployer) CreateGreptimeDBCluster(ctx context.Context, name string, opt
return err
}

values, err := d.render.GenerateHelmValues(*options)
manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.GreptimeDBChartName, options.GreptimeDBChartVersion, options)
if err != nil {
return err
}
d.logger.V(3).Infof("create greptimedb cluster with values: %v", values)

downloadURL, err := d.getChartDownloadURL(ctx, GreptimeDBChartName, options.GreptimeDBChartVersion)
if err != nil {
return err
}

chart, err := d.render.LoadChartFromRemoteCharts(ctx, downloadURL)
if err != nil {
return err
}

manifests, err := d.render.GenerateManifests(ctx, resourceName, resourceNamespace, chart, values)
if err != nil {
return err
}
d.logger.V(3).Infof("create greptimedb cluster with manifests: %s", string(manifests))

if d.dryRun {
d.logger.V(0).Info(string(manifests))
Expand Down Expand Up @@ -190,23 +173,11 @@ func (d *deployer) CreateEtcdCluster(ctx context.Context, name string, options *
)
options.ConfigValues += disableRBACConfig

values, err := d.render.GenerateHelmValues(*options)
if err != nil {
return err
}
d.logger.V(3).Infof("create etcd cluster with values: %v", values)

chart, err := d.render.Pull(ctx, EtcdBitnamiOCIRegistry, DefaultEtcdChartVersion)
manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.EtcdBitnamiOCIRegistry, helm.DefaultEtcdChartVersion, options)
if err != nil {
return err
}

manifests, err := d.render.GenerateManifests(ctx, resourceName, resourceNamespace, chart, values)
if err != nil {
return err
}
d.logger.V(3).Infof("create etcd cluster with manifests: %s", string(manifests))

if d.dryRun {
d.logger.V(0).Info(string(manifests))
return nil
Expand Down Expand Up @@ -234,27 +205,10 @@ func (d *deployer) CreateGreptimeDBOperator(ctx context.Context, name string, op
return err
}

values, err := d.render.GenerateHelmValues(*options)
manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.GreptimeDBOperatorChartName, options.GreptimeDBOperatorChartVersion, options)
if err != nil {
return err
}
d.logger.V(3).Infof("create greptimedb-operator with values: %v", values)

downloadURL, err := d.getChartDownloadURL(ctx, GreptimeDBOperatorChartName, options.GreptimeDBOperatorChartVersion)
if err != nil {
return err
}

chart, err := d.render.LoadChartFromRemoteCharts(ctx, downloadURL)
if err != nil {
return err
}

manifests, err := d.render.GenerateManifests(ctx, GreptimeDBOperatorChartName, resourceNamespace, chart, values)
if err != nil {
return err
}
d.logger.V(3).Infof("create greptimedb-operator with manifests: %s", string(manifests))

if d.dryRun {
d.logger.V(0).Info(string(manifests))
Expand All @@ -280,29 +234,3 @@ func (d *deployer) splitNamescapedName(name string) (string, string, error) {

return split[0], split[1], nil
}

func (d *deployer) getChartDownloadURL(ctx context.Context, chartName, version string) (string, error) {
indexFile, err := d.render.GetIndexFile(ctx, GreptimeChartIndexURL)
if err != nil {
return "", err
}

var downloadURL string
if version == "" {
chartVersion, err := d.render.GetLatestChart(indexFile, chartName)
if err != nil {
return "", err
}
downloadURL = chartVersion.URLs[0]
d.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)
d.logger.V(3).Infof("get given version chart '%s', version '%s', url: '%s'",
chartName, version, downloadURL)
}

return downloadURL, nil
}
14 changes: 7 additions & 7 deletions pkg/deployer/k8s/constants.go → pkg/helm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
// 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 k8s
// limitations under the License.r
package helm

const (
GreptimeDBChartName = "greptimedb"
GreptimeDBOperatorChartName = "greptimedb-operator"
DefaultChartsCache = ".gtctl/charts-cache"

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

EtcdBitnamiOCIRegistry = "oci://registry-1.docker.io/bitnamicharts/etcd"
DefaultEtcdChartVersion = "9.2.0"
GreptimeDBChartName = "greptimedb"
GreptimeDBOperatorChartName = "greptimedb-operator"
EtcdBitnamiOCIRegistry = "oci://registry-1.docker.io/bitnamicharts/etcd"
DefaultEtcdChartVersion = "9.2.0"
)
Loading

0 comments on commit 6f2baf2

Please sign in to comment.