Skip to content

Commit

Permalink
chore(deps): make GetIngressLoadbalancerStatus not change its API aft…
Browse files Browse the repository at this point in the history
…er bumping k8s libs (#473)

Adds converting functions for LoadBalancerStatus to not change the public interface of GetIngressLoadbalancerStatus.
  • Loading branch information
czeslavo authored Dec 9, 2022
1 parent 6ba9271 commit 05b5d7f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
68 changes: 68 additions & 0 deletions internal/conversion/load_balancer_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package conversion

import (
corev1 "k8s.io/api/core/v1"
extv1beta1 "k8s.io/api/extensions/v1beta1"
netv1 "k8s.io/api/networking/v1"
netv1beta1 "k8s.io/api/networking/v1beta1"
)

func NetV1ToCoreV1LoadBalancerStatus(in netv1.IngressLoadBalancerStatus) *corev1.LoadBalancerStatus {
out := &corev1.LoadBalancerStatus{}
for _, i := range in.Ingress {
ports := make([]corev1.PortStatus, 0, len(i.Ports))
for _, p := range i.Ports {
ports = append(ports, corev1.PortStatus{
Port: p.Port,
Protocol: p.Protocol,
Error: p.Error,
})
}
out.Ingress = append(out.Ingress, corev1.LoadBalancerIngress{
IP: i.IP,
Hostname: i.Hostname,
Ports: ports,
})
}
return out
}

func NetV1beta1ToCoreV1LoadBalancerStatus(in netv1beta1.IngressLoadBalancerStatus) *corev1.LoadBalancerStatus {
out := &corev1.LoadBalancerStatus{}
for _, i := range in.Ingress {
ports := make([]corev1.PortStatus, 0, len(i.Ports))
for _, p := range i.Ports {
ports = append(ports, corev1.PortStatus{
Port: p.Port,
Protocol: p.Protocol,
Error: p.Error,
})
}
out.Ingress = append(out.Ingress, corev1.LoadBalancerIngress{
IP: i.IP,
Hostname: i.Hostname,
Ports: ports,
})
}
return out
}

func ExtV1beta1ToCoreV1LoadBalancerStatus(in extv1beta1.IngressLoadBalancerStatus) *corev1.LoadBalancerStatus {
out := &corev1.LoadBalancerStatus{}
for _, i := range in.Ingress {
ports := make([]corev1.PortStatus, 0, len(i.Ports))
for _, p := range i.Ports {
ports = append(ports, corev1.PortStatus{
Port: p.Port,
Protocol: p.Protocol,
Error: p.Error,
})
}
out.Ingress = append(out.Ingress, corev1.LoadBalancerIngress{
IP: i.IP,
Hostname: i.Hostname,
Ports: ports,
})
}
return out
}
18 changes: 16 additions & 2 deletions pkg/clusters/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

"github.com/kong/kubernetes-testing-framework/internal/conversion"
"github.com/kong/kubernetes-testing-framework/pkg/utils/kubernetes/generators"
)

Expand Down Expand Up @@ -74,14 +75,27 @@ func DeleteIngress(ctx context.Context, c Cluster, namespace string, ingress run
// given an Ingress object provided by the caller determine the version and pull a fresh copy
// of the current LoadBalancerStatus for that Ingress object without the caller needing to be
// aware of which version of Ingress they're using.
func GetIngressLoadbalancerStatus(ctx context.Context, c Cluster, namespace string, ingress runtime.Object) (*netv1.IngressLoadBalancerStatus, error) {
// TODO: once we stop supporting old Kubernetes versions <1.19 we can remove this.
func GetIngressLoadbalancerStatus(ctx context.Context, c Cluster, namespace string, ingress runtime.Object) (*corev1.LoadBalancerStatus, error) {
switch obj := ingress.(type) {
case *netv1.Ingress:
refresh, err := c.Client().NetworkingV1().Ingresses(namespace).Get(ctx, obj.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return &refresh.Status.LoadBalancer, nil
return conversion.NetV1ToCoreV1LoadBalancerStatus(refresh.Status.LoadBalancer), nil
case *netv1beta1.Ingress:
refresh, err := c.Client().NetworkingV1beta1().Ingresses(namespace).Get(ctx, obj.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return conversion.NetV1beta1ToCoreV1LoadBalancerStatus(refresh.Status.LoadBalancer), nil
case *extv1beta1.Ingress:
refresh, err := c.Client().ExtensionsV1beta1().Ingresses(namespace).Get(ctx, obj.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return conversion.ExtV1beta1ToCoreV1LoadBalancerStatus(refresh.Status.LoadBalancer), nil
default:
return nil, fmt.Errorf("%T is not a supported ingress type", ingress)
}
Expand Down

0 comments on commit 05b5d7f

Please sign in to comment.