Skip to content

Commit

Permalink
Refactor image client to not be E2E specific
Browse files Browse the repository at this point in the history
The existing client for handling plugin images assumed that the images
were for the e2e plugin. We are trying to add support for other images
so we need to make the image client more generic. This change refactors
the image client to deal with image names rather than the internal
Kubernetes image `Config` type.

Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
  • Loading branch information
zubron committed Dec 19, 2019
1 parent e41ac6e commit d1ee549
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 204 deletions.
252 changes: 100 additions & 152 deletions cmd/sonobuoy/app/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"fmt"
"os"

"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
"github.com/vmware-tanzu/sonobuoy/pkg/image"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
"github.com/vmware-tanzu/sonobuoy/pkg/image"
)

var imagesflags imagesFlags
Expand All @@ -45,8 +45,13 @@ func NewCmdImages() *cobra.Command {
cmd := &cobra.Command{
Use: "images",
Short: "Manage images used in a plugin. Supported plugins are: 'e2e'",
Run: listImages,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if err := listImages(cmd, args); err != nil {
errlog.LogError(err)
os.Exit(1)
}
},
Args: cobra.ExactArgs(0),
}

AddKubeconfigFlag(&imagesflags.kubeconfig, cmd.Flags())
Expand All @@ -56,8 +61,15 @@ func NewCmdImages() *cobra.Command {
pullCmd := &cobra.Command{
Use: "pull",
Short: "Pulls images to local docker client for a specific plugin",
Run: pullImages,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if errs := pullImages(cmd, args); len(errs) > 0 {
for _, err := range errs {
errlog.LogError(err)
}
os.Exit(1)
}
},
Args: cobra.ExactArgs(0),
}
AddE2ERegistryConfigFlag(&imagesflags.e2eRegistryConfig, pullCmd.Flags())
AddKubeconfigFlag(&imagesflags.kubeconfig, pullCmd.Flags())
Expand All @@ -67,18 +79,31 @@ func NewCmdImages() *cobra.Command {
downloadCmd := &cobra.Command{
Use: "download",
Short: "Saves downloaded images from local docker client to a tar file",
Run: downloadImages,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if err := downloadImages(cmd, args); err != nil {
errlog.LogError(err)
os.Exit(1)
}
},
Args: cobra.ExactArgs(0),
}
AddE2ERegistryConfigFlag(&imagesflags.e2eRegistryConfig, downloadCmd.Flags())
AddKubeconfigFlag(&imagesflags.kubeconfig, downloadCmd.Flags())
AddPluginFlag(&imagesflags.plugin, downloadCmd.Flags())

// Push command
pushCmd := &cobra.Command{
Use: "push",
Short: "Pushes images to docker registry for a specific plugin",
Run: pushImages,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if errs := pushImages(cmd, args); len(errs) > 0 {
for _, err := range errs {
errlog.LogError(err)
}
os.Exit(1)
}
},
Args: cobra.ExactArgs(0),
}
AddE2ERegistryConfigFlag(&imagesflags.e2eRegistryConfig, pushCmd.Flags())
AddKubeconfigFlag(&imagesflags.kubeconfig, pushCmd.Flags())
Expand All @@ -89,8 +114,15 @@ func NewCmdImages() *cobra.Command {
deleteCmd := &cobra.Command{
Use: "delete",
Short: "Deletes all images downloaded to local docker client",
Run: deleteImages,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if errs := deleteImages(cmd, args); len(errs) > 0 {
for _, err := range errs {
errlog.LogError(err)
}
os.Exit(1)
}
},
Args: cobra.ExactArgs(0),
}
AddE2ERegistryConfigFlag(&imagesflags.e2eRegistryConfig, deleteCmd.Flags())
AddKubeconfigFlag(&imagesflags.kubeconfig, deleteCmd.Flags())
Expand All @@ -104,228 +136,144 @@ func NewCmdImages() *cobra.Command {
return cmd
}

