Skip to content

Commit ef0a970

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

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

cmd/liqo-controller-manager/main.go

Lines changed: 9 additions & 0 deletions
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("connected to the IPAM server (status: %s)", conn.GetState())
266+
259267
defer conn.Close()
268+
260269
ipamClient = ipam.NewIPAMClient(conn)
261270
}
262271

pkg/utils/grpc/connection.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"time"
21+
22+
"google.golang.org/grpc"
23+
"google.golang.org/grpc/connectivity"
24+
)
25+
26+
// WaitForConnectionReady tries to connect with a gRPC server until it is Ready.
27+
// It stops waiting if the context expires or the connection is not Ready after the timeout.
28+
func WaitForConnectionReady(ctx context.Context, conn *grpc.ClientConn, timeout time.Duration) error {
29+
ctx, cancel := context.WithTimeout(ctx, timeout)
30+
defer cancel()
31+
32+
for {
33+
state := conn.GetState()
34+
switch state {
35+
case connectivity.Idle:
36+
conn.Connect()
37+
case connectivity.Ready:
38+
return nil
39+
default:
40+
}
41+
42+
changed := conn.WaitForStateChange(ctx, state)
43+
if !changed {
44+
return fmt.Errorf("timeout connection to the gRPC server")
45+
}
46+
}
47+
}

pkg/utils/grpc/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

0 commit comments

Comments
 (0)