From 22a2c2171ebbfdbd65caa7d7c7b20492898190fe Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Fri, 23 Jun 2023 10:49:22 +0200 Subject: [PATCH] address comments --- .../controllers/inmemorymachine_controller.go | 13 +++++++++++-- .../controllers/inmemorymachine_controller_test.go | 6 +++--- .../inmemory/internal/server/etcd/handler.go | 13 ++++++++++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go b/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go index d827a1052433..8546ab910fef 100644 --- a/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go +++ b/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller.go @@ -447,7 +447,12 @@ func (r *InMemoryMachineReconciler) reconcileNormalETCD(ctx context.Context, clu // If this is the first etcd member in the cluster, assign a cluster ID if info.clusterID == "" { - info.clusterID = fmt.Sprintf("%d", rand.Uint32()) //nolint:gosec // weak random number generator is good enough here + for { + info.clusterID = fmt.Sprintf("%d", rand.Uint32()) //nolint:gosec // weak random number generator is good enough here + if info.clusterID != "0" { + break + } + } } // Computes a unique memberID. @@ -539,7 +544,11 @@ func (r *InMemoryMachineReconciler) inspectEtcd(ctx context.Context, cloudClient } var leaderFrom time.Time for _, pod := range etcdPods.Items { - info.clusterID = pod.Annotations[cloudv1.EtcdClusterIDAnnotationName] + if info.clusterID == "" { + info.clusterID = pod.Annotations[cloudv1.EtcdClusterIDAnnotationName] + } else if pod.Annotations[cloudv1.EtcdClusterIDAnnotationName] != info.clusterID { + return etcdInfo{}, errors.New("invalid etcd cluster, members have different cluster ID") + } memberID := pod.Annotations[cloudv1.EtcdMemberIDAnnotationName] info.members.Insert(memberID) diff --git a/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller_test.go b/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller_test.go index ce23c4ebeb01..803e2edd4340 100644 --- a/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller_test.go +++ b/test/infrastructure/inmemory/internal/controllers/inmemorymachine_controller_test.go @@ -395,9 +395,9 @@ func TestReconcileNormalEtcd(t *testing.T) { host := "127.0.0.1" wcmux, err := server.NewWorkloadClustersMux(manager, host, server.CustomPorts{ // NOTE: make sure to use ports different than other tests, so we can run tests in parallel - MinPort: server.DefaultMinPort + 1000, - MaxPort: server.DefaultMinPort + 1099, - DebugPort: server.DefaultDebugPort + 10, + MinPort: server.DefaultMinPort + 1200, + MaxPort: server.DefaultMinPort + 1299, + DebugPort: server.DefaultDebugPort + 20, }) g.Expect(err).ToNot(HaveOccurred()) _, err = wcmux.InitWorkloadClusterListener(klog.KObj(cluster).String()) diff --git a/test/infrastructure/inmemory/internal/server/etcd/handler.go b/test/infrastructure/inmemory/internal/server/etcd/handler.go index 6b5e142b9159..d697c20210c6 100644 --- a/test/infrastructure/inmemory/internal/server/etcd/handler.go +++ b/test/infrastructure/inmemory/internal/server/etcd/handler.go @@ -193,13 +193,20 @@ func (b *baseServer) inspectEtcd(ctx context.Context, cloudClient cclient.Client memberList := &pb.MemberListResponse{} statusResponse := &pb.StatusResponse{} + var clusterID int var leaderID int var leaderFrom time.Time for _, pod := range etcdPods.Items { - clusterID, err := strconv.Atoi(pod.Annotations[cloudv1.EtcdClusterIDAnnotationName]) - if err != nil { - return nil, nil, errors.Wrapf(err, "failed read cluster ID annotation from etcd member with name %s", pod.Name) + if clusterID == 0 { + var err error + clusterID, err = strconv.Atoi(pod.Annotations[cloudv1.EtcdClusterIDAnnotationName]) + if err != nil { + return nil, nil, errors.Wrapf(err, "failed read cluster ID annotation from etcd member with name %s", pod.Name) + } + } else if pod.Annotations[cloudv1.EtcdClusterIDAnnotationName] != fmt.Sprintf("%d", clusterID) { + return nil, nil, errors.New("invalid etcd cluster, members have different cluster ID") } + memberID, err := strconv.Atoi(pod.Annotations[cloudv1.EtcdMemberIDAnnotationName]) if err != nil { return nil, nil, errors.Wrapf(err, "failed read member ID annotation from etcd member with name %s", pod.Name)