Skip to content

Commit

Permalink
Merge pull request #8715 from baude/bindings3images
Browse files Browse the repository at this point in the history
Podman image bindings for 3.0
  • Loading branch information
openshift-merge-robot authored Dec 16, 2020
2 parents f1f7b8f + 8d4e196 commit 978c076
Show file tree
Hide file tree
Showing 27 changed files with 2,521 additions and 342 deletions.
75 changes: 51 additions & 24 deletions pkg/api/handlers/libpod/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,14 @@ func ExportImages(w http.ResponseWriter, r *http.Request) {

// Format is mandatory! Currently, we only support multi-image docker
// archives.
if len(query.References) > 1 && query.Format != define.V2s2Archive {
utils.Error(w, "unsupported format", http.StatusInternalServerError, errors.Errorf("multi-image archives must use format of %s", define.V2s2Archive))
return

}

switch query.Format {
case define.V2s2Archive:
case define.V2s2Archive, define.OCIArchive:
tmpfile, err := ioutil.TempFile("", "api.tar")
if err != nil {
utils.Error(w, "unable to create tmpfile", http.StatusInternalServerError, errors.Wrap(err, "unable to create tempfile"))
Expand All @@ -277,6 +283,13 @@ func ExportImages(w http.ResponseWriter, r *http.Request) {
utils.Error(w, "unable to close tmpfile", http.StatusInternalServerError, errors.Wrap(err, "unable to close tempfile"))
return
}
case define.OCIManifestDir, define.V2s2ManifestDir:
tmpdir, err := ioutil.TempDir("", "save")
if err != nil {
utils.Error(w, "unable to create tmpdir", http.StatusInternalServerError, errors.Wrap(err, "unable to create tempdir"))
return
}
output = tmpdir
default:
utils.Error(w, "unsupported format", http.StatusInternalServerError, errors.Errorf("unsupported format %q", query.Format))
return
Expand All @@ -287,7 +300,7 @@ func ExportImages(w http.ResponseWriter, r *http.Request) {
opts := entities.ImageSaveOptions{
Compress: query.Compress,
Format: query.Format,
MultiImageArchive: true,
MultiImageArchive: len(query.References) > 1,
Output: output,
RemoveSignatures: true,
}
Expand Down Expand Up @@ -414,7 +427,6 @@ func PushImage(w http.ResponseWriter, r *http.Request) {
}{
// This is where you can override the golang default value for one of fields
}

if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
return
Expand Down Expand Up @@ -607,12 +619,12 @@ func UntagImage(w http.ResponseWriter, r *http.Request) {
func SearchImages(w http.ResponseWriter, r *http.Request) {
decoder := r.Context().Value("decoder").(*schema.Decoder)
query := struct {
Term string `json:"term"`
Limit int `json:"limit"`
NoTrunc bool `json:"noTrunc"`
Filters []string `json:"filters"`
TLSVerify bool `json:"tlsVerify"`
ListTags bool `json:"listTags"`
Term string `json:"term"`
Limit int `json:"limit"`
NoTrunc bool `json:"noTrunc"`
Filters map[string][]string `json:"filters"`
TLSVerify bool `json:"tlsVerify"`
ListTags bool `json:"listTags"`
}{
// This is where you can override the golang default value for one of fields
}
Expand All @@ -622,24 +634,44 @@ func SearchImages(w http.ResponseWriter, r *http.Request) {
return
}

filter := image.SearchFilter{}
if len(query.Filters) > 0 {
if len(query.Filters["stars"]) > 0 {
stars, err := strconv.Atoi(query.Filters["stars"][0])
if err != nil {
utils.InternalServerError(w, err)
return
}
filter.Stars = stars
}
if len(query.Filters["is-official"]) > 0 {
isOfficial, err := strconv.ParseBool(query.Filters["is-official"][0])
if err != nil {
utils.InternalServerError(w, err)
return
}
filter.IsOfficial = types.NewOptionalBool(isOfficial)
}
if len(query.Filters["is-automated"]) > 0 {
isAutomated, err := strconv.ParseBool(query.Filters["is-automated"][0])
if err != nil {
utils.InternalServerError(w, err)
return
}
filter.IsAutomated = types.NewOptionalBool(isAutomated)
}
}
options := image.SearchOptions{
Limit: query.Limit,
NoTrunc: query.NoTrunc,
ListTags: query.ListTags,
Filter: filter,
}

if _, found := r.URL.Query()["tlsVerify"]; found {
options.InsecureSkipTLSVerify = types.NewOptionalBool(!query.TLSVerify)
}

if _, found := r.URL.Query()["filters"]; found {
filter, err := image.ParseSearchFilter(query.Filters)
if err != nil {
utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse filters parameter for %s", r.URL.String()))
return
}
options.Filter = *filter
}

_, authfile, key, err := auth.GetCredentials(r)
if err != nil {
utils.Error(w, "failed to retrieve repository credentials", http.StatusBadRequest, errors.Wrapf(err, "failed to parse %q header for %s", key, r.URL.String()))
Expand Down Expand Up @@ -678,10 +710,7 @@ func ImagesBatchRemove(w http.ResponseWriter, r *http.Request) {
All bool `schema:"all"`
Force bool `schema:"force"`
Images []string `schema:"images"`
}{
All: false,
Force: false,
}
}{}

if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
Expand All @@ -690,10 +719,8 @@ func ImagesBatchRemove(w http.ResponseWriter, r *http.Request) {
}

opts := entities.ImageRemoveOptions{All: query.All, Force: query.Force}

imageEngine := abi.ImageEngine{Libpod: runtime}
rmReport, rmErrors := imageEngine.Remove(r.Context(), query.Images, opts)

strErrs := errorhandling.ErrorsToStrings(rmErrors)
report := handlers.LibpodImagesRemoveReport{ImageRemoveReport: *rmReport, Errors: strErrs}
utils.WriteResponse(w, http.StatusOK, report)
Expand Down
54 changes: 40 additions & 14 deletions pkg/bindings/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ func (o *{{.StructName}}) ToParams() (url.Values, error) {
}
case reflect.Map:
lowerCaseKeys := make(map[string][]string)
// I dont know if this code is needed anymore, TBD
// for k, v := range filters {
// lowerCaseKeys[strings.ToLower(k)] = v
// }
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
Expand All @@ -102,23 +103,34 @@ func (o *{{.StructName}}) ToParams() (url.Values, error) {
var fieldTmpl = `
// With{{.Name}}
func(o *{{.StructName}}) With{{.Name}}(value {{.Type}}) *{{.StructName}} {
v := &value
v := {{.TypedValue}}
o.{{.Name}} = v
return o
}
// Get{{.Name}}
func(o *{{.StructName}}) Get{{.Name}}() {{.Type}} {
var {{.ZeroName}} {{.Type}}
if o.{{.Name}} == nil {
return {{.ZeroName}}
}
return {{.TypedName}}
}
`

type fieldStruct struct {
Name string
StructName string
Type string
TypedName string
TypedValue string
ZeroName string
}

func main() {
var (
closed bool
fieldStructs []fieldStruct
structNode ast.Node
)
srcFile := os.Getenv("GOFILE")
pkg := os.Getenv("GOPACKAGE")
Expand All @@ -132,14 +144,13 @@ func main() {
if err != nil {
panic(err)
}

// always add reflect
imports := []string{"\"reflect\""}
for _, imp := range f.Imports {
imports = append(imports, imp.Path.Value)
}

out, err := os.Create(strings.ToLower(inputStructName) + "_" + srcFile)
out, err := os.Create(strings.TrimRight(srcFile, ".go") + "_" + strings.Replace(strings.ToLower(inputStructName), "options", "_options", 1) + ".go")
if err != nil {
panic(err)
}
Expand All @@ -166,26 +177,41 @@ func main() {
ref, refOK := n.(*ast.TypeSpec)
if refOK {
if ref.Name.Name == inputStructName {
structNode = n
x := ref.Type.(*ast.StructType)
for _, field := range x.Fields.List {
var (
name string
name, zeroName, typedName, typedValue string
)
typeExpr := field.Type
start := typeExpr.Pos() - 1
end := typeExpr.End() - 1
fieldType := strings.Replace(string(b[start:end]), "*", "", 1)
if len(field.Names) > 0 {
name = field.Names[0].Name
if len(name) < 1 {
panic(errors.New("bad name"))
}
}
for k, v := range name {
zeroName = strings.ToLower(string(v)) + name[k+1:]
break
}
//sub := "*"
typeExpr := field.Type
switch field.Type.(type) {
case *ast.MapType, *ast.StructType, *ast.ArrayType:
typedName = "o." + name
typedValue = "value"
default:
typedName = "*o." + name
typedValue = "&value"
}
start := typeExpr.Pos() - 1
end := typeExpr.End() - 1
fieldType := strings.Replace(string(b[start:end]), "*", "", 1)
fStruct := fieldStruct{
Name: name,
StructName: inputStructName,
Type: fieldType,
TypedName: typedName,
TypedValue: typedValue,
ZeroName: zeroName,
}
fieldStructs = append(fieldStructs, fStruct)
} // for
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 @@ -9,7 +9,11 @@ import (
)

// Diff provides the changes between two container layers
func Diff(ctx context.Context, nameOrID string) ([]archive.Change, error) {
func Diff(ctx context.Context, nameOrID string, options *DiffOptions) ([]archive.Change, error) {
if options == nil {
options = new(DiffOptions)
}
_ = options
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 978c076

Please sign in to comment.