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

fix(f5-virtualserver): skip endpoint creation when VirtualServer is not ready #4996

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions source/f5_virtualserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"sort"
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -147,6 +148,12 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
var endpoints []*endpoint.Endpoint

for _, virtualServer := range virtualServers {
if !isVirtualServerReady(virtualServer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a suggestion

if !virtualServer.isReady()

^ This will crash if virtualServer is null))

Copy link
Contributor Author

@mikejoh mikejoh Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally understand this but we would need to embed the f5.VirtualServer struct in our own data type and then add a method to that, in this context it feels a bit overkill? A helper func (as used now) that operates on a *f5.VirtualServer feels like a good middle ground.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was simply a style suggestion, nice one.

log.Warnf("F5 VirtualServer %s/%s is not ready or is missing an IP address, skipping endpoint creation.",
virtualServer.Namespace, virtualServer.Name)
continue
}

resource := fmt.Sprintf("f5-virtualserver/%s/%s", virtualServer.Namespace, virtualServer.Name)

ttl := getTTLFromAnnotations(virtualServer.Annotations, resource)
Expand All @@ -155,6 +162,7 @@ func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f
if len(targets) == 0 && virtualServer.Spec.VirtualServerAddress != "" {
targets = append(targets, virtualServer.Spec.VirtualServerAddress)
}

if len(targets) == 0 && virtualServer.Status.VSAddress != "" {
targets = append(targets, virtualServer.Status.VSAddress)
}
Expand Down Expand Up @@ -211,3 +219,10 @@ func (vs *f5VirtualServerSource) filterByAnnotations(virtualServers []*f5.Virtua

return filteredList, nil
}

func isVirtualServerReady(vs *f5.VirtualServer) bool {
normalizedStatus := strings.ToLower(vs.Status.Status)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential null pointer as vs is a reference

The null check will help to avoid external-dns crashlooping.

Ideally the null check should be added in method

func (vs *f5VirtualServerSource) endpointsFromVirtualServers
...

for _, virtualServer := range virtualServers {
   if virtualServer == nil {
            continue
        }

Copy link
Contributor Author

@mikejoh mikejoh Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch but AFAICT the vs pointer here are explicitly set earlier and appended to the slice of vs pointer (before passed to the endpointsFromVirtualServers method), this would effectively mitigate any risk of encountering a nil pointer.

Copy link
Contributor

@ivankatliarchuk ivankatliarchuk Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi

Looks like missed that piece ;-) If server is empty and created like that, then you right.

virtualServer := &f5.VirtualServer{}

If the virtual server created like the one above, is there is is a chance to throw a null pointer panic in helper function isVirtualServerReady, as vs.Status is not set?

vs.Status.Status 
vs.Status.VSAddress

Am I correct or missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the review comments, super helpful! 🙏🏻

The status field of the VirtualServer object is defined as a non-pointer: https://github.com/F5Networks/k8s-bigip-ctlr/blob/508f686bf2886a4f95af0f3a822e6ed25bce2ec7/config/apis/cis/v1/types.go#L19. AFAICT this means that in this context, the fields of the VirtualServerStatus struct, will be set to their respective zero values if not explicitly initialized!

normalizedAddress := strings.ToLower(vs.Status.VSAddress)

return normalizedStatus == "ok" && (normalizedAddress != "none" && normalizedAddress != "")
Copy link
Contributor

@ivankatliarchuk ivankatliarchuk Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion to use early return, example

if strings.ToLower(vs.Status.Status) != "ok" {
		return false
	}

normalizedAddress := strings.ToLower(vs.Status.VSAddress)
return normalizedAddress != "none" && normalizedAddress != ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, changed here: a70d5d0

}
66 changes: 66 additions & 0 deletions source/f5_virtualserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestF5VirtualServerEndpoints(t *testing.T) {
},
Status: f5.VirtualServerStatus{
VSAddress: "192.168.1.200",
Status: "OK",
},
},
expected: []*endpoint.Endpoint{
Expand Down Expand Up @@ -97,6 +98,7 @@ func TestF5VirtualServerEndpoints(t *testing.T) {
},
Status: f5.VirtualServerStatus{
VSAddress: "192.168.1.200",
Status: "OK",
},
},
expected: []*endpoint.Endpoint{
Expand Down Expand Up @@ -128,6 +130,7 @@ func TestF5VirtualServerEndpoints(t *testing.T) {
},
Status: f5.VirtualServerStatus{
VSAddress: "192.168.1.100",
Status: "OK",
},
},
expected: []*endpoint.Endpoint{
Expand Down Expand Up @@ -182,6 +185,10 @@ func TestF5VirtualServerEndpoints(t *testing.T) {
Host: "www.example.com",
VirtualServerAddress: "192.168.1.100",
},
Status: f5.VirtualServerStatus{
VSAddress: "192.168.1.100",
Status: "OK",
},
},
expected: []*endpoint.Endpoint{
{
Expand Down Expand Up @@ -214,6 +221,10 @@ func TestF5VirtualServerEndpoints(t *testing.T) {
Host: "www.example.com",
VirtualServerAddress: "192.168.1.100",
},
Status: f5.VirtualServerStatus{
VSAddress: "192.168.1.100",
Status: "OK",
},
},
expected: nil,
},
Expand All @@ -235,6 +246,10 @@ func TestF5VirtualServerEndpoints(t *testing.T) {
Host: "www.example.com",
VirtualServerAddress: "192.168.1.100",
},
Status: f5.VirtualServerStatus{
VSAddress: "192.168.1.100",
Status: "OK",
},
},
expected: []*endpoint.Endpoint{
{
Expand All @@ -248,6 +263,57 @@ func TestF5VirtualServerEndpoints(t *testing.T) {
},
},
},
{
name: "F5 VirtualServer with error status",
virtualServer: f5.VirtualServer{
TypeMeta: metav1.TypeMeta{
APIVersion: f5VirtualServerGVR.GroupVersion().String(),
Kind: "VirtualServer",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-vs",
Namespace: defaultF5VirtualServerNamespace,
Annotations: map[string]string{
"external-dns.alpha.kubernetes.io/ttl": "600",
},
},
Spec: f5.VirtualServerSpec{
Host: "www.example.com",
VirtualServerAddress: "192.168.1.100",
},
Status: f5.VirtualServerStatus{
VSAddress: "",
Status: "ERROR",
Error: "Some error status message",
},
},
expected: nil,
},
{
name: "F5 VirtualServer with missing IP address and OK status",
virtualServer: f5.VirtualServer{
TypeMeta: metav1.TypeMeta{
APIVersion: f5VirtualServerGVR.GroupVersion().String(),
Kind: "VirtualServer",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-vs",
Namespace: defaultF5VirtualServerNamespace,
Annotations: map[string]string{
"external-dns.alpha.kubernetes.io/ttl": "600",
},
},
Spec: f5.VirtualServerSpec{
Host: "www.example.com",
IPAMLabel: "test",
},
Status: f5.VirtualServerStatus{
VSAddress: "None",
Status: "OK",
},
},
expected: nil,
},
}

for _, tc := range tests {
Expand Down
Loading