Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hostname-override arg to be used if specified #69340

Merged
merged 1 commit into from
Oct 16, 2018

Conversation

stevesloka
Copy link
Contributor

What this PR does / why we need it:

Currentlykube-proxy loads configuration from a config file. There are cases where the hostname needs to be overridden (See: kubernetes/kubeadm#857).

There is a flag that allows the hostname to be overridden (hostname-override), but when the config file is loaded, if this arg is set, it's overridden by whatever the config file contains making the argument ignored if it was set.

This PR checks for this argument and uses it if it is passed to kube-proxy.

Which issue(s) this PR fixes:
Fixes #57518
Fixes kubernetes/kubeadm#857

Special notes for your reviewer:

Release note:

kube-proxy argument `hostname-override` can be used to override hostname defined in the configuration file

// @timothysc

Signed-off-by: Steve Sloka steves@heptio.com

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 2, 2018
@@ -81,6 +81,10 @@ import (
"github.com/spf13/pflag"
)

var (
hostNameOverride string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide to proceed with this one-off approach for hostnameOverride, let's make this an unexported field in Options.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err := opts.Complete(); err != nil {
glog.Fatalf("failed complete: %v", err)
}
if err := opts.ProcessArgs(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a new top-level function to the complete/validate/run flow, I'd recommend renaming ProcessArgs() to processHostnameOverrideFlag() and calling it from within Complete()


// TestProcessArgs tests processing args
func TestProcessArgs(t *testing.T) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been renamed to match the code it is testing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... the blank line

testCases := []struct {
name string
hostnameArg string
expHostname string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exp->expected

@@ -204,14 +208,34 @@ func (o *Options) Complete() error {
}
}

err := utilfeature.DefaultFeatureGate.SetFromMap(o.config.FeatureGates)
err := o.processHostnameOverrideFlag()
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gofmt

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also: if err := ...; err != nil {

@@ -350,7 +374,6 @@ with the apiserver API to configure the proxy.`,
if err := initForOS(opts.WindowsService); err != nil {
glog.Fatalf("failed OS init: %v", err)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert?

@stevesloka stevesloka force-pushed the fixHostNameOverride branch 2 times, most recently from 5102ffa to 7b9e797 Compare October 2, 2018 19:32
@stevesloka
Copy link
Contributor Author

@ncdc I addressed your comments, thanks!

@@ -116,6 +116,9 @@ type Options struct {

scheme *runtime.Scheme
codecs serializer.CodecFactory

// hostNameOverrideArg is used to override the hostname of passed from a config file
hostNameOverrideArg string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this hostnameOverride

@@ -116,6 +116,9 @@ type Options struct {

scheme *runtime.Scheme
codecs serializer.CodecFactory

// hostNameOverrideArg is used to override the hostname of passed from a config file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like

hostnameOverride, if set from the command line flag, takes precedence over the `HostnameOverride` value from the config file

return err
}

return nil
}

// processHostnameOverrideFlag processes hostname-override argument
func (o *Options) processHostnameOverrideFlag() error {
// Check if hostname-override argument is set and use value since configFile always
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument -> flag

@@ -221,6 +221,7 @@ nodePortAddresses:
clusterCIDR string
healthzBindAddress string
metricsBindAddress string
hostNameOverride string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no not needed, removing


testCases := []struct {
name string
hostnameArg string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hostnameArg -> hostnameOverrideFlag

expectedHostname: "foo",
},
{
name: "Hostname from argument",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument -> flag

},
}
for _, tc := range testCases {
options := NewOptions()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a go subtest:

t.Run(tc.name, func(t *testing.T) {
  // body of test case
}

err := options.processHostnameOverrideFlag()
assert.NoError(t, err, "unexpected error for %s: %v", tc.name, err)
if tc.expectedHostname != options.config.HostnameOverride {
t.Fatalf("%s: expected hostname: %s, but got: %s", tc.name, tc.expectedHostname, options.config.HostnameOverride)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you switch to using a subtest, you can remove the initial %s for tc.name

options.hostNameOverrideArg = tc.hostnameArg

err := options.processHostnameOverrideFlag()
assert.NoError(t, err, "unexpected error for %s: %v", tc.name, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you switch to using a subtest, you can remove the initial %s for tc.name

@timothysc
Copy link
Member

/ok-to-test
/sig networking
/sig clusterlifecycle

/cc @kubernetes/sig-network-pr-reviews

@k8s-ci-robot k8s-ci-robot added sig/network Categorizes an issue or PR as relevant to SIG Network. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 2, 2018
Signed-off-by: Steve Sloka <steves@heptio.com>
@stevesloka
Copy link
Contributor Author

/retest

@timothysc timothysc added the kind/bug Categorizes issue or PR as related to a bug. label Oct 11, 2018
@k8s-ci-robot k8s-ci-robot removed the needs-kind Indicates a PR lacks a `kind/foo` label and requires one. label Oct 11, 2018
@timothysc
Copy link
Member

@kubernetes/sig-network-pr-reviews - re-ping!
/assign @thockin

return err
}

return nil
}

// processHostnameOverrideFlag processes hostname-override flag
func (o *Options) processHostnameOverrideFlag() error {
// Check if hostname-override flag is set and use value since configFile always overrides
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the flag would always override - the flag reads into the config struct..

@mtaufen

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thockin I did kube-proxy first, as a POC, and it got merged well before all the docs/discussions on the proper way to do things going forward. I didn't realize that there would be a need for per-instance configuration settings as well as those that are shared across all instances. The way kube-proxy specifically works, today, is that it's completely either-or. Either you use a config file, or you use flags. Flags do not override the config file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blech

@thockin
Copy link
Member

thockin commented Oct 16, 2018

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 16, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: stevesloka, thockin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 16, 2018
@mtaufen
Copy link
Contributor

mtaufen commented Oct 16, 2018

You may find that this becomes a general problem.
In the Kubelet we did a general thing to make flags take precedence over config files.
I'm not saying you should exactly emulate the way the Kubelet does the general thing, because there are likely cleaner bootstrap approaches that enable flags to take precedence, but if you notice this kind of thing keeps happening, you may want to consider a more general approach.

@stevesloka
Copy link
Contributor Author

@mtaufen yes this came up with discussions with @ncdc and others. I am going to look to better implement these flags in a future PR, but might need some further discussion.

@anguslees
Copy link
Member

(fwiw, the --{bind-address,metrics-bind-address,healthz-bind-address} flags are further examples that are probably going to be given node-specific values via flags rather than configured in the cluster-wide config)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/network Categorizes an issue or PR as relevant to SIG Network. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
7 participants