Plugins: Pass cancellable context during API server creation (#86545)

This commit is contained in:
Will Browne 2024-04-19 08:22:14 +02:00 committed by GitHub
parent aa825f5dee
commit 8a5c0cfdc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package apiserver
import (
"context"
"os"
"github.com/spf13/cobra"
@ -54,8 +55,14 @@ func newCommandStartExampleAPIServer(o *APIServerOptions, stopCh <-chan struct{}
// TODO: Fix so that TracingOptions.ApplyTo happens before or during loadAPIGroupBuilders.
tracer := newLateInitializedTracingService()
ctx, cancel := context.WithCancel(c.Context())
go func() {
<-stopCh
cancel()
}()
// Load each group from the args
if err := o.loadAPIGroupBuilders(tracer, apis); err != nil {
if err := o.loadAPIGroupBuilders(ctx, tracer, apis); err != nil {
return err
}

View File

@ -1,6 +1,7 @@
package apiserver
import (
"context"
"fmt"
"io"
"net"
@ -50,10 +51,10 @@ func newAPIServerOptions(out, errOut io.Writer) *APIServerOptions {
}
}
func (o *APIServerOptions) loadAPIGroupBuilders(tracer tracing.Tracer, apis []schema.GroupVersion) error {
func (o *APIServerOptions) loadAPIGroupBuilders(ctx context.Context, tracer tracing.Tracer, apis []schema.GroupVersion) error {
o.builders = []builder.APIGroupBuilder{}
for _, gv := range apis {
api, err := o.factory.MakeAPIServer(tracer, gv)
api, err := o.factory.MakeAPIServer(ctx, tracer, gv)
if err != nil {
return err
}

View File

@ -35,7 +35,7 @@ type APIServerFactory interface {
GetEnabled(runtime []RuntimeConfig) ([]schema.GroupVersion, error)
// Make an API server for a given group+version
MakeAPIServer(tracer tracing.Tracer, gv schema.GroupVersion) (builder.APIGroupBuilder, error)
MakeAPIServer(ctx context.Context, tracer tracing.Tracer, gv schema.GroupVersion) (builder.APIGroupBuilder, error)
}
// Zero dependency provider for testing
@ -67,7 +67,7 @@ func (p *DummyAPIFactory) ApplyTo(config *genericapiserver.RecommendedConfig) er
return nil
}
func (p *DummyAPIFactory) MakeAPIServer(tracer tracing.Tracer, gv schema.GroupVersion) (builder.APIGroupBuilder, error) {
func (p *DummyAPIFactory) MakeAPIServer(_ context.Context, tracer tracing.Tracer, gv schema.GroupVersion) (builder.APIGroupBuilder, error) {
if gv.Version != "v0alpha1" {
return nil, fmt.Errorf("only alpha supported now")
}