generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 178
Adds Health gRPC Server and Refactors Main() #148
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/codes" | ||
healthPb "google.golang.org/grpc/health/grpc_health_v1" | ||
"google.golang.org/grpc/status" | ||
"inference.networking.x-k8s.io/gateway-api-inference-extension/pkg/ext-proc/backend" | ||
klog "k8s.io/klog/v2" | ||
) | ||
|
||
type healthServer struct { | ||
datastore *backend.K8sDatastore | ||
} | ||
|
||
func (s *healthServer) Check(ctx context.Context, in *healthPb.HealthCheckRequest) (*healthPb.HealthCheckResponse, error) { | ||
if !s.datastore.HasSynced() { | ||
klog.Infof("gRPC health check not serving: %s", in.String()) | ||
return &healthPb.HealthCheckResponse{Status: healthPb.HealthCheckResponse_NOT_SERVING}, nil | ||
} | ||
klog.Infof("gRPC health check serving: %s", in.String()) | ||
return &healthPb.HealthCheckResponse{Status: healthPb.HealthCheckResponse_SERVING}, nil | ||
} | ||
|
||
func (s *healthServer) Watch(in *healthPb.HealthCheckRequest, srv healthPb.Health_WatchServer) error { | ||
return status.Error(codes.Unimplemented, "Watch is not implemented") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
healthPb "google.golang.org/grpc/health/grpc_health_v1" | ||
"google.golang.org/grpc/status" | ||
"inference.networking.x-k8s.io/gateway-api-inference-extension/api/v1alpha1" | ||
"inference.networking.x-k8s.io/gateway-api-inference-extension/pkg/ext-proc/backend" | ||
"inference.networking.x-k8s.io/gateway-api-inference-extension/pkg/ext-proc/backend/vllm" | ||
|
@@ -29,10 +23,14 @@ import ( | |
) | ||
|
||
var ( | ||
port = flag.Int( | ||
"port", | ||
grpcPort = flag.Int( | ||
"grpcPort", | ||
9002, | ||
"gRPC port") | ||
"The gRPC port used for communicating with Envoy proxy") | ||
grpcHealthPort = flag.Int( | ||
"grpcHealthPort", | ||
9003, | ||
"The port used for gRPC liveness and readiness probes") | ||
targetPodHeader = flag.String( | ||
"targetPodHeader", | ||
"target-pod", | ||
|
@@ -65,55 +63,39 @@ var ( | |
scheme = runtime.NewScheme() | ||
) | ||
|
||
type healthServer struct{} | ||
|
||
func (s *healthServer) Check( | ||
ctx context.Context, | ||
in *healthPb.HealthCheckRequest, | ||
) (*healthPb.HealthCheckResponse, error) { | ||
klog.Infof("Handling grpc Check request + %s", in.String()) | ||
return &healthPb.HealthCheckResponse{Status: healthPb.HealthCheckResponse_SERVING}, nil | ||
} | ||
|
||
func (s *healthServer) Watch(in *healthPb.HealthCheckRequest, srv healthPb.Health_WatchServer) error { | ||
return status.Error(codes.Unimplemented, "Watch is not implemented") | ||
} | ||
|
||
func init() { | ||
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) | ||
utilruntime.Must(v1alpha1.AddToScheme(scheme)) | ||
} | ||
|
||
func main() { | ||
|
||
klog.InitFlags(nil) | ||
flag.Parse() | ||
|
||
ctrl.SetLogger(klog.TODO()) | ||
|
||
// Validate flags | ||
if err := validateFlags(); err != nil { | ||
klog.Fatalf("Failed to validate flags: %v", err) | ||
} | ||
|
||
// Print all flag values | ||
flags := "Flags: " | ||
flag.VisitAll(func(f *flag.Flag) { | ||
flags += fmt.Sprintf("%s=%v; ", f.Name, f.Value) | ||
}) | ||
klog.Info(flags) | ||
|
||
klog.Infof("Listening on %q", fmt.Sprintf(":%d", *port)) | ||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) | ||
// Create a new manager to manage controllers | ||
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{Scheme: scheme}) | ||
if err != nil { | ||
klog.Fatalf("failed to listen: %v", err) | ||
klog.Fatalf("Failed to create controller manager: %v", err) | ||
} | ||
|
||
// Create the data store used to cache watched resources | ||
datastore := backend.NewK8sDataStore() | ||
|
||
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ | ||
Scheme: scheme, | ||
}) | ||
if err != nil { | ||
klog.Error(err, "unable to start manager") | ||
os.Exit(1) | ||
} | ||
|
||
// Create the controllers and register them with the manager | ||
if err := (&backend.InferencePoolReconciler{ | ||
Datastore: datastore, | ||
Scheme: mgr.GetScheme(), | ||
|
@@ -124,7 +106,7 @@ func main() { | |
}, | ||
Record: mgr.GetEventRecorderFor("InferencePool"), | ||
}).SetupWithManager(mgr); err != nil { | ||
klog.Error(err, "Error setting up InferencePoolReconciler") | ||
klog.Fatalf("Failed setting up InferencePoolReconciler: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note the switch to |
||
} | ||
|
||
if err := (&backend.InferenceModelReconciler{ | ||
|
@@ -137,7 +119,7 @@ func main() { | |
}, | ||
Record: mgr.GetEventRecorderFor("InferenceModel"), | ||
}).SetupWithManager(mgr); err != nil { | ||
klog.Error(err, "Error setting up InferenceModelReconciler") | ||
klog.Fatalf("Failed setting up InferenceModelReconciler: %v", err) | ||
} | ||
|
||
if err := (&backend.EndpointSliceReconciler{ | ||
|
@@ -148,53 +130,105 @@ func main() { | |
ServiceName: *serviceName, | ||
Zone: *zone, | ||
}).SetupWithManager(mgr); err != nil { | ||
klog.Error(err, "Error setting up EndpointSliceReconciler") | ||
klog.Fatalf("Failed setting up EndpointSliceReconciler: %v", err) | ||
} | ||
|
||
// Start health and ext-proc servers in goroutines | ||
healthSvr := startHealthServer(datastore, *grpcHealthPort) | ||
extProcSvr := startExternalProcessorServer( | ||
datastore, | ||
*grpcPort, | ||
*refreshPodsInterval, | ||
*refreshMetricsInterval, | ||
*targetPodHeader, | ||
) | ||
|
||
// Start the controller manager. Blocking and will return when shutdown is complete. | ||
klog.Infof("Starting controller manager") | ||
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { | ||
klog.Fatalf("Error starting controller manager: %v", err) | ||
} | ||
klog.Info("Controller manager shutting down") | ||
|
||
// Gracefully shutdown servers | ||
if healthSvr != nil { | ||
klog.Info("Health server shutting down") | ||
healthSvr.GracefulStop() | ||
} | ||
if extProcSvr != nil { | ||
klog.Info("Ext-proc server shutting down") | ||
extProcSvr.GracefulStop() | ||
} | ||
|
||
klog.Info("All components shutdown") | ||
} | ||
|
||
// startHealthServer starts the gRPC health probe server in a goroutine. | ||
func startHealthServer(ds *backend.K8sDatastore, port int) *grpc.Server { | ||
svr := grpc.NewServer() | ||
healthPb.RegisterHealthServer(svr, &healthServer{datastore: ds}) | ||
|
||
errChan := make(chan error) | ||
go func() { | ||
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { | ||
klog.Error(err, "Error running manager") | ||
errChan <- err | ||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | ||
if err != nil { | ||
klog.Fatalf("Health server failed to listen: %v", err) | ||
} | ||
klog.Infof("Health server listening on port: %d", port) | ||
|
||
// Blocking and will return when shutdown is complete. | ||
if err := svr.Serve(lis); err != nil && err != grpc.ErrServerStopped { | ||
klog.Fatalf("Health server failed: %v", err) | ||
} | ||
klog.Info("Health server shutting down") | ||
}() | ||
return svr | ||
} | ||
|
||
s := grpc.NewServer() | ||
// startExternalProcessorServer starts the Envoy external processor server in a goroutine. | ||
func startExternalProcessorServer( | ||
datastore *backend.K8sDatastore, | ||
port int, | ||
refreshPodsInterval, refreshMetricsInterval time.Duration, | ||
targetPodHeader string, | ||
) *grpc.Server { | ||
svr := grpc.NewServer() | ||
|
||
pp := backend.NewProvider(&vllm.PodMetricsClientImpl{}, datastore) | ||
if err := pp.Init(*refreshPodsInterval, *refreshMetricsInterval); err != nil { | ||
klog.Fatalf("failed to initialize: %v", err) | ||
} | ||
extProcPb.RegisterExternalProcessorServer( | ||
s, | ||
handlers.NewServer( | ||
pp, | ||
scheduling.NewScheduler(pp), | ||
*targetPodHeader, | ||
datastore)) | ||
healthPb.RegisterHealthServer(s, &healthServer{}) | ||
|
||
klog.Infof("Starting gRPC server on port :%v", *port) | ||
|
||
// shutdown | ||
var gracefulStop = make(chan os.Signal, 1) | ||
signal.Notify(gracefulStop, syscall.SIGTERM) | ||
signal.Notify(gracefulStop, syscall.SIGINT) | ||
go func() { | ||
select { | ||
case sig := <-gracefulStop: | ||
klog.Infof("caught sig: %+v", sig) | ||
os.Exit(0) | ||
case err := <-errChan: | ||
klog.Infof("caught error in controller: %+v", err) | ||
os.Exit(0) | ||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | ||
if err != nil { | ||
klog.Fatalf("Ext-proc server failed to listen: %v", err) | ||
} | ||
klog.Infof("Ext-proc server listening on port: %d", port) | ||
|
||
// Initialize backend provider | ||
pp := backend.NewProvider(&vllm.PodMetricsClientImpl{}, datastore) | ||
if err := pp.Init(refreshPodsInterval, refreshMetricsInterval); err != nil { | ||
klog.Fatalf("Failed to initialize backend provider: %v", err) | ||
} | ||
|
||
// Register ext_proc handlers | ||
extProcPb.RegisterExternalProcessorServer( | ||
svr, | ||
handlers.NewServer(pp, scheduling.NewScheduler(pp), targetPodHeader, datastore), | ||
) | ||
|
||
// Blocking and will return when shutdown is complete. | ||
if err := svr.Serve(lis); err != nil && err != grpc.ErrServerStopped { | ||
klog.Fatalf("Ext-proc server failed: %v", err) | ||
} | ||
klog.Info("Ext-proc server shutting down") | ||
}() | ||
return svr | ||
} | ||
|
||
err = s.Serve(lis) | ||
if err != nil { | ||
klog.Fatalf("Ext-proc failed with the err: %v", err) | ||
func validateFlags() error { | ||
if *poolName == "" { | ||
return fmt.Errorf("required %q flag not set", "poolName") | ||
} | ||
|
||
if *serviceName == "" { | ||
return fmt.Errorf("required %q flag not set", "serviceName") | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.