Refactor kubeadm config list-images

In preparation for creating a `kubeadm config images pull`
this commit refactors `kubeadm config list-images` into
`kubeadm config images list`.

Signed-off-by: Chuck Ha <ha.chuck@gmail.com>
pull/8/head
Chuck Ha 2018-05-14 13:49:36 -04:00
parent c9591ee6cb
commit 63747e5c75
No known key found for this signature in database
GPG Key ID: D2B2A4E41BEF2D78
7 changed files with 45 additions and 26 deletions

View File

@ -66,7 +66,7 @@ func NewCmdConfig(out io.Writer) *cobra.Command {
cmd.AddCommand(NewCmdConfigUpload(out, &kubeConfigFile))
cmd.AddCommand(NewCmdConfigView(out, &kubeConfigFile))
cmd.AddCommand(NewCmdConfigListImages(out))
cmd.AddCommand(NewCmdConfigImages(out))
return cmd
}
@ -206,50 +206,61 @@ func uploadConfiguration(client clientset.Interface, cfgPath string, defaultcfg
return uploadconfig.UploadConfiguration(internalcfg, client)
}
// NewCmdConfigListImages returns the "kubeadm images" command
func NewCmdConfigListImages(out io.Writer) *cobra.Command {
// NewCmdConfigImages returns the "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))
return cmd
}
// NewCmdConfigImagesList returns the "config images list" command
func NewCmdConfigImagesList(out io.Writer) *cobra.Command {
cfg := &kubeadmapiv1alpha1.MasterConfiguration{}
kubeadmapiv1alpha1.SetDefaults_MasterConfiguration(cfg)
var cfgPath, featureGatesString string
var err error
cmd := &cobra.Command{
Use: "list-images",
Use: "list",
Short: "Print a list of images kubeadm will use. The configuration file is used in case any images or image repositories are customized.",
Run: func(_ *cobra.Command, _ []string) {
if cfg.FeatureGates, err = features.NewFeatureGate(&features.InitFeatureGates, featureGatesString); err != nil {
kubeadmutil.CheckErr(err)
}
listImages, err := NewListImages(cfgPath, cfg)
imagesList, err := NewImagesList(cfgPath, cfg)
kubeadmutil.CheckErr(err)
kubeadmutil.CheckErr(listImages.Run(out))
kubeadmutil.CheckErr(imagesList.Run(out))
},
}
AddListImagesConfigFlag(cmd.PersistentFlags(), cfg, &featureGatesString)
AddListImagesFlags(cmd.PersistentFlags(), &cfgPath)
AddImagesListConfigFlags(cmd.PersistentFlags(), cfg, &featureGatesString)
AddImagesListFlags(cmd.PersistentFlags(), &cfgPath)
return cmd
}
// NewListImages returns a "kubeadm images" command
func NewListImages(cfgPath string, cfg *kubeadmapiv1alpha1.MasterConfiguration) (*ListImages, error) {
// NewImagesList returns the underlying struct for the "kubeadm config images list" command
func NewImagesList(cfgPath string, cfg *kubeadmapiv1alpha1.MasterConfiguration) (*ImagesList, error) {
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
if err != nil {
return nil, fmt.Errorf("could not convert cfg to an internal cfg: %v", err)
}
return &ListImages{
return &ImagesList{
cfg: internalcfg,
}, nil
}
// ListImages defines the struct used for "kubeadm images"
type ListImages struct {
// ImagesList defines the struct used for "kubeadm config images list"
type ImagesList struct {
cfg *kubeadmapi.MasterConfiguration
}
// Run runs the images command and writes the result to the io.Writer passed in
func (i *ListImages) Run(out io.Writer) error {
// Run gets a list of images kubeadm expects to use and writes the result to the io.Writer passed in
func (i *ImagesList) Run(out io.Writer) error {
imgs := images.GetAllImages(i.cfg)
for _, img := range imgs {
fmt.Fprintln(out, img)
@ -258,8 +269,8 @@ func (i *ListImages) Run(out io.Writer) error {
return nil
}
// AddListImagesConfigFlag adds the flags that configure kubeadm
func AddListImagesConfigFlag(flagSet *flag.FlagSet, cfg *kubeadmapiv1alpha1.MasterConfiguration, featureGatesString *string) {
// AddImagesListConfigFlags adds the flags that configure kubeadm (and affect the images kubeadm will use)
func AddImagesListConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1alpha1.MasterConfiguration, featureGatesString *string) {
flagSet.StringVar(
&cfg.KubernetesVersion, "kubernetes-version", cfg.KubernetesVersion,
`Choose a specific Kubernetes version for the control plane.`,
@ -268,7 +279,7 @@ func AddListImagesConfigFlag(flagSet *flag.FlagSet, cfg *kubeadmapiv1alpha1.Mast
"Options are:\n"+strings.Join(features.KnownFeatures(&features.InitFeatureGates), "\n"))
}
// AddListImagesFlags adds the flag that defines the location of the config file
func AddListImagesFlags(flagSet *flag.FlagSet, cfgPath *string) {
// AddImagesListFlags adds the flag that defines the location of the config file
func AddImagesListFlags(flagSet *flag.FlagSet, cfgPath *string) {
flagSet.StringVar(cfgPath, "config", *cfgPath, "Path to kubeadm config file.")
}

View File

@ -37,7 +37,7 @@ const (
func TestNewCmdConfigListImages(t *testing.T) {
var output bytes.Buffer
images := cmd.NewCmdConfigListImages(&output)
images := cmd.NewCmdConfigImagesList(&output)
images.Run(nil, nil)
actual := strings.Split(output.String(), "\n")
if len(actual) != defaultNumberOfImages {
@ -45,7 +45,7 @@ func TestNewCmdConfigListImages(t *testing.T) {
}
}
func TestListImagesRunWithCustomConfigPath(t *testing.T) {
func TestImagesListRunWithCustomConfigPath(t *testing.T) {
testcases := []struct {
name string
expectedImageCount int
@ -99,7 +99,7 @@ func TestListImagesRunWithCustomConfigPath(t *testing.T) {
t.Fatalf("Failed writing a config file: %v", err)
}
i, err := cmd.NewListImages(configFilePath, &kubeadmapiv1alpha1.MasterConfiguration{})
i, err := cmd.NewImagesList(configFilePath, &kubeadmapiv1alpha1.MasterConfiguration{})
if err != nil {
t.Fatalf("Failed getting the kubeadm images command: %v", err)
}
@ -121,7 +121,7 @@ func TestListImagesRunWithCustomConfigPath(t *testing.T) {
}
}
func TestConfigListImagesRunWithoutPath(t *testing.T) {
func TestConfigImagesListRunWithoutPath(t *testing.T) {
testcases := []struct {
name string
cfg kubeadmapiv1alpha1.MasterConfiguration
@ -153,7 +153,7 @@ func TestConfigListImagesRunWithoutPath(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
i, err := cmd.NewListImages("", &tc.cfg)
i, err := cmd.NewImagesList("", &tc.cfg)
if err != nil {
t.Fatalf("did not expect an error while creating the Images command: %v", err)
}

View File

@ -58,7 +58,8 @@ docs/admin/kubeadm_alpha_phase_selfhosting_convert-from-staticpods.md
docs/admin/kubeadm_alpha_phase_upload-config.md
docs/admin/kubeadm_completion.md
docs/admin/kubeadm_config.md
docs/admin/kubeadm_config_list-images.md
docs/admin/kubeadm_config_images.md
docs/admin/kubeadm_config_images_list.md
docs/admin/kubeadm_config_upload.md
docs/admin/kubeadm_config_upload_from-file.md
docs/admin/kubeadm_config_upload_from-flags.md
@ -133,7 +134,8 @@ docs/man/man1/kubeadm-alpha-phase-upload-config.1
docs/man/man1/kubeadm-alpha-phase.1
docs/man/man1/kubeadm-alpha.1
docs/man/man1/kubeadm-completion.1
docs/man/man1/kubeadm-config-list-images.1
docs/man/man1/kubeadm-config-images-list.1
docs/man/man1/kubeadm-config-images.1
docs/man/man1/kubeadm-config-upload-from-file.1
docs/man/man1/kubeadm-config-upload-from-flags.1
docs/man/man1/kubeadm-config-upload.1

View File

@ -0,0 +1,3 @@
This file is autogenerated, but we've stopped checking such files into the
repository to reduce the need for rebases. Please run hack/generate-docs.sh to
populate this file.

View File

@ -0,0 +1,3 @@
This file is autogenerated, but we've stopped checking such files into the
repository to reduce the need for rebases. Please run hack/generate-docs.sh to
populate this file.