@@ -9,21 +9,45 @@ import (
9
9
"helm.sh/helm/v3/pkg/release"
10
10
"helm.sh/helm/v3/pkg/storage/driver"
11
11
"k8s.io/cli-runtime/pkg/genericclioptions"
12
+ "time"
12
13
)
13
14
15
+ // ActionOptions contains general helm action options
16
+ type ActionOptions struct {
17
+ // The duration to wait for helm operations. when zero, wait is disabled.
18
+ Timeout time.Duration
19
+ }
20
+
21
+ // ApplyOptions applies all ActionOption
22
+ func (opts * ActionOptions ) ApplyOptions (options []ActionOption ) {
23
+ for _ , option := range options {
24
+ option (opts )
25
+ }
26
+ }
27
+
28
+ // ActionOption configures ActionOptions.
29
+ type ActionOption func (opts * ActionOptions )
30
+
31
+ // WithTimeout configures timeout for helm action
32
+ func WithTimeout (timeout time.Duration ) ActionOption {
33
+ return func (opts * ActionOptions ) {
34
+ opts .Timeout = timeout
35
+ }
36
+ }
37
+
14
38
// ReleaseManager is responsible for manage helm releases
15
39
type ReleaseManager interface {
16
- // install or upgrade helm release
40
+ // InstallOrUpgradeRelease install or upgrade helm release
17
41
InstallOrUpgradeRelease (chartRepo string , chartName string ,
18
- namespace string , releaseName string , vals map [string ]interface {}) (* release.Release , error )
42
+ namespace string , releaseName string , vals map [string ]interface {}, opts ... ActionOption ) (* release.Release , error )
19
43
20
- // install helm release
44
+ // InstallRelease install helm release
21
45
InstallRelease (chartRepo string , chartName string ,
22
- namespace string , releaseName string , vals map [string ]interface {}) (* release.Release , error )
46
+ namespace string , releaseName string , vals map [string ]interface {}, opts ... ActionOption ) (* release.Release , error )
23
47
24
- // upgrade helm release
48
+ // UpgradeRelease upgrade helm release
25
49
UpgradeRelease (chartRepo string , chartName string ,
26
- namespace string , releaseName string , vals map [string ]interface {}) (* release.Release , error )
50
+ namespace string , releaseName string , vals map [string ]interface {}, opts ... ActionOption ) (* release.Release , error )
27
51
}
28
52
29
53
// NewDefaultReleaseManager constructs new defaultReleaseManager.
@@ -43,28 +67,34 @@ type defaultReleaseManager struct {
43
67
}
44
68
45
69
func (m * defaultReleaseManager ) InstallOrUpgradeRelease (chartRepo string , chartName string ,
46
- namespace string , releaseName string , vals map [string ]interface {}) (* release.Release , error ) {
70
+ namespace string , releaseName string , vals map [string ]interface {}, opts ... ActionOption ) (* release.Release , error ) {
47
71
actionCFG := m .obtainActionConfig (namespace )
48
72
historyAction := action .NewHistory (actionCFG )
49
73
historyAction .Max = 1
50
74
if _ , err := historyAction .Run (releaseName ); err == driver .ErrReleaseNotFound {
51
- return m .InstallRelease (chartRepo , chartName , namespace , releaseName , vals )
75
+ return m .InstallRelease (chartRepo , chartName , namespace , releaseName , vals , opts ... )
52
76
} else {
53
- return m .UpgradeRelease (chartRepo , chartName , namespace , releaseName , vals )
77
+ return m .UpgradeRelease (chartRepo , chartName , namespace , releaseName , vals , opts ... )
54
78
}
55
79
}
56
80
57
81
func (m * defaultReleaseManager ) InstallRelease (chartRepo string , chartName string ,
58
- namespace string , releaseName string , vals map [string ]interface {}) (* release.Release , error ) {
82
+ namespace string , releaseName string , vals map [string ]interface {}, opts ... ActionOption ) (* release.Release , error ) {
59
83
60
84
actionCFG := m .obtainActionConfig (namespace )
61
85
installAction := action .NewInstall (actionCFG )
62
86
installAction .ChartPathOptions .RepoURL = chartRepo
63
87
installAction .Namespace = namespace
64
88
installAction .SkipCRDs = false
65
- installAction .Wait = true
66
89
installAction .ReleaseName = releaseName
67
90
91
+ actionOpts := ActionOptions {}
92
+ actionOpts .ApplyOptions (opts )
93
+ if actionOpts .Timeout != 0 {
94
+ installAction .Wait = true
95
+ installAction .Timeout = actionOpts .Timeout
96
+ }
97
+
68
98
cp , err := installAction .ChartPathOptions .LocateChart (chartName , cli .New ())
69
99
if err != nil {
70
100
return nil , err
@@ -78,14 +108,20 @@ func (m *defaultReleaseManager) InstallRelease(chartRepo string, chartName strin
78
108
}
79
109
80
110
func (m * defaultReleaseManager ) UpgradeRelease (chartRepo string , chartName string ,
81
- namespace string , releaseName string , vals map [string ]interface {}) (* release.Release , error ) {
111
+ namespace string , releaseName string , vals map [string ]interface {}, opts ... ActionOption ) (* release.Release , error ) {
82
112
83
113
actionCFG := m .obtainActionConfig (namespace )
84
114
upgradeAction := action .NewUpgrade (actionCFG )
85
115
upgradeAction .ChartPathOptions .RepoURL = chartRepo
86
116
upgradeAction .Namespace = namespace
87
117
upgradeAction .ResetValues = true
88
- upgradeAction .Wait = true
118
+
119
+ actionOpts := ActionOptions {}
120
+ actionOpts .ApplyOptions (opts )
121
+ if actionOpts .Timeout != 0 {
122
+ upgradeAction .Wait = true
123
+ upgradeAction .Timeout = actionOpts .Timeout
124
+ }
89
125
90
126
cp , err := upgradeAction .ChartPathOptions .LocateChart (chartName , cli .New ())
91
127
if err != nil {
0 commit comments