Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 1db0397

Browse files
committed
remove ImagePrepper in favor of processing individual preppers directly
1 parent ee67c3e commit 1db0397

13 files changed

+133
-128
lines changed

cmd/analyze.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func checkAnalyzeArgNum(args []string) error {
5656
return nil
5757
}
5858

59-
func analyzeImage(imageArg string, analyzerArgs []string) error {
59+
func analyzeImage(imageName string, analyzerArgs []string) error {
6060
analyzeTypes, err := differs.GetAnalyzers(analyzerArgs)
6161
if err != nil {
6262
return err
@@ -67,11 +67,13 @@ func analyzeImage(imageArg string, analyzerArgs []string) error {
6767
return fmt.Errorf("Error getting docker client: %s", err)
6868
}
6969
defer cli.Close()
70-
ip := pkgutil.ImagePrepper{
71-
Source: imageArg,
72-
Client: cli,
70+
71+
prepper, err := getPrepperForImage(imageName)
72+
if err != nil {
73+
return err
7374
}
74-
image, err := ip.GetImage()
75+
76+
image, err := prepper.GetImage()
7577

7678
if !save {
7779
defer pkgutil.CleanupImage(image)

cmd/diff.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,16 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
8080
for imageArg := range imageMap {
8181
go func(imageName string, imageMap map[string]*pkgutil.Image) {
8282
defer wg.Done()
83-
ip := pkgutil.ImagePrepper{
84-
Source: imageName,
85-
Client: cli,
83+
84+
prepper, err := getPrepperForImage(imageName)
85+
if err != nil {
86+
glog.Error(err)
87+
return
8688
}
87-
image, err := ip.GetImage()
89+
image, err := prepper.GetImage()
8890
imageMap[imageName] = &image
8991
if err != nil {
90-
glog.Errorf("Diff may be inaccurate: %s", err.Error())
92+
glog.Warningf("Diff may be inaccurate: %s", err)
9193
}
9294
}(imageArg, imageMap)
9395
}
@@ -109,7 +111,6 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
109111
if save {
110112
glog.Infof("Images were saved at %s and %s", imageMap[image1Arg].FSPath,
111113
imageMap[image2Arg].FSPath)
112-
113114
}
114115
return nil
115116
}

cmd/root.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ package cmd
1818

1919
import (
2020
"context"
21+
"errors"
2122
goflag "flag"
2223
"fmt"
2324
"sort"
2425
"strings"
2526

2627
"github.com/GoogleCloudPlatform/container-diff/differs"
28+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
2729
"github.com/GoogleCloudPlatform/container-diff/util"
2830
"github.com/docker/docker/client"
2931
"github.com/golang/glog"
@@ -37,6 +39,11 @@ var types string
3739

3840
type validatefxn func(args []string) error
3941

42+
const (
43+
DaemonPrefix = "daemon://"
44+
RemotePrefix = "remote://"
45+
)
46+
4047
var RootCmd = &cobra.Command{
4148
Use: "container-diff",
4249
Short: "container-diff is a tool for analyzing and comparing container images",
@@ -92,7 +99,7 @@ func validateArgs(args []string, validatefxns ...validatefxn) error {
9299

93100
func checkIfValidAnalyzer(flagtypes string) error {
94101
if flagtypes == "" {
95-
return nil
102+
return errors.New("Please provide at least one analyzer to run")
96103
}
97104
analyzers := strings.Split(flagtypes, ",")
98105
for _, name := range analyzers {
@@ -103,6 +110,30 @@ func checkIfValidAnalyzer(flagtypes string) error {
103110
return nil
104111
}
105112

113+
func getPrepperForImage(image string) (pkgutil.Prepper, error) {
114+
cli, err := client.NewEnvClient()
115+
if err != nil {
116+
return nil, err
117+
}
118+
if pkgutil.IsTar(image) {
119+
return pkgutil.TarPrepper{
120+
Source: image,
121+
Client: cli,
122+
}, nil
123+
124+
} else if strings.HasPrefix(image, DaemonPrefix) {
125+
return pkgutil.DaemonPrepper{
126+
Source: strings.Replace(image, DaemonPrefix, "", -1),
127+
Client: cli,
128+
}, nil
129+
}
130+
// either has remote prefix or has no prefix, in which case we force remote
131+
return pkgutil.CloudPrepper{
132+
Source: strings.Replace(image, RemotePrefix, "", -1),
133+
Client: cli,
134+
}, nil
135+
}
136+
106137
func init() {
107138
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
108139
}

pkg/util/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go_library(
1717
deps = [
1818
"//vendor/github.com/containers/image/docker:go_default_library",
1919
"//vendor/github.com/containers/image/docker/daemon:go_default_library",
20+
"//vendor/github.com/containers/image/docker/reference:go_default_library",
2021
"//vendor/github.com/containers/image/docker/tarfile:go_default_library",
2122
"//vendor/github.com/containers/image/pkg/compression:go_default_library",
2223
"//vendor/github.com/containers/image/types:go_default_library",

pkg/util/cloud_prepper.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,26 @@ limitations under the License.
1717
package util
1818

1919
import (
20-
"regexp"
21-
2220
"github.com/containers/image/docker"
21+
"github.com/docker/docker/client"
2322
)
2423

2524
// CloudPrepper prepares images sourced from a Cloud registry
2625
type CloudPrepper struct {
27-
ImagePrepper
26+
Source string
27+
Client *client.Client
2828
}
2929

3030
func (p CloudPrepper) Name() string {
3131
return "Cloud Registry"
3232
}
3333

3434
func (p CloudPrepper) GetSource() string {
35-
return p.ImagePrepper.Source
35+
return p.Source
3636
}
3737

38-
func (p CloudPrepper) SupportsImage() bool {
39-
pattern := regexp.MustCompile("^.+/.+(:.+){0,1}$")
40-
image := p.ImagePrepper.Source
41-
if exp := pattern.FindString(image); exp != image || CheckTar(image) {
42-
return false
43-
}
44-
return true
38+
func (p CloudPrepper) GetImage() (Image, error) {
39+
return getImage(p)
4540
}
4641

4742
func (p CloudPrepper) GetFileSystem() (string, error) {

pkg/util/daemon_prepper.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,28 @@ package util
1818

1919
import (
2020
"context"
21-
"regexp"
2221

2322
"github.com/containers/image/docker/daemon"
23+
24+
"github.com/docker/docker/client"
2425
"github.com/golang/glog"
2526
)
2627

2728
type DaemonPrepper struct {
28-
ImagePrepper
29+
Source string
30+
Client *client.Client
2931
}
3032

3133
func (p DaemonPrepper) Name() string {
3234
return "Local Daemon"
3335
}
3436

3537
func (p DaemonPrepper) GetSource() string {
36-
return p.ImagePrepper.Source
38+
return p.Source
3739
}
3840

39-
func (p DaemonPrepper) SupportsImage() bool {
40-
pattern := regexp.MustCompile("[a-z|0-9]{12}")
41-
if exp := pattern.FindString(p.ImagePrepper.Source); exp != p.ImagePrepper.Source {
42-
return false
43-
}
44-
return true
41+
func (p DaemonPrepper) GetImage() (Image, error) {
42+
return getImage(p)
4543
}
4644

4745
func (p DaemonPrepper) GetFileSystem() (string, error) {

pkg/util/image_prep_utils.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"archive/tar"
2121
"encoding/json"
2222
"errors"
23+
"fmt"
2324
"io/ioutil"
2425
"os"
2526
"path/filepath"
@@ -30,10 +31,12 @@ import (
3031
"github.com/golang/glog"
3132
)
3233

33-
var orderedPreppers = []func(ip ImagePrepper) Prepper{
34-
func(ip ImagePrepper) Prepper { return DaemonPrepper{ImagePrepper: ip} },
35-
func(ip ImagePrepper) Prepper { return CloudPrepper{ImagePrepper: ip} },
36-
func(ip ImagePrepper) Prepper { return TarPrepper{ImagePrepper: ip} },
34+
type Prepper interface {
35+
Name() string
36+
GetConfig() (ConfigSchema, error)
37+
GetFileSystem() (string, error)
38+
GetImage() (Image, error)
39+
GetSource() string
3740
}
3841

3942
type Image struct {
@@ -55,6 +58,27 @@ type ConfigSchema struct {
5558
History []ImageHistoryItem `json:"history"`
5659
}
5760

61+
func getImage(p Prepper) (Image, error) {
62+
glog.Infof("Retrieving image %s from source %s", p.GetSource(), p.Name())
63+
imgPath, err := p.GetFileSystem()
64+
if err != nil {
65+
return Image{}, err
66+
}
67+
68+
config, err := p.GetConfig()
69+
if err != nil {
70+
glog.Error("Error retrieving History: ", err)
71+
}
72+
73+
glog.Infof("Finished prepping image %s", p.GetSource())
74+
return Image{
75+
Source: p.GetSource(),
76+
FSPath: imgPath,
77+
Config: config,
78+
}, nil
79+
return Image{}, fmt.Errorf("Could not retrieve image %s from source", p.GetSource())
80+
}
81+
5882
func getImageFromTar(tarPath string) (string, error) {
5983
glog.Info("Extracting image tar to obtain image file system")
6084
path := strings.TrimSuffix(tarPath, filepath.Ext(tarPath))

pkg/util/image_prepper.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

pkg/util/tar_prepper.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@ import (
2525
"strings"
2626

2727
"github.com/containers/image/docker/tarfile"
28+
"github.com/docker/docker/client"
2829
"github.com/golang/glog"
2930
)
3031

3132
type TarPrepper struct {
32-
ImagePrepper
33+
Source string
34+
Client *client.Client
3335
}
3436

3537
func (p TarPrepper) Name() string {
3638
return "Tar Archive"
3739
}
3840

3941
func (p TarPrepper) GetSource() string {
40-
return p.ImagePrepper.Source
42+
return p.Source
4143
}
4244

43-
func (p TarPrepper) SupportsImage() bool {
44-
return IsTar(p.ImagePrepper.Source)
45+
func (p TarPrepper) GetImage() (Image, error) {
46+
return getImage(p)
4547
}
4648

4749
func (p TarPrepper) GetFileSystem() (string, error) {

0 commit comments

Comments
 (0)