forked from knative/serving
/
domains.go
132 lines (112 loc) · 4.27 KB
/
domains.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
/*
Copyright 2019 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
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 domains
import (
"bytes"
"context"
"fmt"
"strings"
"text/template"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
pkgnet "knative.dev/pkg/network"
netv1alpha1 "knative.dev/serving/pkg/apis/networking/v1alpha1"
"knative.dev/serving/pkg/apis/serving"
v1 "knative.dev/serving/pkg/apis/serving/v1"
"knative.dev/serving/pkg/network"
"knative.dev/serving/pkg/reconciler/route/config"
"knative.dev/serving/pkg/reconciler/route/resources/labels"
)
// HTTPScheme is the string representation of http.
const HTTPScheme string = "http"
// GetAllDomainsAndTags returns all of the domains and tags(including subdomains) associated with a Route
func GetAllDomainsAndTags(ctx context.Context, r *v1.Route, names []string, visibility map[string]netv1alpha1.IngressVisibility) (map[string]string, error) {
domainTagMap := make(map[string]string)
for _, name := range names {
meta := r.ObjectMeta.DeepCopy()
hostname, err := HostnameFromTemplate(ctx, meta.Name, name)
if err != nil {
return nil, err
}
labels.SetVisibility(meta, visibility[name] == netv1alpha1.IngressVisibilityClusterLocal)
subDomain, err := DomainNameFromTemplate(ctx, *meta, hostname)
if err != nil {
return nil, err
}
domainTagMap[subDomain] = name
}
return domainTagMap, nil
}
// DomainNameFromTemplate generates domain name base on the template specified in the `config-network` ConfigMap.
// name is the "subdomain" which will be referred as the "name" in the template
func DomainNameFromTemplate(ctx context.Context, r metav1.ObjectMeta, name string) (string, error) {
domainConfig := config.FromContext(ctx).Domain
rLabels := r.Labels
domain := domainConfig.LookupDomainForLabels(rLabels)
annotations := r.Annotations
// These are the available properties they can choose from.
// We could add more over time - e.g. RevisionName if we thought that
// might be of interest to people.
data := network.DomainTemplateValues{
Name: name,
Namespace: r.Namespace,
Domain: domain,
Annotations: annotations,
}
networkConfig := config.FromContext(ctx).Network
buf := bytes.Buffer{}
var templ *template.Template
// If the route is "cluster local" then don't use the user-defined
// domain template, use the default one
if rLabels[serving.VisibilityLabelKey] == serving.VisibilityClusterLocal {
templ = template.Must(template.New("domain-template").Parse(
network.DefaultDomainTemplate))
} else {
templ = networkConfig.GetDomainTemplate()
}
if err := templ.Execute(&buf, data); err != nil {
return "", fmt.Errorf("error executing the DomainTemplate: %w", err)
}
return buf.String(), nil
}
// HostnameFromTemplate generates domain name base on the template specified in the `config-network` ConfigMap.
// name is the "subdomain" which will be referred as the "name" in the template
func HostnameFromTemplate(ctx context.Context, name string, tag string) (string, error) {
if tag == "" {
return name, nil
}
// These are the available properties they can choose from.
// We could add more over time - e.g. RevisionName if we thought that
// might be of interest to people.
data := network.TagTemplateValues{
Name: name,
Tag: tag,
}
networkConfig := config.FromContext(ctx).Network
buf := bytes.Buffer{}
if err := networkConfig.GetTagTemplate().Execute(&buf, data); err != nil {
return "", fmt.Errorf("error executing the TagTemplate: %w", err)
}
return buf.String(), nil
}
// URL generates the a string representation of a URL.
func URL(scheme, fqdn string) *apis.URL {
return &apis.URL{
Scheme: scheme,
Host: fqdn,
}
}
// IsClusterLocal checks if a domain is only visible with cluster.
func IsClusterLocal(domain string) bool {
return strings.HasSuffix(domain, pkgnet.GetClusterDomainName())
}