func listImages(cmd *cobra.Command, args []string) {

switch imagesflags.plugin {
case "e2e":

if len(imagesflags.e2eRegistryConfig) > 0 {
// Check if the e2e file exists
if _, err := os.Stat(imagesflags.e2eRegistryConfig); err != nil {
errlog.LogError(errors.Errorf("file does not exist or cannot be opened: %v", imagesflags.e2eRegistryConfig))
os.Exit(1)
}
}
func getClusterVersion(kubeconfig Kubeconfig) (string, error) {
sbc, err := getSonobuoyClientFromKubecfg(imagesflags.kubeconfig)
if err != nil {
return "", errors.Wrap(err, "couldn't create sonobuoy client")
}

sbc, err := getSonobuoyClientFromKubecfg(imagesflags.kubeconfig)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
}
version, err := sbc.Version()
if err != nil {
return "", errors.Wrap(err, "couldn't get Sonobuoy client")
}

version, err := sbc.Version()
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get Sonobuoy client"))
os.Exit(1)
}
return version, nil
}

// Get list of images that match the version
registry, err := image.NewRegistryList("", version)
func listImages(cmd *cobra.Command, args []string) error {
switch imagesflags.plugin {
case "e2e":
version, err := getClusterVersion(imagesflags.kubeconfig)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't init Registry List"))
os.Exit(1)
return errors.Wrap(err, "failed to get cluster version")
}

images, err := registry.GetImageConfigs()
images, err := image.GetE2EImages(imagesflags.e2eRegistryConfig, version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get images for version"))
os.Exit(1)
return errors.Wrap(err, "couldn't get images")
}

for _, v := range images {
fmt.Println(v.GetE2EImage())
for _, image := range images {
fmt.Println(image)
}
default:
errlog.LogError(errors.Errorf("Unsupported plugin: %v", imagesflags.plugin))
os.Exit(1)
return errors.Errorf("Unsupported plugin: %v", imagesflags.plugin)
}

return nil
}

func pullImages(cmd *cobra.Command, args []string) {
func pullImages(cmd *cobra.Command, args []string) []error {
switch imagesflags.plugin {
case "e2e":

sbc, err := getSonobuoyClientFromKubecfg(imagesflags.kubeconfig)
version, err := getClusterVersion(imagesflags.kubeconfig)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
return []error{errors.Wrap(err, "failed to get cluster version")}
}

version, err := sbc.Version()
images, err := image.GetE2EImages(imagesflags.e2eRegistryConfig, version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get Sonobuoy client"))
os.Exit(1)
}

upstreamImages, err := image.GetImages(imagesflags.e2eRegistryConfig, version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't init upstream registry list"))
os.Exit(1)
return []error{errors.Wrap(err, "couldn't get images")}
}

// Init client
imageClient := image.NewImageClient()

// Pull all images
errs := imageClient.PullImages(upstreamImages, numDockerRetries)
for _, err := range errs {
errlog.LogError(err)
}

if len(errs) > 0 {
os.Exit(1)
}

return imageClient.PullImages(images, numDockerRetries)
default:
errlog.LogError(errors.Errorf("Unsupported plugin: %v", imagesflags.plugin))
os.Exit(1)
return []error{errors.Errorf("Unsupported plugin: %v", imagesflags.plugin)}
}

}

func downloadImages(cmd *cobra.Command, args []string) {
func downloadImages(cmd *cobra.Command, args []string) error {
switch imagesflags.plugin {
case "e2e":

sbc, err := getSonobuoyClientFromKubecfg(imagesflags.kubeconfig)
version, err := getClusterVersion(imagesflags.kubeconfig)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
return errors.Wrap(err, "failed to get cluster version")
}

version, err := sbc.Version()
images, err := image.GetE2EImages(imagesflags.e2eRegistryConfig, version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get Sonobuoy client"))
os.Exit(1)
}

upstreamImages, err := image.GetImages(defaultE2ERegistries, version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't init upstream registry list"))
os.Exit(1)
}

images := []string{}
for _, v := range upstreamImages {
images = append(images, v.GetE2EImage())
return errors.Wrap(err, "couldn't get images")
}

// Init client
imageClient := image.NewImageClient()

fileName, err := imageClient.DownloadImages(images, version)
if err != nil {
errlog.LogError(err)
os.Exit(1)
return err
}

fmt.Println(fileName)

default:
errlog.LogError(errors.Errorf("Unsupported plugin: %v", imagesflags.plugin))
os.Exit(1)
return errors.Errorf("Unsupported plugin: %v", imagesflags.plugin)
}
}

