mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Merge pull request #63811 from chuckha/list-images
Automatic merge from submit-queue (batch tested with PRs 63272, 63782, 63715, 63811, 63803). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. 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> **What this PR does / why we need it**: Prepares the `kubeadm config images` subcommand so we can add more functionality to it. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Related to kubernetes/kubeadm#812 **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
commit
8673c64d09
@ -66,7 +66,7 @@ func NewCmdConfig(out io.Writer) *cobra.Command {
|
|||||||
|
|
||||||
cmd.AddCommand(NewCmdConfigUpload(out, &kubeConfigFile))
|
cmd.AddCommand(NewCmdConfigUpload(out, &kubeConfigFile))
|
||||||
cmd.AddCommand(NewCmdConfigView(out, &kubeConfigFile))
|
cmd.AddCommand(NewCmdConfigView(out, &kubeConfigFile))
|
||||||
cmd.AddCommand(NewCmdConfigListImages(out))
|
cmd.AddCommand(NewCmdConfigImages(out))
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,50 +206,61 @@ func uploadConfiguration(client clientset.Interface, cfgPath string, defaultcfg
|
|||||||
return uploadconfig.UploadConfiguration(internalcfg, client)
|
return uploadconfig.UploadConfiguration(internalcfg, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCmdConfigListImages returns the "kubeadm images" command
|
// NewCmdConfigImages returns the "config images" command
|
||||||
func NewCmdConfigListImages(out io.Writer) *cobra.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{}
|
cfg := &kubeadmapiv1alpha1.MasterConfiguration{}
|
||||||
kubeadmapiv1alpha1.SetDefaults_MasterConfiguration(cfg)
|
kubeadmapiv1alpha1.SetDefaults_MasterConfiguration(cfg)
|
||||||
var cfgPath, featureGatesString string
|
var cfgPath, featureGatesString string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
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.",
|
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) {
|
Run: func(_ *cobra.Command, _ []string) {
|
||||||
if cfg.FeatureGates, err = features.NewFeatureGate(&features.InitFeatureGates, featureGatesString); err != nil {
|
if cfg.FeatureGates, err = features.NewFeatureGate(&features.InitFeatureGates, featureGatesString); err != nil {
|
||||||
kubeadmutil.CheckErr(err)
|
kubeadmutil.CheckErr(err)
|
||||||
}
|
}
|
||||||
listImages, err := NewListImages(cfgPath, cfg)
|
imagesList, err := NewImagesList(cfgPath, cfg)
|
||||||
kubeadmutil.CheckErr(err)
|
kubeadmutil.CheckErr(err)
|
||||||
kubeadmutil.CheckErr(listImages.Run(out))
|
kubeadmutil.CheckErr(imagesList.Run(out))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
AddListImagesConfigFlag(cmd.PersistentFlags(), cfg, &featureGatesString)
|
AddImagesListConfigFlags(cmd.PersistentFlags(), cfg, &featureGatesString)
|
||||||
AddListImagesFlags(cmd.PersistentFlags(), &cfgPath)
|
AddImagesListFlags(cmd.PersistentFlags(), &cfgPath)
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewListImages returns a "kubeadm images" command
|
// NewImagesList returns the underlying struct for the "kubeadm config images list" command
|
||||||
func NewListImages(cfgPath string, cfg *kubeadmapiv1alpha1.MasterConfiguration) (*ListImages, error) {
|
func NewImagesList(cfgPath string, cfg *kubeadmapiv1alpha1.MasterConfiguration) (*ImagesList, error) {
|
||||||
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
|
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not convert cfg to an internal cfg: %v", err)
|
return nil, fmt.Errorf("could not convert cfg to an internal cfg: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ListImages{
|
return &ImagesList{
|
||||||
cfg: internalcfg,
|
cfg: internalcfg,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListImages defines the struct used for "kubeadm images"
|
// ImagesList defines the struct used for "kubeadm config images list"
|
||||||
type ListImages struct {
|
type ImagesList struct {
|
||||||
cfg *kubeadmapi.MasterConfiguration
|
cfg *kubeadmapi.MasterConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run runs the images command and writes the result to the io.Writer passed in
|
// Run gets a list of images kubeadm expects to use and writes the result to the io.Writer passed in
|
||||||
func (i *ListImages) Run(out io.Writer) error {
|
func (i *ImagesList) Run(out io.Writer) error {
|
||||||
imgs := images.GetAllImages(i.cfg)
|
imgs := images.GetAllImages(i.cfg)
|
||||||
for _, img := range imgs {
|
for _, img := range imgs {
|
||||||
fmt.Fprintln(out, img)
|
fmt.Fprintln(out, img)
|
||||||
@ -258,8 +269,8 @@ func (i *ListImages) Run(out io.Writer) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddListImagesConfigFlag adds the flags that configure kubeadm
|
// AddImagesListConfigFlags adds the flags that configure kubeadm (and affect the images kubeadm will use)
|
||||||
func AddListImagesConfigFlag(flagSet *flag.FlagSet, cfg *kubeadmapiv1alpha1.MasterConfiguration, featureGatesString *string) {
|
func AddImagesListConfigFlags(flagSet *flag.FlagSet, cfg *kubeadmapiv1alpha1.MasterConfiguration, featureGatesString *string) {
|
||||||
flagSet.StringVar(
|
flagSet.StringVar(
|
||||||
&cfg.KubernetesVersion, "kubernetes-version", cfg.KubernetesVersion,
|
&cfg.KubernetesVersion, "kubernetes-version", cfg.KubernetesVersion,
|
||||||
`Choose a specific Kubernetes version for the control plane.`,
|
`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"))
|
"Options are:\n"+strings.Join(features.KnownFeatures(&features.InitFeatureGates), "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddListImagesFlags adds the flag that defines the location of the config file
|
// AddImagesListFlags adds the flag that defines the location of the config file
|
||||||
func AddListImagesFlags(flagSet *flag.FlagSet, cfgPath *string) {
|
func AddImagesListFlags(flagSet *flag.FlagSet, cfgPath *string) {
|
||||||
flagSet.StringVar(cfgPath, "config", *cfgPath, "Path to kubeadm config file.")
|
flagSet.StringVar(cfgPath, "config", *cfgPath, "Path to kubeadm config file.")
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ const (
|
|||||||
|
|
||||||
func TestNewCmdConfigListImages(t *testing.T) {
|
func TestNewCmdConfigListImages(t *testing.T) {
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
images := cmd.NewCmdConfigListImages(&output)
|
images := cmd.NewCmdConfigImagesList(&output)
|
||||||
images.Run(nil, nil)
|
images.Run(nil, nil)
|
||||||
actual := strings.Split(output.String(), "\n")
|
actual := strings.Split(output.String(), "\n")
|
||||||
if len(actual) != defaultNumberOfImages {
|
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 {
|
testcases := []struct {
|
||||||
name string
|
name string
|
||||||
expectedImageCount int
|
expectedImageCount int
|
||||||
@ -99,7 +99,7 @@ func TestListImagesRunWithCustomConfigPath(t *testing.T) {
|
|||||||
t.Fatalf("Failed writing a config file: %v", err)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Failed getting the kubeadm images command: %v", err)
|
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 {
|
testcases := []struct {
|
||||||
name string
|
name string
|
||||||
cfg kubeadmapiv1alpha1.MasterConfiguration
|
cfg kubeadmapiv1alpha1.MasterConfiguration
|
||||||
@ -153,7 +153,7 @@ func TestConfigListImagesRunWithoutPath(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
i, err := cmd.NewListImages("", &tc.cfg)
|
i, err := cmd.NewImagesList("", &tc.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("did not expect an error while creating the Images command: %v", err)
|
t.Fatalf("did not expect an error while creating the Images command: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -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_alpha_phase_upload-config.md
|
||||||
docs/admin/kubeadm_completion.md
|
docs/admin/kubeadm_completion.md
|
||||||
docs/admin/kubeadm_config.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.md
|
||||||
docs/admin/kubeadm_config_upload_from-file.md
|
docs/admin/kubeadm_config_upload_from-file.md
|
||||||
docs/admin/kubeadm_config_upload_from-flags.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-phase.1
|
||||||
docs/man/man1/kubeadm-alpha.1
|
docs/man/man1/kubeadm-alpha.1
|
||||||
docs/man/man1/kubeadm-completion.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-file.1
|
||||||
docs/man/man1/kubeadm-config-upload-from-flags.1
|
docs/man/man1/kubeadm-config-upload-from-flags.1
|
||||||
docs/man/man1/kubeadm-config-upload.1
|
docs/man/man1/kubeadm-config-upload.1
|
||||||
|
3
docs/man/man1/kubeadm-config-images-list.1
Normal file
3
docs/man/man1/kubeadm-config-images-list.1
Normal 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.
|
3
docs/man/man1/kubeadm-config-images.1
Normal file
3
docs/man/man1/kubeadm-config-images.1
Normal 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.
|
Loading…
Reference in New Issue
Block a user