Newer
Older
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/agpl-3.0.html
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 components
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.libre.sh/interfaces"
meta "k8s.libre.sh/meta"
ingress "k8s.libre.sh/objects/ingress"
service "k8s.libre.sh/objects/service"
)
// +kubebuilder:object:generate=true
type Network struct {
IngressMeta *meta.ObjectMeta `json:"ingressMeta,omitempty"`
ServiceMeta *meta.ObjectMeta `json:"serviceMeta,omitempty"`
*Backend `json:",inline"`
}
type Service struct {
*meta.ObjectMeta `json:"meta,omitempty"`
*Backend `json:"backend,omitempty"`
}
func (s *Service) Mutate(obj interfaces.Object) error {
service.MutateService(s, obj.(*corev1.Service), s.ObjectMeta)
meta.MutateMeta(s, obj)
return nil
}
func (s *Service) GetObject() interfaces.Object {
return &corev1.Service{}
}
type Ingress struct {
*meta.ObjectMeta `json:"commonMeta,omitempty"`
Backends []Backend `json:"backends,omitempty"`
}
// +kubebuilder:object:generate=true
type Backend struct {
ServiceName string `json:"name,omitempty"`
Hostname string `json:"hostname,omitempty"`
Paths []string `json:"routes,omitempty"`
TLS bool `json:"tls,omitempty"`
TLSSecretRef string `json:"tlsSecretRef,omitempty"`
Port service.Port `json:"port,omitempty"`
ServiceType corev1.ServiceType `json:"type,omitempty"`
}
func (i *Ingress) Mutate(obj interfaces.Object) error {
ingress.MutateIngress(i, obj.(*networkingv1beta1.Ingress))
meta.MutateMeta(i, obj)
return nil
}
func (i *Ingress) GetObject() interfaces.Object {
return &networkingv1beta1.Ingress{}
}
func (b *Backend) GetServiceType() corev1.ServiceType { return b.ServiceType }
func (b *Backend) GetServicePorts() []corev1.ServicePort {
ports := []corev1.ServicePort{}
ports = append(ports, b.GetServicePort())
return ports
}
func (b *Backend) GetServicePort() corev1.ServicePort {
port := corev1.ServicePort{
TargetPort: intstr.FromString(b.Port.Name),
Port: b.Port.Port,
Name: b.Port.Name,
}
if len(b.Port.Protocol) > 0 {
port.Protocol = b.Port.Protocol
}
return port
}
func (b *Backend) GetIngressBackendPaths() []networkingv1beta1.HTTPIngressPath {
bkpaths := []networkingv1beta1.HTTPIngressPath{}
for _, p := range b.Paths {
path := networkingv1beta1.HTTPIngressPath{
Path: p,
Backend: networkingv1beta1.IngressBackend{
// TODO TO FIX
ServiceName: b.ServiceName,
ServicePort: intstr.FromString(b.Port.Name),
},
}
bkpaths = append(bkpaths, path)
}
return bkpaths
}
func (b *Backend) GetIngressRule() networkingv1beta1.IngressRule {
rule := networkingv1beta1.IngressRule{
Host: b.Hostname,
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: b.GetIngressBackendPaths(),
},
},
}
return rule
}
func (i *Ingress) GetIngressRules() []networkingv1beta1.IngressRule {
rules := []networkingv1beta1.IngressRule{}
for _, ing := range i.Backends {
rules = append(rules, ing.GetIngressRule())
}
return rules
}
func (b *Backend) GetIngressTLS() networkingv1beta1.IngressTLS {
tls := networkingv1beta1.IngressTLS{}
if b.TLS {
if len(b.TLSSecretRef) == 0 {
b.TLSSecretRef = fmt.Sprintf("%v-%v", strings.Replace(b.Hostname, ".", "-", -1), "tls")
}
tls = networkingv1beta1.IngressTLS{
SecretName: b.TLSSecretRef,
}
tls.Hosts = append(tls.Hosts, b.Hostname)
}
return tls
}
func (i *Ingress) GetIngressTLS() []networkingv1beta1.IngressTLS {
tlsList := []networkingv1beta1.IngressTLS{}
for _, ing := range i.Backends {
tlsList = append(tlsList, ing.GetIngressTLS())
}
return tlsList
}