func pushImages(cmd *cobra.Command, args []string) {
return nil
}

func pushImages(cmd *cobra.Command, args []string) []error {
switch imagesflags.plugin {
case "e2e":

if len(imagesflags.e2eRegistryConfig) > 0 {
// Check if the e2e file exists
if _, err := os.Stat(imagesflags.e2eRegistryConfig); err != nil {
errlog.LogError(errors.Errorf("file does not exist or cannot be opened: %v", imagesflags.e2eRegistryConfig))
os.Exit(1)
}
}

// Check if the e2e file exists
if _, err := os.Stat(imagesflags.e2eRegistryConfig); os.IsNotExist(err) {
errlog.LogError(errors.Errorf("file does not exist or cannot be opened: %v", imagesflags.e2eRegistryConfig))
os.Exit(1)
}

sbc, err := getSonobuoyClientFromKubecfg(imagesflags.kubeconfig)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
}

version, err := sbc.Version()
version, err := getClusterVersion(imagesflags.kubeconfig)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get Sonobuoy client"))
os.Exit(1)
return []error{errors.Wrap(err, "failed to get cluster version")}
}

upstreamImages, err := image.GetImages("", version)
upstreamImages, err := image.GetE2EImages("", version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't init upstream registry list"))
os.Exit(1)
return []error{errors.Wrap(err, "couldn't get images")}
}

privateImages, err := image.GetImages(imagesflags.e2eRegistryConfig, version)
privateImages, err := image.GetE2EImages(imagesflags.e2eRegistryConfig, version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't init upstream registry list"))
os.Exit(1)
return []error{errors.Wrap(err, "couldn't get images")}
}

// Init client
imageClient := image.NewImageClient()

// Push all images
errs := imageClient.PushImages(upstreamImages, privateImages, numDockerRetries)
for _, err := range errs {
errlog.LogError(err)
}

return imageClient.PushImages(upstreamImages, privateImages, numDockerRetries)
default:
errlog.LogError(errors.Errorf("Unsupported plugin: %v", imagesflags.plugin))
os.Exit(1)
return []error{errors.Errorf("Unsupported plugin: %v", imagesflags.plugin)}
}

}

func deleteImages(cmd *cobra.Command, args []string) {
func deleteImages(cmd *cobra.Command, args []string) []error {
switch imagesflags.plugin {
case "e2e":
sbc, err := getSonobuoyClientFromKubecfg(imagesflags.kubeconfig)
version, err := getClusterVersion(imagesflags.kubeconfig)
if err != nil {
errlog.LogError(errors.Wrap(err, "could not create sonobuoy client"))
os.Exit(1)
return []error{errors.Wrap(err, "failed to get cluster version")}
}

version, err := sbc.Version()
images, err := image.GetE2EImages(imagesflags.e2eRegistryConfig, version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't get Sonobuoy client"))
os.Exit(1)
}

images, err := image.GetImages(imagesflags.e2eRegistryConfig, version)
if err != nil {
errlog.LogError(errors.Wrap(err, "couldn't init registry list"))
os.Exit(1)
return []error{errors.Wrap(err, "couldn't get images")}
}

// Init client
imageClient := image.NewImageClient()

errs := imageClient.DeleteImages(images, numDockerRetries)
for _, err := range errs {
errlog.LogError(err)
}
return imageClient.DeleteImages(images, numDockerRetries)

default:
errlog.LogError(errors.Errorf("Unsupported plugin: %v", imagesflags.plugin))
os.Exit(1)
return []error{errors.Errorf("Unsupported plugin: %v", imagesflags.plugin)}
}
}
Loading

0 comments on commit d1ee549

Please sign in to comment.