-
Notifications
You must be signed in to change notification settings - Fork 103
/
ingresses.go
115 lines (102 loc) · 4.43 KB
/
ingresses.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
// Copyright 2019-2022 The Liqo 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 forge
import (
netv1 "k8s.io/api/networking/v1"
netv1apply "k8s.io/client-go/applyconfigurations/networking/v1"
)
// RemoteIngress forges the apply patch for the reflected ingress, given the local one.
func RemoteIngress(local *netv1.Ingress, targetNamespace string) *netv1apply.IngressApplyConfiguration {
return netv1apply.Ingress(local.GetName(), targetNamespace).
WithLabels(local.GetLabels()).WithLabels(ReflectionLabels()).
WithAnnotations(FilterIngressAnnotations(local.GetAnnotations())).
WithSpec(RemoteIngressSpec(local.Spec.DeepCopy()))
}
// FilterIngressAnnotations filters the ingress annotations to be reflected, removing the ingress class annotation.
func FilterIngressAnnotations(local map[string]string) map[string]string {
res := make(map[string]string)
for k, v := range local {
if k != "kubernetes.io/ingress.class" {
res[k] = v
}
}
return res
}
// RemoteIngressSpec forges the apply patch for the specs of the reflected ingress, given the local one.
// It expects the local object to be a deepcopy, as it is mutated.
func RemoteIngressSpec(local *netv1.IngressSpec) *netv1apply.IngressSpecApplyConfiguration {
return netv1apply.IngressSpec().
WithDefaultBackend(RemoteIngressBackend(local.DefaultBackend)).
WithRules(RemoteIngressRules(local.Rules)...).
WithTLS(RemoteIngressTLS(local.TLS)...)
}
// RemoteIngressBackend forges the apply patch for the backend of the reflected ingress, given the local one.
func RemoteIngressBackend(local *netv1.IngressBackend) *netv1apply.IngressBackendApplyConfiguration {
if local == nil {
return nil
}
return netv1apply.IngressBackend().
WithResource(RemoteTypedLocalObjectReference(local.Resource)).
WithService(RemoteIngressService(local.Service))
}
// RemoteIngressService forges the apply patch for the service of the reflected ingress, given the local one.
func RemoteIngressService(local *netv1.IngressServiceBackend) *netv1apply.IngressServiceBackendApplyConfiguration {
if local == nil {
return nil
}
return netv1apply.IngressServiceBackend().
WithName(local.Name).
WithPort(netv1apply.ServiceBackendPort().
WithName(local.Port.Name).
WithNumber(local.Port.Number))
}
// RemoteIngressRules forges the apply patch for the rules of the reflected ingress, given the local ones.
func RemoteIngressRules(local []netv1.IngressRule) []*netv1apply.IngressRuleApplyConfiguration {
remote := make([]*netv1apply.IngressRuleApplyConfiguration, len(local))
for i := range local {
remote[i] = netv1apply.IngressRule().
WithHost(local[i].Host).
WithHTTP(RemoteIngressHTTP(local[i].HTTP))
}
return remote
}
// RemoteIngressHTTP forges the apply patch for the HTTPIngressRuleValue of the reflected ingress, given the local one.
func RemoteIngressHTTP(local *netv1.HTTPIngressRuleValue) *netv1apply.HTTPIngressRuleValueApplyConfiguration {
if local == nil {
return nil
}
return netv1apply.HTTPIngressRuleValue().
WithPaths(RemoteIngressPaths(local.Paths)...)
}
// RemoteIngressPaths forges the apply patch for the paths of the reflected ingress, given the local ones.
func RemoteIngressPaths(local []netv1.HTTPIngressPath) []*netv1apply.HTTPIngressPathApplyConfiguration {
remote := make([]*netv1apply.HTTPIngressPathApplyConfiguration, len(local))
for i := range local {
remote[i] = netv1apply.HTTPIngressPath().
WithPath(local[i].Path).
WithBackend(RemoteIngressBackend(&local[i].Backend))
remote[i].PathType = local[i].PathType
}
return remote
}
// RemoteIngressTLS forges the apply patch for the TLS configs of the reflected ingress, given the local ones.
func RemoteIngressTLS(local []netv1.IngressTLS) []*netv1apply.IngressTLSApplyConfiguration {
remote := make([]*netv1apply.IngressTLSApplyConfiguration, len(local))
for i := range local {
remote[i] = netv1apply.IngressTLS().
WithHosts(local[i].Hosts...).
WithSecretName(local[i].SecretName)
}
return remote
}