Skip to content

Commit 928fe8b

Browse files
authored
Feat: Adding namespace handle logic when Install()/Uninstall()... (#174)
* Feat: Adding namespace handle logic when Install()/Uninstall()... Signed-off-by: Daniel Hu <[email protected]> * Fix: param render error Signed-off-by: Daniel Hu <[email protected]>
1 parent ee274d9 commit 928fe8b

File tree

18 files changed

+203
-59
lines changed

18 files changed

+203
-59
lines changed

docs/argocd_plugin.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ tools:
1313
# options for the plugin
1414
# checkout the version from the GitHub releases
1515
options:
16+
# need to create the namespace or not, default: false
17+
create_namespace: false
1618
# Helm repo information
1719
repo:
1820
# name of the Helm repo
@@ -27,8 +29,6 @@ tools:
2729
release_name: argocd
2830
# k8s namespace where argocd will be installed
2931
namespace: argocd
30-
# need to create the namespace or not
31-
create_namespace: False
3232
# whether to wait for the release to be deployed or not
3333
wait: true
3434
# the time to wait for any individual Kubernetes operation (like Jobs for hooks)

docs/kube-prometheus_plugin.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ tools:
1515
version: 0.0.1
1616
# options for the plugin
1717
options:
18+
# need to create the namespace or not, default: false
19+
create_namespace: false
1820
# Helm repo information
1921
repo:
2022
# name of the Helm repo
@@ -29,8 +31,6 @@ tools:
2931
release_name: dev
3032
# k8s namespace where kube-prometheus will be installed
3133
namespace: monitoring
32-
# need to create the namespace or not
33-
create_namespace: True
3434
# whether to wait for the release to be deployed or not
3535
wait: true
3636
# the time to wait for any individual Kubernetes operation (like Jobs for hooks)

examples/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ tools:
5050
kind: argocd
5151
version: 0.0.2
5252
options:
53+
create_namespace: true
5354
repo:
5455
name: argo
5556
url: https://argoproj.github.io/argo-helm
@@ -80,6 +81,7 @@ tools:
8081
kind: kube-prometheus
8182
version: 0.0.2
8283
options:
84+
create_namespace: true
8385
repo:
8486
name: prometheus-community
8587
url: https://prometheus-community.github.io/helm-charts

internal/pkg/plugin/argocd/install.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77

88
"github.com/merico-dev/stream/internal/pkg/log"
99
"github.com/merico-dev/stream/pkg/util/helm"
10+
"github.com/merico-dev/stream/pkg/util/k8s"
1011
)
1112

1213
// Install installs ArgoCD with provided options.
1314
func Install(options *map[string]interface{}) (bool, error) {
14-
var param helm.HelmParam
15+
var param Param
1516
if err := mapstructure.Decode(*options, &param); err != nil {
1617
return false, err
1718
}
@@ -23,7 +24,11 @@ func Install(options *map[string]interface{}) (bool, error) {
2324
return false, fmt.Errorf("params are illegal")
2425
}
2526

26-
h, err := helm.NewHelm(&param)
27+
if err := dealWithNsWhenInstall(&param); err != nil {
28+
return false, err
29+
}
30+
31+
h, err := helm.NewHelm(param.GetHelmParam())
2732
if err != nil {
2833
return false, err
2934
}
@@ -35,3 +40,16 @@ func Install(options *map[string]interface{}) (bool, error) {
3540

3641
return true, nil
3742
}
43+
44+
func dealWithNsWhenInstall(param *Param) error {
45+
if !param.CreateNamespace {
46+
return nil
47+
}
48+
49+
kubeClient, err := k8s.NewClient()
50+
if err != nil {
51+
return err
52+
}
53+
54+
return kubeClient.CreateNamespace(param.Chart.Namespace)
55+
}

internal/pkg/plugin/argocd/ishealthy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/mitchellh/mapstructure"
77

88
"github.com/merico-dev/stream/internal/pkg/log"
9-
"github.com/merico-dev/stream/pkg/util/helm"
109
"github.com/merico-dev/stream/pkg/util/k8s"
1110
)
1211

@@ -22,7 +21,7 @@ const (
2221
)
2322

