Skip to content

Commit

Permalink
Merge pull request #1542 from ingvagabund/descheduler-server-apply
Browse files Browse the repository at this point in the history
DeschedulerServer: new Apply function for applying configuration
  • Loading branch information
k8s-ci-robot authored Nov 13, 2024
2 parents 1e48cfe + fb4b874 commit da52983
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
30 changes: 25 additions & 5 deletions cmd/descheduler/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import (

"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiserver "k8s.io/apiserver/pkg/server"
apiserveroptions "k8s.io/apiserver/pkg/server/options"
clientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
componentbaseconfig "k8s.io/component-base/config"
componentbaseoptions "k8s.io/component-base/config/options"
"k8s.io/klog/v2"

"sigs.k8s.io/descheduler/pkg/apis/componentconfig"
"sigs.k8s.io/descheduler/pkg/apis/componentconfig/v1alpha1"
deschedulerscheme "sigs.k8s.io/descheduler/pkg/descheduler/scheme"
Expand All @@ -40,11 +44,12 @@ const (
type DeschedulerServer struct {
componentconfig.DeschedulerConfiguration

Client clientset.Interface
EventClient clientset.Interface
SecureServing *apiserveroptions.SecureServingOptionsWithLoopback
DisableMetrics bool
EnableHTTP2 bool
Client clientset.Interface
EventClient clientset.Interface
SecureServing *apiserveroptions.SecureServingOptionsWithLoopback
SecureServingInfo *apiserver.SecureServingInfo
DisableMetrics bool
EnableHTTP2 bool
}

// NewDeschedulerServer creates a new DeschedulerServer with default parameters
Expand Down Expand Up @@ -107,3 +112,18 @@ func (rs *DeschedulerServer) AddFlags(fs *pflag.FlagSet) {

rs.SecureServing.AddFlags(fs)
}

func (rs *DeschedulerServer) Apply() error {
// loopbackClientConfig is a config for a privileged loopback connection
var loopbackClientConfig *restclient.Config
var secureServing *apiserver.SecureServingInfo
if err := rs.SecureServing.ApplyTo(&secureServing, &loopbackClientConfig); err != nil {
klog.ErrorS(err, "failed to apply secure server configuration")
return err
}

secureServing.DisableHTTP2 = !rs.EnableHTTP2
rs.SecureServingInfo = secureServing

return nil
}
64 changes: 31 additions & 33 deletions cmd/descheduler/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import (

"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/watch"
apiserver "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/pkg/server/mux"
restclient "k8s.io/client-go/rest"
"k8s.io/component-base/featuregate"
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
Expand Down Expand Up @@ -67,40 +65,16 @@ func NewDeschedulerCommand(out io.Writer) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// loopbackClientConfig is a config for a privileged loopback connection
var loopbackClientConfig *restclient.Config
var secureServing *apiserver.SecureServingInfo
if err := s.SecureServing.ApplyTo(&secureServing, &loopbackClientConfig); err != nil {
klog.ErrorS(err, "failed to apply secure server configuration")
if err = s.Apply(); err != nil {
klog.ErrorS(err, "failed to apply")
return err
}

secureServing.DisableHTTP2 = !s.EnableHTTP2

ctx, done := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)

pathRecorderMux := mux.NewPathRecorderMux("descheduler")
if !s.DisableMetrics {
pathRecorderMux.Handle("/metrics", legacyregistry.HandlerWithReset())
}

healthz.InstallHandler(pathRecorderMux, healthz.NamedCheck("Descheduler", healthz.PingHealthz.Check))

stoppedCh, _, err := secureServing.Serve(pathRecorderMux, 0, ctx.Done())
if err != nil {
klog.Fatalf("failed to start secure server: %v", err)
if err = Run(cmd.Context(), s); err != nil {
klog.ErrorS(err, "failed to run descheduler server")
return err
}

if err = Run(ctx, s); err != nil {
klog.ErrorS(err, "descheduler server")
return err
}

done()
// wait for metrics server to close
<-stoppedCh

return nil
},
}
Expand All @@ -114,8 +88,23 @@ func NewDeschedulerCommand(out io.Writer) *cobra.Command {
return cmd
}

func Run(ctx context.Context, rs *options.DeschedulerServer) error {
err := tracing.NewTracerProvider(ctx, rs.Tracing.CollectorEndpoint, rs.Tracing.TransportCert, rs.Tracing.ServiceName, rs.Tracing.ServiceNamespace, rs.Tracing.SampleRate, rs.Tracing.FallbackToNoOpProviderOnError)
func Run(rootCtx context.Context, rs *options.DeschedulerServer) error {
ctx, done := signal.NotifyContext(rootCtx, syscall.SIGINT, syscall.SIGTERM)

pathRecorderMux := mux.NewPathRecorderMux("descheduler")
if !rs.DisableMetrics {
pathRecorderMux.Handle("/metrics", legacyregistry.HandlerWithReset())
}

healthz.InstallHandler(pathRecorderMux, healthz.NamedCheck("Descheduler", healthz.PingHealthz.Check))

stoppedCh, _, err := rs.SecureServingInfo.Serve(pathRecorderMux, 0, ctx.Done())
if err != nil {
klog.Fatalf("failed to start secure server: %v", err)
return err
}

err = tracing.NewTracerProvider(ctx, rs.Tracing.CollectorEndpoint, rs.Tracing.TransportCert, rs.Tracing.ServiceName, rs.Tracing.ServiceNamespace, rs.Tracing.SampleRate, rs.Tracing.FallbackToNoOpProviderOnError)
if err != nil {
klog.ErrorS(err, "failed to create tracer provider")
}
Expand All @@ -124,5 +113,14 @@ func Run(ctx context.Context, rs *options.DeschedulerServer) error {
// increase the fake watch channel so the dry-run mode can be run
// over a cluster with thousands of pods
watch.DefaultChanSize = 100000
return descheduler.Run(ctx, rs)
err = descheduler.Run(ctx, rs)
if err != nil {
return err
}

done()
// wait for metrics server to close
<-stoppedCh

return nil
}

0 comments on commit da52983

Please sign in to comment.