Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #1209 [Expose custom health check ports in the firewall] into release-1-9 #1220

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pkg/firewalls/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"time"

apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -181,8 +182,15 @@ func (fwc *FirewallController) sync(key string) error {
}
}

var additionalPorts []string
if flags.F.EnableBackendConfigHealthCheck {
hcPorts := fwc.getCustomHealthCheckPorts(gceSvcPorts)
additionalPorts = append(additionalPorts, hcPorts...)
}
additionalPorts = append(additionalPorts, negPorts...)

// Ensure firewall rule for the cluster and pass any NEG endpoint ports.
if err := fwc.firewallPool.Sync(nodeNames, negPorts, additionalRanges); err != nil {
if err := fwc.firewallPool.Sync(nodeNames, additionalPorts, additionalRanges); err != nil {
if fwErr, ok := err.(*FirewallXPNError); ok {
// XPN: Raise an event on each ingress
for _, ing := range gceIngresses {
Expand Down Expand Up @@ -217,3 +225,15 @@ func (fwc *FirewallController) ilbFirewallSrcRange(gceIngresses []*v1beta1.Ingre

return "", ErrNoILBIngress
}

func (fwc *FirewallController) getCustomHealthCheckPorts(svcPorts []utils.ServicePort) []string {
var result []string

for _, svcPort := range svcPorts {
if svcPort.BackendConfig != nil && svcPort.BackendConfig.Spec.HealthCheck != nil && svcPort.BackendConfig.Spec.HealthCheck.Port != nil {
result = append(result, strconv.FormatInt(*svcPort.BackendConfig.Spec.HealthCheck.Port, 10))
}
}

return result
}
38 changes: 38 additions & 0 deletions pkg/firewalls/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
api_v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/fake"
v1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
backendconfigclient "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned/fake"
test "k8s.io/ingress-gce/pkg/test"
"k8s.io/ingress-gce/pkg/utils"
Expand Down Expand Up @@ -102,3 +104,39 @@ func TestFirewallCreateDelete(t *testing.T) {
t.Fatalf("cloud.GetFirewall(%v) = _, %v, want _, 404 error", ruleName, err)
}
}

func TestGetCustomHealthCheckPorts(t *testing.T) {
t.Parallel()

testCases := []struct {
desc string
svcPorts []utils.ServicePort
expect []string
}{
{
desc: "One service port with custom port",
svcPorts: []utils.ServicePort{utils.ServicePort{BackendConfig: &v1.BackendConfig{Spec: v1.BackendConfigSpec{HealthCheck: &v1.HealthCheckConfig{Port: utils.NewInt64Pointer(8000)}}}}},
expect: []string{"8000"},
},
{
desc: "Two service ports with custom port",
svcPorts: []utils.ServicePort{utils.ServicePort{BackendConfig: &v1.BackendConfig{Spec: v1.BackendConfigSpec{HealthCheck: &v1.HealthCheckConfig{Port: utils.NewInt64Pointer(8000)}}}},
utils.ServicePort{BackendConfig: &v1.BackendConfig{Spec: v1.BackendConfigSpec{HealthCheck: &v1.HealthCheckConfig{Port: utils.NewInt64Pointer(9000)}}}}},
expect: []string{"8000", "9000"},
},
{
desc: "No service ports",
expect: nil,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
fwc := newFirewallController()
result := fwc.getCustomHealthCheckPorts(tc.svcPorts)
if diff := cmp.Diff(tc.expect, result); diff != "" {
t.Errorf("unexpected diff of ports (-want +got): \n%s", diff)
}
})
}
}
5 changes: 5 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,8 @@ func MakeL4ILBServiceDescription(svcName, ip string, version meta.Version) (stri
func NewStringPointer(s string) *string {
return &s
}

// NewInt64Pointer returns a pointer to the provided int64 literal
func NewInt64Pointer(i int64) *int64 {
return &i
}