/
client.go
229 lines (195 loc) · 5.32 KB
/
client.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package client
import (
"context"
"fmt"
"strings"
"github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/repo"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/cluster-init/config"
erdahelm "github.com/erda-project/erda/pkg/helm"
kc "github.com/erda-project/erda/pkg/k8sclient/config"
)
const (
defaultRepoName = "stable"
InstallModeRemote = "REMOTE"
RepoModeRemote = "REMOTE"
RepoModeLocal = "LOCAL"
LocalRepoPath = "/app/charts"
ErdaBaseCharts = "erda-base"
ErdaAddonsCharts = "erda-addons"
ErdaCharts = "erda"
)
type Option func(client *Client)
type Client struct {
config *config.Config
}
func New(opts ...Option) *Client {
c := Client{}
for _, op := range opts {
op(&c)
}
return &c
}
func WithConfig(cfg *config.Config) Option {
return func(c *Client) {
c.config = cfg
}
}
func (c *Client) Execute() error {
logrus.Debugf("load config: %+v", c.config)
opts, err := c.newHelmClientOptions()
if err != nil {
return fmt.Errorf("get helm client error: %v", err)
}
hc, err := erdahelm.New(opts...)
if err != nil {
return err
}
switch strings.ToUpper(c.config.RepoMode) {
case RepoModeRemote:
// TODO: support repo auth info.
e := &repo.Entry{Name: defaultRepoName, URL: c.config.RepoURL}
if err = hc.AddOrUpdateRepo(e); err != nil {
return err
}
}
if c.config.Reinstall {
charts := c.getInitCharts()
for _, chart := range charts {
chart.Action = erdahelm.ActionUninstall
}
m := erdahelm.Manager{
HelmClient: hc,
Charts: charts,
LocalRepoName: defaultRepoName,
}
if err := m.Execute(); err != nil {
return err
}
}
m := erdahelm.Manager{
HelmClient: hc,
Charts: c.getInitCharts(),
LocalRepoName: defaultRepoName,
}
if err := m.Execute(); err != nil {
return err
}
// Label node only local mode
// TODO: support label remote with rest.config
if strings.ToUpper(c.config.RepoMode) != InstallModeRemote {
rc, err := rest.InClusterConfig()
if err != nil {
logrus.Errorf("get incluster rest config error: %v", err)
return err
}
cs, err := kubernetes.NewForConfig(rc)
if err != nil {
logrus.Errorf("create clientSet error: %v", err)
return err
}
nodes, err := cs.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil {
logrus.Errorf("get kubernetes nodes error: %v", err)
return err
}
logrus.Infof("get nodes success, node count: %d", len(nodes.Items))
labels := c.parseLabels()
if len(labels) == 0 {
return nil
}
logrus.Infof("start to label nodes, labels: %+v", labels)
for _, node := range nodes.Items {
for k, v := range labels {
node.Labels[k] = v
}
if _, err = cs.CoreV1().Nodes().Update(context.Background(), &node, metav1.UpdateOptions{}); err != nil {
logrus.Errorf("label node: %s, error: %v", node.Name, err)
return err
}
}
}
return nil
}
// newHelmClientOptions create helm client options
func (c *Client) newHelmClientOptions() ([]erdahelm.Option, error) {
opts := make([]erdahelm.Option, 0)
switch strings.ToUpper(c.config.InstallMode) {
case InstallModeRemote:
b := bundle.New(bundle.WithClusterManager())
cluster, err := b.GetCluster(c.config.TargetCluster)
if err != nil {
return nil, err
}
rc, err := kc.ParseManageConfig(c.config.TargetCluster, cluster.ManageConfig)
if err != nil {
return nil, err
}
opts = append(opts, erdahelm.WithRESTClientGetter(erdahelm.NewRESTClientGetterImpl(rc)))
}
switch strings.ToUpper(c.config.RepoMode) {
case RepoModeLocal:
opts = append(opts, erdahelm.WithLocalChartDiscoverDir(LocalRepoPath))
}
return opts, nil
}
func (c *Client) getInitCharts() []*erdahelm.ChartSpec {
return []*erdahelm.ChartSpec{
{
ReleaseName: ErdaBaseCharts,
ChartName: ErdaBaseCharts,
Version: c.config.Version,
Action: erdahelm.ActionInstall,
Values: c.config.ChartErdaBaseValues,
},
{
ReleaseName: ErdaAddonsCharts,
ChartName: ErdaAddonsCharts,
Version: c.config.Version,
Action: erdahelm.ActionInstall,
Values: c.config.ChartErdaAddonsValues,
},
{
ReleaseName: ErdaCharts,
ChartName: ErdaCharts,
Version: c.config.Version,
Action: erdahelm.ActionInstall,
Values: c.config.ChartErdaValues,
},
}
}
func (c *Client) parseLabels() map[string]string {
res := make(map[string]string, 0)
if c.config.NodeLabels == "" {
return res
}
labels := strings.Split(c.config.NodeLabels, ",")
for _, label := range labels {
keys := strings.Split(label, "=")
switch len(keys) {
case 1:
res[keys[0]] = ""
case 2:
res[keys[0]] = keys[1]
default:
continue
}
}
return res
}