Skip to content

Commit

Permalink
ingress:bugfix - updates ingress api version (networking.k8s.io/v1bet…
Browse files Browse the repository at this point in the history
…a1 -> v1) (#152)

Signed-off-by: Vinicius Niche <viniciusnichecorrea@gmail.com>
  • Loading branch information
vniche committed Feb 23, 2022
1 parent 82f6568 commit 145219d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 52 deletions.
4 changes: 2 additions & 2 deletions internal/horusec/usecase/kubeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
autoscaling "k8s.io/api/autoscaling/v2beta2"
batch "k8s.io/api/batch/v1"
core "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1beta1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/ZupIT/horusec-operator/api/v2alpha1"
Expand All @@ -33,7 +33,7 @@ type KubernetesClient interface {
UpdateHorusStatus(ctx context.Context, horus *v2alpha1.HorusecPlatform) error
ListAutoscalingByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]autoscaling.HorizontalPodAutoscaler, error)
ListDeploymentsByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]apps.Deployment, error)
ListIngressByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]networking.Ingress, error)
ListIngressByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]networkingv1.Ingress, error)
ListJobsByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]batch.Job, error)
ListPodsByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]core.Pod, error)
ListServiceAccountsByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]core.ServiceAccount, error)
Expand Down
4 changes: 2 additions & 2 deletions internal/horusec/usecase/resourcebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
networkingv1 "k8s.io/api/networking/v1"

"github.com/ZupIT/horusec-operator/api/v2alpha1"
)

type ResourceBuilder interface {
AutoscalingFor(resource *v2alpha1.HorusecPlatform) ([]autoscalingv2beta2.HorizontalPodAutoscaler, error)
DeploymentsFor(resource *v2alpha1.HorusecPlatform) ([]appsv1.Deployment, error)
IngressFor(resource *v2alpha1.HorusecPlatform) ([]networkingv1beta1.Ingress, error)
IngressFor(resource *v2alpha1.HorusecPlatform) ([]networkingv1.Ingress, error)
JobsFor(resource *v2alpha1.HorusecPlatform) ([]batchv1.Job, error)
ServiceAccountsFor(resource *v2alpha1.HorusecPlatform) ([]corev1.ServiceAccount, error)
ServicesFor(resource *v2alpha1.HorusecPlatform) ([]corev1.Service, error)
Expand Down
10 changes: 5 additions & 5 deletions internal/inventory/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
"fmt"

"github.com/google/go-cmp/cmp"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/ZupIT/horusec-operator/internal/k8s"
networkingv1 "k8s.io/api/networking/v1"
)

