make new command functions to keep private when they are not required to be public

This commit is contained in:
SataQiu 2020-10-10 11:45:08 +08:00
parent acb3beaae4
commit 646f4e2b62
22 changed files with 86 additions and 86 deletions

View File

@ -33,7 +33,7 @@ func NewCmdAlpha(in io.Reader, out io.Writer) *cobra.Command {
const shDeprecatedMessage = "self-hosting support in kubeadm is deprecated " +
"and will be removed in a future release"
shCommand := NewCmdSelfhosting(in)
shCommand := newCmdSelfhosting(in)
shCommand.Deprecated = shDeprecatedMessage
for _, cmd := range shCommand.Commands() {
cmd.Deprecated = shDeprecatedMessage

View File

@ -100,7 +100,7 @@ func NewCmdCertsUtility(out io.Writer) *cobra.Command {
cmd.AddCommand(newCmdCertsRenewal(out))
cmd.AddCommand(newCmdCertsExpiration(out, constants.KubernetesDir))
cmd.AddCommand(NewCmdCertificateKey())
cmd.AddCommand(newCmdCertificateKey())
cmd.AddCommand(newCmdGenCSR())
return cmd
}
@ -178,8 +178,8 @@ func runGenCSR(config *genCSRConfig) error {
return nil
}
// NewCmdCertificateKey returns cobra.Command for certificate key generate
func NewCmdCertificateKey() *cobra.Command {
// newCmdCertificateKey returns cobra.Command for certificate key generate
func newCmdCertificateKey() *cobra.Command {
return &cobra.Command{
Use: "certificate-key",
Short: "Generate certificate keys",

View File

@ -56,8 +56,8 @@ var (
`)
)
// NewCmdSelfhosting returns the self-hosting Cobra command
func NewCmdSelfhosting(in io.Reader) *cobra.Command {
// newCmdSelfhosting returns the self-hosting Cobra command
func newCmdSelfhosting(in io.Reader) *cobra.Command {
cmd := &cobra.Command{
Use: "selfhosting",
Aliases: []string{"selfhosted", "self-hosting"},

View File

@ -81,13 +81,13 @@ func NewKubeadmCommand(in io.Reader, out, err io.Writer) *cobra.Command {
cmds.ResetFlags()
cmds.AddCommand(NewCmdCompletion(out, ""))
cmds.AddCommand(NewCmdConfig(out))
cmds.AddCommand(NewCmdInit(out, nil))
cmds.AddCommand(NewCmdJoin(out, nil))
cmds.AddCommand(NewCmdReset(in, out, nil))
cmds.AddCommand(NewCmdVersion(out))
cmds.AddCommand(NewCmdToken(out, err))
cmds.AddCommand(newCmdCompletion(out, ""))
cmds.AddCommand(newCmdConfig(out))
cmds.AddCommand(newCmdInit(out, nil))
cmds.AddCommand(newCmdJoin(out, nil))
cmds.AddCommand(newCmdReset(in, out, nil))
cmds.AddCommand(newCmdVersion(out))
cmds.AddCommand(newCmdToken(out, err))
cmds.AddCommand(upgrade.NewCmdUpgrade(out))
cmds.AddCommand(alpha.NewCmdAlpha(in, out))
options.AddKubeadmOtherFlags(cmds.PersistentFlags(), &rootfsPath)

View File

@ -96,8 +96,8 @@ func GetSupportedShells() []string {
return shells
}
// NewCmdCompletion returns the "kubeadm completion" command
func NewCmdCompletion(out io.Writer, boilerPlate string) *cobra.Command {
// newCmdCompletion returns the "kubeadm completion" command
func newCmdCompletion(out io.Writer, boilerPlate string) *cobra.Command {
cmd := &cobra.Command{
Use: "completion SHELL",
Short: "Output shell completion code for the specified shell (bash or zsh)",

View File

@ -31,15 +31,15 @@ func TestNewCmdCompletion(t *testing.T) {
if len(shells) == 0 {
t.Errorf(shellsError)
}
// test NewCmdCompletion with a valid shell.
// use a dummy parent command as NewCmdCompletion needs it.
// test newCmdCompletion with a valid shell.
// use a dummy parent command as newCmdCompletion needs it.
parentCmd := &cobra.Command{}
args := []string{"completion", shells[0]}
parentCmd.SetArgs(args)
cmd := NewCmdCompletion(&out, "")
cmd := newCmdCompletion(&out, "")
parentCmd.AddCommand(cmd)
if err := parentCmd.Execute(); err != nil {
t.Errorf("Cannot exectute NewCmdCompletion: %v", err)
t.Errorf("Cannot exectute newCmdCompletion: %v", err)
}
}

View File

@ -63,8 +63,8 @@ var (
}
)
// NewCmdConfig returns cobra.Command for "kubeadm config" command
func NewCmdConfig(out io.Writer) *cobra.Command {
// newCmdConfig returns cobra.Command for "kubeadm config" command
func newCmdConfig(out io.Writer) *cobra.Command {
var kubeConfigFile string
cmd := &cobra.Command{
@ -87,15 +87,15 @@ func NewCmdConfig(out io.Writer) *cobra.Command {
options.AddKubeConfigFlag(cmd.PersistentFlags(), &kubeConfigFile)
kubeConfigFile = cmdutil.GetKubeConfigPath(kubeConfigFile)
cmd.AddCommand(NewCmdConfigPrint(out))
cmd.AddCommand(NewCmdConfigMigrate(out))
cmd.AddCommand(NewCmdConfigView(out, &kubeConfigFile))
cmd.AddCommand(NewCmdConfigImages(out))
cmd.AddCommand(newCmdConfigPrint(out))
cmd.AddCommand(newCmdConfigMigrate(out))
cmd.AddCommand(newCmdConfigView(out, &kubeConfigFile))
cmd.AddCommand(newCmdConfigImages(out))
return cmd
}
// NewCmdConfigPrint returns cobra.Command for "kubeadm config print" command
func NewCmdConfigPrint(out io.Writer) *cobra.Command {
// newCmdConfigPrint returns cobra.Command for "kubeadm config print" command
func newCmdConfigPrint(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "print",
Short: "Print configuration",
@ -104,18 +104,18 @@ func NewCmdConfigPrint(out io.Writer) *cobra.Command {
For details, see: https://godoc.org/k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2`),
RunE: cmdutil.SubCmdRunE("print"),
}
cmd.AddCommand(NewCmdConfigPrintInitDefaults(out))
cmd.AddCommand(NewCmdConfigPrintJoinDefaults(out))
cmd.AddCommand(newCmdConfigPrintInitDefaults(out))
cmd.AddCommand(newCmdConfigPrintJoinDefaults(out))
return cmd
}
// NewCmdConfigPrintInitDefaults returns cobra.Command for "kubeadm config print init-defaults" command
func NewCmdConfigPrintInitDefaults(out io.Writer) *cobra.Command {
// newCmdConfigPrintInitDefaults returns cobra.Command for "kubeadm config print init-defaults" command
func newCmdConfigPrintInitDefaults(out io.Writer) *cobra.Command {
return newCmdConfigPrintActionDefaults(out, "init", getDefaultInitConfigBytes)
}
// NewCmdConfigPrintJoinDefaults returns cobra.Command for "kubeadm config print join-defaults" command
func NewCmdConfigPrintJoinDefaults(out io.Writer) *cobra.Command {
// newCmdConfigPrintJoinDefaults returns cobra.Command for "kubeadm config print join-defaults" command
func newCmdConfigPrintJoinDefaults(out io.Writer) *cobra.Command {
return newCmdConfigPrintActionDefaults(out, "join", getDefaultNodeConfigBytes)
}
@ -249,8 +249,8 @@ func getDefaultNodeConfigBytes() ([]byte, error) {
return configutil.MarshalKubeadmConfigObject(internalcfg)
}
// NewCmdConfigMigrate returns cobra.Command for "kubeadm config migrate" command
func NewCmdConfigMigrate(out io.Writer) *cobra.Command {
// newCmdConfigMigrate returns cobra.Command for "kubeadm config migrate" command
func newCmdConfigMigrate(out io.Writer) *cobra.Command {
var oldCfgPath, newCfgPath string
cmd := &cobra.Command{
Use: "migrate",
@ -300,8 +300,8 @@ func NewCmdConfigMigrate(out io.Writer) *cobra.Command {
return cmd
}
// NewCmdConfigView returns cobra.Command for "kubeadm config view" command
func NewCmdConfigView(out io.Writer, kubeConfigFile *string) *cobra.Command {
// newCmdConfigView returns cobra.Command for "kubeadm config view" command
func newCmdConfigView(out io.Writer, kubeConfigFile *string) *cobra.Command {
return &cobra.Command{
Use: "view",
Short: "View the kubeadm configuration stored inside the cluster",
@ -337,20 +337,20 @@ func RunConfigView(out io.Writer, client clientset.Interface) error {
return nil
}
// NewCmdConfigImages returns the "kubeadm config images" command
func NewCmdConfigImages(out io.Writer) *cobra.Command {
// newCmdConfigImages returns the "kubeadm config images" command
func newCmdConfigImages(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "images",
Short: "Interact with container images used by kubeadm",
RunE: cmdutil.SubCmdRunE("images"),
}
cmd.AddCommand(NewCmdConfigImagesList(out, nil))
cmd.AddCommand(NewCmdConfigImagesPull())
cmd.AddCommand(newCmdConfigImagesList(out, nil))
cmd.AddCommand(newCmdConfigImagesPull())
return cmd
}
// NewCmdConfigImagesPull returns the `kubeadm config images pull` command
func NewCmdConfigImagesPull() *cobra.Command {
// newCmdConfigImagesPull returns the `kubeadm config images pull` command
func newCmdConfigImagesPull() *cobra.Command {
externalClusterCfg := &kubeadmapiv1beta2.ClusterConfiguration{}
kubeadmscheme.Scheme.Default(externalClusterCfg)
externalInitCfg := &kubeadmapiv1beta2.InitConfiguration{}
@ -410,8 +410,8 @@ func PullControlPlaneImages(runtime utilruntime.ContainerRuntime, cfg *kubeadmap
return nil
}
// NewCmdConfigImagesList returns the "kubeadm config images list" command
func NewCmdConfigImagesList(out io.Writer, mockK8sVersion *string) *cobra.Command {
// newCmdConfigImagesList returns the "kubeadm config images list" command
func newCmdConfigImagesList(out io.Writer, mockK8sVersion *string) *cobra.Command {
externalcfg := &kubeadmapiv1beta2.ClusterConfiguration{}
kubeadmscheme.Scheme.Default(externalcfg)
var cfgPath, featureGatesString string

View File

@ -56,7 +56,7 @@ var (
func TestNewCmdConfigImagesList(t *testing.T) {
var output bytes.Buffer
mockK8sVersion := dummyKubernetesVersionStr
images := NewCmdConfigImagesList(&output, &mockK8sVersion)
images := newCmdConfigImagesList(&output, &mockK8sVersion)
if err := images.RunE(nil, nil); err != nil {
t.Fatalf("Error from running the images command: %v", err)
}
@ -401,7 +401,7 @@ func TestMigrate(t *testing.T) {
defer cleanup()
var output bytes.Buffer
command := NewCmdConfigMigrate(&output)
command := newCmdConfigMigrate(&output)
if err := command.Flags().Set("old-config", configFile); err != nil {
t.Fatalf("failed to set old-config flag")
}
@ -447,7 +447,7 @@ func TestNewCmdConfigPrintActionDefaults(t *testing.T) {
constants.ClusterConfigurationKind,
constants.InitConfigurationKind,
},
cmdProc: NewCmdConfigPrintInitDefaults,
cmdProc: newCmdConfigPrintInitDefaults,
},
{
name: "InitConfiguration: KubeProxyConfiguration",
@ -457,7 +457,7 @@ func TestNewCmdConfigPrintActionDefaults(t *testing.T) {
"KubeProxyConfiguration",
},
componentConfigs: "KubeProxyConfiguration",
cmdProc: NewCmdConfigPrintInitDefaults,
cmdProc: newCmdConfigPrintInitDefaults,
},
{
name: "InitConfiguration: KubeProxyConfiguration and KubeletConfiguration",
@ -468,14 +468,14 @@ func TestNewCmdConfigPrintActionDefaults(t *testing.T) {
"KubeletConfiguration",
},
componentConfigs: "KubeProxyConfiguration,KubeletConfiguration",
cmdProc: NewCmdConfigPrintInitDefaults,
cmdProc: newCmdConfigPrintInitDefaults,
},
{
name: "JoinConfiguration: No component configs",
expectedKinds: []string{
constants.JoinConfigurationKind,
},
cmdProc: NewCmdConfigPrintJoinDefaults,
cmdProc: newCmdConfigPrintJoinDefaults,
},
{
name: "JoinConfiguration: KubeProxyConfiguration",
@ -484,7 +484,7 @@ func TestNewCmdConfigPrintActionDefaults(t *testing.T) {
"KubeProxyConfiguration",
},
componentConfigs: "KubeProxyConfiguration",
cmdProc: NewCmdConfigPrintJoinDefaults,
cmdProc: newCmdConfigPrintJoinDefaults,
},
{
name: "JoinConfiguration: KubeProxyConfiguration and KubeletConfiguration",
@ -494,7 +494,7 @@ func TestNewCmdConfigPrintActionDefaults(t *testing.T) {
"KubeletConfiguration",
},
componentConfigs: "KubeProxyConfiguration,KubeletConfiguration",
cmdProc: NewCmdConfigPrintJoinDefaults,
cmdProc: newCmdConfigPrintJoinDefaults,
},
}

View File

@ -127,10 +127,10 @@ type initData struct {
patchesDir string
}
// NewCmdInit returns "kubeadm init" command.
// newCmdInit returns "kubeadm init" command.
// NB. initOptions is exposed as parameter for allowing unit testing of
// the newInitOptions method, that implements all the command options validation logic
func NewCmdInit(out io.Writer, initOptions *initOptions) *cobra.Command {
func newCmdInit(out io.Writer, initOptions *initOptions) *cobra.Command {
if initOptions == nil {
initOptions = newInitOptions()
}

View File

@ -153,7 +153,7 @@ func TestNewInitData(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// initialize an external init option and inject it to the init cmd
initOptions := newInitOptions()
cmd := NewCmdInit(nil, initOptions)
cmd := newCmdInit(nil, initOptions)
// sets cmd flags (that will be reflected on the init options)
for f, v := range tc.flags {

View File

@ -147,10 +147,10 @@ type joinData struct {
patchesDir string
}
// NewCmdJoin returns "kubeadm join" command.
// newCmdJoin returns "kubeadm join" command.
// NB. joinOptions is exposed as parameter for allowing unit testing of
// the newJoinData method, that implements all the command options validation logic
func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
func newCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
if joinOptions == nil {
joinOptions = newJoinOptions()
}

View File

@ -255,7 +255,7 @@ func TestNewJoinData(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// initialize an external join option and inject it to the join cmd
joinOptions := newJoinOptions()
cmd := NewCmdJoin(nil, joinOptions)
cmd := newCmdJoin(nil, joinOptions)
// sets cmd flags (that will be reflected on the join options)
for f, v := range tc.flags {

View File

@ -158,8 +158,8 @@ func AddResetFlags(flagSet *flag.FlagSet, resetOptions *resetOptions) {
cmdutil.AddCRISocketFlag(flagSet, &resetOptions.criSocketPath)
}
// NewCmdReset returns the "kubeadm reset" command
func NewCmdReset(in io.Reader, out io.Writer, resetOptions *resetOptions) *cobra.Command {
// newCmdReset returns the "kubeadm reset" command
func newCmdReset(in io.Reader, out io.Writer, resetOptions *resetOptions) *cobra.Command {
if resetOptions == nil {
resetOptions = newResetOptions()
}

View File

@ -54,8 +54,8 @@ import (
"k8s.io/kubernetes/cmd/kubeadm/app/util/output"
)
// NewCmdToken returns cobra.Command for token management
func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
// newCmdToken returns cobra.Command for token management
func newCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
var kubeConfigFile string
var dryRun bool
tokenCmd := &cobra.Command{
@ -149,7 +149,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
bto.AddDescriptionFlag(createCmd.Flags())
tokenCmd.AddCommand(createCmd)
tokenCmd.AddCommand(NewCmdTokenGenerate(out))
tokenCmd.AddCommand(newCmdTokenGenerate(out))
outputFlags := output.NewOutputFlags(&tokenTextPrintFlags{}).WithTypeSetter(outputapischeme.Scheme).WithDefaultOutput(output.TextOutput)
@ -208,8 +208,8 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
return tokenCmd
}
// NewCmdTokenGenerate returns cobra.Command to generate new token
func NewCmdTokenGenerate(out io.Writer) *cobra.Command {
// newCmdTokenGenerate returns cobra.Command to generate new token
func newCmdTokenGenerate(out io.Writer) *cobra.Command {
return &cobra.Command{
Use: "generate",
Short: "Generate and print a bootstrap token, but do not create it on the server",

View File

@ -186,7 +186,7 @@ func TestNewCmdTokenGenerate(t *testing.T) {
var buf bytes.Buffer
args := []string{}
cmd := NewCmdTokenGenerate(&buf)
cmd := newCmdTokenGenerate(&buf)
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
@ -242,8 +242,8 @@ func TestNewCmdToken(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// the command is created for each test so that the kubeConfigFile
// variable in NewCmdToken() is reset.
cmd := NewCmdToken(&buf, &bufErr)
// variable in newCmdToken() is reset.
cmd := newCmdToken(&buf, &bufErr)
if _, err = f.WriteString(tc.configToWrite); err != nil {
t.Errorf("Unable to write test file %q: %v", fullPath, err)
}
@ -255,7 +255,7 @@ func TestNewCmdToken(t *testing.T) {
cmd.SetArgs(tc.args)
err := cmd.Execute()
if (err != nil) != tc.expectedError {
t.Errorf("Test case %q: NewCmdToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
t.Errorf("Test case %q: newCmdToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
}
// restore the environment variable.
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, storedEnv)

View File

@ -59,8 +59,8 @@ func (f *applyFlags) sessionIsInteractive() bool {
return !(f.nonInteractiveMode || f.dryRun || f.force)
}
// NewCmdApply returns the cobra command for `kubeadm upgrade apply`
func NewCmdApply(apf *applyPlanFlags) *cobra.Command {
// newCmdApply returns the cobra command for `kubeadm upgrade apply`
func newCmdApply(apf *applyPlanFlags) *cobra.Command {
flags := &applyFlags{
applyPlanFlags: apf,
imagePullTimeout: defaultImagePullTimeout,

View File

@ -54,8 +54,8 @@ var (
defaultSchedulerManifestPath = constants.GetStaticPodFilepath(constants.KubeScheduler, constants.GetStaticPodDirectory())
)
// NewCmdDiff returns the cobra command for `kubeadm upgrade diff`
func NewCmdDiff(out io.Writer) *cobra.Command {
// newCmdDiff returns the cobra command for `kubeadm upgrade diff`
func newCmdDiff(out io.Writer) *cobra.Command {
flags := &diffFlags{
kubeConfigPath: constants.GetAdminKubeConfigPath(),
out: out,

View File

@ -62,8 +62,8 @@ type nodeData struct {
ignorePreflightErrors sets.String
}
// NewCmdNode returns the cobra command for `kubeadm upgrade node`
func NewCmdNode() *cobra.Command {
// newCmdNode returns the cobra command for `kubeadm upgrade node`
func newCmdNode() *cobra.Command {
nodeOptions := newNodeOptions()
nodeRunner := workflow.NewRunner()

View File

@ -44,8 +44,8 @@ type planFlags struct {
*applyPlanFlags
}
// NewCmdPlan returns the cobra command for `kubeadm upgrade plan`
func NewCmdPlan(apf *applyPlanFlags) *cobra.Command {
// newCmdPlan returns the cobra command for `kubeadm upgrade plan`
func newCmdPlan(apf *applyPlanFlags) *cobra.Command {
flags := &planFlags{
applyPlanFlags: apf,
}

View File

@ -57,10 +57,10 @@ func NewCmdUpgrade(out io.Writer) *cobra.Command {
RunE: cmdutil.SubCmdRunE("upgrade"),
}
cmd.AddCommand(NewCmdApply(flags))
cmd.AddCommand(NewCmdPlan(flags))
cmd.AddCommand(NewCmdDiff(out))
cmd.AddCommand(NewCmdNode())
cmd.AddCommand(newCmdApply(flags))
cmd.AddCommand(newCmdPlan(flags))
cmd.AddCommand(newCmdDiff(out))
cmd.AddCommand(newCmdNode())
return cmd
}

View File

@ -35,8 +35,8 @@ type Version struct {
ClientVersion *apimachineryversion.Info `json:"clientVersion"`
}
// NewCmdVersion provides the version information of kubeadm.
func NewCmdVersion(out io.Writer) *cobra.Command {
// newCmdVersion provides the version information of kubeadm.
func newCmdVersion(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the version of kubeadm",

View File

@ -26,7 +26,7 @@ import (
func TestNewCmdVersion(t *testing.T) {
var buf bytes.Buffer
cmd := NewCmdVersion(&buf)
cmd := newCmdVersion(&buf)
if err := cmd.Execute(); err != nil {
t.Errorf("Cannot execute version command: %v", err)
}
@ -36,7 +36,7 @@ func TestRunVersion(t *testing.T) {
var buf bytes.Buffer
iface := make(map[string]interface{})
flagNameOutput := "output"
cmd := NewCmdVersion(&buf)
cmd := newCmdVersion(&buf)
testCases := []struct {
name string