Skip to content

Commit 44e51cb

Browse files
committed
add an annotation to the kingress mapping tags to hostnames
1 parent 56f2186 commit 44e51cb

1 file changed

Lines changed: 45 additions & 8 deletions

File tree

pkg/reconciler/route/resources/ingress.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ import (
4343
"knative.dev/serving/pkg/reconciler/route/traffic"
4444
)
4545

46+
const (
47+
TagToHostAnnotationKey = networking.GroupName + "/tag-to-host"
48+
)
49+
4650
// MakeIngressTLS creates IngressTLS to configure the ingress TLS.
4751
func MakeIngressTLS(cert *netv1alpha1.Certificate, hostNames []string) netv1alpha1.IngressTLS {
4852
return netv1alpha1.IngressTLS{
@@ -79,11 +83,12 @@ func MakeIngressWithRollout(
7983
ingressClass string,
8084
acmeChallenges ...netv1alpha1.HTTP01Challenge,
8185
) (*netv1alpha1.Ingress, error) {
82-
spec, err := makeIngressSpec(ctx, r, tls, tc, ro, acmeChallenges...)
86+
spec, tagToHost, err := makeIngressSpec(ctx, r, tls, tc, ro, acmeChallenges...)
8387
if err != nil {
8488
return nil, err
8589
}
86-
return &netv1alpha1.Ingress{
90+
91+
ing := &netv1alpha1.Ingress{
8792
ObjectMeta: metav1.ObjectMeta{
8893
Name: names.Ingress(r),
8994
Namespace: r.Namespace,
@@ -98,7 +103,24 @@ func MakeIngressWithRollout(
98103
OwnerReferences: []metav1.OwnerReference{*kmeta.NewControllerRef(r)},
99104
},
100105
Spec: spec,
101-
}, nil
106+
}
107+
108+
if len(tagToHost) > 0 {
109+
ing.Annotations[TagToHostAnnotationKey] = serializeTagToHostMap(ctx, tagToHost)
110+
}
111+
112+
return ing, nil
113+
}
114+
115+
func serializeTagToHostMap(ctx context.Context, mapping map[string][]string) string {
116+
sr, err := json.Marshal(mapping)
117+
if err != nil {
118+
// This must never happen in the normal course of things.
119+
logging.FromContext(ctx).Warnw("Error serializing tag to host mapping: "+spew.Sprint(mapping),
120+
zap.Error(err))
121+
return ""
122+
}
123+
return string(sr)
102124
}
103125

104126
func serializeRollout(ctx context.Context, r *traffic.Rollout) string {
@@ -120,7 +142,9 @@ func makeIngressSpec(
120142
tc *traffic.Config,
121143
ro *traffic.Rollout,
122144
acmeChallenges ...netv1alpha1.HTTP01Challenge,
123-
) (netv1alpha1.IngressSpec, error) {
145+
) (netv1alpha1.IngressSpec, map[string][]string, error) {
146+
tagToHost := make(map[string][]string)
147+
124148
// Domain should have been specified in route status
125149
// before calling this func.
126150
names := make([]string, 0, len(tc.Targets))
@@ -152,7 +176,7 @@ func makeIngressSpec(
152176
for _, visibility := range visibilities {
153177
domains, err := domains.GetDomainsForVisibility(ctx, name, r, visibility)
154178
if err != nil {
155-
return netv1alpha1.IngressSpec{}, err
179+
return netv1alpha1.IngressSpec{}, nil, err
156180
}
157181
domainRules := makeIngressRules(domains, r.Namespace,
158182
visibility, tc.Targets[name], ro.RolloutsByTag(name), networkConfig.SystemInternalTLSEnabled())
@@ -205,19 +229,32 @@ func makeIngressSpec(
205229
}
206230

207231
rules = append(rules, domainRules...)
232+
233+
if name != traffic.DefaultTarget {
234+
longestDomain := ""
235+
for d := range domains {
236+
if len(longestDomain) < len(d) {
237+
longestDomain = d
238+
}
239+
}
240+
241+
tagToHost[name] = append(tagToHost[name], longestDomain)
242+
}
208243
}
209244
}
210245

211246
httpOption, err := servingnetworking.GetHTTPOption(ctx, config.FromContext(ctx).Network, r.GetAnnotations())
212247
if err != nil {
213-
return netv1alpha1.IngressSpec{}, err
248+
return netv1alpha1.IngressSpec{}, nil, err
214249
}
215250

216-
return netv1alpha1.IngressSpec{
251+
spec := netv1alpha1.IngressSpec{
217252
Rules: rules,
218253
TLS: tls,
219254
HTTPOption: httpOption,
220-
}, nil
255+
}
256+
257+
return spec, tagToHost, nil
221258
}
222259

223260
// MakeACMEIngressPath converts an ACME challenge into an HTTPIngressPath.

0 commit comments

Comments
 (0)