This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathname.go
More file actions
248 lines (224 loc) · 9.19 KB
/
name.go
File metadata and controls
248 lines (224 loc) · 9.19 KB
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
// Copyright 2019 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package name
import (
"fmt"
"strings"
"istio.io/api/operator/v1alpha1"
"istio.io/operator/pkg/tpath"
"istio.io/operator/pkg/util"
)
const (
// OperatorAPINamespace is the API namespace for operator config.
// TODO: move this to a base definitions file when one is created.
OperatorAPINamespace = "operator.istio.io"
)
// ComponentName is a component name string, typed to constrain allowed values.
type ComponentName string
const (
// IstioComponent names corresponding to the IstioOperator proto component names. Must be the same, since these
// are used for struct traversal.
IstioBaseComponentName ComponentName = "Base"
PilotComponentName ComponentName = "Pilot"
GalleyComponentName ComponentName = "Galley"
SidecarInjectorComponentName ComponentName = "SidecarInjector"
PolicyComponentName ComponentName = "Policy"
TelemetryComponentName ComponentName = "Telemetry"
CitadelComponentName ComponentName = "Citadel"
CertManagerComponentName ComponentName = "CertManager"
NodeAgentComponentName ComponentName = "NodeAgent"
CNIComponentName ComponentName = "Cni"
// Gateway components
IngressComponentName ComponentName = "IngressGateways"
EgressComponentName ComponentName = "EgressGateways"
// Addon components
AddonComponentName ComponentName = "Addon"
// Operator components
IstioOperatorComponentName ComponentName = "IstioOperator"
IstioOperatorCustomResourceName ComponentName = "IstioOperatorCustomResource"
)
var (
AllCoreComponentNames = []ComponentName{
IstioBaseComponentName,
PilotComponentName,
GalleyComponentName,
SidecarInjectorComponentName,
PolicyComponentName,
TelemetryComponentName,
CitadelComponentName,
CertManagerComponentName,
NodeAgentComponentName,
CNIComponentName,
}
allComponentNamesMap = make(map[ComponentName]bool)
// ComponentNameToHelmComponentPath defines mapping from component name to helm component root path.
// TODO: merge this with the componentMaps defined in translateConfig
ComponentNameToHelmComponentPath = map[ComponentName]string{
PilotComponentName: "pilot",
GalleyComponentName: "galley",
SidecarInjectorComponentName: "sidecarInjectorWebhook",
PolicyComponentName: "mixer.policy",
TelemetryComponentName: "mixer.telemetry",
CitadelComponentName: "security",
CertManagerComponentName: "certmanager",
NodeAgentComponentName: "nodeagent",
IngressComponentName: "gateways.istio-ingressgateway",
EgressComponentName: "gateways.istio-egressgateway",
CNIComponentName: "cni",
}
)
func init() {
for _, n := range AllCoreComponentNames {
allComponentNamesMap[n] = true
}
}
// ManifestMap is a map of ComponentName to its manifest string.
type ManifestMap map[ComponentName][]string
// IsCoreComponent reports whether cn is a core component.
func (cn ComponentName) IsCoreComponent() bool {
return allComponentNamesMap[cn]
}
// IsGateway reports whether cn is a gateway component.
func (cn ComponentName) IsGateway() bool {
return cn == IngressComponentName || cn == EgressComponentName
}
// IsAddon reports whether cn is an addon component.
func (cn ComponentName) IsAddon() bool {
return cn == AddonComponentName
}
// IsComponentEnabledInSpec reports whether the given component is enabled in the given spec.
// IsComponentEnabledInSpec assumes that controlPlaneSpec has been validated.
// TODO: remove extra validations when comfort level is high enough.
func IsComponentEnabledInSpec(componentName ComponentName, controlPlaneSpec *v1alpha1.IstioOperatorSpec) (bool, error) {
// for addon components, enablement is defined in values part.
if componentName == AddonComponentName {
enabled, _, err := IsComponentEnabledFromValue(string(componentName), controlPlaneSpec.Values)
return enabled, err
}
// for Istio components, check whether override path exist in values part first then ISCP.
valuePath := ComponentNameToHelmComponentPath[componentName]
enabled, pathExist, err := IsComponentEnabledFromValue(valuePath, controlPlaneSpec.Values)
// only return value when path exists
if err == nil && pathExist {
return enabled, nil
}
if componentName == IngressComponentName {
return len(controlPlaneSpec.Components.IngressGateways) != 0, nil
}
if componentName == EgressComponentName {
return len(controlPlaneSpec.Components.EgressGateways) != 0, nil
}
componentNodeI, found, err := tpath.GetFromStructPath(controlPlaneSpec, "Components."+string(componentName)+".Enabled")
if err != nil {
return false, fmt.Errorf("error in IsComponentEnabledInSpec GetFromStructPath componentEnabled for component=%s: %s",
componentName, err)
}
if !found || componentNodeI == nil {
return false, nil
}
componentNode, ok := componentNodeI.(*v1alpha1.BoolValueForPB)
if !ok {
return false, fmt.Errorf("component %s enabled has bad type %T, expect *v1alpha1.BoolValueForPB", componentName, componentNodeI)
}
if componentNode == nil {
return false, nil
}
return componentNode.Value, nil
}
// IsComponentEnabledFromValue get whether component is enabled in helm value.yaml tree.
// valuePath points to component path in the values tree.
func IsComponentEnabledFromValue(valuePath string, valueSpec map[string]interface{}) (enabled bool, pathExist bool, err error) {
enabledPath := valuePath + ".enabled"
enableNodeI, found, err := tpath.GetFromTreePath(valueSpec, util.ToYAMLPath(enabledPath))
if err != nil {
return false, false, fmt.Errorf("error finding component enablement path: %s in helm value.yaml tree", enabledPath)
}
if !found {
// Some components do not specify enablement should be treated as enabled if the root node in the component subtree exists.
_, found, err := tpath.GetFromTreePath(valueSpec, util.ToYAMLPath(valuePath))
if err != nil {
return false, false, err
}
if found {
return true, false, nil
}
return false, false, nil
}
enableNode, ok := enableNodeI.(bool)
if !ok {
return false, true, fmt.Errorf("node at valuePath %s has bad type %T, expect bool", enabledPath, enableNodeI)
}
return enableNode, true, nil
}
// NamespaceFromValue gets the namespace value in helm value.yaml tree.
func NamespaceFromValue(valuePath string, valueSpec map[string]interface{}) (string, error) {
nsNodeI, found, err := tpath.GetFromTreePath(valueSpec, util.ToYAMLPath(valuePath))
if err != nil {
return "", fmt.Errorf("namespace path not found: %s from helm value.yaml tree", valuePath)
}
if !found || nsNodeI == nil {
return "", nil
}
nsNode, ok := nsNodeI.(string)
if !ok {
return "", fmt.Errorf("node at helm value.yaml tree path %s has bad type %T, expect string", valuePath, nsNodeI)
}
return nsNode, nil
}
// Namespace returns the namespace for the component. It follows these rules:
// 1. If DefaultNamespace is unset, log and error and return the empty string.
// 2. If the feature and component namespaces are unset, return DefaultNamespace.
// 3. If the feature namespace is set but component name is unset, return the feature namespace.
// 4. Otherwise return the component namespace.
// Namespace assumes that controlPlaneSpec has been validated.
// TODO: remove extra validations when comfort level is high enough.
func Namespace(componentName ComponentName, controlPlaneSpec *v1alpha1.IstioOperatorSpec) (string, error) {
defaultNamespaceI, found, err := tpath.GetFromStructPath(controlPlaneSpec, "MeshConfig.RootNamespace")
if !found {
return "", fmt.Errorf("can't find any setting for defaultNamespace for component=%s", componentName)
}
if err != nil {
return "", fmt.Errorf("error in Namepsace for component=%s: %s", componentName, err)
}
defaultNamespace, ok := defaultNamespaceI.(string)
if !ok {
return "", fmt.Errorf("defaultNamespace has bad type %T, expect string", defaultNamespaceI)
}
if defaultNamespace == "" {
return "", fmt.Errorf("defaultNamespace must be set")
}
componentNodeI, found, err := tpath.GetFromStructPath(controlPlaneSpec, "Components."+string(componentName)+".Namespace")
if err != nil {
return "", fmt.Errorf("error in Namepsace GetFromStructPath componentNamespace for component=%s: %s", componentName, err)
}
if !found {
return defaultNamespace, nil
}
if componentNodeI == nil {
return defaultNamespace, nil
}
componentNamespace, ok := componentNodeI.(string)
if !ok {
return "", fmt.Errorf("component %s enabled has bad type %T, expect string", componentName, componentNodeI)
}
if componentNamespace == "" {
return defaultNamespace, nil
}
return componentNamespace, nil
}
// TitleCase returns a capitalized version of n.
func TitleCase(n ComponentName) ComponentName {
s := string(n)
return ComponentName(strings.ToUpper(s[0:1]) + s[1:])
}