Skip to content

Commit 04eabe5

Browse files
p0lyn0mialdamemi
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"
1 parent 75bef3c commit 04eabe5

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type Config struct {
4949

5050
// LeaderElection is optional.
5151
LeaderElection *leaderelection.LeaderElectionConfig
52+
53+
// OpenShiftContext is additional context that we need to launch the kube-scheduler for openshift
54+
OpenShiftContext OpenShiftContext
5255
}
5356

5457
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import (
4646
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
4747
"k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
4848
netutils "k8s.io/utils/net"
49+
50+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
4951
)
5052

5153
// Options has all the params needed to run a Scheduler
@@ -71,6 +73,9 @@ type Options struct {
7173

7274
// Flags hold the parsed CLI flags.
7375
Flags *cliflag.NamedFlagSets
76+
77+
// OpenShiftContext is additional context that we need to launch the kube-scheduler for openshift.
78+
OpenShiftContext schedulerappconfig.OpenShiftContext
7479
}
7580

7681
// NewOptions returns default scheduler app options.
@@ -184,6 +189,7 @@ func (o *Options) initFlags() {
184189
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file.")
185190
fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the configuration values to this file and exit.")
186191
fs.StringVar(&o.Master, "master", o.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
192+
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")
187193

188194
o.SecureServing.AddFlags(nfs.FlagSet("secure serving"))
189195
o.Authentication.AddFlags(nfs.FlagSet("authentication"))
@@ -262,13 +268,19 @@ func (o *Options) Config() (*schedulerappconfig.Config, error) {
262268
if err := o.ApplyTo(c); err != nil {
263269
return nil, err
264270
}
271+
c.OpenShiftContext = o.OpenShiftContext
265272

266273
// Prepare kube config.
267274
kubeConfig, err := createKubeConfig(c.ComponentConfig.ClientConnection, o.Master)
268275
if err != nil {
269276
return nil, err
270277
}
271278

279+
if c.OpenShiftContext.PreferredHostRoundTripperWrapperFn != nil {
280+
libgorestclient.DefaultServerName(kubeConfig)
281+
kubeConfig.Wrap(c.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
282+
}
283+
272284
// Prepare kube clients.
273285
client, eventClient, err := createClients(kubeConfig)
274286
if err != nil {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package options
2+
3+
import kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
4+
5+
func LoadKubeSchedulerConfiguration(file string) (*kubeschedulerconfig.KubeSchedulerConfiguration, error) {
6+
return loadConfigFromFile(file)
7+
}

cmd/kube-scheduler/app/patch.go

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

cmd/kube-scheduler/app/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Op
125125
cancel()
126126
}()
127127

128+
if err := setUpPreferredHostForOpenShift(opts); err != nil {
129+
return err
130+
}
131+
128132
cc, sched, err := Setup(ctx, opts, registryOptions...)
129133
if err != nil {
130134
return err
@@ -138,6 +142,11 @@ func Run(ctx context.Context, cc *schedulerserverconfig.CompletedConfig, sched *
138142
// To help debugging, immediately log version
139143
klog.InfoS("Starting Kubernetes Scheduler", "version", version.Get())
140144

145+
// start the localhost health monitor early so that it can be used by the LE client
146+
if cc.OpenShiftContext.PreferredHostHealthMonitor != nil {
147+
go cc.OpenShiftContext.PreferredHostHealthMonitor.Run(ctx)
148+
}
149+
141150
// Configz registration.
142151
if cz, err := configz.New("componentconfig"); err == nil {
143152
cz.Set(cc.ComponentConfig)

0 commit comments

Comments
 (0)