/
ingresses.go
286 lines (241 loc) · 8.5 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package harbor
import (
"context"
"fmt"
goharborv1 "github.com/goharbor/harbor-operator/apis/goharbor.io/v1beta1"
harbormetav1 "github.com/goharbor/harbor-operator/apis/meta/v1alpha1"
"github.com/goharbor/harbor-operator/controllers"
"github.com/goharbor/harbor-operator/pkg/graph"
"github.com/pkg/errors"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
NCPIngressValueTrue = "true"
ContourIngressValueTrue = "true"
)
type CoreIngress graph.Resource
func (r *Reconciler) AddCoreIngress(ctx context.Context, harbor *goharborv1.Harbor, core Core, portal Portal) (CoreIngress, error) {
ingress, err := r.GetCoreIngress(ctx, harbor)
if err != nil {
return nil, errors.Wrap(err, "cannot get core ingress")
}
ingressRes, err := r.Controller.AddIngressToManage(ctx, ingress, core, portal)
return CoreIngress(ingressRes), errors.Wrap(err, "cannot add core ingress")
}
func (r *Reconciler) GetCoreIngress(ctx context.Context, harbor *goharborv1.Harbor) (*netv1.Ingress, error) {
var tls []netv1.IngressTLS
if harbor.Spec.Expose.Core.TLS.Enabled() {
tls = []netv1.IngressTLS{{
SecretName: harbor.Spec.Expose.Core.TLS.CertificateRef,
Hosts: []string{harbor.Spec.Expose.Core.Ingress.Host},
}}
}
rules, err := r.GetCoreIngressRules(ctx, harbor)
if err != nil {
return nil, errors.Wrap(err, "ingress rules")
}
return &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: r.NormalizeName(ctx, harbor.GetName()),
Namespace: harbor.GetNamespace(),
Annotations: r.GetCoreIngressAnnotations(ctx, harbor),
},
Spec: netv1.IngressSpec{
TLS: tls,
Rules: rules,
IngressClassName: harbor.Spec.Expose.Core.Ingress.IngressClassName,
},
}, nil
}
func (r *Reconciler) GetCoreIngressRules(ctx context.Context, harbor *goharborv1.Harbor) ([]netv1.IngressRule, error) {
var portalBackend *netv1.IngressBackend
coreBackend := netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: r.NormalizeName(ctx, harbor.GetName(), controllers.Core.String()),
Port: netv1.ServiceBackendPort{
Number: harbor.Spec.InternalTLS.GetInternalPort(harbormetav1.CoreTLS),
},
},
Resource: nil,
}
if harbor.Spec.Portal != nil {
portalBackend = &netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: r.NormalizeName(ctx, harbor.GetName(), "portal"),
Port: netv1.ServiceBackendPort{
Number: harbor.Spec.InternalTLS.GetInternalPort(harbormetav1.PortalTLS),
},
},
}
}
ruleValue, err := r.GetCoreIngressRuleValue(ctx, harbor, &coreBackend, portalBackend)
if err != nil {
return nil, errors.Wrap(err, "rule value")
}
return []netv1.IngressRule{{
Host: harbor.Spec.Expose.Core.Ingress.Host,
IngressRuleValue: *ruleValue,
}}, nil
}
type NotaryIngress graph.Resource
func (r *Reconciler) AddNotaryIngress(ctx context.Context, harbor *goharborv1.Harbor, notary NotaryServer) (NotaryIngress, error) {
ingress, err := r.GetNotaryServerIngress(ctx, harbor)
if err != nil {
return nil, errors.Wrap(err, "cannot get notary ingress")
}
ingressRes, err := r.Controller.AddIngressToManage(ctx, ingress, notary)
return NotaryIngress(ingressRes), errors.Wrapf(err, "cannot add notary ingress")
}
func (r *Reconciler) GetNotaryServerIngress(ctx context.Context, harbor *goharborv1.Harbor) (*netv1.Ingress, error) {
if harbor.Spec.Notary == nil {
return nil, nil
}
if harbor.Spec.Expose.Notary == nil {
return nil, nil
}
var tls []netv1.IngressTLS
if harbor.Spec.Expose.Notary.TLS.Enabled() {
tls = []netv1.IngressTLS{{
SecretName: harbor.Spec.Expose.Notary.TLS.CertificateRef,
Hosts: []string{harbor.Spec.Expose.Notary.Ingress.Host},
}}
}
ingressRules, err := r.GetNotaryIngressRules(ctx, harbor)
if err != nil {
return nil, errors.Wrap(err, "cannot get notary ingress rules")
}
return &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: r.NormalizeName(ctx, harbor.GetName(), controllers.NotaryServer.String()),
Namespace: harbor.GetNamespace(),
Annotations: r.GetNotaryIngressAnnotations(ctx, harbor),
},
Spec: netv1.IngressSpec{
TLS: tls,
Rules: ingressRules,
IngressClassName: harbor.Spec.Expose.Notary.Ingress.IngressClassName,
},
}, nil
}
func (r *Reconciler) GetNotaryIngressRules(ctx context.Context, harbor *goharborv1.Harbor) ([]netv1.IngressRule, error) {
backend := netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: r.NormalizeName(ctx, harbor.GetName(), controllers.NotaryServer.String()),
Port: netv1.ServiceBackendPort{
Number: harbor.Spec.InternalTLS.GetInternalPort(harbormetav1.NotaryServerTLS),
},
},
}
pathTypePrefix := netv1.PathTypePrefix
return []netv1.IngressRule{
{
Host: harbor.Spec.Expose.Notary.Ingress.Host,
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
{
Path: "/",
PathType: &pathTypePrefix,
Backend: backend,
},
},
},
},
},
}, nil
}
func (r *Reconciler) GetCoreIngressAnnotations(ctx context.Context, harbor *goharborv1.Harbor) map[string]string {
// https://github.com/kubernetes/ingress-nginx/blob/master/internal/ingress/annotations/backendprotocol/main.go#L34
protocol := "HTTP"
if harbor.Spec.InternalTLS.IsEnabled() {
protocol = "HTTPS"
}
annotations := map[string]string{
"nginx.ingress.kubernetes.io/backend-protocol": protocol,
// resolve 413(Too Large Entity) error when push large image. It only works for NGINX ingress.
"nginx.ingress.kubernetes.io/proxy-body-size": "0",
}
if harbor.Spec.Expose.Core.Ingress.Controller == harbormetav1.IngressControllerNCP {
annotations["ncp/use-regex"] = NCPIngressValueTrue
if harbor.Spec.InternalTLS.IsEnabled() {
annotations["ncp/http-redirect"] = NCPIngressValueTrue
}
} else if harbor.Spec.Expose.Core.Ingress.Controller == harbormetav1.IngressControllerContour {
if harbor.Spec.InternalTLS.IsEnabled() {
annotations["ingress.kubernetes.io/force-ssl-redirect"] = ContourIngressValueTrue
}
}
for key, value := range harbor.Spec.Expose.Core.Ingress.Annotations {
annotations[key] = value
}
return annotations
}
func (r *Reconciler) GetNotaryIngressAnnotations(ctx context.Context, harbor *goharborv1.Harbor) map[string]string {
// https://github.com/kubernetes/ingress-nginx/blob/master/internal/ingress/annotations/backendprotocol/main.go#L34
protocol := "HTTP"
if harbor.Spec.InternalTLS.IsEnabled() {
protocol = "HTTPS"
}
annotations := map[string]string{
"nginx.ingress.kubernetes.io/backend-protocol": protocol,
// resolve 413(Too Large Entity) error when push large image. It only works for NGINX ingress.
"nginx.ingress.kubernetes.io/proxy-body-size": "0",
}
if harbor.Spec.Expose.Core.Ingress.Controller == harbormetav1.IngressControllerNCP {
annotations["ncp/use-regex"] = NCPIngressValueTrue
if harbor.Spec.InternalTLS.IsEnabled() {
annotations["ncp/http-redirect"] = NCPIngressValueTrue
}
} else if harbor.Spec.Expose.Core.Ingress.Controller == harbormetav1.IngressControllerContour {
if harbor.Spec.InternalTLS.IsEnabled() {
annotations["ingress.kubernetes.io/force-ssl-redirect"] = ContourIngressValueTrue
}
}
for key, value := range harbor.Spec.Expose.Notary.Ingress.Annotations {
annotations[key] = value
}
return annotations
}
type ErrInvalidIngressController struct {
Controller harbormetav1.IngressController
}
func (err ErrInvalidIngressController) Error() string {
return fmt.Sprintf("controller %s unsupported", err.Controller)
}
func (r *Reconciler) GetCoreIngressRuleValue(ctx context.Context, harbor *goharborv1.Harbor, core, portal *netv1.IngressBackend) (*netv1.IngressRuleValue, error) {
pathTypePrefix := netv1.PathTypePrefix
rule := &netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{{
Path: "/api/",
PathType: &pathTypePrefix,
Backend: *core,
}, {
Path: "/service/",
PathType: &pathTypePrefix,
Backend: *core,
}, {
Path: "/v2", // distribution APIs will request to `/v2` so don't append slash for this ingress path
PathType: &pathTypePrefix,
Backend: *core,
}, {
Path: "/chartrepo/",
PathType: &pathTypePrefix,
Backend: *core,
}, {
Path: "/c/",
PathType: &pathTypePrefix,
Backend: *core,
}},
},
}
if portal != nil {
rule.HTTP.Paths = append(rule.HTTP.Paths, netv1.HTTPIngressPath{
Path: "/",
PathType: &pathTypePrefix,
Backend: *portal,
})
}
return rule, nil
}