Skip to content

Commit a639ae4

Browse files
authored
operator: internal refactoring of components (istio#52098)
This is a NOP change, just cleaning up. we do not need to nest each component through 3 structs and an interface; can just expose the struct directly
1 parent b507c28 commit a639ae4

File tree

3 files changed

+116
-274
lines changed

3 files changed

+116
-274
lines changed

operator/pkg/component/component.go

+71-179
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
"istio.io/api/operator/v1alpha1"
2929
"istio.io/istio/operator/pkg/helm"
30-
"istio.io/istio/operator/pkg/metrics"
3130
"istio.io/istio/operator/pkg/name"
3231
"istio.io/istio/operator/pkg/patch"
3332
"istio.io/istio/operator/pkg/tpath"
@@ -58,24 +57,7 @@ type Options struct {
5857
Version *version.Info
5958
}
6059

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 {
7961
*Options
8062
ComponentName name.ComponentName
8163
// resourceName is the name of all resources for this component.
@@ -89,41 +71,32 @@ type CommonComponentFields struct {
8971
renderer helm.TemplateRenderer
9072
}
9173

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() {
11076
// type assert is guaranteed to work in this context.
11177
return c.componentSpec.(*v1alpha1.GatewaySpec).Enabled.GetValue()
11278
}
113-
return isCoreComponentEnabled(c.CommonComponentFields)
79+
80+
return c.isCoreComponentEnabled()
11481
}
11582

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
11891
}
11992

120-
func (c *IstioComponentBase) RenderManifest() (string, error) {
93+
func (c *IstioComponent) RenderManifest() (string, error) {
12194
return renderManifest(c)
12295
}
12396

12497
// 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
127100
switch cn {
128101
case name.IstioBaseComponentName:
129102
component = NewCRDComponent(opts)
@@ -141,161 +114,85 @@ func NewCoreComponent(cn name.ComponentName, opts *Options) IstioComponent {
141114
return component
142115
}
143116

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,
158122
}
159123
}
160124

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 {
168127
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,
177132
}
178133
}
179134

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 {
186137
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,
194141
}
195142
}
196143

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 {
204146
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,
212150
}
213151
}
214152

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,
233161
}
234162
}
235163

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,
254172
}
255173
}
256174

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,
271180
}
272181
}
273182

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
285184
// 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) {
287186
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)
290188
}
291189

292190
if !cf.Enabled() {
293-
return disabledYAMLStr(cf.ComponentName(), cf.CommonComponentFields.ResourceName), nil
191+
return disabledYAMLStr(cf.ComponentName, cf.ResourceName), nil
294192
}
295193

296-
mergedYAML, err := cf.Translator.TranslateHelmValues(cf.InstallSpec, cf.componentSpec, cf.ComponentName())
194+
mergedYAML, err := cf.Translator.TranslateHelmValues(cf.InstallSpec, cf.componentSpec, cf.ComponentName)
297195
if err != nil {
298-
metrics.CountManifestRenderError(cf.ComponentName(), metrics.HelmTranslateIOPToValuesError)
299196
return "", err
300197
}
301198

@@ -306,26 +203,24 @@ func renderManifest(cf *IstioComponentBase) (string, error) {
306203
})
307204
if err != nil {
308205
log.Errorf("Error rendering the manifest: %s", err)
309-
metrics.CountManifestRenderError(cf.ComponentName(), metrics.HelmChartRenderError)
310206
return "", err
311207
}
312208
my += helm.YAMLSeparator + "\n"
313209
scope.Debugf("Initial manifest with merged values:\n%s\n", my)
314210

315211
// 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)
318214
if err != nil {
319-
metrics.CountManifestRenderError(cf.ComponentName(), metrics.K8SSettingsOverlayError)
320215
return "", err
321216
}
322-
cnOutput := string(cf.CommonComponentFields.ComponentName)
217+
cnOutput := string(cf.ComponentName)
323218
my = "# Resources for " + cnOutput + " component\n\n" + my
324219
scope.Debugf("Manifest after k8s API settings:\n%s\n", my)
325220

326221
// 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() {
329224
pathToK8sOverlay += fmt.Sprintf("%d.", cf.index)
330225
}
331226

@@ -337,35 +232,32 @@ func renderManifest(cf *IstioComponentBase) (string, error) {
337232
}
338233
if !found {
339234
scope.Debugf("Manifest after resources: \n%s\n", my)
340-
metrics.CountManifestRender(cf.ComponentName())
341235
return my, nil
342236
}
343237
kyo, err := yaml.Marshal(overlays)
344238
if err != nil {
345239
return "", err
346240
}
347241
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)
349243
if err != nil {
350-
metrics.CountManifestRenderError(cf.ComponentName(), metrics.K8SManifestPatchError)
351244
return "", err
352245
}
353246

354247
scope.Debugf("Manifest after resources and overlay: \n%s\n", ret)
355-
metrics.CountManifestRender(cf.ComponentName())
356248
return ret, nil
357249
}
358250

359251
// createHelmRenderer creates a helm renderer for the component defined by c and returns a ptr to it.
360252
// 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 {
362254
iop := c.InstallSpec
363255
cns := string(c.ComponentName)
364256
helmSubdir := c.Translator.ComponentMap(cns).HelmSubdir
365257
return helm.NewHelmRenderer(iop.InstallPackagePath, helmSubdir, cns, c.Namespace, c.Version)
366258
}
367259

368-
func isCoreComponentEnabled(c *CommonComponentFields) bool {
260+
func (c *IstioComponent) isCoreComponentEnabled() bool {
369261
enabled, err := c.Translator.IsComponentEnabled(c.ComponentName, c.InstallSpec)
370262
if err != nil {
371263
return false

0 commit comments

Comments
 (0)