Skip to content

Commit

Permalink
imageutil: helper to return buildinfo from image config
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Mar 7, 2022
1 parent d1a8f13 commit c61838f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
19 changes: 0 additions & 19 deletions util/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,25 +349,6 @@ func GetMetadata(metadata map[string][]byte, key string, reqFrontend string, req
return dtbi, nil
}

// FromImageConfig returns build info from image config.
func FromImageConfig(dt []byte) (*binfotypes.BuildInfo, error) {
if len(dt) == 0 {
return nil, nil
}
var config binfotypes.ImageConfig
if err := json.Unmarshal(dt, &config); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal image config")
}
if len(config.BuildInfo) == 0 {
return nil, nil
}
bi, err := Decode(config.BuildInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to decode build info from image config")
}
return &bi, nil
}

func reduceMapString(m1 map[string]string, m2 map[string]*string) map[string]string {
if m1 == nil && m2 == nil {
return nil
Expand Down
32 changes: 32 additions & 0 deletions util/imageutil/buildinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package imageutil

import (
"encoding/base64"
"encoding/json"

binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/pkg/errors"
)

// BuildInfo returns build info from image config.
func BuildInfo(dt []byte) (*binfotypes.BuildInfo, error) {
if len(dt) == 0 {
return nil, nil
}
var config binfotypes.ImageConfig
if err := json.Unmarshal(dt, &config); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal image config")
}
if len(config.BuildInfo) == 0 {
return nil, nil
}
dtbi, err := base64.StdEncoding.DecodeString(config.BuildInfo)
if err != nil {
return nil, err
}
var bi binfotypes.BuildInfo
if err = json.Unmarshal(dtbi, &bi); err != nil {
return nil, errors.Wrap(err, "failed to decode buildinfo from image config")
}
return &bi, nil
}

0 comments on commit c61838f

Please sign in to comment.