Skip to content

Commit 2bfbd4c

Browse files
committed
feat: add liveness and readiness probe support
Signed-off-by: Armando Ruocco <[email protected]>
1 parent 74d4f5d commit 2bfbd4c

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

cmd/manager/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/cloudnative-pg/machinery/pkg/log"
88
"github.com/spf13/cobra"
99

10+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cmd/healthcheck"
1011
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cmd/instance"
1112
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cmd/operator"
1213
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cmd/restore"
@@ -29,6 +30,7 @@ func main() {
2930
rootCmd.AddCommand(instance.NewCmd())
3031
rootCmd.AddCommand(operator.NewCmd())
3132
rootCmd.AddCommand(restore.NewCmd())
33+
rootCmd.AddCommand(healthcheck.NewCmd())
3234

3335
if err := rootCmd.Execute(); err != nil {
3436
fmt.Println(err)

internal/cmd/healthcheck/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package healthcheck contains the logic to execute an healthcheck on the plugin through a command
2+
package healthcheck

internal/cmd/healthcheck/main.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package healthcheck
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
8+
"github.com/cloudnative-pg/machinery/pkg/log"
9+
"github.com/spf13/cobra"
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/credentials/insecure"
12+
"google.golang.org/grpc/health/grpc_health_v1"
13+
14+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
15+
)
16+
17+
// NewCmd returns the healthcheck command
18+
func NewCmd() *cobra.Command {
19+
cmd := &cobra.Command{
20+
Use: "healthcheck",
21+
Short: "healthcheck commands",
22+
}
23+
24+
cmd.AddCommand(unixHealthCheck())
25+
26+
return cmd
27+
}
28+
29+
func unixHealthCheck() *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: "unix",
32+
Short: "unix healthcheck",
33+
RunE: func(cmd *cobra.Command, _ []string) error {
34+
dialPath := fmt.Sprintf("unix://%s", path.Join("/plugins", metadata.PluginName))
35+
cli, cliErr := grpc.NewClient(dialPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
36+
if cliErr != nil {
37+
log.Error(cliErr, "error while building client")
38+
return cliErr
39+
}
40+
41+
healthCli := grpc_health_v1.NewHealthClient(cli)
42+
res, healthErr := healthCli.Check(
43+
cmd.Context(),
44+
&grpc_health_v1.HealthCheckRequest{},
45+
)
46+
if healthErr != nil {
47+
log.Error(healthErr, "healthcheck call failed")
48+
return healthErr
49+
}
50+
51+
log.Info("received status: %s", res.Status.String())
52+
switch res.Status {
53+
case grpc_health_v1.HealthCheckResponse_SERVING:
54+
os.Exit(0)
55+
case grpc_health_v1.HealthCheckResponse_UNKNOWN:
56+
os.Exit(1)
57+
case grpc_health_v1.HealthCheckResponse_NOT_SERVING:
58+
os.Exit(2)
59+
default:
60+
os.Exit(125)
61+
}
62+
63+
return nil
64+
},
65+
}
66+
67+
return cmd
68+
}

internal/cnpgi/common/health.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package common
2+
3+
import (
4+
"context"
5+
6+
"github.com/cloudnative-pg/machinery/pkg/log"
7+
"google.golang.org/grpc"
8+
"google.golang.org/grpc/health/grpc_health_v1"
9+
)
10+
11+
// AddHealthCheck adds a health check service to the gRPC server with the tag 'plugin-barman-cloud'
12+
func AddHealthCheck(server *grpc.Server) {
13+
grpc_health_v1.RegisterHealthServer(server, &healthServer{}) // replaces default registration
14+
}
15+
16+
type healthServer struct {
17+
grpc_health_v1.UnimplementedHealthServer
18+
}
19+
20+
// Check is the response handle for the healthcheck request
21+
func (h healthServer) Check(
22+
ctx context.Context,
23+
_ *grpc_health_v1.HealthCheckRequest,
24+
) (*grpc_health_v1.HealthCheckResponse, error) {
25+
contextLogger := log.FromContext(ctx)
26+
contextLogger.Trace("serving health check response")
27+
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
28+
}

internal/cnpgi/instance/start.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/cloudnative-pg/cnpg-i/pkg/wal"
99
"google.golang.org/grpc"
1010
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/common"
1113
)
1214

1315
// CNPGI is the implementation of the PostgreSQL sidecar
@@ -41,6 +43,7 @@ func (c *CNPGI) Start(ctx context.Context) error {
4143
ClusterObjectKey: c.ClusterObjectKey,
4244
InstanceName: c.InstanceName,
4345
})
46+
common.AddHealthCheck(server)
4447
return nil
4548
}
4649

internal/cnpgi/operator/lifecycle.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,25 @@ func reconcilePodSpec(
220220
},
221221
}
222222

223+
baseProbe := &corev1.Probe{
224+
PeriodSeconds: 10,
225+
FailureThreshold: 3,
226+
ProbeHandler: corev1.ProbeHandler{
227+
Exec: &corev1.ExecAction{
228+
Command: []string{"manager", "healthcheck", "unix"},
229+
},
230+
},
231+
}
232+
233+
readinessProbe := baseProbe.DeepCopy()
234+
readinessProbe.InitialDelaySeconds = 10
235+
223236
// fixed values
224237
sidecarConfig.Name = "plugin-barman-cloud"
225238
sidecarConfig.Image = viper.GetString("sidecar-image")
226239
sidecarConfig.ImagePullPolicy = cluster.Spec.ImagePullPolicy
240+
sidecarConfig.ReadinessProbe = readinessProbe.DeepCopy()
241+
sidecarConfig.LivenessProbe = baseProbe.DeepCopy()
227242

228243
// merge the main container envs if they aren't already set
229244
for _, container := range spec.Containers {

internal/cnpgi/restore/start.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
restore "github.com/cloudnative-pg/cnpg-i/pkg/restore/job"
88
"google.golang.org/grpc"
99
"sigs.k8s.io/controller-runtime/pkg/client"
10+
11+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/common"
1012
)
1113

1214
// CNPGI is the implementation of the PostgreSQL sidecar
@@ -31,6 +33,9 @@ func (c *CNPGI) Start(ctx context.Context) error {
3133
PgDataPath: c.PGDataPath,
3234
PgWalFolderToSymlink: PgWalVolumePgWalPath,
3335
})
36+
37+
common.AddHealthCheck(server)
38+
3439
return nil
3540
}
3641

0 commit comments

Comments
 (0)