@@ -27,7 +27,6 @@ import (
27
27
28
28
"istio.io/api/operator/v1alpha1"
29
29
"istio.io/istio/operator/pkg/helm"
30
- "istio.io/istio/operator/pkg/metrics"
31
30
"istio.io/istio/operator/pkg/name"
32
31
"istio.io/istio/operator/pkg/patch"
33
32
"istio.io/istio/operator/pkg/tpath"
@@ -58,24 +57,7 @@ type Options struct {
58
57
Version * version.Info
59
58
}
60
59
61
- // IstioComponent defines the interface for a component.
62
- type IstioComponent interface {
63
- // ComponentName returns the name of the component.
64
- ComponentName () name.ComponentName
65
- // ResourceName returns the name of the resources of the component.
66
- ResourceName () string
67
- // Namespace returns the namespace for the component.
68
- Namespace () string
69
- // Enabled reports whether the component is enabled.
70
- Enabled () bool
71
- // Run starts the component. Must be called before the component is used.
72
- Run () error
73
- // RenderManifest returns a string with the rendered manifest for the component.
74
- RenderManifest () (string , error )
75
- }
76
-
77
- // CommonComponentFields is a struct common to all components.
78
- type CommonComponentFields struct {
60
+ type IstioComponent struct {
79
61
* Options
80
62
ComponentName name.ComponentName
81
63
// resourceName is the name of all resources for this component.
@@ -89,41 +71,32 @@ type CommonComponentFields struct {
89
71
renderer helm.TemplateRenderer
90
72
}
91
73
92
- type IstioComponentBase struct {
93
- * CommonComponentFields
94
- }
95
-
96
- func (c * IstioComponentBase ) ComponentName () name.ComponentName {
97
- return c .CommonComponentFields .ComponentName
98
- }
99
-
100
- func (c * IstioComponentBase ) ResourceName () string {
101
- return c .CommonComponentFields .ResourceName
102
- }
103
-
104
- func (c * IstioComponentBase ) Namespace () string {
105
- return c .CommonComponentFields .Namespace
106
- }
107
-
108
- func (c * IstioComponentBase ) Enabled () bool {
109
- if c .CommonComponentFields .ComponentName .IsGateway () {
74
+ func (c * IstioComponent ) Enabled () bool {
75
+ if c .ComponentName .IsGateway () {
110
76
// type assert is guaranteed to work in this context.
111
77
return c .componentSpec .(* v1alpha1.GatewaySpec ).Enabled .GetValue ()
112
78
}
113
- return isCoreComponentEnabled (c .CommonComponentFields )
79
+
80
+ return c .isCoreComponentEnabled ()
114
81
}
115
82
116
- func (c * IstioComponentBase ) Run () error {
117
- return runComponent (c .CommonComponentFields )
83
+ func (c * IstioComponent ) Run () error {
84
+ r := c .createHelmRenderer ()
85
+ if err := r .Run (); err != nil {
86
+ return err
87
+ }
88
+ c .renderer = r
89
+ c .started = true
90
+ return nil
118
91
}
119
92
120
- func (c * IstioComponentBase ) RenderManifest () (string , error ) {
93
+ func (c * IstioComponent ) RenderManifest () (string , error ) {
121
94
return renderManifest (c )
122
95
}
123
96
124
97
// NewCoreComponent creates a new IstioComponent with the given componentName and options.
125
- func NewCoreComponent (cn name.ComponentName , opts * Options ) IstioComponent {
126
- var component IstioComponent
98
+ func NewCoreComponent (cn name.ComponentName , opts * Options ) * IstioComponent {
99
+ var component * IstioComponent
127
100
switch cn {
128
101
case name .IstioBaseComponentName :
129
102
component = NewCRDComponent (opts )
@@ -141,161 +114,85 @@ func NewCoreComponent(cn name.ComponentName, opts *Options) IstioComponent {
141
114
return component
142
115
}
143
116
144
- // BaseComponent is the base component.
145
- type BaseComponent struct {
146
- * IstioComponentBase
147
- }
148
-
149
- // NewCRDComponent creates a new BaseComponent and returns a pointer to it.
150
- func NewCRDComponent (opts * Options ) * BaseComponent {
151
- return & BaseComponent {
152
- & IstioComponentBase {
153
- & CommonComponentFields {
154
- Options : opts ,
155
- ComponentName : name .IstioBaseComponentName ,
156
- },
157
- },
117
+ // NewCRDComponent creates a new IstioComponent and returns a pointer to it.
118
+ func NewCRDComponent (opts * Options ) * IstioComponent {
119
+ return & IstioComponent {
120
+ Options : opts ,
121
+ ComponentName : name .IstioBaseComponentName ,
158
122
}
159
123
}
160
124
161
- // PilotComponent is the pilot component.
162
- type PilotComponent struct {
163
- * IstioComponentBase
164
- }
165
-
166
- // NewPilotComponent creates a new PilotComponent and returns a pointer to it.
167
- func NewPilotComponent (opts * Options ) * PilotComponent {
125
+ // NewPilotComponent creates a new IstioComponent and returns a pointer to it.
126
+ func NewPilotComponent (opts * Options ) * IstioComponent {
168
127
cn := name .PilotComponentName
169
- return & PilotComponent {
170
- & IstioComponentBase {
171
- & CommonComponentFields {
172
- Options : opts ,
173
- ComponentName : cn ,
174
- ResourceName : opts .Translator .ComponentMaps [cn ].ResourceName ,
175
- },
176
- },
128
+ return & IstioComponent {
129
+ Options : opts ,
130
+ ComponentName : cn ,
131
+ ResourceName : opts .Translator .ComponentMaps [cn ].ResourceName ,
177
132
}
178
133
}
179
134
180
- type CNIComponent struct {
181
- * IstioComponentBase
182
- }
183
-
184
- // NewCNIComponent creates a new NewCNIComponent and returns a pointer to it.
185
- func NewCNIComponent (opts * Options ) * CNIComponent {
135
+ // NewCNIComponent creates a new IstioComponent and returns a pointer to it.
136
+ func NewCNIComponent (opts * Options ) * IstioComponent {
186
137
cn := name .CNIComponentName
187
- return & CNIComponent {
188
- & IstioComponentBase {
189
- & CommonComponentFields {
190
- Options : opts ,
191
- ComponentName : cn ,
192
- },
193
- },
138
+ return & IstioComponent {
139
+ Options : opts ,
140
+ ComponentName : cn ,
194
141
}
195
142
}
196
143
197
- // IstiodRemoteComponent is the istiod remote component.
198
- type IstiodRemoteComponent struct {
199
- * IstioComponentBase
200
- }
201
-
202
- // NewIstiodRemoteComponent creates a new NewIstiodRemoteComponent and returns a pointer to it.
203
- func NewIstiodRemoteComponent (opts * Options ) * IstiodRemoteComponent {
144
+ // NewIstiodRemoteComponent creates a new IstioComponent and returns a pointer to it.
145
+ func NewIstiodRemoteComponent (opts * Options ) * IstioComponent {
204
146
cn := name .IstiodRemoteComponentName
205
- return & IstiodRemoteComponent {
206
- & IstioComponentBase {
207
- & CommonComponentFields {
208
- Options : opts ,
209
- ComponentName : cn ,
210
- },
211
- },
147
+ return & IstioComponent {
148
+ Options : opts ,
149
+ ComponentName : cn ,
212
150
}
213
151
}
214
152
215
- // IngressComponent is the ingress gateway component.
216
- type IngressComponent struct {
217
- * IstioComponentBase
218
- }
219
-
220
- // NewIngressComponent creates a new IngressComponent and returns a pointer to it.
221
- func NewIngressComponent (resourceName string , index int , spec * v1alpha1.GatewaySpec , opts * Options ) * IngressComponent {
222
- cn := name .IngressComponentName
223
- return & IngressComponent {
224
- & IstioComponentBase {
225
- CommonComponentFields : & CommonComponentFields {
226
- Options : opts ,
227
- ComponentName : cn ,
228
- ResourceName : resourceName ,
229
- index : index ,
230
- componentSpec : spec ,
231
- },
232
- },
153
+ // NewIngressComponent creates a new IstioComponent and returns a pointer to it.
154
+ func NewIngressComponent (resourceName string , index int , spec * v1alpha1.GatewaySpec , opts * Options ) * IstioComponent {
155
+ return & IstioComponent {
156
+ Options : opts ,
157
+ ComponentName : name .IngressComponentName ,
158
+ ResourceName : resourceName ,
159
+ index : index ,
160
+ componentSpec : spec ,
233
161
}
234
162
}
235
163
236
- // EgressComponent is the egress gateway component.
237
- type EgressComponent struct {
238
- * IstioComponentBase
239
- }
240
-
241
- // NewEgressComponent creates a new IngressComponent and returns a pointer to it.
242
- func NewEgressComponent (resourceName string , index int , spec * v1alpha1.GatewaySpec , opts * Options ) * EgressComponent {
243
- cn := name .EgressComponentName
244
- return & EgressComponent {
245
- & IstioComponentBase {
246
- CommonComponentFields : & CommonComponentFields {
247
- Options : opts ,
248
- ComponentName : cn ,
249
- index : index ,
250
- componentSpec : spec ,
251
- ResourceName : resourceName ,
252
- },
253
- },
164
+ // NewEgressComponent creates a new IstioComponent and returns a pointer to it.
165
+ func NewEgressComponent (resourceName string , index int , spec * v1alpha1.GatewaySpec , opts * Options ) * IstioComponent {
166
+ return & IstioComponent {
167
+ Options : opts ,
168
+ ComponentName : name .EgressComponentName ,
169
+ index : index ,
170
+ componentSpec : spec ,
171
+ ResourceName : resourceName ,
254
172
}
255
173
}
256
174
257
- // ZtunnelComponent is the istio ztunnel component.
258
- type ZtunnelComponent struct {
259
- * IstioComponentBase
260
- }
261
-
262
- // NewZtunnelComponent creates a new ZtunnelComponent and returns a pointer to it.
263
- func NewZtunnelComponent (opts * Options ) * ZtunnelComponent {
264
- return & ZtunnelComponent {
265
- & IstioComponentBase {
266
- & CommonComponentFields {
267
- Options : opts ,
268
- ComponentName : name .ZtunnelComponentName ,
269
- },
270
- },
175
+ // NewZtunnelComponent creates a new IstioComponent and returns a pointer to it.
176
+ func NewZtunnelComponent (opts * Options ) * IstioComponent {
177
+ return & IstioComponent {
178
+ Options : opts ,
179
+ ComponentName : name .ZtunnelComponentName ,
271
180
}
272
181
}
273
182
274
- // runComponent performs startup tasks for the component defined by the given CommonComponentFields.
275
- func runComponent (c * CommonComponentFields ) error {
276
- r := createHelmRenderer (c )
277
- if err := r .Run (); err != nil {
278
- return err
279
- }
280
- c .renderer = r
281
- c .started = true
282
- return nil
283
- }
284
-
183
+ // runCompon
285
184
// renderManifest renders the manifest for the component defined by c and returns the resulting string.
286
- func renderManifest (cf * IstioComponentBase ) (string , error ) {
185
+ func renderManifest (cf * IstioComponent ) (string , error ) {
287
186
if ! cf .started {
288
- metrics .CountManifestRenderError (cf .ComponentName (), metrics .RenderNotStartedError )
289
- return "" , fmt .Errorf ("component %s not started in RenderManifest" , cf .CommonComponentFields .ComponentName )
187
+ return "" , fmt .Errorf ("component %s not started in RenderManifest" , cf .ComponentName )
290
188
}
291
189
292
190
if ! cf .Enabled () {
293
- return disabledYAMLStr (cf .ComponentName () , cf . CommonComponentFields .ResourceName ), nil
191
+ return disabledYAMLStr (cf .ComponentName , cf .ResourceName ), nil
294
192
}
295
193
296
- mergedYAML , err := cf .Translator .TranslateHelmValues (cf .InstallSpec , cf .componentSpec , cf .ComponentName () )
194
+ mergedYAML , err := cf .Translator .TranslateHelmValues (cf .InstallSpec , cf .componentSpec , cf .ComponentName )
297
195
if err != nil {
298
- metrics .CountManifestRenderError (cf .ComponentName (), metrics .HelmTranslateIOPToValuesError )
299
196
return "" , err
300
197
}
301
198
@@ -306,26 +203,24 @@ func renderManifest(cf *IstioComponentBase) (string, error) {
306
203
})
307
204
if err != nil {
308
205
log .Errorf ("Error rendering the manifest: %s" , err )
309
- metrics .CountManifestRenderError (cf .ComponentName (), metrics .HelmChartRenderError )
310
206
return "" , err
311
207
}
312
208
my += helm .YAMLSeparator + "\n "
313
209
scope .Debugf ("Initial manifest with merged values:\n %s\n " , my )
314
210
315
211
// Add the k8s resources from IstioOperatorSpec.
316
- my , err = cf .Translator .OverlayK8sSettings (my , cf .InstallSpec , cf .CommonComponentFields . ComponentName ,
317
- cf .CommonComponentFields . ResourceName , cf .index )
212
+ my , err = cf .Translator .OverlayK8sSettings (my , cf .InstallSpec , cf .ComponentName ,
213
+ cf .ResourceName , cf .index )
318
214
if err != nil {
319
- metrics .CountManifestRenderError (cf .ComponentName (), metrics .K8SSettingsOverlayError )
320
215
return "" , err
321
216
}
322
- cnOutput := string (cf .CommonComponentFields . ComponentName )
217
+ cnOutput := string (cf .ComponentName )
323
218
my = "# Resources for " + cnOutput + " component\n \n " + my
324
219
scope .Debugf ("Manifest after k8s API settings:\n %s\n " , my )
325
220
326
221
// Add the k8s resource overlays from IstioOperatorSpec.
327
- pathToK8sOverlay := fmt .Sprintf ("Components.%s." , cf .CommonComponentFields . ComponentName )
328
- if cf .CommonComponentFields . ComponentName .IsGateway () {
222
+ pathToK8sOverlay := fmt .Sprintf ("Components.%s." , cf .ComponentName )
223
+ if cf .ComponentName .IsGateway () {
329
224
pathToK8sOverlay += fmt .Sprintf ("%d." , cf .index )
330
225
}
331
226
@@ -337,35 +232,32 @@ func renderManifest(cf *IstioComponentBase) (string, error) {
337
232
}
338
233
if ! found {
339
234
scope .Debugf ("Manifest after resources: \n %s\n " , my )
340
- metrics .CountManifestRender (cf .ComponentName ())
341
235
return my , nil
342
236
}
343
237
kyo , err := yaml .Marshal (overlays )
344
238
if err != nil {
345
239
return "" , err
346
240
}
347
241
scope .Infof ("Applying Kubernetes overlay: \n %s\n " , kyo )
348
- ret , err := patch .YAMLManifestPatch (my , cf .Namespace () , overlays )
242
+ ret , err := patch .YAMLManifestPatch (my , cf .Namespace , overlays )
349
243
if err != nil {
350
- metrics .CountManifestRenderError (cf .ComponentName (), metrics .K8SManifestPatchError )
351
244
return "" , err
352
245
}
353
246
354
247
scope .Debugf ("Manifest after resources and overlay: \n %s\n " , ret )
355
- metrics .CountManifestRender (cf .ComponentName ())
356
248
return ret , nil
357
249
}
358
250
359
251
// createHelmRenderer creates a helm renderer for the component defined by c and returns a ptr to it.
360
252
// If a helm subdir is not found in ComponentMap translations, it is assumed to be "addon/<component name>".
361
- func createHelmRenderer (c * CommonComponentFields ) helm.TemplateRenderer {
253
+ func (c * IstioComponent ) createHelmRenderer ( ) helm.TemplateRenderer {
362
254
iop := c .InstallSpec
363
255
cns := string (c .ComponentName )
364
256
helmSubdir := c .Translator .ComponentMap (cns ).HelmSubdir
365
257
return helm .NewHelmRenderer (iop .InstallPackagePath , helmSubdir , cns , c .Namespace , c .Version )
366
258
}
367
259
368
- func isCoreComponentEnabled (c * CommonComponentFields ) bool {
260
+ func (c * IstioComponent ) isCoreComponentEnabled ( ) bool {
369
261
enabled , err := c .Translator .IsComponentEnabled (c .ComponentName , c .InstallSpec )
370
262
if err != nil {
371
263
return false
0 commit comments