Skip to content

Commit 511aa0c

Browse files
committed
fix: updateLoadBalancer only when all nodes are initialized
1 parent 374ae20 commit 511aa0c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

scaleway/loadbalancers.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"golang.org/x/exp/slices"
3131
v1 "k8s.io/api/core/v1"
32+
"k8s.io/cloud-provider/api"
3233
"k8s.io/klog/v2"
3334

3435
scwipam "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
@@ -566,6 +567,10 @@ func (l *loadbalancers) updateLoadBalancer(ctx context.Context, loadbalancer *sc
566567
return fmt.Errorf("invalid value for annotation %s: expected boolean", serviceAnnotationLoadBalancerExternallyManaged)
567568
}
568569

570+
if err := nodesInitialized(nodes); err != nil {
571+
return err
572+
}
573+
569574
nodes = filterNodes(service, nodes)
570575
if l.pnID != "" {
571576
respPN, err := l.api.ListLBPrivateNetworks(&scwlb.ZonedAPIListLBPrivateNetworksRequest{
@@ -1673,3 +1678,16 @@ func ptrBoolToString(b *bool) string {
16731678
}
16741679
return fmt.Sprintf("%t", *b)
16751680
}
1681+
1682+
// nodesInitialized verifies that all nodes are initialized before using them as LoadBalancer targets.
1683+
func nodesInitialized(nodes []*v1.Node) error {
1684+
for _, node := range nodes {
1685+
if slices.ContainsFunc(node.Spec.Taints, func(taint v1.Taint) bool {
1686+
return taint.Key == api.TaintExternalCloudProvider
1687+
}) {
1688+
return fmt.Errorf("node %s is not yet initialized", node.Name)
1689+
}
1690+
}
1691+
1692+
return nil
1693+
}

scaleway/loadbalancers_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/scaleway/scaleway-sdk-go/scw"
2828
v1 "k8s.io/api/core/v1"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
"k8s.io/cloud-provider/api"
3031
)
3132

3233
func TestGetValueForPort(t *testing.T) {
@@ -1255,3 +1256,57 @@ func deepCloneBackend(original *scwlb.Backend) *scwlb.Backend {
12551256

12561257
return clone
12571258
}
1259+
1260+
func Test_nodesInitialized(t *testing.T) {
1261+
type args struct {
1262+
nodes []*v1.Node
1263+
}
1264+
tests := []struct {
1265+
name string
1266+
args args
1267+
wantErr bool
1268+
}{
1269+
{
1270+
name: "node initialized",
1271+
args: args{
1272+
nodes: []*v1.Node{
1273+
{
1274+
ObjectMeta: metav1.ObjectMeta{
1275+
Name: "test",
1276+
},
1277+
},
1278+
},
1279+
},
1280+
wantErr: false,
1281+
},
1282+
{
1283+
name: "node not initialized",
1284+
args: args{
1285+
nodes: []*v1.Node{
1286+
{
1287+
ObjectMeta: metav1.ObjectMeta{
1288+
Name: "test",
1289+
},
1290+
Spec: v1.NodeSpec{
1291+
Taints: []v1.Taint{
1292+
{
1293+
Key: api.TaintExternalCloudProvider,
1294+
Value: "true",
1295+
Effect: v1.TaintEffectNoSchedule,
1296+
},
1297+
},
1298+
},
1299+
},
1300+
},
1301+
},
1302+
wantErr: true,
1303+
},
1304+
}
1305+
for _, tt := range tests {
1306+
t.Run(tt.name, func(t *testing.T) {
1307+
if err := nodesInitialized(tt.args.nodes); (err != nil) != tt.wantErr {
1308+
t.Errorf("nodesInitialized() error = %v, wantErr %v", err, tt.wantErr)
1309+
}
1310+
})
1311+
}
1312+
}

0 commit comments

Comments
 (0)