Skip to content

Commit 0f4e262

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 5ba4ba0 commit 0f4e262

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
@@ -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

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ import (
5252
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
5353
"k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
5454
netutils "k8s.io/utils/net"
55+
56+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
5557
)
5658

5759
// Options has all the params needed to run a Scheduler
@@ -80,6 +82,9 @@ type Options struct {
8082

8183
// Flags hold the parsed CLI flags.
8284
Flags *cliflag.NamedFlagSets
85+
86+
// OpenShiftContext is additional context that we need to launch the kube-scheduler for openshift.
87+
OpenShiftContext schedulerappconfig.OpenShiftContext
8388
}
8489

8590
// NewOptions returns default scheduler app options.
@@ -196,6 +201,7 @@ func (o *Options) initFlags() {
196201
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file.")
197202
fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the configuration values to this file and exit.")
198203
fs.StringVar(&o.Master, "master", o.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
204+
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")
199205

200206
o.SecureServing.AddFlags(nfs.FlagSet("secure serving"))
201207
o.Authentication.AddFlags(nfs.FlagSet("authentication"))
@@ -241,6 +247,10 @@ func (o *Options) ApplyTo(logger klog.Logger, c *schedulerappconfig.Config) erro
241247
if err != nil {
242248
return err
243249
}
250+
if c.OpenShiftContext.PreferredHostRoundTripperWrapperFn != nil {
251+
libgorestclient.DefaultServerName(kubeConfig)
252+
kubeConfig.Wrap(c.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
253+
}
244254
c.KubeConfig = kubeConfig
245255

246256
if err := o.SecureServing.ApplyTo(&c.SecureServing, &c.LoopbackClientConfig); err != nil {
@@ -298,6 +308,7 @@ func (o *Options) Config(ctx context.Context) (*schedulerappconfig.Config, error
298308
}
299309

300310
c := &schedulerappconfig.Config{}
311+
c.OpenShiftContext = o.OpenShiftContext
301312
if err := o.ApplyTo(logger, c); err != nil {
302313
return nil, err
303314
}
+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
@@ -149,6 +149,11 @@ func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Op
149149
cancel()
150150
}()
151151

152+
logger := klog.FromContext(ctx)
153+
if err := setUpPreferredHostForOpenShift(logger, opts); err != nil {
154+
return err
155+
}
156+
152157
cc, sched, err := Setup(ctx, opts, registryOptions...)
153158
if err != nil {
154159
return err
@@ -167,6 +172,11 @@ func Run(ctx context.Context, cc *schedulerserverconfig.CompletedConfig, sched *
167172

168173
logger.Info("Golang settings", "GOGC", os.Getenv("GOGC"), "GOMAXPROCS", os.Getenv("GOMAXPROCS"), "GOTRACEBACK", os.Getenv("GOTRACEBACK"))
169174

175+
// start the localhost health monitor early so that it can be used by the LE client
176+
if cc.OpenShiftContext.PreferredHostHealthMonitor != nil {
177+
go cc.OpenShiftContext.PreferredHostHealthMonitor.Run(ctx)
178+
}
179+
170180
// Configz registration.
171181
if cz, err := configz.New("componentconfig"); err != nil {
172182
return fmt.Errorf("unable to register configz: %s", err)

0 commit comments

Comments
 (0)