This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
/
destination.go
142 lines (125 loc) · 4.23 KB
/
destination.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
// Copyright 2017 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 convert
import (
"istio.io/api/networking/v1alpha3"
"istio.io/api/routing/v1alpha1"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/log"
)
// DestinationPolicies converts v1alpha1 destination policies to v1alpha3 destination rules
func DestinationPolicies(configs []model.Config) []model.Config {
policies := make([]*v1alpha1.DestinationPolicy, 0)
for _, config := range configs {
if config.Type == model.DestinationPolicy.Type {
policies = append(policies, config.Spec.(*v1alpha1.DestinationPolicy))
}
}
destinationRules := make(map[string]*v1alpha3.DestinationRule) // host -> destination rule
for _, policy := range policies {
host := convertIstioService(policy.Destination)
if destinationRules[host] == nil {
destinationRules[host] = &v1alpha3.DestinationRule{Host: host}
}
destinationRule := destinationRules[host]
destinationRule.Subsets = append(destinationRule.Subsets, convertDestinationPolicy(policy))
}
out := make([]model.Config, 0)
for host, rule := range destinationRules {
out = append(out, model.Config{
ConfigMeta: model.ConfigMeta{
Type: model.DestinationRule.Type,
Name: host,
Namespace: configs[0].Namespace,
Domain: configs[0].Domain,
},
Spec: rule,
})
}
return out
}
func convertLoadBalancing(in *v1alpha1.LoadBalancing) *v1alpha3.LoadBalancerSettings {
if in == nil {
return nil
}
switch v := in.LbPolicy.(type) {
case *v1alpha1.LoadBalancing_Custom:
log.Warnf("Custom load balancing not supported")
case *v1alpha1.LoadBalancing_Name:
simple := &v1alpha3.LoadBalancerSettings_Simple{}
switch v.Name {
case v1alpha1.LoadBalancing_ROUND_ROBIN:
simple.Simple = v1alpha3.LoadBalancerSettings_ROUND_ROBIN
case v1alpha1.LoadBalancing_LEAST_CONN:
simple.Simple = v1alpha3.LoadBalancerSettings_LEAST_CONN
case v1alpha1.LoadBalancing_RANDOM:
simple.Simple = v1alpha3.LoadBalancerSettings_RANDOM
}
return &v1alpha3.LoadBalancerSettings{
LbPolicy: simple,
}
}
return nil
}
func convertDestinationPolicy(in *v1alpha1.DestinationPolicy) *v1alpha3.Subset {
if in == nil {
return nil
}
out := &v1alpha3.Subset{
Name: labelsToSubsetName(in.Destination.Labels),
Labels: in.Destination.Labels,
TrafficPolicy: &v1alpha3.TrafficPolicy{
LoadBalancer: convertLoadBalancing(in.LoadBalancing),
},
}
// We can't have an unnamed subset
if out.Name == "" {
out.Name = "default"
}
// TODO: in.Source
if in.Source != nil {
log.Warnf("Destination policy source not supported")
}
if in.CircuitBreaker != nil {
if in.CircuitBreaker.GetCustom() != nil {
log.Warnf("Custom circuit breaker policy not supported")
}
if cb := in.CircuitBreaker.GetSimpleCb(); cb != nil {
out.TrafficPolicy.ConnectionPool = &v1alpha3.ConnectionPoolSettings{
Http: &v1alpha3.ConnectionPoolSettings_HTTPSettings{
Http2MaxRequests: cb.HttpMaxRequests,
Http1MaxPendingRequests: cb.HttpMaxPendingRequests,
MaxRequestsPerConnection: cb.HttpMaxRequestsPerConnection,
MaxRetries: cb.HttpMaxRetries,
},
Tcp: &v1alpha3.ConnectionPoolSettings_TCPSettings{
MaxConnections: cb.MaxConnections,
},
}
// TODO: out.TrafficPolicy.ConnectionPool.Tcp.MaxConnections =
out.TrafficPolicy.OutlierDetection = &v1alpha3.OutlierDetection{
Http: &v1alpha3.OutlierDetection_HTTPSettings{
Interval: convertGogoDuration(cb.HttpDetectionInterval),
ConsecutiveErrors: cb.HttpConsecutiveErrors,
BaseEjectionTime: convertGogoDuration(cb.SleepWindow),
MaxEjectionPercent: cb.HttpMaxEjectionPercent,
},
}
}
}
if in.Custom != nil {
log.Warn("Custom destination policy not supported")
}
return out
}