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

kubeadm: support fetching configuration from the original cluster for 'upgrade diff' #80025

Merged
merged 1 commit into from
Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions cmd/kubeadm/app/cmd/upgrade/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/klog"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/controlplane"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
)

type diffFlags struct {
Expand All @@ -40,6 +43,7 @@ type diffFlags struct {
schedulerManifestPath string
newK8sVersionStr string
contextLines int
kubeConfigPath string
cfgPath string
out io.Writer
}
Expand All @@ -53,7 +57,8 @@ var (
// NewCmdDiff returns the cobra command for `kubeadm upgrade diff`
func NewCmdDiff(out io.Writer) *cobra.Command {
flags := &diffFlags{
out: out,
kubeConfigPath: kubeadmconstants.GetAdminKubeConfigPath(),
out: out,
}

cmd := &cobra.Command{
Expand All @@ -65,6 +70,7 @@ func NewCmdDiff(out io.Writer) *cobra.Command {
},
}

options.AddKubeConfigFlag(cmd.Flags(), &flags.kubeConfigPath)
options.AddConfigFlag(cmd.Flags(), &flags.cfgPath)
cmd.Flags().StringVar(&flags.apiServerManifestPath, "api-server-manifest", defaultAPIServerManifestPath, "path to API server manifest")
cmd.Flags().StringVar(&flags.controllerManagerManifestPath, "controller-manager-manifest", defaultControllerManagerManifestPath, "path to controller manifest")
Expand All @@ -75,13 +81,22 @@ func NewCmdDiff(out io.Writer) *cobra.Command {
}

func runDiff(flags *diffFlags, args []string) error {

// If the version is specified in config file, pick up that value.
cfg, err := configutil.LoadInitConfigurationFromFile(flags.cfgPath)
var err error
var cfg *kubeadmapi.InitConfiguration
if flags.cfgPath != "" {
cfg, err = configutil.LoadInitConfigurationFromFile(flags.cfgPath)
} else {
Copy link
Member

Choose a reason for hiding this comment

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

can you add a test case for this ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @yastij
This does lack test cases. But FetchInitConfigurationFromCluster requires a running kubernetes cluster. How should we write this kind of test cases? Any examples?

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 want to unit test this, we need to mock FetchInitConfigurationFromCluster

Copy link
Member

Choose a reason for hiding this comment

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

yeah, let's not do the mock/unit test of the function in question for now.

Copy link
Member

Choose a reason for hiding this comment

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

I can settle with that, modulo a TODO or something to make us remember to do this

Copy link
Member Author

Choose a reason for hiding this comment

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

@yastij Where should we put the TODO:? in diff_test.go ?

Copy link
Member

Choose a reason for hiding this comment

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

diff_test.go seems reasonable to me

Copy link
Member Author

Choose a reason for hiding this comment

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

@yastij Done

client, err := kubeconfigutil.ClientSetFromFile(flags.kubeConfigPath)
if err != nil {
return errors.Wrapf(err, "couldn't create a Kubernetes client from file %q", flags.kubeConfigPath)
}
cfg, err = configutil.FetchInitConfigurationFromCluster(client, flags.out, "upgrade/diff", false)
}
if err != nil {
return err
}

// If the version is specified in config file, pick up that value.
if cfg.KubernetesVersion != "" {
flags.newK8sVersionStr = cfg.KubernetesVersion
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kubeadm/app/cmd/upgrade/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestRunDiff(t *testing.T) {
out: ioutil.Discard,
}

// TODO: Add test cases for empty cfgPath, it should automatically fetch cfg from cluster
testCases := []struct {
name string
args []string
Expand Down