Skip to content

Commit

Permalink
squash! fix: Handle existing load balancer class (#41, l7mp/stunner#104)
Browse files Browse the repository at this point in the history
fix: Open health-check service-port for LB Services only

# Conflicts:
#	internal/renderer/service_util.go
  • Loading branch information
rg0now committed Jan 3, 2024
1 parent a5628d3 commit 332ee55
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/renderer/service_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,14 @@ func (r *Renderer) createLbService4Gateway(c *RenderContext, gw *gwapiv1b1.Gatew
}
}

healthCheckPort, err := setHealthCheck(as, svc)
if err != nil {
c.log.V(1).Info("could not set health check port", "error", err.Error())
} else if healthCheckPort != 0 {
c.log.V(1).Info("health check port opened", "port", healthCheckPort)
// Open the health-check port for LoadBalancer Services only
if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
healthCheckPort, err := setHealthCheck(svc.GetAnnotations(), svc)
if err != nil {
c.log.V(1).Info("could not set health check port", "error", err.Error())
} else if healthCheckPort != 0 {
c.log.V(1).Info("health check port opened", "port", healthCheckPort)
}
}

// copy the LoadBalancer annotations from the GatewayConfig
Expand Down
55 changes: 55 additions & 0 deletions internal/renderer/service_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,8 @@ func TestRenderServiceUtil(t *testing.T) {
w := testutils.TestGwConfig.DeepCopy()
w.Spec.LoadBalancerServiceAnnotations = make(map[string]string)
w.Spec.LoadBalancerServiceAnnotations[opdefault.ServiceTypeAnnotationKey] = "ClusterIP"
w.Spec.LoadBalancerServiceAnnotations["service.beta.kubernetes.io/do-loadbalancer-healthcheck-port"] = "8080"
w.Spec.LoadBalancerServiceAnnotations["service.beta.kubernetes.io/do-loadbalancer-healthcheck-protocol"] = "HTTP"
c.cfs = []stnrv1a1.GatewayConfig{*w}
},
tester: func(t *testing.T, r *Renderer) {
Expand All @@ -1405,6 +1407,15 @@ func TestRenderServiceUtil(t *testing.T) {

spec := s.Spec
assert.Equal(t, corev1.ServiceTypeClusterIP, spec.Type, "svc type")

// clusterIP services do not need a health-check service-port
found := false
for _, sp := range spec.Ports {
if sp.Protocol == "TCP" && sp.Port == int32(8080) {
found = true
}
}
assert.False(t, found, "health-check port exists")
},
},
{
Expand All @@ -1418,6 +1429,8 @@ func TestRenderServiceUtil(t *testing.T) {
w := testutils.TestGwConfig.DeepCopy()
w.Spec.LoadBalancerServiceAnnotations = make(map[string]string)
w.Spec.LoadBalancerServiceAnnotations[opdefault.ServiceTypeAnnotationKey] = "NodePort"
w.Spec.LoadBalancerServiceAnnotations["service.beta.kubernetes.io/do-loadbalancer-healthcheck-port"] = "8080"
w.Spec.LoadBalancerServiceAnnotations["service.beta.kubernetes.io/do-loadbalancer-healthcheck-protocol"] = "HTTP"
c.cfs = []stnrv1a1.GatewayConfig{*w}
},
tester: func(t *testing.T, r *Renderer) {
Expand All @@ -1437,6 +1450,15 @@ func TestRenderServiceUtil(t *testing.T) {

spec := s.Spec
assert.Equal(t, corev1.ServiceTypeNodePort, spec.Type, "svc type")

// NodePort services do not need a health-check service-port
found := false
for _, sp := range spec.Ports {
if sp.Protocol == "TCP" && sp.Port == int32(8080) {
found = true
}
}
assert.False(t, found, "health-check port exists")
},
},
{
Expand All @@ -1450,6 +1472,8 @@ func TestRenderServiceUtil(t *testing.T) {
gw := testutils.TestGw.DeepCopy()
ann := make(map[string]string)
ann[opdefault.ServiceTypeAnnotationKey] = "ClusterIP"
ann["service.beta.kubernetes.io/do-loadbalancer-healthcheck-port"] = "8080"
ann["service.beta.kubernetes.io/do-loadbalancer-healthcheck-protocol"] = "HTTP"
gw.SetAnnotations(ann)
c.gws = []gwapiv1b1.Gateway{*gw}
},
Expand All @@ -1470,6 +1494,15 @@ func TestRenderServiceUtil(t *testing.T) {

spec := s.Spec
assert.Equal(t, corev1.ServiceTypeClusterIP, spec.Type, "svc type")

// ClusterIPservices do not need a health-check service-port
found := false
for _, sp := range spec.Ports {
if sp.Protocol == "TCP" && sp.Port == int32(8080) {
found = true
}
}
assert.False(t, found, "health-check port exists")
},
},
{
Expand All @@ -1483,6 +1516,8 @@ func TestRenderServiceUtil(t *testing.T) {
w := testutils.TestGwConfig.DeepCopy()
w.Spec.LoadBalancerServiceAnnotations = make(map[string]string)
w.Spec.LoadBalancerServiceAnnotations[opdefault.ServiceTypeAnnotationKey] = "ClusterIP"
w.Spec.LoadBalancerServiceAnnotations["service.beta.kubernetes.io/do-loadbalancer-healthcheck-port"] = "8080"
w.Spec.LoadBalancerServiceAnnotations["service.beta.kubernetes.io/do-loadbalancer-healthcheck-protocol"] = "HTTP"
c.cfs = []stnrv1a1.GatewayConfig{*w}
gw := testutils.TestGw.DeepCopy()
ann := make(map[string]string)
Expand All @@ -1507,6 +1542,15 @@ func TestRenderServiceUtil(t *testing.T) {

spec := s.Spec
assert.Equal(t, corev1.ServiceTypeNodePort, spec.Type, "svc type")

// NodePort services do not need a health-check service-port
found := false
for _, sp := range spec.Ports {
if sp.Protocol == "TCP" && sp.Port == int32(8080) {
found = true
}
}
assert.False(t, found, "health-check port exists")
},
},
{
Expand All @@ -1520,6 +1564,8 @@ func TestRenderServiceUtil(t *testing.T) {
gw := testutils.TestGw.DeepCopy()
ann := make(map[string]string)
ann[opdefault.ServiceTypeAnnotationKey] = "NodePort"
ann["service.beta.kubernetes.io/do-loadbalancer-healthcheck-port"] = "8080"
ann["service.beta.kubernetes.io/do-loadbalancer-healthcheck-protocol"] = "HTTP"
gw.SetAnnotations(ann)
c.gws = []gwapiv1b1.Gateway{*gw}
},
Expand All @@ -1540,6 +1586,15 @@ func TestRenderServiceUtil(t *testing.T) {

spec := s.Spec
assert.Equal(t, corev1.ServiceTypeNodePort, spec.Type, "svc type")

// NodePort services do not need a health-check service-port
found := false
for _, sp := range spec.Ports {
if sp.Protocol == "TCP" && sp.Port == int32(8080) {
found = true
}
}
assert.False(t, found, "health-check port exists")
},
},
{
Expand Down

0 comments on commit 332ee55

Please sign in to comment.