Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

podman Image diff between two images #10667

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cmd/podman/common/diffChanges.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ func ChangesToJSON(diffs *entities.DiffReport) error {
return errors.Errorf("output kind %q not recognized", row.Kind)
}
}

// Pull in configured json library
enc := json.NewEncoder(os.Stdout)
return enc.Encode(body)
output, err := json.MarshalIndent(body, "", " ")
if err != nil {
return err
}
fmt.Fprintln(os.Stdout, string(output))
return nil
}

func ChangesToTable(diffs *entities.DiffReport) error {
Expand Down
8 changes: 7 additions & 1 deletion cmd/podman/images/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
// podman container _inspect_
diffCmd = &cobra.Command{
Use: "diff [options] IMAGE",
Args: cobra.ExactArgs(1),
Args: cobra.RangeArgs(1, 2),
Short: "Inspect changes to the image's file systems",
Long: `Displays changes to the image's filesystem. The image will be compared to its parent layer.`,
RunE: diff,
Expand Down Expand Up @@ -48,6 +48,12 @@ func diff(cmd *cobra.Command, args []string) error {
return errors.New("image diff does not support --latest")
}

if len(args) == 2 {
diffOpts.OtherImg = args[1]
} else {
diffOpts.OtherImg = ""
}

results, err := registry.ImageEngine().Diff(registry.GetContext(), args[0], *diffOpts)
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion pkg/bindings/images/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ func Diff(ctx context.Context, nameOrID string, options *DiffOptions) ([]archive
if err != nil {
return nil, err
}
params, err := options.ToParams()
if err != nil {
return nil, err
}
response, err := conn.DoRequest(nil, http.MethodGet, "/images/%s/changes", params, nil, nameOrID)

response, err := conn.DoRequest(nil, http.MethodGet, "/images/%s/changes", nil, nil, nameOrID)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/bindings/images/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type RemoveOptions struct {
//go:generate go run ../generator/generator.go DiffOptions
// DiffOptions are optional options image diffs
type DiffOptions struct {
OtherImg string
}

//go:generate go run ../generator/generator.go ListOptions
Expand Down
16 changes: 16 additions & 0 deletions pkg/bindings/images/types_diff_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ func (o *DiffOptions) Changed(fieldName string) bool {
func (o *DiffOptions) ToParams() (url.Values, error) {
return util.ToParams(o)
}

// WithOtherImg
func (o *DiffOptions) WithOtherImg(value string) *DiffOptions {
v := &value
o.OtherImg = v
return o
}

// GetOtherImg
func (o *DiffOptions) GetOtherImg() string {
var otherImg string
if o.OtherImg == nil {
return otherImg
}
return *o.OtherImg
}
1 change: 1 addition & 0 deletions pkg/domain/entities/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type DiffOptions struct {
Format string `json:",omitempty"` // CLI only
Latest bool `json:",omitempty"` // API and CLI, only supported by containers
Archive bool `json:",omitempty"` // CLI only
OtherImg string
}

// DiffReport provides changes for object
Expand Down
14 changes: 12 additions & 2 deletions pkg/domain/infra/abi/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,22 @@ func (ir *ImageEngine) Import(ctx context.Context, options entities.ImageImportO

return &entities.ImageImportReport{Id: imageID}, nil
}
func (ir *ImageEngine) Diff(_ context.Context, nameOrID string, options entities.DiffOptions) (*entities.DiffReport, error) {
var (
parent string
child string
)
if options.OtherImg == "" {
parent, child = "", nameOrID
} else {
parent, child = nameOrID, options.OtherImg
}

func (ir *ImageEngine) Diff(_ context.Context, nameOrID string, _ entities.DiffOptions) (*entities.DiffReport, error) {
changes, err := ir.Libpod.GetDiff("", nameOrID)
changes, err := ir.Libpod.GetDiff(parent, child)
if err != nil {
return nil, err
}

return &entities.DiffReport{Changes: changes}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/domain/infra/tunnel/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,9 @@ func (ir *ImageEngine) Save(ctx context.Context, nameOrID string, tags []string,
}

// Diff reports the changes to the given image
func (ir *ImageEngine) Diff(ctx context.Context, nameOrID string, _ entities.DiffOptions) (*entities.DiffReport, error) {
func (ir *ImageEngine) Diff(ctx context.Context, nameOrID string, opts entities.DiffOptions) (*entities.DiffReport, error) {
options := new(images.DiffOptions)
options.OtherImg = opts.OtherImg
changes, err := images.Diff(ir.ClientCtx, nameOrID, options)
if err != nil {
return nil, err
Expand Down
28 changes: 28 additions & 0 deletions test/e2e/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,32 @@ var _ = Describe("Podman diff", func() {
Expect(session.LineInOutputContains("A /tmp/diff-test")).To(BeTrue())
Expect(session.ExitCode()).To(Equal(0))
})

It("podman image diff of image", func() {
session := podmanTest.Podman([]string{"image", "diff", ALPINE, BB})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(len(session.OutputToStringArray())).To(BeNumerically(">", 0))
})

It("podman image diff of single image", func() {
session := podmanTest.Podman([]string{"image", "diff", BB})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(len(session.OutputToStringArray())).To(BeNumerically(">", 0))
})

It("podman diff bogus image", func() {
session := podmanTest.Podman([]string{"image", "diff", "1234", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(125))
})

It("podman image diff of the same image", func() {
session := podmanTest.Podman([]string{"image", "diff", ALPINE, ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(len(session.OutputToStringArray())).To(BeNumerically("==", 0))
})

})