/
proxy_config.go
157 lines (139 loc) · 5.02 KB
/
proxy_config.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
// Copyright 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 model
import (
"istio.io/api/annotation"
meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/api/networking/v1beta1"
"istio.io/istio/pkg/config/labels"
"istio.io/istio/pkg/config/mesh"
"istio.io/istio/pkg/config/schema/gvk"
"istio.io/istio/pkg/util/protomarshal"
)
// ProxyConfigs organizes ProxyConfig configuration by namespace.
type ProxyConfigs struct {
// namespaceToProxyConfigs
namespaceToProxyConfigs map[string][]*v1beta1.ProxyConfig
// root namespace
rootNamespace string
}
// EffectiveProxyConfig generates the correct merged ProxyConfig for a given ProxyConfigTarget.
func (p *ProxyConfigs) EffectiveProxyConfig(meta *NodeMetadata, mc *meshconfig.MeshConfig) *meshconfig.ProxyConfig {
if p == nil || meta == nil {
return nil
}
effectiveProxyConfig := mesh.DefaultProxyConfig()
// Merge the proxy config from default config.
effectiveProxyConfig = mergeWithPrecedence(mc.GetDefaultConfig(), effectiveProxyConfig)
if p.rootNamespace != "" {
effectiveProxyConfig = mergeWithPrecedence(p.mergedGlobalConfig(), effectiveProxyConfig)
}
if meta.Namespace != p.rootNamespace {
namespacedConfig := p.mergedNamespaceConfig(meta.Namespace)
effectiveProxyConfig = mergeWithPrecedence(namespacedConfig, effectiveProxyConfig)
}
workloadConfig := p.mergedWorkloadConfig(meta.Namespace, meta.Labels)
// Check for proxy.istio.io/config annotation and merge it with lower priority than the
// workload-matching ProxyConfig CRs.
if v, ok := meta.Annotations[annotation.ProxyConfig.Name]; ok {
pca, err := proxyConfigFromAnnotation(v)
if err == nil {
workloadConfig = mergeWithPrecedence(workloadConfig, pca)
}
}
effectiveProxyConfig = mergeWithPrecedence(workloadConfig, effectiveProxyConfig)
return effectiveProxyConfig
}
func GetProxyConfigs(store ConfigStore, mc *meshconfig.MeshConfig) *ProxyConfigs {
proxyconfigs := &ProxyConfigs{
namespaceToProxyConfigs: map[string][]*v1beta1.ProxyConfig{},
rootNamespace: mc.GetRootNamespace(),
}
resources := store.List(gvk.ProxyConfig, NamespaceAll)
sortConfigByCreationTime(resources)
ns := proxyconfigs.namespaceToProxyConfigs
for _, resource := range resources {
ns[resource.Namespace] = append(ns[resource.Namespace], resource.Spec.(*v1beta1.ProxyConfig))
}
return proxyconfigs
}
func (p *ProxyConfigs) mergedGlobalConfig() *meshconfig.ProxyConfig {
return p.mergedNamespaceConfig(p.rootNamespace)
}
// mergedNamespaceConfig merges ProxyConfig resources matching the given namespace.
func (p *ProxyConfigs) mergedNamespaceConfig(namespace string) *meshconfig.ProxyConfig {
for _, pc := range p.namespaceToProxyConfigs[namespace] {
if pc.GetSelector() == nil {
// return the first match. this is consistent since
// we sort the resources by creation time beforehand.
return toMeshConfigProxyConfig(pc)
}
}
return nil
}
// mergedWorkloadConfig merges ProxyConfig resources matching the given namespace and labels.
func (p *ProxyConfigs) mergedWorkloadConfig(namespace string, l map[string]string) *meshconfig.ProxyConfig {
for _, pc := range p.namespaceToProxyConfigs[namespace] {
if len(pc.GetSelector().GetMatchLabels()) == 0 {
continue
}
selector := labels.Instance(pc.GetSelector().GetMatchLabels())
if selector.SubsetOf(l) {
// return the first match. this is consistent since
// we sort the resources by creation time beforehand.
return toMeshConfigProxyConfig(pc)
}
}
return nil
}
// mergeWithPrecedence merges the ProxyConfigs together with earlier items having
// the highest priority.
func mergeWithPrecedence(pcs ...*meshconfig.ProxyConfig) *meshconfig.ProxyConfig {
merged := &meshconfig.ProxyConfig{}
for i := len(pcs) - 1; i >= 0; i-- {
if pcs[i] == nil {
continue
}
proxyConfigYaml, err := protomarshal.ToYAML(pcs[i])
if err != nil {
continue
}
mergedConfig, err := mesh.MergeProxyConfig(proxyConfigYaml, merged)
if err == nil {
continue
}
merged = mergedConfig
}
return merged
}
func toMeshConfigProxyConfig(pc *v1beta1.ProxyConfig) *meshconfig.ProxyConfig {
mcpc := &meshconfig.ProxyConfig{}
if pc.Concurrency != nil {
mcpc.Concurrency = pc.Concurrency
}
if pc.EnvironmentVariables != nil {
mcpc.ProxyMetadata = pc.EnvironmentVariables
}
if pc.Image != nil {
mcpc.Image = pc.Image
}
return mcpc
}
func proxyConfigFromAnnotation(pcAnnotation string) (*meshconfig.ProxyConfig, error) {
pc := &meshconfig.ProxyConfig{}
if err := protomarshal.ApplyYAML(pcAnnotation, pc); err != nil {
return nil, err
}
return pc, nil
}