mirror of
https://github.com/containers/podman.git
synced 2025-10-15 02:06:42 +08:00
fix podman container exists and diff for storage containers
Current these commands only check if a container exists in libpod. With this fix, the commands will also check if they are in containers/storage. This allows users to look at differences within a buildah or CRI-O container. Currently buildah diff does not exists, so this helps out in that situation as well as in CRI-O since the cri does not implement a diff command. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
@ -24,7 +24,6 @@ Check if an container called `webclient` exists in local storage (the container
|
|||||||
$ podman container exists webclient
|
$ podman container exists webclient
|
||||||
$ echo $?
|
$ echo $?
|
||||||
0
|
0
|
||||||
$
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Check if an container called `webbackend` exists in local storage (the container does not actually exist).
|
Check if an container called `webbackend` exists in local storage (the container does not actually exist).
|
||||||
@ -32,7 +31,6 @@ Check if an container called `webbackend` exists in local storage (the container
|
|||||||
$ podman container exists webbackend
|
$ podman container exists webbackend
|
||||||
$ echo $?
|
$ echo $?
|
||||||
1
|
1
|
||||||
$
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## SEE ALSO
|
## SEE ALSO
|
||||||
|
@ -62,18 +62,22 @@ func (r *Runtime) ApplyDiffTarStream(to string, diff io.Reader) error {
|
|||||||
func (r *Runtime) getLayerID(id string) (string, error) {
|
func (r *Runtime) getLayerID(id string) (string, error) {
|
||||||
var toLayer string
|
var toLayer string
|
||||||
toImage, err := r.imageRuntime.NewFromLocal(id)
|
toImage, err := r.imageRuntime.NewFromLocal(id)
|
||||||
|
if err == nil {
|
||||||
|
return toImage.TopLayer(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
targetID, err := r.store.Lookup(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
toCtr, err := r.store.Container(id)
|
targetID = id
|
||||||
|
}
|
||||||
|
toCtr, err := r.store.Container(targetID)
|
||||||
|
if err != nil {
|
||||||
|
toLayer, err = layers.FullID(r.store, targetID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
toLayer, err = layers.FullID(r.store, id)
|
return "", errors.Errorf("layer, image, or container %s does not exist", id)
|
||||||
if err != nil {
|
|
||||||
return "", errors.Errorf("layer, image, or container %s does not exist", id)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
toLayer = toCtr.LayerID
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
toLayer = toImage.TopLayer()
|
toLayer = toCtr.LayerID
|
||||||
}
|
}
|
||||||
return toLayer, nil
|
return toLayer, nil
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,10 @@ func (r *Runtime) ListStorageContainers() ([]*StorageContainer, error) {
|
|||||||
return finalCtrs, nil
|
return finalCtrs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Runtime) StorageContainer(idOrName string) (*storage.Container, error) {
|
||||||
|
return r.store.Container(idOrName)
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveStorageContainer removes a container from c/storage.
|
// RemoveStorageContainer removes a container from c/storage.
|
||||||
// The container WILL NOT be removed if it exists in libpod.
|
// The container WILL NOT be removed if it exists in libpod.
|
||||||
// Accepts ID or full name of container.
|
// Accepts ID or full name of container.
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/containers/podman/v2/pkg/api/handlers/compat"
|
"github.com/containers/podman/v2/pkg/api/handlers/compat"
|
||||||
"github.com/containers/podman/v2/pkg/api/handlers/utils"
|
"github.com/containers/podman/v2/pkg/api/handlers/utils"
|
||||||
"github.com/containers/podman/v2/pkg/domain/entities"
|
"github.com/containers/podman/v2/pkg/domain/entities"
|
||||||
|
"github.com/containers/podman/v2/pkg/domain/infra/abi"
|
||||||
"github.com/containers/podman/v2/pkg/ps"
|
"github.com/containers/podman/v2/pkg/ps"
|
||||||
"github.com/gorilla/schema"
|
"github.com/gorilla/schema"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -19,8 +20,12 @@ import (
|
|||||||
|
|
||||||
func ContainerExists(w http.ResponseWriter, r *http.Request) {
|
func ContainerExists(w http.ResponseWriter, r *http.Request) {
|
||||||
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
||||||
|
// Now use the ABI implementation to prevent us from having duplicate
|
||||||
|
// code.
|
||||||
|
containerEngine := abi.ContainerEngine{Libpod: runtime}
|
||||||
|
|
||||||
name := utils.GetName(r)
|
name := utils.GetName(r)
|
||||||
_, err := runtime.LookupContainer(name)
|
report, err := containerEngine.ContainerExists(r.Context(), name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Cause(err) == define.ErrNoSuchCtr {
|
if errors.Cause(err) == define.ErrNoSuchCtr {
|
||||||
utils.ContainerNotFound(w, name, err)
|
utils.ContainerNotFound(w, name, err)
|
||||||
@ -30,7 +35,11 @@ func ContainerExists(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
utils.WriteResponse(w, http.StatusNoContent, "")
|
if report.Value {
|
||||||
|
utils.WriteResponse(w, http.StatusNoContent, "")
|
||||||
|
} else {
|
||||||
|
utils.ContainerNotFound(w, name, define.ErrNoSuchCtr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListContainers(w http.ResponseWriter, r *http.Request) {
|
func ListContainers(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -74,11 +74,17 @@ func getContainersByContext(all, latest bool, names []string, runtime *libpod.Ru
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Should return *entities.ContainerExistsReport, error
|
// ContainerExists returns whether the container exists in container storage
|
||||||
func (ic *ContainerEngine) ContainerExists(ctx context.Context, nameOrID string) (*entities.BoolReport, error) {
|
func (ic *ContainerEngine) ContainerExists(ctx context.Context, nameOrID string) (*entities.BoolReport, error) {
|
||||||
_, err := ic.Libpod.LookupContainer(nameOrID)
|
_, err := ic.Libpod.LookupContainer(nameOrID)
|
||||||
if err != nil && errors.Cause(err) != define.ErrNoSuchCtr {
|
if err != nil {
|
||||||
return nil, err
|
if errors.Cause(err) != define.ErrNoSuchCtr {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Check if container exists in storage
|
||||||
|
if _, storageErr := ic.Libpod.StorageContainer(nameOrID); storageErr == nil {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &entities.BoolReport{Value: err == nil}, nil
|
return &entities.BoolReport{Value: err == nil}, nil
|
||||||
}
|
}
|
||||||
|
@ -460,4 +460,21 @@ json-file | f
|
|||||||
is "$output" "$expect" "podman run with --tz=local, matches host"
|
is "$output" "$expect" "podman run with --tz=local, matches host"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "podman container exists" {
|
||||||
|
rand=$(random_string 30)
|
||||||
|
run_podman 1 container exists myctr
|
||||||
|
|
||||||
|
run_podman create --name myctr $IMAGE /bin/true
|
||||||
|
run_podman container exists myctr
|
||||||
|
|
||||||
|
# Create a container that podman does not know about
|
||||||
|
run buildah from $IMAGE
|
||||||
|
cid="$output"
|
||||||
|
|
||||||
|
# exists should succeed
|
||||||
|
run_podman container exists $cid
|
||||||
|
|
||||||
|
run buildah rm $cid
|
||||||
|
}
|
||||||
|
|
||||||
# vim: filetype=sh
|
# vim: filetype=sh
|
||||||
|
@ -32,4 +32,26 @@ load helpers
|
|||||||
run_podman rm $n
|
run_podman rm $n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "podman diff with buildah container " {
|
||||||
|
rand_file=$(random_string 10)
|
||||||
|
run buildah from --name buildahctr $IMAGE
|
||||||
|
run buildah run buildahctr sh -c "touch /$rand_file;rm /etc/services"
|
||||||
|
|
||||||
|
run_podman diff --format json buildahctr
|
||||||
|
|
||||||
|
# Expected results for each type of diff
|
||||||
|
declare -A expect=(
|
||||||
|
[added]="/$rand_file"
|
||||||
|
[changed]="/etc"
|
||||||
|
[deleted]="/etc/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
for field in ${!expect[@]}; do
|
||||||
|
result=$(jq -r -c ".${field}[]" <<<"$output")
|
||||||
|
is "$result" "${expect[$field]}" "$field"
|
||||||
|
done
|
||||||
|
|
||||||
|
run buildah rm buildahctr
|
||||||
|
}
|
||||||
|
|
||||||
# vim: filetype=sh
|
# vim: filetype=sh
|
||||||
|
Reference in New Issue
Block a user