Skip to content

Commit

Permalink
podman manifest exists
Browse files Browse the repository at this point in the history
Add podman manifest exists command with remote support.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Paul Holzinger authored and Achilleas Tzenetopoulos committed Jan 26, 2021
1 parent 01e62c7 commit 496180d
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/podman/manifest/exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package manifest

import (
"github.com/containers/podman/v2/cmd/podman/common"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/spf13/cobra"
)

var (
existsCmd = &cobra.Command{
Use: "exists MANIFEST",
Short: "Check if a manifest list exists in local storage",
Long: `If the manifest list exists in local storage, podman manifest exists exits with 0, otherwise the exit code will be 1.`,
Args: cobra.ExactArgs(1),
RunE: exists,
ValidArgsFunction: common.AutocompleteImages,
Example: "podman manifest exists mylist",
}
)

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: existsCmd,
Parent: manifestCmd,
})
}

func exists(cmd *cobra.Command, args []string) error {
found, err := registry.ImageEngine().ManifestExists(registry.GetContext(), args[0])
if err != nil {
return err
}
if !found.Value {
registry.SetExitCode(1)
}
return nil
}
2 changes: 2 additions & 0 deletions docs/source/manifest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Create and manipulate manifest lists and image indexes

:doc:`create <markdown/podman-manifest-create.1>` Create a manifest list or image index

:doc:`exists <markdown/podman-manifest-exists.1>` Check if the given manifest list exists in local storage

:doc:`inspect <markdown/podman-manifest-inspect.1>` Display a manifest list or image index

:doc:`push <markdown/podman-manifest-push.1>` Push a manifest list or image index to a registry
Expand Down
43 changes: 43 additions & 0 deletions docs/source/markdown/podman-manifest-exists.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
% podman-manifest-exists(1)

## NAME
podman\-manifest\-exists - Check if the given manifest list exists in local storage

## SYNOPSIS
**podman manifest exists** *manifest*

## DESCRIPTION
**podman manifest exists** checks if a manifest list exists on local storage. Podman will
return an exit code of `0` when the manifest is found. A `1` will be returned otherwise.
An exit code of `125` indicates there was another issue.


## OPTIONS

#### **--help**, **-h**

Print usage statement.

## EXAMPLE

Check if a manifest list called `list1` exists (the manifest list does actually exist).
```
$ podman manifest exists list1
$ echo $?
0
$
```

Check if an manifest called `mylist` exists (the manifest list does not actually exist).
```
$ podman manifest exists mylist
$ echo $?
1
$
```

## SEE ALSO
podman(1), podman-manifest-create(1), podman-manifest-remove(1)

## HISTORY
January 2021, Originally compiled by Paul Holzinger `<[email protected]>`
1 change: 1 addition & 0 deletions docs/source/markdown/podman-manifest.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The `podman manifest` command provides subcommands which can be used to:
| add | [podman-manifest-add(1)](podman-manifest-add.1.md) | Add an image to a manifest list or image index. |
| annotate | [podman-manifest-annotate(1)](podman-manifest-annotate.1.md) | Add or update information about an entry in a manifest list or image index. |
| create | [podman-manifest-create(1)](podman-manifest-create.1.md) | Create a manifest list or image index. |
| exists | [podman-manifest-exists(1)](podman-manifest-exists.1.md) | Check if the given manifest list exists in local storage |
| inspect | [podman-manifest-inspect(1)](podman-manifest-inspect.1.md) | Display a manifest list or image index. |
| push | [podman-manifest-push(1)](podman-manifest-push.1.md) | Push a manifest list or image index to a registry. |
| remove | [podman-manifest-remove(1)](podman-manifest-remove.1.md) | Remove an image from a manifest list or image index. |
Expand Down
9 changes: 9 additions & 0 deletions libpod/image/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func (i *Image) InspectManifest() (*manifest.Schema2List, error) {
return list.Docker(), nil
}

// ExistsManifest checks if a manifest list exists
func (i *Image) ExistsManifest() (bool, error) {
_, err := i.getManifestList()
if err != nil {
return false, err
}
return true, nil
}

