-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathistio.go
192 lines (180 loc) · 6.75 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
package istio
import (
"context"
"fmt"
"github.com/layer5io/meshery-adapter-library/adapter"
"github.com/layer5io/meshery-adapter-library/common"
adapterconfig "github.com/layer5io/meshery-adapter-library/config"
"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/meshkit/logger"
)
// Istio represents the istio adapter and embeds adapter.Adapter
type Istio struct {
adapter.Adapter // Type Embedded
}
// New initializes istio handler.
func New(c adapterconfig.Handler, l logger.Handler, kc adapterconfig.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, 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.ValidateSMIConformance(&adapter.SmiTestOptions{
Ctx: context.TODO(),
OpID: ee.Operationid,
})
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) {
_, err := hh.InstallAddon(opReq.Namespace, opReq.IsDeleteOperation, opReq.OperationName)
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.EnvoyPatchFile]
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
}