-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.go
58 lines (49 loc) · 1.13 KB
/
host.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
package matcher
import (
"fmt"
"github.com/blackstorm/ingress-go/pkg/controller/label"
netv1 "k8s.io/api/networking/v1"
"k8s.io/klog/v2"
)
type Host struct {
host string
label.BaseLabeler
}
func newHost(host string, ingress *netv1.Ingress) Host {
return Host{
host: host,
BaseLabeler: label.BaseLabeler{
LabelName: fmt.Sprintf("%s:%s:%s", host, ingress.Namespace, ingress.Name),
},
}
}
type HostMatcher map[string]*linkedHost
func newHostMatcher() HostMatcher {
return make(HostMatcher)
}
func (m HostMatcher) match(host string) bool {
linked, ok := m[host]
return ok && linked != nil
}
func (m HostMatcher) add(host Host) {
if linked, ok := m[host.host]; ok {
if linked.find(host) != nil {
klog.Infof("host %s existed skip add", host.host)
} else {
klog.Infof("add host %s to matcher", host.host)
linked.append(&host)
}
} else {
klog.Infof("add host %s to matcher", host.host)
m[host.host] = newLinkedHost(&host)
}
}
func (m HostMatcher) delete(host Host) {
if linked, ok := m[host.host]; ok {
if res := linked.find(host); res != nil {
if res.remove() == -1 {
m[host.host] = nil
}
}
}
}