forked from erda-project/erda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
183 lines (159 loc) · 4.29 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2021 Terminus, Inc.
//
// 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 (
"context"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/release"
)
const (
ActionInstall = iota
ActionUpgrade
ActionUninstall
)
var (
defaultTimeOut = 90 * time.Second
defaultCheckTime = 20 * time.Second
)
type Manager struct {
Charts []*ChartSpec
HelmClient Helm
LocalRepoName string
TimeOut time.Duration
}
type ChartSpec struct {
ReleaseName string
ChartName string
Version string
Action int
// overwrite values.yaml from chart, only use for install action
// like: key=value,key.nKey.value; you can use ParseValues convert map to values
Values string
}
func (m *Manager) Execute() error {
for _, chart := range m.Charts {
// check release spec result
isCompleted, err := m.checkDeployed(chart)
if err != nil {
return err
}
// release is spec status, skip execute
if isCompleted {
continue
}
// execute action
if err = m.ActionExecute(chart); err != nil {
return err
}
if err = m.controller(chart); err != nil {
return err
}
<-time.After(defaultCheckTime)
}
return nil
}
func (m *Manager) controller(chart *ChartSpec) error {
timeOut := defaultTimeOut
if m.TimeOut != 0 {
timeOut = m.TimeOut
}
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
for {
select {
case <-ctx.Done():
return fmt.Errorf("[%s] check status time out", chart.ReleaseName)
default:
res, err := m.checkDeployed(chart)
if err != nil {
return err
}
if res {
return nil
}
continue
}
}
}
func (m *Manager) ActionExecute(chart *ChartSpec) error {
switch chart.Action {
case ActionInstall:
return m.HelmClient.InstallRelease(chart.ReleaseName, m.transChartName(chart.ChartName), chart.Version,
chart.Values)
case ActionUpgrade:
return m.HelmClient.UpgradeRelease(chart.ReleaseName, m.LocalRepoName, chart.Version)
case ActionUninstall:
return m.HelmClient.UninstallRelease(chart.ReleaseName)
default:
return fmt.Errorf("not supported action")
}
}
// checkDeployed check release deployed
func (m *Manager) checkDeployed(chart *ChartSpec) (bool, error) {
logrus.Infof("[%s] check release status", chart.ChartName)
// Get release deploy history
releases, err := m.HelmClient.GetReleaseHistory(chart.ReleaseName)
if err != nil {
return false, err
}
var lr *release.Release
// release had exist on target cluster
if len(releases) != 0 {
lr = releases[len(releases)-1]
} else {
if chart.Action == ActionUninstall {
logrus.Infof("[%s] check chart uninstall success", chart.ChartName)
return true, nil
}
}
if lr == nil {
return false, nil
}
if lr.Chart.Metadata.Version != chart.Version {
logrus.Warnf("[%s] installed version %s, spce: %s", chart.ReleaseName, lr.Chart.Metadata.Version, chart.Version)
}
// check status by action type
switch chart.Action {
case ActionInstall:
if lr.Info.Status != "deployed" {
logrus.Infof("[%s] check status is %s", chart.ChartName, lr.Info.Status)
return false, nil
}
logrus.Infof("[%s] check chart install success", chart.ChartName)
return true, nil
case ActionUpgrade:
if lr.Info.Status != "deployed" {
logrus.Infof("[%s] check status is %s", chart.ChartName, lr.Info.Status)
return false, nil
}
logrus.Infof("[%s] check chart upgrade success", chart.ChartName)
return true, nil
case ActionUninstall:
return false, nil
}
return true, nil
}
func (m *Manager) transChartName(chartName string) string {
return fmt.Sprintf("%s/%s", m.LocalRepoName, chartName)
}
func ParseValues(preValues map[string]string) string {
values := make([]string, 0)
for k, v := range preValues {
values = append(values, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(values, ",")
}