//nolint:gocritic, funlen // to improve in the future
func ForIngresses(existing, desired []networkingv1beta1.Ingress) k8s.Objects {
func ForIngresses(existing, desired []networkingv1.Ingress) k8s.Objects {
var update []client.Object
mcreate := ingressMap(desired)
mdelete := ingressMap(existing)
Expand Down Expand Up @@ -62,16 +62,16 @@ func ForIngresses(existing, desired []networkingv1beta1.Ingress) k8s.Objects {
}

//nolint:gocritic // to improve in the future
func ingressMap(deps []networkingv1beta1.Ingress) map[string]networkingv1beta1.Ingress {
m := map[string]networkingv1beta1.Ingress{}
func ingressMap(deps []networkingv1.Ingress) map[string]networkingv1.Ingress {
m := map[string]networkingv1.Ingress{}
for _, d := range deps {
m[fmt.Sprintf("%s.%s", d.Namespace, d.Name)] = d
}
return m
}

//nolint // to improve in the future
func ingressList(m map[string]networkingv1beta1.Ingress) []client.Object {
func ingressList(m map[string]networkingv1.Ingress) []client.Object {
var l []client.Object
for _, v := range m {
obj := v
Expand Down
6 changes: 3 additions & 3 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
autoscaling "k8s.io/api/autoscaling/v2beta2"
batch "k8s.io/api/batch/v1"
core "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1beta1"
networkingv1 "k8s.io/api/networking/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -130,15 +130,15 @@ func (d *Client) ListDeploymentsByOwner(ctx context.Context, owner *v2alpha1.Hor
return list.Items, nil
}

func (d *Client) ListIngressByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]networking.Ingress, error) {
func (d *Client) ListIngressByOwner(ctx context.Context, owner *v2alpha1.HorusecPlatform) ([]networkingv1.Ingress, error) {
span, ctx := tracing.StartSpanFromContext(ctx)
defer span.Finish()

opts := []client.ListOption{
client.InNamespace(owner.GetNamespace()),
client.MatchingLabels(owner.GetDefaultLabel()),
}
list := &networking.IngressList{}
list := &networkingv1.IngressList{}
if err := d.List(ctx, list, opts...); err != nil {
return nil, span.HandleError(fmt.Errorf("failed to list %s ingress: %w", owner.GetName(), err))
}
Expand Down
6 changes: 3 additions & 3 deletions internal/resources/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ package resources
import (
"fmt"

networkingv1beta1 "k8s.io/api/networking/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/ZupIT/horusec-operator/api/v2alpha1"
"github.com/ZupIT/horusec-operator/internal/resources/ingress"
networkingv1 "k8s.io/api/networking/v1"
)

func (b *Builder) IngressFor(resource *v2alpha1.HorusecPlatform) ([]networkingv1beta1.Ingress, error) {
var desiredList []networkingv1beta1.Ingress
func (b *Builder) IngressFor(resource *v2alpha1.HorusecPlatform) ([]networkingv1.Ingress, error) {
var desiredList []networkingv1.Ingress
if !resource.GetAllIngressIsDisabled() {
desired := ingress.NewIngress(resource)
if err := controllerutil.SetControllerReference(resource, &desired, b.scheme); err != nil {
Expand Down
45 changes: 24 additions & 21 deletions internal/resources/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,35 @@ package ingress
import (
"sort"

networkingv1beta1 "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/ZupIT/horusec-operator/api/v2alpha1"
networkingv1 "k8s.io/api/networking/v1"
)

//nolint:funlen // improve in the future
func NewIngress(resource *v2alpha1.HorusecPlatform) networkingv1beta1.Ingress {
return networkingv1beta1.Ingress{
func NewIngress(resource *v2alpha1.HorusecPlatform) networkingv1.Ingress {
return networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: resource.GetName(),
Namespace: resource.GetNamespace(),
Labels: resource.GetDefaultLabel(),
},
Spec: networkingv1beta1.IngressSpec{
Spec: networkingv1.IngressSpec{
Rules: newIngressRules(resource),
TLS: newIngressTLS(resource),
},
}
}

func newIngressRules(resource *v2alpha1.HorusecPlatform) []networkingv1beta1.IngressRule {
func newIngressRules(resource *v2alpha1.HorusecPlatform) []networkingv1.IngressRule {
hosts := mapHosts(resource)
rules := make([]networkingv1beta1.IngressRule, 0, len(hosts))
rules := make([]networkingv1.IngressRule, 0, len(hosts))
for host, backends := range hosts {
rules = append(rules, networkingv1beta1.IngressRule{
rules = append(rules, networkingv1.IngressRule{
Host: host,
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{Paths: backends},
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{Paths: backends},
},
})
}
Expand All @@ -56,11 +55,11 @@ func newIngressRules(resource *v2alpha1.HorusecPlatform) []networkingv1beta1.Ing
return rules
}

func newIngressTLS(resource *v2alpha1.HorusecPlatform) []networkingv1beta1.IngressTLS {
func newIngressTLS(resource *v2alpha1.HorusecPlatform) []networkingv1.IngressTLS {
secrets := mapTLSSecrets(resource)
tls := make([]networkingv1beta1.IngressTLS, 0, len(secrets))
tls := make([]networkingv1.IngressTLS, 0, len(secrets))
for secret, hosts := range secrets {
tls = append(tls, networkingv1beta1.IngressTLS{
tls = append(tls, networkingv1.IngressTLS{
Hosts: hosts,
SecretName: secret,
})
Expand All @@ -74,20 +73,24 @@ func newIngressTLS(resource *v2alpha1.HorusecPlatform) []networkingv1beta1.Ingre
return tls
}

func newHTTPIngressPath(path, service string) networkingv1beta1.HTTPIngressPath {
prefix := networkingv1beta1.PathTypePrefix
return networkingv1beta1.HTTPIngressPath{
func newHTTPIngressPath(path, service string) networkingv1.HTTPIngressPath {
prefix := networkingv1.PathTypePrefix
return networkingv1.HTTPIngressPath{
Path: path,
PathType: &prefix,
Backend: networkingv1beta1.IngressBackend{
ServiceName: service,
ServicePort: intstr.FromString("http"),
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: service,
Port: networkingv1.ServiceBackendPort{
Name: "http",
},
},
},
}
}

func mapHosts(r *v2alpha1.HorusecPlatform) map[string][]networkingv1beta1.HTTPIngressPath {
hosts := make(map[string][]networkingv1beta1.HTTPIngressPath, 0)
func mapHosts(r *v2alpha1.HorusecPlatform) map[string][]networkingv1.HTTPIngressPath {
hosts := make(map[string][]networkingv1.HTTPIngressPath, 0)
for _, ingress := range r.Ingresses() {
if ingress.IsEnabled() {
path := ingress.GetPath()
Expand Down
32 changes: 16 additions & 16 deletions test/kubernetes_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 145219d

Please sign in to comment.