Skip to content

Commit aa9dde2

Browse files
p0lyn0mialsoltysh
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" openshift-rebase(v1.24):source=04eabe53d2a openshift-rebase(v1.24):source=04eabe53d2a openshift-rebase(v1.24):source=04eabe53d2a 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(v1.24):source=f89b437f6f0
1 parent 01542fc commit aa9dde2

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

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

+3
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 {
+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

+12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import (
4747
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
4848
"k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
4949
netutils "k8s.io/utils/net"
50+
51+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
5052
)
5153

5254
// Options has all the params needed to run a Scheduler
@@ -72,6 +74,9 @@ type Options struct {
7274

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

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

191197
o.SecureServing.AddFlags(nfs.FlagSet("secure serving"))
192198
o.Authentication.AddFlags(nfs.FlagSet("authentication"))
@@ -270,13 +276,19 @@ func (o *Options) Config() (*schedulerappconfig.Config, error) {
270276
if err := o.ApplyTo(c); err != nil {
271277
return nil, err
272278
}
279+
c.OpenShiftContext = o.OpenShiftContext
273280

274281
// Prepare kube config.
275282
kubeConfig, err := createKubeConfig(c.ComponentConfig.ClientConnection, o.Master)
276283
if err != nil {
277284
return nil, err
278285
}
279286

287+
if c.OpenShiftContext.PreferredHostRoundTripperWrapperFn != nil {
288+
libgorestclient.DefaultServerName(kubeConfig)
289+
kubeConfig.Wrap(c.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
290+
}
291+
280292
// Prepare kube clients.
281293
client, eventClient, err := createClients(kubeConfig)
282294
if err != nil {
+7
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

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 := kubeSchedulerOptions.Master
21+
var kubeConfig string
22+
23+
// We cannot load component config anymore as the options are not being initialized.
24+
// if there was no kubeconfig specified we won't be able to get cluster info.
25+
// in that case try to load the configuration and read kubeconfig directly from it if it was provided.
26+
if len(kubeSchedulerOptions.ConfigFile) > 0 {
27+
cfg, err := options.LoadKubeSchedulerConfiguration(kubeSchedulerOptions.ConfigFile)
28+
if err != nil {
29+
return err
30+
}
31+
kubeConfig = cfg.ClientConnection.Kubeconfig
32+
}
33+
34+
config, err := clientcmd.BuildConfigFromFlags(master, kubeConfig)
35+
if err != nil {
36+
return err
37+
}
38+
libgorestclient.DefaultServerName(config)
39+
40+
targetProvider := health.StaticTargetProvider{"localhost:6443"}
41+
kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor, err = health.New(targetProvider, createRestConfigForHealthMonitor(config))
42+
if err != nil {
43+
return err
44+
}
45+
kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor.
46+
WithHealthyProbesThreshold(3).
47+
WithUnHealthyProbesThreshold(5).
48+
WithProbeInterval(5 * time.Second).
49+
WithProbeResponseTimeout(2 * time.Second).
50+
WithMetrics(health.Register(legacyregistry.MustRegister))
51+
52+
kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn = libgorestclient.NewPreferredHostRoundTripper(func() string {
53+
healthyTargets, _ := kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor.Targets()
54+
if len(healthyTargets) == 1 {
55+
return healthyTargets[0]
56+
}
57+
return ""
58+
})
59+
60+
kubeSchedulerOptions.Authentication.WithCustomRoundTripper(kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
61+
kubeSchedulerOptions.Authorization.WithCustomRoundTripper(kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
62+
return nil
63+
}
64+
65+
func createRestConfigForHealthMonitor(restConfig *rest.Config) *rest.Config {
66+
restConfigCopy := *restConfig
67+
rest.AddUserAgent(&restConfigCopy, "kube-scheduler-health-monitor")
68+
69+
return &restConfigCopy
70+
}

cmd/kube-scheduler/app/server.go

+9
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Op
134134
cancel()
135135
}()
136136

137+
if err := setUpPreferredHostForOpenShift(opts); err != nil {
138+
return err
139+
}
140+
137141
cc, sched, err := Setup(ctx, opts, registryOptions...)
138142
if err != nil {
139143
return err
@@ -149,6 +153,11 @@ func Run(ctx context.Context, cc *schedulerserverconfig.CompletedConfig, sched *
149153

150154
klog.InfoS("Golang settings", "GOGC", os.Getenv("GOGC"), "GOMAXPROCS", os.Getenv("GOMAXPROCS"), "GOTRACEBACK", os.Getenv("GOTRACEBACK"))
151155

156+
// start the localhost health monitor early so that it can be used by the LE client
157+
if cc.OpenShiftContext.PreferredHostHealthMonitor != nil {
158+
go cc.OpenShiftContext.PreferredHostHealthMonitor.Run(ctx)
159+
}
160+
152161
// Configz registration.
153162
if cz, err := configz.New("componentconfig"); err == nil {
154163
cz.Set(cc.ComponentConfig)

0 commit comments

Comments
 (0)