Skip to content

Commit d2c005a

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 44f4729 commit d2c005a

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ type Config struct {
6565

6666
// ComponentGlobalsRegistry is the registry where the effective versions and feature gates for all components are stored.
6767
ComponentGlobalsRegistry basecompatibility.ComponentGlobalsRegistry
68+
69+
// OpenShiftContext is additional context that we need to launch the kube-scheduler for openshift
70+
OpenShiftContext OpenShiftContext
6871
}
6972

7073
type completedConfig struct {
+15
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

+11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ import (
5454
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
5555
"k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
5656
netutils "k8s.io/utils/net"
57+
58+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
5759
)
5860

5961
// Options has all the params needed to run a Scheduler
@@ -82,6 +84,9 @@ type Options struct {
8284

8385
// Flags hold the parsed CLI flags.
8486
Flags *cliflag.NamedFlagSets
87+
88+
// OpenShiftContext is additional context that we need to launch the kube-scheduler for openshift.
89+
OpenShiftContext schedulerappconfig.OpenShiftContext
8590
}
8691

8792
// NewOptions returns default scheduler app options.
@@ -203,6 +208,7 @@ func (o *Options) initFlags() {
203208
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file.")
204209
fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the configuration values to this file and exit.")
205210
fs.StringVar(&o.Master, "master", o.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
211+
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")
206212

207213
o.SecureServing.AddFlags(nfs.FlagSet("secure serving"))
208214
o.Authentication.AddFlags(nfs.FlagSet("authentication"))
@@ -248,6 +254,10 @@ func (o *Options) ApplyTo(logger klog.Logger, c *schedulerappconfig.Config) erro
248254
if err != nil {
249255
return err
250256
}
257+
if c.OpenShiftContext.PreferredHostRoundTripperWrapperFn != nil {
258+
libgorestclient.DefaultServerName(kubeConfig)
259+
kubeConfig.Wrap(c.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
260+
}
251261
c.KubeConfig = kubeConfig
252262

253263
if err := o.SecureServing.ApplyTo(&c.SecureServing, &c.LoopbackClientConfig); err != nil {
@@ -305,6 +315,7 @@ func (o *Options) Config(ctx context.Context) (*schedulerappconfig.Config, error
305315
}
306316

307317
c := &schedulerappconfig.Config{}
318+
c.OpenShiftContext = o.OpenShiftContext
308319
if err := o.ApplyTo(logger, c); err != nil {
309320
return nil, err
310321
}
+11
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

+72
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

+10
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Op
154154
cancel()
155155
}()
156156

157+
logger := klog.FromContext(ctx)
158+
if err := setUpPreferredHostForOpenShift(logger, opts); err != nil {
159+
return err
160+
}
161+
157162
cc, sched, err := Setup(ctx, opts, registryOptions...)
158163
if err != nil {
159164
return err
@@ -172,6 +177,11 @@ func Run(ctx context.Context, cc *schedulerserverconfig.CompletedConfig, sched *
172177

173178
logger.Info("Golang settings", "GOGC", os.Getenv("GOGC"), "GOMAXPROCS", os.Getenv("GOMAXPROCS"), "GOTRACEBACK", os.Getenv("GOTRACEBACK"))
174179

180+
// start the localhost health monitor early so that it can be used by the LE client
181+
if cc.OpenShiftContext.PreferredHostHealthMonitor != nil {
182+
go cc.OpenShiftContext.PreferredHostHealthMonitor.Run(ctx)
183+
}
184+
175185
// Configz registration.
176186
if cz, err := configz.New("componentconfig"); err != nil {
177187
return fmt.Errorf("unable to register configz: %s", err)

0 commit comments

Comments
 (0)