istio
113 строк · 4.3 Кб
1// Copyright Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package helmreconciler
16
17import (
18"io"
19"strings"
20
21"istio.io/istio/operator/pkg/name"
22"istio.io/istio/pkg/log"
23)
24
25const (
26// MetadataNamespace is the namespace for mesh metadata (labels, annotations)
27MetadataNamespace = "install.operator.istio.io"
28// OwningResourceName represents the name of the owner to which the resource relates
29OwningResourceName = MetadataNamespace + "/owning-resource"
30// OwningResourceNamespace represents the namespace of the owner to which the resource relates
31OwningResourceNamespace = MetadataNamespace + "/owning-resource-namespace"
32// OwningResourceNotPruned indicates that the resource should not be pruned during reconciliation cycles,
33// note this will not prevent the resource from being deleted if the owning resource is deleted.
34OwningResourceNotPruned = MetadataNamespace + "/owning-resource-not-pruned"
35// operatorLabelStr indicates Istio operator is managing this resource.
36operatorLabelStr = name.OperatorAPINamespace + "/managed"
37// operatorReconcileStr indicates that the operator will reconcile the resource.
38operatorReconcileStr = "Reconcile"
39// IstioComponentLabelStr indicates which Istio component a resource belongs to.
40IstioComponentLabelStr = name.OperatorAPINamespace + "/component"
41// istioVersionLabelStr indicates the Istio version of the installation.
42istioVersionLabelStr = name.OperatorAPINamespace + "/version"
43)
44
45var (
46// TestMode sets the controller into test mode. Used for unit tests to bypass things like waiting on resources.
47TestMode = false
48
49scope = log.RegisterScope("installer", "installer")
50)
51
52func init() {
53// Tree representation and wait channels are an inversion of ComponentDependencies and are constructed from it.
54buildInstallTree()
55}
56
57// ComponentTree represents a tree of component dependencies.
58type (
59ComponentTree map[name.ComponentName]any
60componentNameToListMap map[name.ComponentName][]name.ComponentName
61)
62
63var (
64// ComponentDependencies is a tree of component dependencies. The semantics are ComponentDependencies[cname] gives
65// the subtree of components that must wait for cname to be installed before starting installation themselves.
66ComponentDependencies = componentNameToListMap{
67name.PilotComponentName: {
68name.CNIComponentName,
69name.IngressComponentName,
70name.EgressComponentName,
71},
72name.IstioBaseComponentName: {
73name.PilotComponentName,
74},
75name.CNIComponentName: {
76name.ZtunnelComponentName,
77},
78}
79
80// InstallTree is a top down hierarchy tree of dependencies where children must wait for the parent to complete
81// before starting installation.
82InstallTree = make(ComponentTree)
83)
84
85// buildInstallTree builds a tree from buildInstallTree where parents are the root of each subtree.
86func buildInstallTree() {
87// Starting with root, recursively insert each first level child into each node.
88insertChildrenRecursive(name.IstioBaseComponentName, InstallTree, ComponentDependencies)
89}
90
91func insertChildrenRecursive(componentName name.ComponentName, tree ComponentTree, children componentNameToListMap) {
92tree[componentName] = make(ComponentTree)
93for _, child := range children[componentName] {
94insertChildrenRecursive(child, tree[componentName].(ComponentTree), children)
95}
96}
97
98// InstallTreeString returns a string representation of the dependency tree.
99func InstallTreeString() string {
100var sb strings.Builder
101buildInstallTreeString(name.IstioBaseComponentName, "", &sb)
102return sb.String()
103}
104
105func buildInstallTreeString(componentName name.ComponentName, prefix string, sb io.StringWriter) {
106_, _ = sb.WriteString(prefix + string(componentName) + "\n")
107if _, ok := InstallTree[componentName].(ComponentTree); !ok {
108return
109}
110for k := range InstallTree[componentName].(ComponentTree) {
111buildInstallTreeString(k, prefix+" ", sb)
112}
113}
114