Skip to content

Commit

Permalink
fix(Ingress) API version
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Guyennet <simon.guyennet@corp.ovh.com>
  • Loading branch information
sguyennet committed Mar 8, 2021
1 parent 3066ae5 commit e3b17a0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
3 changes: 2 additions & 1 deletion controllers/goharbor/harbor/harbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
netv1beta1 "k8s.io/api/networking/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
)
Expand Down Expand Up @@ -66,7 +67,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
Owns(&corev1.Secret{}).
Owns(&certv1.Issuer{}).
Owns(&certv1.Certificate{}).
Owns(&netv1.Ingress{}).
Owns(&netv1beta1.Ingress{}).
Owns(&netv1.NetworkPolicy{}).
WithOptions(controller.Options{
MaxConcurrentReconciles: int(concurrentReconcile),
Expand Down
95 changes: 42 additions & 53 deletions controllers/goharbor/harbor/ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"github.com/goharbor/harbor-operator/controllers/goharbor/notaryserver"
"github.com/goharbor/harbor-operator/pkg/graph"
"github.com/pkg/errors"
netv1 "k8s.io/api/networking/v1"
netv1beta1 "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

const (
Expand All @@ -32,15 +33,15 @@ func (r *Reconciler) AddCoreIngress(ctx context.Context, harbor *goharborv1alpha
return CoreIngress(ingressRes), errors.Wrap(err, "cannot add core ingress")
}

func (r *Reconciler) GetCoreIngress(ctx context.Context, harbor *goharborv1alpha2.Harbor) (*netv1.Ingress, error) {
func (r *Reconciler) GetCoreIngress(ctx context.Context, harbor *goharborv1alpha2.Harbor) (*netv1beta1.Ingress, error) {
if harbor.Spec.Expose.Core.Ingress == nil {
return nil, nil
}

var tls []netv1.IngressTLS
var tls []netv1beta1.IngressTLS

if harbor.Spec.Expose.Core.TLS.Enabled() {
tls = []netv1.IngressTLS{{
tls = []netv1beta1.IngressTLS{{
SecretName: harbor.Spec.Expose.Core.TLS.CertificateRef,
}}
}
Expand All @@ -50,54 +51,46 @@ func (r *Reconciler) GetCoreIngress(ctx context.Context, harbor *goharborv1alpha
return nil, errors.Wrap(err, "ingress rules")
}

return &netv1.Ingress{
return &netv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: r.NormalizeName(ctx, harbor.GetName()),
Namespace: harbor.GetNamespace(),
Annotations: r.GetCoreIngressAnnotations(ctx, harbor),
},
Spec: netv1.IngressSpec{
Spec: netv1beta1.IngressSpec{
TLS: tls,
Rules: rules,
},
}, nil
}

func (r *Reconciler) GetCoreIngressRules(ctx context.Context, harbor *goharborv1alpha2.Harbor) ([]netv1.IngressRule, error) {
func (r *Reconciler) GetCoreIngressRules(ctx context.Context, harbor *goharborv1alpha2.Harbor) ([]netv1beta1.IngressRule, error) {
corePort, err := harbor.Spec.InternalTLS.GetInternalPort(harbormetav1.CoreTLS)
if err != nil {
return nil, errors.Wrapf(err, "%s internal port", harbormetav1.CoreTLS)
}

coreBackend := netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: r.NormalizeName(ctx, harbor.GetName(), controllers.Core.String()),
Port: netv1.ServiceBackendPort{
Number: corePort,
},
},
coreBackend := netv1beta1.IngressBackend{
ServiceName: r.NormalizeName(ctx, harbor.GetName(), controllers.Core.String()),
ServicePort: intstr.FromInt(int(corePort)),
}

portalPort, err := harbor.Spec.InternalTLS.GetInternalPort(harbormetav1.PortalTLS)
if err != nil {
return nil, errors.Wrapf(err, "%s internal port", harbormetav1.PortalTLS)
}

portalBackend := netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: r.NormalizeName(ctx, harbor.GetName(), controllers.Portal.String()),
Port: netv1.ServiceBackendPort{
Number: portalPort,
},
},
portalBackend := netv1beta1.IngressBackend{
ServiceName: r.NormalizeName(ctx, harbor.GetName(), "portal"),
ServicePort: intstr.FromInt(int(portalPort)),
}

ruleValue, err := r.GetCoreIngressRuleValue(ctx, harbor, coreBackend, portalBackend)
if err != nil {
return nil, errors.Wrap(err, "rule value")
}

return []netv1.IngressRule{{
return []netv1beta1.IngressRule{{
Host: harbor.Spec.Expose.Core.Ingress.Host,
IngressRuleValue: *ruleValue,
}}, nil
Expand All @@ -116,7 +109,7 @@ func (r *Reconciler) AddNotaryIngress(ctx context.Context, harbor *goharborv1alp
return NotaryIngress(ingressRes), errors.Wrapf(err, "cannot add notary ingress")
}

func (r *Reconciler) GetNotaryServerIngresse(ctx context.Context, harbor *goharborv1alpha2.Harbor) (*netv1.Ingress, error) {
func (r *Reconciler) GetNotaryServerIngresse(ctx context.Context, harbor *goharborv1alpha2.Harbor) (*netv1beta1.Ingress, error) {
if harbor.Spec.Notary == nil {
return nil, nil
}
Expand All @@ -125,38 +118,34 @@ func (r *Reconciler) GetNotaryServerIngresse(ctx context.Context, harbor *goharb
return nil, nil
}

var tls []netv1.IngressTLS
var tls []netv1beta1.IngressTLS

if harbor.Spec.Expose.Notary.TLS.Enabled() {
tls = []netv1.IngressTLS{{
tls = []netv1beta1.IngressTLS{{
SecretName: harbor.Spec.Expose.Notary.TLS.CertificateRef,
}}
}

pathTypeExact := netv1.PathTypeExact
pathTypeExact := netv1beta1.PathTypeExact

return &netv1.Ingress{
return &netv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: r.NormalizeName(ctx, harbor.GetName(), controllers.NotaryServer.String()),
Namespace: harbor.GetNamespace(),
Annotations: r.GetNotaryIngressAnnotations(ctx, harbor),
},
Spec: netv1.IngressSpec{
Spec: netv1beta1.IngressSpec{
TLS: tls,
Rules: []netv1.IngressRule{{
Rules: []netv1beta1.IngressRule{{
Host: harbor.Spec.Expose.Notary.Ingress.Host,
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{{
IngressRuleValue: netv1beta1.IngressRuleValue{
HTTP: &netv1beta1.HTTPIngressRuleValue{
Paths: []netv1beta1.HTTPIngressPath{{
Path: "/",
PathType: &pathTypeExact,
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: r.NormalizeName(ctx, harbor.GetName(), controllers.NotaryServer.String()),
Port: netv1.ServiceBackendPort{
Number: notaryserver.PublicPort,
},
},
Backend: netv1beta1.IngressBackend{
ServiceName: r.NormalizeName(ctx, harbor.GetName(), controllers.NotaryServer.String()),
ServicePort: intstr.FromInt(notaryserver.PublicPort),
},
}},
},
Expand Down Expand Up @@ -216,14 +205,14 @@ func (err ErrInvalidIngressController) Error() string {
return fmt.Sprintf("controller %s unsupported", err.Controller)
}

func (r *Reconciler) GetCoreIngressRuleValue(ctx context.Context, harbor *goharborv1alpha2.Harbor, core, portal netv1.IngressBackend) (*netv1.IngressRuleValue, error) { // nolint:funlen
pathTypePrefix := netv1.PathTypePrefix
func (r *Reconciler) GetCoreIngressRuleValue(ctx context.Context, harbor *goharborv1alpha2.Harbor, core, portal netv1beta1.IngressBackend) (*netv1beta1.IngressRuleValue, error) { // nolint:funlen
pathTypePrefix := netv1beta1.PathTypePrefix

switch harbor.Spec.Expose.Core.Ingress.Controller {
case harbormetav1.IngressControllerDefault:
return &netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{{
return &netv1beta1.IngressRuleValue{
HTTP: &netv1beta1.HTTPIngressRuleValue{
Paths: []netv1beta1.HTTPIngressPath{{
Path: "/",
PathType: &pathTypePrefix,
Backend: portal,
Expand Down Expand Up @@ -251,11 +240,11 @@ func (r *Reconciler) GetCoreIngressRuleValue(ctx context.Context, harbor *goharb
},
}, nil
case harbormetav1.IngressControllerGCE:
pathTypePrefix := netv1.PathTypePrefix
pathTypePrefix := netv1beta1.PathTypePrefix

return &netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{{
return &netv1beta1.IngressRuleValue{
HTTP: &netv1beta1.HTTPIngressRuleValue{
Paths: []netv1beta1.HTTPIngressPath{{
Path: "/",
PathType: &pathTypePrefix,
Backend: portal,
Expand Down Expand Up @@ -283,12 +272,12 @@ func (r *Reconciler) GetCoreIngressRuleValue(ctx context.Context, harbor *goharb
},
}, nil
case harbormetav1.IngressControllerNCP:
pathTypeExact := netv1.PathTypeExact
pathTypePrefix := netv1.PathTypePrefix
pathTypeExact := netv1beta1.PathTypeExact
pathTypePrefix := netv1beta1.PathTypePrefix

return &netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{{
return &netv1beta1.IngressRuleValue{
HTTP: &netv1beta1.HTTPIngressRuleValue{
Paths: []netv1beta1.HTTPIngressPath{{
Path: "/",
PathType: &pathTypeExact,
Backend: portal,
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
netv1beta1 "k8s.io/api/networking/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -304,7 +305,7 @@ func (c *Controller) AddIssuerToManage(ctx context.Context, resource *certv1.Iss
return res, g.AddResource(ctx, res, dependencies, c.ProcessFunc(ctx, resource, dependencies...))
}

func (c *Controller) AddIngressToManage(ctx context.Context, resource *netv1.Ingress, dependencies ...graph.Resource) (graph.Resource, error) {
func (c *Controller) AddIngressToManage(ctx context.Context, resource *netv1beta1.Ingress, dependencies ...graph.Resource) (graph.Resource, error) {
if resource == nil {
return nil, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
certv1 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
netv1beta1 "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
controllerruntime "sigs.k8s.io/controller-runtime"
Expand All @@ -36,8 +36,8 @@ var _ = Context("Adding", func() {
&corev1.Secret{}: func(c *Controller, ctx context.Context, res resources.Resource, dep ...graph.Resource) (graph.Resource, error) {
return c.AddSecretToManage(ctx, res.(*corev1.Secret), dep...)
},
&netv1.Ingress{}: func(c *Controller, ctx context.Context, res resources.Resource, dep ...graph.Resource) (graph.Resource, error) {
return c.AddIngressToManage(ctx, res.(*netv1.Ingress), dep...)
&netv1beta1.Ingress{}: func(c *Controller, ctx context.Context, res resources.Resource, dep ...graph.Resource) (graph.Resource, error) {
return c.AddIngressToManage(ctx, res.(*netv1beta1.Ingress), dep...)
},
&certv1.Certificate{}: func(c *Controller, ctx context.Context, res resources.Resource, dep ...graph.Resource) (graph.Resource, error) {
return c.AddCertificateToManage(ctx, res.(*certv1.Certificate), dep...)
Expand Down

0 comments on commit e3b17a0

Please sign in to comment.