forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Output aligned to docker structures Signed-off-by: Jhon Honce <[email protected]>
- Loading branch information
Showing
7 changed files
with
158 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
service: main.go | ||
go build . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package serviceapi | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/containers/libpod/libpod" | ||
) | ||
|
||
type serviceHandler func(w http.ResponseWriter, r *http.Request, runtime *libpod.Runtime) | ||
|
||
func (h serviceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
h(w, r, libpodRuntime) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package serviceapi | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/containers/libpod/libpod" | ||
) | ||
|
||
func containers(w http.ResponseWriter, r *http.Request, runtime *libpod.Runtime) { | ||
http.NotFound(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package serviceapi | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/containers/libpod/libpod" | ||
) | ||
|
||
func images(w http.ResponseWriter, r *http.Request, runtime *libpod.Runtime) { | ||
contentType := r.Header.Get("Content-Type") | ||
if contentType != "" && contentType != "application/json" { | ||
http.Error(w, | ||
fmt.Sprintf("%s is not a supported Content-Type", r.Header.Get("Content-Type")), | ||
http.StatusUnsupportedMediaType) | ||
return | ||
} | ||
|
||
images, err := runtime.ImageRuntime().GetImages() | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
var summaries []*ImageSummary | ||
for _, img := range images { | ||
i, err := ImageToImageSummary(img) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
summaries = append(summaries, i) | ||
} | ||
|
||
buffer, err := json.Marshal(summaries) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
io.WriteString(w, string(buffer)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package serviceapi | ||
|
||
import ( | ||
"context" | ||
|
||
podman "github.com/containers/libpod/libpod/image" | ||
docker "github.com/docker/docker/api/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type ImageSummary struct { | ||
docker.ImageSummary | ||
} | ||
|
||
func ImageToImageSummary(p *podman.Image) (*ImageSummary, error) { | ||
containers, err := p.Containers() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to obtain Containers for image %s", p.ID()) | ||
} | ||
containerCount := len(containers) | ||
|
||
var digests []string | ||
for _, d := range p.Digests() { | ||
digests = append(digests, string(d)) | ||
} | ||
|
||
tags, err := p.RepoTags() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to obtain RepoTags for image %s", p.ID()) | ||
} | ||
|
||
// parent, err := p.GetParent(context.TODO()) | ||
// if err != nil { | ||
// return nil, errors.Wrapf(err, "Failed to obtain ParentID for image %s", p.ID()) | ||
// } | ||
|
||
labels, err := p.Labels(context.TODO()) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to obtain Labels for image %s", p.ID()) | ||
} | ||
|
||
size, err := p.Size(context.TODO()) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to obtain Size for image %s", p.ID()) | ||
} | ||
return &ImageSummary{docker.ImageSummary{ | ||
Containers: int64(containerCount), | ||
Created: p.Created().Unix(), | ||
ID: p.ID(), | ||
Labels: labels, | ||
ParentID: "parent.ID()", | ||
RepoDigests: digests, | ||
RepoTags: tags, | ||
SharedSize: 0, | ||
Size: int64(*size), | ||
VirtualSize: int64(*size), | ||
}}, nil | ||
} |