forked from maistra/istio-operator
/
rendering_strategy_v1_x.go
157 lines (138 loc) · 6.02 KB
/
rendering_strategy_v1_x.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
package versions
import (
"context"
"fmt"
"path"
jaegerv1 "github.com/acostamanowarrior/istio-operator/pkg/apis/external/jaeger/v1"
v1 "github.com/acostamanowarrior/istio-operator/pkg/apis/maistra/v1"
v2 "github.com/acostamanowarrior/istio-operator/pkg/apis/maistra/v2"
"github.com/acostamanowarrior/istio-operator/pkg/controller/common"
"github.com/acostamanowarrior/istio-operator/pkg/controller/common/cni"
"github.com/acostamanowarrior/istio-operator/pkg/controller/common/helm"
pkgerrors "github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/helm/pkg/manifest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type v1xRenderingStrategy struct{}
func (rs *v1xRenderingStrategy) render(ctx context.Context, v version, cr *common.ControllerResources, cniConfig cni.Config, smcp *v2.ServiceMeshControlPlane) (map[string][]manifest.Manifest, error) {
log := common.LogFromContext(ctx)
//Generate the spec
v1spec := &v1.ControlPlaneSpec{}
if err := cr.Scheme.Convert(&smcp.Spec, v1spec, nil); err != nil {
return nil, err
}
v1spec.Version = v.String()
if v1spec.Istio == nil {
v1spec.Istio = v1.NewHelmValues(make(map[string]interface{}))
}
var err error
smcp.Status.AppliedValues, err = v.ApplyProfiles(ctx, cr, v1spec, smcp.GetNamespace())
if err != nil {
log.Error(err, "warning: failed to apply ServiceMeshControlPlane templates")
return nil, err
}
spec := &smcp.Status.AppliedValues
if spec.ThreeScale == nil {
spec.ThreeScale = v1.NewHelmValues(make(map[string]interface{}))
}
err = spec.Istio.SetField("global.operatorNamespace", common.GetOperatorNamespace())
if err != nil {
return nil, err
}
err = spec.Istio.SetField("istio_cni.enabled", cniConfig.Enabled)
if err != nil {
return nil, fmt.Errorf("Could not set field status.lastAppliedConfiguration.istio.istio_cni.enabled: %v", err)
}
err = spec.Istio.SetField("istio_cni.istio_cni_network", v.GetCNINetworkName())
if err != nil {
return nil, fmt.Errorf("Could not set field status.lastAppliedConfiguration.istio.istio_cni.istio_cni_network: %v", err)
}
// MAISTRA-1330
err = spec.Istio.SetField("global.istioNamespace", smcp.GetNamespace())
if err != nil {
return nil, fmt.Errorf("Could not set field status.lastAppliedConfiguration.istio.global.istioNamespace: %v", err)
}
// MAISTRA-2014 - external jaeger with v2 resource
// note, if converted from v1 resource that was already specifying zipkin address and in-cluster url,
// tracing will already be disabled, so none of this is necessary
if isComponentEnabled(spec.Istio, "tracing") {
if provider, _, _ := spec.Istio.GetString("tracing.provider"); provider == "jaeger" {
// if we're not installing the jaeger resource, we need to determine what has been installed,
// so control plane rules are created correctly
jaegerResource, _, _ := spec.Istio.GetString("tracing.jaeger.resourceName")
if jaegerResource == "" {
jaegerResource = "jaeger"
}
// set the correct zipkin address
spec.Istio.SetField("global.tracer.zipkin.address", fmt.Sprintf("%s-collector.%s.svc:9411", jaegerResource, smcp.GetNamespace()))
jaeger := &jaegerv1.Jaeger{}
jaeger.SetName(jaegerResource)
jaeger.SetNamespace(smcp.GetNamespace())
if err := cr.Client.Get(ctx, client.ObjectKey{Name: jaeger.GetName(), Namespace: jaeger.GetNamespace()}, jaeger); err == nil {
if !metav1.IsControlledBy(jaeger, smcp) {
// if the resource exists, we never overwrite it
if err := spec.Istio.SetField("tracing.enabled", false); err != nil {
return nil, fmt.Errorf("error disabling jaeger install")
}
if jaegerInClusterURL, _, _ := spec.Istio.GetString("kiali.jaegerInClusterURL"); jaegerInClusterURL == "" {
// we won't override any user value
spec.Istio.SetField("kiali.jaegerInClusterURL", fmt.Sprintf("https://%s-query.%s.svc", jaegerResource, smcp.GetNamespace()))
}
if strategy, _, _ := jaeger.Spec.GetString("strategy"); strategy == "" || strategy == "allInOne" {
spec.Istio.SetField("tracing.jaeger.template", "all-in-one")
} else {
// we just want it to not be all-in-one. see the charts
spec.Istio.SetField("tracing.jaeger.template", "production-elasticsearch")
}
}
} else if !(errors.IsNotFound(err) || errors.IsGone(err)) {
if meta.IsNoMatchError(err) {
return nil, NewDependencyMissingError("Jaeger CRD", err)
}
return nil, pkgerrors.Wrapf(err, "error retrieving jaeger resource \"%s/%s\"", smcp.GetNamespace(), jaegerResource)
} else if err := spec.Istio.SetField("tracing.jaeger.install", true); err != nil {
return nil, pkgerrors.Wrapf(err, "error enabling jaeger install")
}
}
}
// convert back to the v2 type
smcp.Status.AppliedSpec = v2.ControlPlaneSpec{}
err = cr.Scheme.Convert(&smcp.Status.AppliedValues, &smcp.Status.AppliedSpec, nil)
if err != nil {
return nil, fmt.Errorf("Unexpected error setting Status.AppliedSpec: %v", err)
}
//Render the charts
allErrors := []error{}
var threeScaleRenderings map[string][]manifest.Manifest
log.Info("rendering helm charts")
log.V(2).Info("rendering Istio charts")
istioRenderings, _, err := helm.RenderChart(path.Join(v.GetChartsDir(), "istio"), smcp.GetNamespace(), spec.Istio.GetContent())
if err != nil {
allErrors = append(allErrors, err)
}
if isEnabled(spec.ThreeScale) {
log.V(2).Info("rendering 3scale charts")
threeScaleRenderings, _, err = helm.RenderChart(path.Join(v.GetChartsDir(), "maistra-threescale"), smcp.GetNamespace(), spec.ThreeScale.GetContent())
if err != nil {
allErrors = append(allErrors, err)
}
} else {
threeScaleRenderings = map[string][]manifest.Manifest{}
}
if len(allErrors) > 0 {
return nil, utilerrors.NewAggregate(allErrors)
}
// merge the rendernings
renderings := map[string][]manifest.Manifest{}
for key, value := range istioRenderings {
renderings[key] = value
}
for key, value := range threeScaleRenderings {
renderings[key] = value
}
return renderings, nil
}