2423
func IsHealthy(options *map[string]interface{}) (bool, error) {
25-
var param helm.HelmParam
24+
var param Param
2625
if err := mapstructure.Decode(*options, &param); err != nil {
2726
return false, err
2827
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package argocd
2+
3+
import "github.com/merico-dev/stream/pkg/util/helm"
4+
5+
// NOTICE: Don't use:
6+
// type Param struct {
7+
// CreateNamespace bool `mapstructure:"create_namespace"`
8+
// helm.HelmParam
9+
// }
10+
// or
11+
// type Param struct {
12+
// CreateNamespace bool `mapstructure:"create_namespace"`
13+
// *helm.HelmParam
14+
// }
15+
// see pr #174 for more info
16+
17+
// Param is the struct for parameters used by the argocd package.
18+
type Param struct {
19+
CreateNamespace bool `mapstructure:"create_namespace"`
20+
Repo helm.Repo
21+
Chart helm.Chart
22+
}
23+
24+
func (p *Param) GetHelmParam() *helm.HelmParam {
25+
return &helm.HelmParam{
26+
Repo: p.Repo,
27+
Chart: p.Chart,
28+
}
29+
}

internal/pkg/plugin/argocd/reinstall.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import (
77

88
"github.com/merico-dev/stream/internal/pkg/log"
99
"github.com/merico-dev/stream/pkg/util/helm"
10-
"github.com/merico-dev/stream/pkg/util/k8s"
1110
)
1211

1312
func Reinstall(options *map[string]interface{}) (bool, error) {
14-
var param helm.HelmParam
13+
var param Param
1514
if err := mapstructure.Decode(*options, &param); err != nil {
1615
return false, err
1716
}
@@ -23,7 +22,7 @@ func Reinstall(options *map[string]interface{}) (bool, error) {
2322
return false, fmt.Errorf("params are illegal")
2423
}
2524

26-
h, err := helm.NewHelm(&param)
25+
h, err := helm.NewHelm(param.GetHelmParam())
2726
if err != nil {
2827
return false, err
2928
}
@@ -33,17 +32,15 @@ func Reinstall(options *map[string]interface{}) (bool, error) {
3332
return false, err
3433
}
3534

36-
// delete the namespace
37-
kubeClient, err := k8s.NewClient()
38-
if err != nil {
35+
if err := dealWithNsWhenUninstall(&param); err != nil {
3936
return false, err
4037
}
41-
if err = kubeClient.DeleteNamespace(param.Chart.Namespace); err != nil {
42-
log.Errorf("Failed to delete the %s namespace: %s", param.Chart.Namespace, err)
38+
39+
// install
40+
if err := dealWithNsWhenInstall(&param); err != nil {
4341
return false, err
4442
}
4543

46-
// install
4744
log.Info("Installing or updating argocd helm chart ...")
4845
if err = h.InstallOrUpgradeChart(); err != nil {
4946
return false, err

internal/pkg/plugin/argocd/uninstall.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func Uninstall(options *map[string]interface{}) (bool, error) {
14-
var param helm.HelmParam
14+
var param Param
1515
if err := mapstructure.Decode(*options, &param); err != nil {
1616
return false, err
1717
}
@@ -23,7 +23,7 @@ func Uninstall(options *map[string]interface{}) (bool, error) {
2323
return false, fmt.Errorf("params are illegal")
2424
}
2525

26-
h, err := helm.NewHelm(&param)
26+
h, err := helm.NewHelm(param.GetHelmParam())
2727
if err != nil {
2828
return false, err
2929
}
@@ -33,15 +33,22 @@ func Uninstall(options *map[string]interface{}) (bool, error) {
3333
return false, err
3434
}
3535

36-
// delete the namespace
37-
kubeClient, err := k8s.NewClient()
38-
if err != nil {
39-
return false, err
40-
}
41-
if err = kubeClient.DeleteNamespace(param.Chart.Namespace); err != nil {
42-
log.Errorf("Failed to delete the %s namespace: %s", param.Chart.Namespace, err)
36+
if err := dealWithNsWhenUninstall(&param); err != nil {
4337
return false, err
4438
}
4539

4640
return true, nil
4741
}
42+
43+
func dealWithNsWhenUninstall(param *Param) error {
44+
if !param.CreateNamespace {
45+
return nil
46+
}
47+
48+
kubeClient, err := k8s.NewClient()
49+
if err != nil {
50+
return err
51+
}
52+
53+
return kubeClient.DeleteNamespace(param.Chart.Namespace)
54+
}

internal/pkg/plugin/argocd/validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import (
55
)
66

77
// validate validates the options provided by the core.
8-
func validate(param *helm.HelmParam) []error {
9-
return helm.Validate(param)
8+
func validate(param *Param) []error {
9+
return helm.Validate(param.GetHelmParam())
1010
}

internal/pkg/plugin/kubeprometheus/install.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77

88
"github.com/merico-dev/stream/internal/pkg/log"
99
"github.com/merico-dev/stream/pkg/util/helm"
10+
"github.com/merico-dev/stream/pkg/util/k8s"
1011
)
1112

1213
// Install installs kube-prometheus with provided options.
1314
func Install(options *map[string]interface{}) (bool, error) {
14-
var param helm.HelmParam
15+
var param Param
1516
if err := mapstructure.Decode(*options, &param); err != nil {
1617
return false, err
1718
}
@@ -23,7 +24,11 @@ func Install(options *map[string]interface{}) (bool, error) {
2324
return false, fmt.Errorf("params are illegal")
2425
}
2526

26-
h, err := helm.NewHelm(&param)
27+
if err := dealWithNsWhenInstall(&param); err != nil {
28+
return false, err
29+
}
30+
31+
h, err := helm.NewHelm(param.GetHelmParam())
2732
if err != nil {
2833
return false, err
2934
}
@@ -35,3 +40,16 @@ func Install(options *map[string]interface{}) (bool, error) {
3540

3641
return true, nil
3742
}
43+
44+
func dealWithNsWhenInstall(param *Param) error {
45+
if !param.CreateNamespace {
46+
return nil
47+
}
48+
49+
kubeClient, err := k8s.NewClient()
50+
if err != nil {
51+
return err
52+
}
53+
54+
return kubeClient.CreateNamespace(param.Chart.Namespace)
55+
}

0 commit comments

Comments
 (0)