forked from kubernetes-sigs/dra-driver-nvidia-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth.go
More file actions
144 lines (125 loc) · 4.13 KB
/
health.go
File metadata and controls
144 lines (125 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2025 The Kubernetes Authors.
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"net"
"net/url"
"path"
"strconv"
"sync"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
drapb "k8s.io/kubelet/pkg/apis/dra/v1beta1"
registerapi "k8s.io/kubelet/pkg/apis/pluginregistration/v1"
)
type healthcheck struct {
grpc_health_v1.UnimplementedHealthServer
server *grpc.Server
wg sync.WaitGroup
regClient registerapi.RegistrationClient
draClient drapb.DRAPluginClient
}
func startHealthcheck(ctx context.Context, config *Config) (*healthcheck, error) {
port := config.flags.healthcheckPort
if port < 0 {
return nil, nil
}
addr := net.JoinHostPort("", strconv.Itoa(port))
lis, err := net.Listen("tcp", addr)
if err != nil {
return nil, fmt.Errorf("failed to listen for healthcheck service at %s: %w", addr, err)
}
regSockPath := (&url.URL{
Scheme: "unix",
// TODO: this needs to adapt when seamless upgrades
// are enabled and the filename includes a uid.
Path: path.Join(config.flags.kubeletRegistrarDirectoryPath, DriverName+"-reg.sock"),
}).String()
klog.V(6).Infof("Connecting to registration socket path=%s", regSockPath)
regConn, err := grpc.NewClient(
regSockPath,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, fmt.Errorf("connect to registration socket: %w", err)
}
draSockPath := (&url.URL{
Scheme: "unix",
Path: path.Join(config.DriverPluginPath(), "dra.sock"),
}).String()
klog.V(6).Infof("Connecting to DRA socket path=%s", draSockPath)
draConn, err := grpc.NewClient(
draSockPath,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, fmt.Errorf("connect to DRA socket: %w", err)
}
server := grpc.NewServer()
healthcheck := &healthcheck{
server: server,
regClient: registerapi.NewRegistrationClient(regConn),
draClient: drapb.NewDRAPluginClient(draConn),
}
grpc_health_v1.RegisterHealthServer(server, healthcheck)
healthcheck.wg.Add(1)
go func() {
defer healthcheck.wg.Done()
klog.Infof("Starting healthcheck service at %s", lis.Addr().String())
if err := server.Serve(lis); err != nil {
klog.Errorf("Failed to serve healthcheck service on %s: %v", addr, err)
}
}()
return healthcheck, nil
}
func (h *healthcheck) Stop() {
if h.server != nil {
klog.Info("Stopping healthcheck service")
h.server.GracefulStop()
}
h.wg.Wait()
}
// Check implements [grpc_health_v1.HealthServer].
func (h *healthcheck) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
knownServices := map[string]struct{}{"": {}, "liveness": {}}
if _, known := knownServices[req.GetService()]; !known {
return nil, status.Error(codes.NotFound, "unknown service")
}
status := &grpc_health_v1.HealthCheckResponse{
Status: grpc_health_v1.HealthCheckResponse_NOT_SERVING,
}
info, err := h.regClient.GetInfo(ctx, ®isterapi.InfoRequest{})
if err != nil {
klog.ErrorS(err, "failed to call GetInfo")
return status, nil
}
klog.V(6).Infof("Successfully invoked GetInfo: %v", info)
_, err = h.draClient.NodePrepareResources(ctx, &drapb.NodePrepareResourcesRequest{})
if err != nil {
klog.ErrorS(err, "failed to call NodePrepareResources")
return status, nil
}
klog.V(6).Info("Successfully invoked NodePrepareResources")
status.Status = grpc_health_v1.HealthCheckResponse_SERVING
return status, nil
}