-
Notifications
You must be signed in to change notification settings - Fork 682
/
interesting_types.go
127 lines (109 loc) · 4.98 KB
/
interesting_types.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
package entrypoint
import (
"context"
"fmt"
"github.com/datawire/ambassador/v2/pkg/kates"
"github.com/datawire/dlib/dlog"
)
// thingToWatch is... uh... a thing we're gonna watch. Specifically, it's a
// K8s type name and an optional field selector.
type thingToWatch struct {
typename string
fieldselector string
}
// GetQueries takes a set of interesting types, and returns a set of kates.Query to watch
// for them.
func GetQueries(ctx context.Context, interestingTypes map[string]thingToWatch) []kates.Query {
ns := kates.NamespaceAll
if IsAmbassadorSingleNamespace() {
ns = GetAmbassadorNamespace()
}
fs := GetAmbassadorFieldSelector()
ls := GetAmbassadorLabelSelector()
var queries []kates.Query
for snapshotname, queryinfo := range interestingTypes {
query := kates.Query{
Namespace: ns,
Name: snapshotname,
Kind: queryinfo.typename,
FieldSelector: queryinfo.fieldselector,
LabelSelector: ls,
}
if query.FieldSelector == "" {
query.FieldSelector = fs
}
queries = append(queries, query)
dlog.Debugf(ctx, "WATCHER: watching %#v", query)
}
return queries
}
// GetInterestingTypes takes a list of available server types, and returns the types we think
// are interesting to watch.
func GetInterestingTypes(ctx context.Context, serverTypeList []kates.APIResource) map[string]thingToWatch {
fs := GetAmbassadorFieldSelector()
endpointFs := "metadata.namespace!=kube-system"
if fs != "" {
endpointFs += fmt.Sprintf(",%s", fs)
}
// We set interestingTypes to the list of types that we'd like to watch (if that type exits
// in this cluster).
//
// - The key in the map is the how we'll label them in the snapshot we pass to the rest of
// Ambassador.
// - The typename in the map values should be the qualified "${name}.${group}", where
// "${name} is lowercase+plural.
// - If the map value doesn't set a field selector, then `fs` (above) will be used.
//
// Most of the interestingTypes are static, but it's completely OK to add types based
// on runtime considerations, as we do for IngressClass and the KNative stuff.
interestingTypes := map[string]thingToWatch{
"Services": {typename: "services."},
"Endpoints": {typename: "endpoints.", fieldselector: endpointFs},
// Note that we pull secrets into "K8sSecrets".
// ReconcileSecrets will pull over the ones we need into "Secrets"
// and "Endpoints" respectively.
"K8sSecrets": {typename: "secrets."},
//"Ingresses": {typename: "ingresses.networking.k8s.io"}, // new in Kubernetes 1.14, deprecating ingresses.extensions
"Ingresses": {typename: "ingresses.extensions"}, // new in Kubernetes 1.2
"AuthServices": {typename: "authservices.getambassador.io"},
"ConsulResolvers": {typename: "consulresolvers.getambassador.io"},
"DevPortals": {typename: "devportals.getambassador.io"},
"Hosts": {typename: "ambassadorhosts.x.getambassador.io"},
"KubernetesEndpointResolvers": {typename: "kubernetesendpointresolvers.getambassador.io"},
"KubernetesServiceResolvers": {typename: "kubernetesserviceresolvers.getambassador.io"},
"Listeners": {typename: "ambassadorlisteners.x.getambassador.io"},
"LogServices": {typename: "logservices.getambassador.io"},
"Mappings": {typename: "ambassadormappings.x.getambassador.io"},
"Modules": {typename: "modules.getambassador.io"},
"RateLimitServices": {typename: "ratelimitservices.getambassador.io"},
"TCPMappings": {typename: "ambassadortcpmappings.x.getambassador.io"},
"TLSContexts": {typename: "tlscontexts.getambassador.io"},
"TracingServices": {typename: "tracingservices.getambassador.io"},
// Gateway API resources
"GatewayClasses": {typename: "gatewayclasses.networking.x-k8s.io"},
"Gateways": {typename: "gateways.networking.x-k8s.io"},
"HTTPRoutes": {typename: "httproutes.networking.x-k8s.io"},
}
if !IsAmbassadorSingleNamespace() {
interestingTypes["IngressClasses"] = thingToWatch{typename: "ingressclasses.networking.k8s.io"} // new in Kubernetes 1.18
}
if IsKnativeEnabled() {
// Note: These keynames have a "KNative" prefix, to avoid clashing with the
// standard "networking.k8s.io" and "extensions" types.
interestingTypes["KNativeClusterIngresses"] = thingToWatch{typename: "clusteringresses.networking.internal.knative.dev"}
interestingTypes["KNativeIngresses"] = thingToWatch{typename: "ingresses.networking.internal.knative.dev"}
}
if serverTypeList != nil {
serverTypes := make(map[string]kates.APIResource, len(serverTypeList))
for _, typeinfo := range serverTypeList {
serverTypes[typeinfo.Name+"."+typeinfo.Group] = typeinfo
}
for k, queryinfo := range interestingTypes {
if _, haveType := serverTypes[queryinfo.typename]; !haveType {
dlog.Infof(ctx, "Warning, unable to watch %s, unknown kind.", queryinfo.typename)
delete(interestingTypes, k)
}
}
}
return interestingTypes
}