Skip to content

Commit

Permalink
Merge pull request #844 from jxs1211/feat-838
Browse files Browse the repository at this point in the history
feat: pkg/util/helm Enhancement (sub task of pkg/util Enhancement #83
  • Loading branch information
xavier-hou authored Jul 15, 2022
2 parents fbe0f86 + 047007e commit b646833
Show file tree
Hide file tree
Showing 5 changed files with 644 additions and 23 deletions.
64 changes: 41 additions & 23 deletions pkg/util/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,20 @@ type Helm struct {
helmclient.Client
}

func NewHelm(param *HelmParam) (*Helm, error) {
var hClient helmclient.Client
var err error
if hClient, err = helmclient.New(
type Option func(*Helm)

func NewHelm(param *HelmParam, option ...Option) (*Helm, error) {
hClient, err := helmclient.New(
&helmclient.Options{
Namespace: param.Chart.Namespace,
RepositoryCache: "/tmp/.helmcache",
RepositoryConfig: "/tmp/.helmrepo",
Debug: true,
},
); err != nil {
return nil, err
}

tmout, err := time.ParseDuration(param.Chart.Timeout)
)
if err != nil {
return nil, err
}

entry := &repo.Entry{
Name: param.Repo.Name,
URL: param.Repo.URL,
Expand All @@ -47,14 +42,15 @@ func NewHelm(param *HelmParam) (*Helm, error) {
InsecureSkipTLSverify: false,
PassCredentialsAll: false,
}

// 'Wait' will automatically be set to true when using Atomic.
atomic := true
if !param.Chart.Wait {
atomic = false
}

chartSpec := &helmclient.ChartSpec{
tmout, err := time.ParseDuration(param.Chart.Timeout)
if err != nil {
return nil, err
}
spec := &helmclient.ChartSpec{
ReleaseName: param.Chart.ReleaseName,
ChartName: param.Chart.ChartName,
Namespace: param.Chart.Namespace,
Expand All @@ -80,34 +76,56 @@ func NewHelm(param *HelmParam) (*Helm, error) {
CleanupOnFail: false,
DryRun: false,
}

helm := &Helm{
h := &Helm{
Entry: entry,
ChartSpec: chartSpec,
ChartSpec: spec,
Client: hClient,
}

if err = helm.AddOrUpdateChartRepo(*helm.Entry); err != nil {
for _, op := range option {
op(h)
}

if err = h.AddOrUpdateChartRepo(*entry); err != nil {
return nil, err
}
return h, nil
}

return helm, nil
func WithEntry(entry *repo.Entry) Option {
return func(r *Helm) {
r.Entry = entry
}
}

func WithChartSpec(spec *helmclient.ChartSpec) Option {
return func(r *Helm) {
r.ChartSpec = spec
}
}

func WithClient(client helmclient.Client) Option {
return func(r *Helm) {
r.Client = client
}
}

func (h *Helm) AddOrUpdateChartRepo(entry repo.Entry) error {
return h.Client.AddOrUpdateChartRepo(entry)
}

func (h *Helm) InstallOrUpgradeChart() error {
_, err := h.Client.InstallOrUpgradeChart(context.TODO(), h.ChartSpec)
return err
}

func (h *Helm) UninstallHelmChartRelease() error {
var err error
if err = h.UninstallReleaseByName(h.ChartSpec.ReleaseName); err != nil {
func (h *Helm) UninstallHelmChartRelease() (err error) {
if err = h.Client.UninstallReleaseByName(h.ChartSpec.ReleaseName); err != nil {
if strings.Contains(err.Error(), "not found") {
log.Warn("Release is not found, maybe it has been deleted.")
return nil
}
return err
}

return nil
}
Loading

0 comments on commit b646833

Please sign in to comment.