forked from knative/serving
/
domain.go
102 lines (87 loc) · 2.87 KB
/
domain.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
/*
Copyright 2018 The Knative 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
https://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 config
import (
"fmt"
"strings"
"github.com/ghodss/yaml"
corev1 "k8s.io/api/core/v1"
)
const (
DomainConfigName = "config-domain"
)
// LabelSelector represents map of {key,value} pairs. A single {key,value} in the
// map is equivalent to a requirement key == value. The requirements are ANDed.
type LabelSelector struct {
Selector map[string]string `json:"selector,omitempty"`
}
func (s *LabelSelector) specificity() int {
return len(s.Selector)
}
// Matches returns whether the given labels meet the requirement of the selector.
func (s *LabelSelector) Matches(labels map[string]string) bool {
for label, expectedValue := range s.Selector {
value, ok := labels[label]
if !ok || expectedValue != value {
return false
}
}
return true
}
// Domain maps domains to routes by matching the domain's
// label selectors to the route's labels.
type Domain struct {
// Domains map from domain to label selector. If a route has
// labels matching a particular selector, it will use the
// corresponding domain. If multiple selectors match, we choose
// the most specific selector.
Domains map[string]*LabelSelector
}
// NewDomainFromConfigMap creates a Domain from the supplied ConfigMap
func NewDomainFromConfigMap(configMap *corev1.ConfigMap) (*Domain, error) {
c := Domain{Domains: map[string]*LabelSelector{}}
hasDefault := false
for k, v := range configMap.Data {
labelSelector := LabelSelector{}
err := yaml.Unmarshal([]byte(v), &labelSelector)
if err != nil {
return nil, err
}
c.Domains[k] = &labelSelector
if len(labelSelector.Selector) == 0 {
hasDefault = true
}
}
if !hasDefault {
return nil, fmt.Errorf("Config %#v must have a default domain", configMap.Data)
}
return &c, nil
}
// LookupDomainForLabels returns a domain given a set of labels.
// Since we reject configuration without a default domain, this should
// always return a value.
func (c *Domain) LookupDomainForLabels(labels map[string]string) string {
domain := ""
specificity := -1
for k, selector := range c.Domains {
// Ignore if selector doesn't match, or decrease the specificity.
if !selector.Matches(labels) || selector.specificity() < specificity {
continue
}
if selector.specificity() > specificity || strings.Compare(k, domain) < 0 {
domain = k
specificity = selector.specificity()
}
}
return domain
}