2021-01-19 11:49:50 +01:00
|
|
|
// +build kube
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
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 helm
|
|
|
|
|
|
|
|
import (
|
2021-01-28 14:14:13 +01:00
|
|
|
"context"
|
2021-01-19 11:49:50 +01:00
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
|
|
action "helm.sh/helm/v3/pkg/action"
|
|
|
|
chart "helm.sh/helm/v3/pkg/chart"
|
|
|
|
loader "helm.sh/helm/v3/pkg/chart/loader"
|
|
|
|
env "helm.sh/helm/v3/pkg/cli"
|
|
|
|
"helm.sh/helm/v3/pkg/release"
|
2021-01-28 14:14:13 +01:00
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
2021-01-19 11:49:50 +01:00
|
|
|
)
|
|
|
|
|
2021-01-26 09:43:12 +01:00
|
|
|
// Actions helm actions
|
|
|
|
type Actions struct {
|
2021-01-28 14:14:13 +01:00
|
|
|
Config *action.Configuration
|
|
|
|
Namespace string
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:12 +01:00
|
|
|
// NewHelmActions new helm action
|
2021-01-28 14:14:13 +01:00
|
|
|
func NewHelmActions(ctx context.Context, getter genericclioptions.RESTClientGetter) (*Actions, error) {
|
|
|
|
if getter == nil {
|
|
|
|
settings := env.New()
|
|
|
|
getter = settings.RESTClientGetter()
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 14:14:13 +01:00
|
|
|
namespace := "default"
|
|
|
|
if ns, _, err := getter.ToRawKubeConfigLoader().Namespace(); err == nil {
|
|
|
|
namespace = ns
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
2021-01-28 14:14:13 +01:00
|
|
|
actions := &Actions{
|
|
|
|
Config: &action.Configuration{
|
|
|
|
RESTClientGetter: getter,
|
|
|
|
},
|
|
|
|
Namespace: namespace,
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
2021-01-28 14:14:13 +01:00
|
|
|
err := actions.Config.Init(getter, namespace, "configmap", log.Printf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
2021-01-28 14:14:13 +01:00
|
|
|
|
|
|
|
return actions, actions.Config.KubeClient.IsReachable()
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:12 +01:00
|
|
|
//InstallChartFromDir install from dir
|
|
|
|
func (hc *Actions) InstallChartFromDir(name string, chartpath string) error {
|
2021-01-19 11:49:50 +01:00
|
|
|
chart, err := loader.Load(chartpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return hc.InstallChart(name, chart)
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:12 +01:00
|
|
|
// InstallChart instal chart
|
|
|
|
func (hc *Actions) InstallChart(name string, chart *chart.Chart) error {
|
2021-01-19 11:49:50 +01:00
|
|
|
actInstall := action.NewInstall(hc.Config)
|
|
|
|
actInstall.ReleaseName = name
|
2021-01-28 14:14:13 +01:00
|
|
|
actInstall.Namespace = hc.Namespace
|
2021-01-19 11:49:50 +01:00
|
|
|
|
|
|
|
release, err := actInstall.Run(chart, map[string]interface{}{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Println("Release status: ", release.Info.Status)
|
|
|
|
log.Println(release.Info.Description)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:12 +01:00
|
|
|
// Uninstall uninstall chart
|
|
|
|
func (hc *Actions) Uninstall(name string) error {
|
2021-01-19 11:49:50 +01:00
|
|
|
release, err := hc.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if release == nil {
|
2021-01-26 09:43:12 +01:00
|
|
|
return errors.New("no release found with the name provided")
|
2021-01-19 11:49:50 +01:00
|
|
|
}
|
|
|
|
actUninstall := action.NewUninstall(hc.Config)
|
|
|
|
response, err := actUninstall.Run(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Println(response.Release.Info.Description)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:12 +01:00
|
|
|
// Get get released object for a named chart
|
|
|
|
func (hc *Actions) Get(name string) (*release.Release, error) {
|
2021-01-19 11:49:50 +01:00
|
|
|
actGet := action.NewGet(hc.Config)
|
|
|
|
return actGet.Run(name)
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:43:12 +01:00
|
|
|
// ListReleases lists chart releases
|
|
|
|
func (hc *Actions) ListReleases() ([]compose.Stack, error) {
|
2021-01-19 11:49:50 +01:00
|
|
|
actList := action.NewList(hc.Config)
|
|
|
|
releases, err := actList.Run()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := []compose.Stack{}
|
|
|
|
for _, rel := range releases {
|
|
|
|
result = append(result, compose.Stack{
|
|
|
|
ID: rel.Name,
|
|
|
|
Name: rel.Name,
|
|
|
|
Status: string(rel.Info.Status),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|