// RemoveManifest removes the given digest from the manifest list.
func (i *Image) RemoveManifest(d digest.Digest) (string, error) {
list, err := i.getManifestList()
Expand Down
18 changes: 18 additions & 0 deletions pkg/api/handlers/libpod/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ func ManifestCreate(w http.ResponseWriter, r *http.Request) {
utils.WriteResponse(w, http.StatusOK, handlers.IDResponse{ID: manID})
}

// ExistsManifest check if a manifest list exists
func ExistsManifest(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value("runtime").(*libpod.Runtime)
name := utils.GetName(r)

ic := abi.ImageEngine{Libpod: runtime}
report, err := ic.ManifestExists(r.Context(), name)
if err != nil {
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err)
return
}
if !report.Value {
utils.Error(w, "manifest not found", http.StatusNotFound, errors.New("manifest not found"))
return
}
utils.WriteResponse(w, http.StatusNoContent, "")
}

func ManifestInspect(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value("runtime").(*libpod.Runtime)
name := utils.GetName(r)
Expand Down
20 changes: 20 additions & 0 deletions pkg/api/server/register_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ func (s *APIServer) registerManifestHandlers(r *mux.Router) error {
// 500:
// $ref: "#/responses/InternalError"
r.Handle(VersionedPath("/libpod/manifests/create"), s.APIHandler(libpod.ManifestCreate)).Methods(http.MethodPost)
// swagger:operation GET /libpod/manifests/{name}/exists manifests Exists
// ---
// summary: Exists
// description: Check if manifest list exists
// parameters:
// - in: path
// name: name
// type: string
// required: true
// description: the name of the manifest list
// produces:
// - application/json
// responses:
// 204:
// description: manifest list exists
// 404:
// $ref: '#/responses/NoSuchManifest'
// 500:
// $ref: '#/responses/InternalError'
r.Handle(VersionedPath("/libpod/manifests/{name}/exists"), s.APIHandler(libpod.ExistsManifest)).Methods(http.MethodGet)
// swagger:operation GET /libpod/manifests/{name:.*}/json manifests Inspect
// ---
// summary: Inspect
Expand Down
13 changes: 13 additions & 0 deletions pkg/bindings/manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ func Create(ctx context.Context, names, images []string, options *CreateOptions)
return idr.ID, response.Process(&idr)
}

// Exists returns true if a given maifest list exists
func Exists(ctx context.Context, name string, options *ExistsOptions) (bool, error) {
conn, err := bindings.GetClient(ctx)
if err != nil {
return false, err
}
response, err := conn.DoRequest(nil, http.MethodGet, "/manifests/%s/exists", nil, nil, name)
if err != nil {
return false, err
}
return response.IsSuccess(), nil
}

// Inspect returns a manifest list for a given name.
func Inspect(ctx context.Context, name string, options *InspectOptions) (*manifest.Schema2List, error) {
var list manifest.Schema2List
Expand Down
6 changes: 6 additions & 0 deletions pkg/bindings/manifests/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ type CreateOptions struct {
All *bool
}

//go:generate go run ../generator/generator.go ExistsOptions
// ExistsOptions are optional options for checking
// if a manifest list exists
type ExistsOptions struct {
}

//go:generate go run ../generator/generator.go AddOptions
// AddOptions are optional options for adding manifests
type AddOptions struct {
Expand Down
88 changes: 88 additions & 0 deletions pkg/bindings/manifests/types_exists_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package manifests

import (
"net/url"
"reflect"
"strconv"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

/*
This file is generated automatically by go generate. Do not edit.
*/

// Changed
func (o *ExistsOptions) Changed(fieldName string) bool {
r := reflect.ValueOf(o)
value := reflect.Indirect(r).FieldByName(fieldName)
return !value.IsNil()
}

// ToParams
func (o *ExistsOptions) ToParams() (url.Values, error) {
params := url.Values{}
if o == nil {
return params, nil
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
s := reflect.ValueOf(o)
if reflect.Ptr == s.Kind() {
s = s.Elem()
}
sType := s.Type()
for i := 0; i < s.NumField(); i++ {
fieldName := sType.Field(i).Name
if !o.Changed(fieldName) {
continue
}
fieldName = strings.ToLower(fieldName)
f := s.Field(i)
if reflect.Ptr == f.Kind() {
f = f.Elem()
}
switch f.Kind() {
case reflect.Bool:
params.Set(fieldName, strconv.FormatBool(f.Bool()))
case reflect.String:
params.Set(fieldName, f.String())
case reflect.Int, reflect.Int64:
// f.Int() is always an int64
params.Set(fieldName, strconv.FormatInt(f.Int(), 10))
case reflect.Uint, reflect.Uint64:
// f.Uint() is always an uint64
params.Set(fieldName, strconv.FormatUint(f.Uint(), 10))
case reflect.Slice:
typ := reflect.TypeOf(f.Interface()).Elem()
switch typ.Kind() {
case reflect.String:
sl := f.Slice(0, f.Len())
s, ok := sl.Interface().([]string)
if !ok {
return nil, errors.New("failed to convert to string slice")
}
for _, val := range s {
params.Add(fieldName, val)
}
default:
return nil, errors.Errorf("unknown slice type %s", f.Kind().String())
}
case reflect.Map:
lowerCaseKeys := make(map[string][]string)
iter := f.MapRange()
for iter.Next() {
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string)

}
s, err := json.MarshalToString(lowerCaseKeys)
if err != nil {
return nil, err
}

params.Set(fieldName, s)
}
}
return params, nil
}
1 change: 1 addition & 0 deletions pkg/domain/entities/engine_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ImageEngine interface {
Unmount(ctx context.Context, images []string, options ImageUnmountOptions) ([]*ImageUnmountReport, error)
Untag(ctx context.Context, nameOrID string, tags []string, options ImageUntagOptions) error
ManifestCreate(ctx context.Context, names, images []string, opts ManifestCreateOptions) (string, error)
ManifestExists(ctx context.Context, name string) (*BoolReport, error)
ManifestInspect(ctx context.Context, name string) ([]byte, error)
ManifestAdd(ctx context.Context, opts ManifestAddOptions) (string, error)
ManifestAnnotate(ctx context.Context, names []string, opts ManifestAnnotateOptions) (string, error)
Expand Down
12 changes: 12 additions & 0 deletions pkg/domain/infra/abi/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func (ir *ImageEngine) ManifestCreate(ctx context.Context, names, images []strin
return imageID, err
}

// ManifestExists checks if a manifest list with the given name exists in local storage
func (ir *ImageEngine) ManifestExists(ctx context.Context, name string) (*entities.BoolReport, error) {
if image, err := ir.Libpod.ImageRuntime().NewFromLocal(name); err == nil {
exists, err := image.ExistsManifest()
if err != nil && errors.Cause(err) != buildahManifests.ErrManifestTypeNotSupported {
return nil, err
}
return &entities.BoolReport{Value: exists}, nil
}
return &entities.BoolReport{Value: false}, nil
}

// ManifestInspect returns the content of a manifest list or image
func (ir *ImageEngine) ManifestInspect(ctx context.Context, name string) ([]byte, error) {
if newImage, err := ir.Libpod.ImageRuntime().NewFromLocal(name); err == nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/domain/infra/tunnel/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ func (ir *ImageEngine) ManifestCreate(ctx context.Context, names, images []strin
return imageID, err
}

// ManifestExists checks if a manifest list with the given name exists
func (ir *ImageEngine) ManifestExists(ctx context.Context, name string) (*entities.BoolReport, error) {
exists, err := manifests.Exists(ir.ClientCtx, name, nil)
if err != nil {
return nil, err
}
return &entities.BoolReport{Value: exists}, nil
}

// ManifestInspect returns contents of manifest list with given name
func (ir *ImageEngine) ManifestInspect(ctx context.Context, name string) ([]byte, error) {
list, err := manifests.Inspect(ir.ClientCtx, name, nil)
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,19 @@ var _ = Describe("Podman manifest", func() {
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Not(Equal(0)))
})

It("podman manifest exists", func() {
manifestList := "manifest-list"
session := podmanTest.Podman([]string{"manifest", "create", manifestList})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())

session = podmanTest.Podman([]string{"manifest", "exists", manifestList})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))

session = podmanTest.Podman([]string{"manifest", "exists", "no-manifest"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(1))
})
})

0 comments on commit 496180d

Please sign in to comment.