Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1548 #1814

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/peer"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
k8sLabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -162,6 +162,64 @@ type nfdMaster struct {
config *NFDConfig
}

// customHealthServer implements grpc_health_v1.HealthServer
type customHealthServer struct {
grpc_health_v1.UnimplementedHealthServer
k8sclient k8sclient.Interface
}

func (s *customHealthServer) SetK8sClient(client k8sclient.Interface) {
s.k8sclient = client
}

// Check method for customHealthServer
func (s *customHealthServer) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
klog.InfoS("Check request received")
metricServerStatus, err := s.CheckPods("kube-system", "k8s-app=metrics-server")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry for the ambiguity. I wasn't thinking about kubernetes metrics-server but the metrics http server inside nfd-master itself that is started with utils.CreateMetricsServer(). We could e.g. just set a health flag to false if the goroutine started there exits.

if err != nil {
klog.ErrorS(err, "Error getting metricserver pods")
}
klog.InfoS("Metric serverstatus", "status", metricServerStatus)
if !metricServerStatus {
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}, nil
}


return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
}

// Watch method for customHealthServer
func (s *customHealthServer) Watch(req *grpc_health_v1.HealthCheckRequest, srv grpc_health_v1.Health_WatchServer) error {
klog.InfoS("Watch request received")
return nil
}

func (s *customHealthServer) CheckPods(namespace string, labelSelector string) (bool, error) {
pods, err := getPods(s.k8sclient, namespace, labelSelector)
if err != nil {
return false, err
}
// TODO: print if items is empty

status := false
for _, pod := range pods.Items {
klog.InfoS("Found pod name", "name", pod.Name)
if pod.Status.Phase != "Running" {
klog.InfoS("Pod is not in running state", "name", pod.Name)
} else {
klog.InfoS("Pod is in running state", "name", pod.Name)
status = true
// can stop here : return status, nil
// not stopping for logs.
}
}
return status, nil
}

func (s *customHealthServer) CheckNfdApiController() (bool, error) {

}

// NewNfdMaster creates a new NfdMaster server instance.
func NewNfdMaster(opts ...NfdMasterOption) (NfdMaster, error) {
nfd := &nfdMaster{
Expand Down Expand Up @@ -412,7 +470,11 @@ func (m *nfdMaster) startGrpcHealthServer(errChan chan<- error) error {
}

s := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(s, health.NewServer())
klog.InfoS("Creating health server")
healthCheckServer := &customHealthServer{}
grpc_health_v1.RegisterHealthServer(s, healthCheckServer)
klog.InfoS("Setting the client to the healthCheckServer")
healthCheckServer.SetK8sClient(m.k8sClient)
klog.InfoS("gRPC health server serving", "port", m.args.GrpcHealthPort)

go func() {
Expand Down Expand Up @@ -1602,3 +1664,13 @@ func patchNode(cli k8sclient.Interface, nodeName string, patches []utils.JsonPat
func patchNodeStatus(cli k8sclient.Interface, nodeName string, patches []utils.JsonPatch) error {
return patchNode(cli, nodeName, patches, "status")
}

func getPod(cli k8sclient.Interface, namespace string, podName string) (*corev1.Pod, error) {
return cli.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
}

func getPods(cli k8sclient.Interface, namespace string, labelSelector string) (*corev1.PodList, error) {
return cli.CoreV1().Pods(namespace).List(context.TODO(), v1.ListOptions{
LabelSelector: labelSelector,
})
}