-
Notifications
You must be signed in to change notification settings - Fork 104
/
istio.go
254 lines (232 loc) · 8.63 KB
/
istio.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package istio
import (
"context"
"fmt"
"github.com/layer5io/meshery-adapter-library/adapter"
"github.com/layer5io/meshery-adapter-library/common"
"github.com/layer5io/meshery-adapter-library/meshes"
"github.com/layer5io/meshery-adapter-library/status"
internalconfig "github.com/layer5io/meshery-istio/internal/config"
"github.com/layer5io/meshery-istio/istio/oam"
meshkitCfg "github.com/layer5io/meshkit/config"
"github.com/layer5io/meshkit/logger"
"github.com/layer5io/meshkit/models/oam/core/v1alpha1"
)
// Istio represents the istio adapter and embeds adapter.Adapter
type Istio struct {
adapter.Adapter // Type Embedded
}
// New initializes istio handler.
func New(c meshkitCfg.Handler, l logger.Handler, kc meshkitCfg.Handler) adapter.Handler {
return &Istio{
Adapter: adapter.Adapter{
Config: c,
Log: l,
KubeconfigHandler: kc,
},
}
}
// ApplyOperation applies the operation on istio
func (istio *Istio) ApplyOperation(ctx context.Context, opReq adapter.OperationRequest) error {
operations := make(adapter.Operations)
err := istio.Config.GetObject(adapter.OperationsKey, &operations)
if err != nil {
return err
}
e := &adapter.Event{
Operationid: opReq.OperationID,
Summary: status.Deploying,
Details: "Operation is not supported",
}
switch opReq.OperationName {
case internalconfig.IstioOperation:
go func(hh *Istio, ee *adapter.Event) {
version := string(operations[opReq.OperationName].Versions[0])
stat, err := hh.installIstio(opReq.IsDeleteOperation, false, version, opReq.Namespace)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s Istio service mesh", stat)
e.Details = err.Error()
hh.StreamErr(e, err)
return
}
ee.Summary = fmt.Sprintf("Istio service mesh %s successfully", stat)
ee.Details = fmt.Sprintf("The Istio service mesh is now %s.", stat)
hh.StreamInfo(e)
}(istio, e)
case common.BookInfoOperation, common.HTTPBinOperation, common.ImageHubOperation, common.EmojiVotoOperation:
go func(hh *Istio, ee *adapter.Event) {
appName := operations[opReq.OperationName].AdditionalProperties[common.ServiceName]
stat, err := hh.installSampleApp(opReq.Namespace, opReq.IsDeleteOperation, operations[opReq.OperationName].Templates)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s %s application", stat, appName)
e.Details = err.Error()
hh.StreamErr(e, err)
return
}
ee.Summary = fmt.Sprintf("%s application %s successfully", appName, stat)
ee.Details = fmt.Sprintf("The %s application is now %s.", appName, stat)
hh.StreamInfo(e)
}(istio, e)
case common.SmiConformanceOperation:
go func(hh *Istio, ee *adapter.Event) {
name := operations[opReq.OperationName].Description
_, err := hh.RunSMITest(adapter.SMITestOptions{
Ctx: context.TODO(),
OperationID: ee.Operationid,
Labels: map[string]string{
"istio-injection": "enabled",
},
Namespace: "meshery",
Manifest: string(operations[opReq.OperationName].Templates[0]),
Annotations: make(map[string]string),
})
if err != nil {
e.Summary = fmt.Sprintf("Error while %s %s test", status.Running, name)
e.Details = err.Error()
hh.StreamErr(e, err)
return
}
ee.Summary = fmt.Sprintf("%s test %s successfully", name, status.Completed)
ee.Details = ""
hh.StreamInfo(e)
}(istio, e)
case internalconfig.DenyAllPolicyOperation, internalconfig.StrictMTLSPolicyOperation, internalconfig.MutualMTLSPolicyOperation, internalconfig.DisableMTLSPolicyOperation:
go func(hh *Istio, ee *adapter.Event) {
stat, err := hh.applyPolicy(opReq.Namespace, opReq.IsDeleteOperation, operations[opReq.OperationName].Templates)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s policy", stat)
e.Details = err.Error()
hh.StreamErr(e, err)
return
}
ee.Summary = fmt.Sprintf("Policy %s successfully", status.Deployed)
ee.Details = ""
hh.StreamInfo(e)
}(istio, e)
case common.CustomOperation:
go func(hh *Istio, ee *adapter.Event) {
stat, err := hh.applyCustomOperation(opReq.Namespace, opReq.CustomBody, opReq.IsDeleteOperation)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s custom operation", stat)
e.Details = err.Error()
hh.StreamErr(e, err)
return
}
ee.Summary = fmt.Sprintf("Manifest %s successfully", status.Deployed)
ee.Details = ""
hh.StreamInfo(e)
}(istio, e)
case internalconfig.LabelNamespace:
go func(hh *Istio, ee *adapter.Event) {
err := hh.LoadNamespaceToMesh(opReq.Namespace, opReq.IsDeleteOperation)
operation := "enabled"
if opReq.IsDeleteOperation {
operation = "removed"
}
if err != nil {
e.Summary = fmt.Sprintf("Error while labelling %s", opReq.Namespace)
e.Details = err.Error()
hh.StreamErr(e, err)
return
}
ee.Summary = fmt.Sprintf("Label updated on %s namespace", opReq.Namespace)
ee.Details = fmt.Sprintf("ISTIO-INJECTION label %s on %s namespace", operation, opReq.Namespace)
hh.StreamInfo(e)
}(istio, e)
case internalconfig.PrometheusAddon, internalconfig.GrafanaAddon, internalconfig.KialiAddon, internalconfig.JaegerAddon, internalconfig.ZipkinAddon:
go func(hh *Istio, ee *adapter.Event) {
svcname := operations[opReq.OperationName].AdditionalProperties[common.ServiceName]
patches := make([]string, 0)
patches = append(patches, operations[opReq.OperationName].AdditionalProperties[internalconfig.ServicePatchFile])
_, err := hh.installAddon(opReq.Namespace, opReq.IsDeleteOperation, svcname, patches, operations[opReq.OperationName].Templates)
operation := "install"
if opReq.IsDeleteOperation {
operation = "uninstall"
}
if err != nil {
e.Summary = fmt.Sprintf("Error while %sing %s", operation, opReq.OperationName)
e.Details = err.Error()
hh.StreamErr(e, err)
return
}
ee.Summary = fmt.Sprintf("Succesfully %sed %s", operation, opReq.OperationName)
ee.Details = fmt.Sprintf("Succesfully %sed %s from the %s namespace", operation, opReq.OperationName, opReq.Namespace)
hh.StreamInfo(e)
}(istio, e)
case internalconfig.IstioVetOperation:
go func(hh *Istio, ee *adapter.Event) {
responseChan := make(chan *adapter.Event, 1)
go hh.RunVet(responseChan)
for msg := range responseChan {
switch msg.EType {
case int32(meshes.EventType_ERROR):
istio.StreamErr(msg, ErrIstioVet(fmt.Errorf(msg.Details)))
case int32(meshes.EventType_WARN):
istio.StreamWarn(msg, ErrIstioVet(fmt.Errorf(msg.Details)))
default:
istio.StreamInfo(msg)
}
}
istio.Log.Info("Done")
}(istio, e)
case internalconfig.EnvoyFilterOperation:
go func(hh *Istio, ee *adapter.Event) {
appName := operations[opReq.OperationName].AdditionalProperties[common.ServiceName]
patchFile := operations[opReq.OperationName].AdditionalProperties[internalconfig.FilterPatchFile]
stat, err := hh.patchWithEnvoyFilter(opReq.Namespace, opReq.IsDeleteOperation, appName, operations[opReq.OperationName].Templates, patchFile)
if err != nil {
e.Summary = fmt.Sprintf("Error while %s %s application", stat, appName)
e.Details = err.Error()
hh.StreamErr(e, err)
return
}
ee.Summary = fmt.Sprintf("%s application %s successfully", appName, stat)
ee.Details = fmt.Sprintf("The %s application is now %s.", appName, stat)
hh.StreamInfo(e)
}(istio, e)
default:
istio.StreamErr(e, ErrOpInvalid)
}
return nil
}
// ProcessOAM will handles the grpc invocation for handling OAM objects
func (istio *Istio) ProcessOAM(ctx context.Context, oamReq adapter.OAMRequest) (string, error) {
var comps []v1alpha1.Component
for _, acomp := range oamReq.OamComps {
comp, err := oam.ParseApplicationComponent(acomp)
if err != nil {
istio.Log.Error(ErrParseOAMComponent)
continue
}
comps = append(comps, comp)
}
config, err := oam.ParseApplicationConfiguration(oamReq.OamConfig)
if err != nil {
istio.Log.Error(ErrParseOAMConfig)
}
// If operation is delete then first HandleConfiguration and then handle the deployment
if oamReq.DeleteOp {
// Process configuration
msg2, err := istio.HandleApplicationConfiguration(config, oamReq.DeleteOp)
if err != nil {
return msg2, ErrProcessOAM(err)
}
// Process components
msg1, err := istio.HandleComponents(comps, oamReq.DeleteOp)
if err != nil {
return msg1 + "\n" + msg2, ErrProcessOAM(err)
}
return msg1 + "\n" + msg2, nil
}
// Process components
msg1, err := istio.HandleComponents(comps, oamReq.DeleteOp)
if err != nil {
return msg1, ErrProcessOAM(err)
}
// Process configuration
msg2, err := istio.HandleApplicationConfiguration(config, oamReq.DeleteOp)
if err != nil {
return msg1 + "\n" + msg2, ErrProcessOAM(err)
}
return msg1 + "\n" + msg2, nil
}