Skip to content

Commit 29a6925

Browse files
committed
fix: wait for ipam grpc server to be Ready
1 parent c765c47 commit 29a6925

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

cmd/liqo-controller-manager/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import (
5858
dynamicutils "github.com/liqotech/liqo/pkg/utils/dynamic"
5959
liqoerrors "github.com/liqotech/liqo/pkg/utils/errors"
6060
flagsutils "github.com/liqotech/liqo/pkg/utils/flags"
61+
grpcutils "github.com/liqotech/liqo/pkg/utils/grpc"
6162
"github.com/liqotech/liqo/pkg/utils/indexer"
6263
ipamips "github.com/liqotech/liqo/pkg/utils/ipam/mapping"
6364
"github.com/liqotech/liqo/pkg/utils/mapper"
@@ -256,7 +257,15 @@ func main() {
256257
klog.Errorf("failed to establish a connection to the IPAM %q", *ipamServer)
257258
os.Exit(1)
258259
}
260+
261+
if err := grpcutils.WaitForConnectionReady(ctx, conn, 10*time.Second); err != nil {
262+
klog.Errorf("failed to establish a connection to the IPAM server %q", *ipamServer)
263+
os.Exit(1)
264+
}
265+
klog.Infof("IPAM connected: %s", conn.GetState())
266+
259267
defer conn.Close()
268+
260269
ipamClient = ipam.NewIPAMClient(conn)
261270
}
262271

@@ -278,7 +287,8 @@ func main() {
278287

279288
GenevePort: *genevePort,
280289
}); err != nil {
281-
klog.Fatalf("Unable to setup the networking module: %v", err)
290+
klog.Errorf("Unable to setup the networking module: %v", err)
291+
os.Exit(1)
282292
}
283293
}
284294

pkg/utils/grpc/connection.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package grpc

pkg/utils/grpc/doc.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package grpc provides a set of utilities to manage grpc connections.
16+
package grpc
17+
18+
import (
19+
"context"
20+
"fmt"
21+
"time"
22+
23+
"google.golang.org/grpc"
24+
"google.golang.org/grpc/connectivity"
25+
)
26+
27+
// WaitForConnectionReady waits for the connection to a gRPC server to be Ready.
28+
func WaitForConnectionReady(ctx context.Context, conn *grpc.ClientConn, timeout time.Duration) error {
29+
// Add a contxt with a timeout to avoid waiting indefinitely.
30+
ctx, cancel := context.WithTimeout(ctx, timeout)
31+
defer cancel()
32+
33+
// Attempt to connect to the gRPC server.
34+
conn.Connect()
35+
36+
for {
37+
state := conn.GetState()
38+
if state == connectivity.Ready {
39+
break
40+
}
41+
42+
changed := conn.WaitForStateChange(ctx, state)
43+
if !changed {
44+
return fmt.Errorf("timeout connection to the gRPC server")
45+
}
46+
}
47+
48+
return nil
49+
}

0 commit comments

Comments
 (0)