generated from synopkg/meshplay-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.go
187 lines (171 loc) · 5.12 KB
/
install.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
// Copyright 2020 Layer5, 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 consul
import (
"fmt"
"sync"
"github.com/layer5io/meshery-adapter-library/adapter"
"github.com/layer5io/meshery-adapter-library/meshes"
opstatus "github.com/layer5io/meshery-adapter-library/status"
"github.com/layer5io/meshkit/utils"
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
)
const (
repo = "https://helm.releases.hashicorp.com"
chart = "consul"
)
func (h *Consul) installConsul(del bool, version, namespace string, kubeconfigs []string) (string, error) {
h.Log.Debug(fmt.Sprintf("Requested install of version: %s", version))
h.Log.Debug(fmt.Sprintf("Requested action is delete: %v", del))
h.Log.Debug(fmt.Sprintf("Requested action is in namespace: %s", namespace))
st := opstatus.Installing
if del {
st = opstatus.Removing
}
err := h.Config.GetObject(adapter.MeshSpecKey, h)
if err != nil {
return st, ErrMeshConfig(err)
}
_, err = h.applyHelmChart(del, version, namespace, kubeconfigs)
if err != nil {
return st, ErrApplyHelmChart(err)
}
st = opstatus.Installed
if del {
st = opstatus.Removed
}
return st, nil
}
func (h *Consul) applyManifests(request adapter.OperationRequest, operation adapter.Operation, kubeconfigs []string) (string, error) {
status := opstatus.Installing
if request.IsDeleteOperation {
status = opstatus.Removing
}
h.Log.Info(fmt.Sprintf("%s %s", status, operation.Description))
var errs []error
var wg sync.WaitGroup
var errMx sync.Mutex
for _, k8sconfig := range kubeconfigs {
wg.Add(1)
go func(k8sconfig string) {
defer wg.Done()
kClient, err := mesherykube.New([]byte(k8sconfig))
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
if operation.Type == int32(meshes.OpCategory_CUSTOM) {
err := kClient.ApplyManifest([]byte(request.CustomBody), mesherykube.ApplyOptions{
Namespace: request.Namespace,
Update: true,
Delete: request.IsDeleteOperation,
})
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
} else {
for _, template := range operation.Templates {
tpl := []byte(template.String())
merged, err := utils.MergeToTemplate(tpl, map[string]string{"namespace": request.Namespace})
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
continue
}
err = kClient.ApplyManifest(merged, mesherykube.ApplyOptions{
Namespace: request.Namespace,
Update: true,
Delete: request.IsDeleteOperation,
})
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
continue
}
}
}
}(k8sconfig)
}
wg.Wait()
if len(errs) != 0 {
return status, ErrApplyOperation(mergeErrors(errs))
}
return opstatus.Deployed, nil
}
// applyHelmChart installs or deletes an application packaged as Helm chart.
//
// Information about the Helm chart is specified in operation additional properties (keys defined in keys.go):
// the repository, the chart, the version, and the name of a values-file.
// The chart is the only required value, defaults are handled by ApplyHelmChart from meshkit.
// The values-file is expected in the consul/config_templates folder. It corresponds to a file specified
// by the -f (--values) flag of the Helm CLI.
func (h *Consul) applyHelmChart(del bool, version string, ns string, kubeconfigs []string) (string, error) {
status := opstatus.Installing
var act mesherykube.HelmChartAction
if del {
status = opstatus.Removing
act = mesherykube.UNINSTALL
} else {
act = mesherykube.INSTALL
}
var errs []error
var errMx sync.Mutex
var wg sync.WaitGroup
for _, kubeconfig := range kubeconfigs {
wg.Add(1)
go func(kubeconfig string) {
defer wg.Done()
kClient, err := mesherykube.New([]byte(kubeconfig))
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
err = kClient.ApplyHelmChart(mesherykube.ApplyHelmChartConfig{
Namespace: ns,
CreateNamespace: true,
Action: act,
OverrideValues: map[string]interface{}{
"server": map[string]interface{}{
"affinity": nil, //By default Consul does not allow more than one server pods on a single node
},
},
ChartLocation: mesherykube.HelmChartLocation{
Repository: repo,
Chart: chart,
Version: version,
},
})
if err != nil {
errMx.Lock()
errs = append(errs, err)
errMx.Unlock()
return
}
}(kubeconfig)
}
wg.Wait()
if len(errs) != 0 {
return status, ErrApplyOperation(mergeErrors(errs))
}
return opstatus.Deployed, nil
}