Skip to content

Commit 53bfc70

Browse files
p0lyn0mialbertinatto
authored andcommitted
UPSTREAM: <carry>: allows for switching KS to talk to Kube API over localhost
to force KS to use localhost set the following flag in kubescheduler (oc edit kubescheduler cluster) unsupportedConfigOverrides: arguments: unsupported-kube-api-over-localhost:: - "true" UPSTREAM: <carry>: allows for switching KS to talk to Kube API over localhost-squash to other This commit is addendum to 04eabe5 to stop using cc and start relying on scheduler config options OpenShift-Rebase-Source: aa9dde2 UPSTREAM: <carry>: allows for switching KS to talk to Kube API over localhost
1 parent f5e2c17 commit 53bfc70

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

cmd/kube-scheduler/app/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ type Config struct {
5757
// value, the pod will be moved from unschedulablePods to backoffQ or activeQ.
5858
// If this value is empty, the default value (5min) will be used.
5959
PodMaxInUnschedulablePodsDuration time.Duration
60+
61+
// OpenShiftContext is additional context that we need to launch the kube-scheduler for openshift
62+
OpenShiftContext OpenShiftContext
6063
}
6164

6265
type completedConfig struct {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package config
2+
3+
import (
4+
"k8s.io/client-go/transport"
5+
6+
"github.com/openshift/library-go/pkg/monitor/health"
7+
)
8+
9+
// OpenShiftContext is additional context that we need to launch the kube-scheduler for openshift.
10+
// Basically, this holds our additional config information.
11+
type OpenShiftContext struct {
12+
UnsupportedKubeAPIOverPreferredHost bool
13+
PreferredHostRoundTripperWrapperFn transport.WrapperFunc
14+
PreferredHostHealthMonitor *health.Prober
15+
}

cmd/kube-scheduler/app/options/options.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import (
4949
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
5050
"k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
5151
netutils "k8s.io/utils/net"
52+
53+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
5254
)
5355

5456
// Options has all the params needed to run a Scheduler
@@ -74,6 +76,9 @@ type Options struct {
7476

7577
// Flags hold the parsed CLI flags.
7678
Flags *cliflag.NamedFlagSets
79+
80+
// OpenShiftContext is additional context that we need to launch the kube-scheduler for openshift.
81+
OpenShiftContext schedulerappconfig.OpenShiftContext
7782
}
7883

7984
// NewOptions returns default scheduler app options.
@@ -183,6 +188,7 @@ func (o *Options) initFlags() {
183188
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file.")
184189
fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the configuration values to this file and exit.")
185190
fs.StringVar(&o.Master, "master", o.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
191+
fs.BoolVar(&o.OpenShiftContext.UnsupportedKubeAPIOverPreferredHost, "unsupported-kube-api-over-localhost", false, "when set makes KS prefer talking to localhost kube-apiserver (when available) instead of an LB")
186192

187193
o.SecureServing.AddFlags(nfs.FlagSet("secure serving"))
188194
o.Authentication.AddFlags(nfs.FlagSet("authentication"))
@@ -225,6 +231,10 @@ func (o *Options) ApplyTo(logger klog.Logger, c *schedulerappconfig.Config) erro
225231
if err != nil {
226232
return err
227233
}
234+
if c.OpenShiftContext.PreferredHostRoundTripperWrapperFn != nil {
235+
libgorestclient.DefaultServerName(kubeConfig)
236+
kubeConfig.Wrap(c.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
237+
}
228238
c.KubeConfig = kubeConfig
229239

230240
if err := o.SecureServing.ApplyTo(&c.SecureServing, &c.LoopbackClientConfig); err != nil {
@@ -273,6 +283,7 @@ func (o *Options) Config(ctx context.Context) (*schedulerappconfig.Config, error
273283
}
274284

275285
c := &schedulerappconfig.Config{}
286+
c.OpenShiftContext = o.OpenShiftContext
276287
if err := o.ApplyTo(logger, c); err != nil {
277288
return nil, err
278289
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package options
2+
3+
import (
4+
"k8s.io/klog/v2"
5+
6+
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
7+
)
8+
9+
func LoadKubeSchedulerConfiguration(logger klog.Logger, file string) (*kubeschedulerconfig.KubeSchedulerConfiguration, error) {
10+
return LoadConfigFromFile(logger, file)
11+
}

cmd/kube-scheduler/app/patch.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package app
2+
3+
import (
4+
"time"
5+
6+
"k8s.io/klog/v2"
7+
8+
"k8s.io/client-go/rest"
9+
"k8s.io/client-go/tools/clientcmd"
10+
"k8s.io/component-base/metrics/legacyregistry"
11+
"k8s.io/kubernetes/cmd/kube-scheduler/app/options"
12+
13+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
14+
"github.com/openshift/library-go/pkg/monitor/health"
15+
)
16+
17+
func setUpPreferredHostForOpenShift(logger klog.Logger, kubeSchedulerOptions *options.Options) error {
18+
if !kubeSchedulerOptions.OpenShiftContext.UnsupportedKubeAPIOverPreferredHost {
19+
return nil
20+
}
21+
22+
master := kubeSchedulerOptions.Master
23+
var kubeConfig string
24+
25+
// We cannot load component config anymore as the options are not being initialized.
26+
// if there was no kubeconfig specified we won't be able to get cluster info.
27+
// in that case try to load the configuration and read kubeconfig directly from it if it was provided.
28+
if len(kubeSchedulerOptions.ConfigFile) > 0 {
29+
cfg, err := options.LoadKubeSchedulerConfiguration(logger, kubeSchedulerOptions.ConfigFile)
30+
if err != nil {
31+
return err
32+
}
33+
kubeConfig = cfg.ClientConnection.Kubeconfig
34+
}
35+
36+
config, err := clientcmd.BuildConfigFromFlags(master, kubeConfig)
37+
if err != nil {
38+
return err
39+
}
40+
libgorestclient.DefaultServerName(config)
41+
42+
targetProvider := health.StaticTargetProvider{"localhost:6443"}
43+
kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor, err = health.New(targetProvider, createRestConfigForHealthMonitor(config))
44+
if err != nil {
45+
return err
46+
}
47+
kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor.
48+
WithHealthyProbesThreshold(3).
49+
WithUnHealthyProbesThreshold(5).
50+
WithProbeInterval(5 * time.Second).
51+
WithProbeResponseTimeout(2 * time.Second).
52+
WithMetrics(health.Register(legacyregistry.MustRegister))
53+
54+
kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn = libgorestclient.NewPreferredHostRoundTripper(func() string {
55+
healthyTargets, _ := kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor.Targets()
56+
if len(healthyTargets) == 1 {
57+
return healthyTargets[0]
58+
}
59+
return ""
60+
})
61+
62+
kubeSchedulerOptions.Authentication.WithCustomRoundTripper(kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
63+
kubeSchedulerOptions.Authorization.WithCustomRoundTripper(kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
64+
return nil
65+
}
66+
67+
func createRestConfigForHealthMonitor(restConfig *rest.Config) *rest.Config {
68+
restConfigCopy := *restConfig
69+
rest.AddUserAgent(&restConfigCopy, "kube-scheduler-health-monitor")
70+
71+
return &restConfigCopy
72+
}

cmd/kube-scheduler/app/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Op
137137
cancel()
138138
}()
139139

140+
logger := klog.FromContext(ctx)
141+
if err := setUpPreferredHostForOpenShift(logger, opts); err != nil {
142+
return err
143+
}
144+
140145
cc, sched, err := Setup(ctx, opts, registryOptions...)
141146
if err != nil {
142147
return err
@@ -155,6 +160,11 @@ func Run(ctx context.Context, cc *schedulerserverconfig.CompletedConfig, sched *
155160

156161
logger.Info("Golang settings", "GOGC", os.Getenv("GOGC"), "GOMAXPROCS", os.Getenv("GOMAXPROCS"), "GOTRACEBACK", os.Getenv("GOTRACEBACK"))
157162

163+
// start the localhost health monitor early so that it can be used by the LE client
164+
if cc.OpenShiftContext.PreferredHostHealthMonitor != nil {
165+
go cc.OpenShiftContext.PreferredHostHealthMonitor.Run(ctx)
166+
}
167+
158168
// Configz registration.
159169
if cz, err := configz.New("componentconfig"); err == nil {
160170
cz.Set(cc.ComponentConfig)

0 commit comments

Comments
 (0)