Skip to content

Commit cbc295e

Browse files
committed
Renames the label designating Nodes that are running in the control plane
This accommodates the change made in v1.20 which became the standard in 1.24. This will require users to have Kubernetes v1.24 or higher; while NLK will run on lower versions, the control plane Nodes will not be excluded.
1 parent 6a7497e commit cbc295e

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The NGINX Loadbalancer for Kubernetes, or _NLK_, is a Kubernetes controller that
3333

3434
### What you will need
3535

36-
- [ ] A Kubernetes cluster running on-premise.
36+
- [ ] A On-Premise Kubernetes cluster running version 1.24 or higher.
3737
- [ ] One or more NGINX Plus hosts running outside your Kubernetes cluster (NGINX Plus hosts must have the ability to route traffic to the cluster).
3838

3939
There is a more detailed [Installation Reference](docs/README.md) available in the `docs/` directory.

internal/communication/roundtripper_test.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ package communication
88
import (
99
"bytes"
1010
"context"
11-
"github.com/nginxinc/kubernetes-nginx-ingress/internal/configuration"
12-
"k8s.io/client-go/kubernetes/fake"
11+
"fmt"
1312
netHttp "net/http"
13+
"net/http/httptest"
1414
"testing"
15+
16+
"github.com/nginxinc/kubernetes-nginx-ingress/internal/configuration"
17+
"k8s.io/client-go/kubernetes/fake"
1518
)
1619

1720
func TestNewRoundTripper(t *testing.T) {
@@ -47,32 +50,49 @@ func TestNewRoundTripper(t *testing.T) {
4750
}
4851

4952
func TestRoundTripperRoundTrip(t *testing.T) {
53+
// Create a mock HTTP server
54+
mockServer := httptest.NewServer(netHttp.HandlerFunc(func(w netHttp.ResponseWriter, r *netHttp.Request) {
55+
w.Header().Set("Content-Type", "application/json")
56+
w.Header().Set("x-mock-header", "test-value")
57+
w.WriteHeader(netHttp.StatusOK)
58+
fmt.Fprintln(w, `{"message": "mock response"}`)
59+
}))
60+
defer mockServer.Close()
61+
62+
// Initialize dependencies
5063
k8sClient := fake.NewSimpleClientset()
5164
settings, err := configuration.NewSettings(context.Background(), k8sClient)
65+
if err != nil {
66+
t.Fatalf("Unexpected error creating settings: %v", err)
67+
}
68+
5269
headers := NewHeaders()
5370
transport := NewTransport(NewTlsConfig(settings))
5471
roundTripper := NewRoundTripper(headers, transport)
5572

56-
request, err := NewRequest("GET", "http://example.com", nil)
73+
// Use the mock server URL
74+
request, err := NewRequest("GET", mockServer.URL, nil)
5775
if err != nil {
58-
t.Fatalf(`Unexpected error: %v`, err)
76+
t.Fatalf("Unexpected error: %v", err)
5977
}
6078

6179
request.Header.Set("Content-Type", "application/json")
6280
request.Header.Set("x-nginx-loadbalancer-kubernetes", "nlk")
6381

82+
// Perform the request
6483
response, err := roundTripper.RoundTrip(request)
6584
if err != nil {
66-
t.Fatalf(`Unexpected error: %v`, err)
85+
t.Fatalf("Unexpected error: %v", err)
6786
}
6887

6988
if response == nil {
70-
t.Fatalf(`response should not be nil`)
89+
t.Fatalf("Response should not be nil")
7190
}
7291

92+
// Validate response headers
7393
headerLen := len(response.Header)
7494
if headerLen <= 2 {
75-
t.Fatalf(`response.Header should have at least 2 elements, found %d`, headerLen)
95+
t.Fatalf("Response headers should have at least 2 elements, found %d", headerLen)
7696
}
7797
}
7898

internal/observation/watcher.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package observation
88
import (
99
"errors"
1010
"fmt"
11+
"time"
12+
1113
"github.com/nginxinc/kubernetes-nginx-ingress/internal/configuration"
1214
"github.com/nginxinc/kubernetes-nginx-ingress/internal/core"
1315
"github.com/sirupsen/logrus"
@@ -16,7 +18,6 @@ import (
1618
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1719
"k8s.io/client-go/informers"
1820
"k8s.io/client-go/tools/cache"
19-
"time"
2021
)
2122

2223
// Watcher is responsible for watching for changes to Kubernetes resources.
@@ -163,7 +164,7 @@ func (w *Watcher) initializeEventListeners() error {
163164
return nil
164165
}
165166

166-
// notMasterNode retrieves the IP Addresses of the nodes in the cluster. Currently, the master node is excluded. This is
167+
// notControlPlaneNode retrieves the IP Addresses of the nodes in the cluster. Currently, the master node is excluded. This is
167168
// because the master node may or may not be a worker node and thus may not be able to route traffic.
168169
func (w *Watcher) retrieveNodeIps() ([]string, error) {
169170
started := time.Now()
@@ -180,7 +181,7 @@ func (w *Watcher) retrieveNodeIps() ([]string, error) {
180181
for _, node := range nodes.Items {
181182

182183
// this is kind of a broad assumption, should probably make this a configurable option
183-
if w.notMasterNode(node) {
184+
if w.notControlPlaneNode(node) {
184185
for _, address := range node.Status.Addresses {
185186
if address.Type == v1.NodeInternalIP {
186187
nodeIps = append(nodeIps, address.Address)
@@ -194,11 +195,11 @@ func (w *Watcher) retrieveNodeIps() ([]string, error) {
194195
return nodeIps, nil
195196
}
196197

197-
// notMasterNode determines if the node is a master node.
198-
func (w *Watcher) notMasterNode(node v1.Node) bool {
199-
logrus.Debug("Watcher::notMasterNode")
198+
// notControlPlaneNode determines if the node is a master node.
199+
func (w *Watcher) notControlPlaneNode(node v1.Node) bool {
200+
logrus.Debug("Watcher::notControlPlaneNode")
200201

201-
_, found := node.Labels["node-role.kubernetes.io/master"]
202+
_, found := node.Labels["node-role.kubernetes.io/control-plane"]
202203

203204
return !found
204205
}

0 commit comments

Comments
 (0)