forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_buildpack.go
168 lines (144 loc) · 5.29 KB
/
inspect_buildpack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package pack
import (
"context"
"fmt"
"sort"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/buildpackage"
"github.com/buildpacks/pack/internal/dist"
)
type BuildpackInfo struct {
BuildpackMetadata buildpackage.Metadata
Buildpacks []dist.BuildpackInfo
Order dist.Order
BuildpackLayers dist.BuildpackLayers
Location buildpack.LocatorType
}
type InspectBuildpackOptions struct {
BuildpackName string
Daemon bool
Registry string
}
type ImgWrapper struct {
v1.ImageConfig
}
func (iw ImgWrapper) Label(name string) (string, error) {
return iw.Labels[name], nil
}
func (c *Client) InspectBuildpack(opts InspectBuildpackOptions) (*BuildpackInfo, error) {
locatorType, err := buildpack.GetLocatorType(opts.BuildpackName, "", []dist.BuildpackInfo{})
if err != nil {
return nil, err
}
var layersMd dist.BuildpackLayers
var buildpackMd buildpackage.Metadata
switch locatorType {
case buildpack.RegistryLocator:
buildpackMd, layersMd, err = metadataFromRegistry(c, opts.BuildpackName, opts.Registry)
case buildpack.PackageLocator:
buildpackMd, layersMd, err = metadataFromImage(c, opts.BuildpackName, opts.Daemon)
case buildpack.URILocator:
buildpackMd, layersMd, err = metadataFromArchive(c.downloader, opts.BuildpackName)
default:
return nil, fmt.Errorf("unable to handle locator %q: for buildpack %q", locatorType, opts.BuildpackName)
}
if err != nil {
return nil, err
}
return &BuildpackInfo{
BuildpackMetadata: buildpackMd,
BuildpackLayers: layersMd,
Order: extractOrder(buildpackMd),
Buildpacks: extractBuildpacks(layersMd),
Location: locatorType,
}, nil
}
func metadataFromRegistry(client *Client, name, registry string) (buildpackMd buildpackage.Metadata, layersMd dist.BuildpackLayers, err error) {
registryCache, err := client.getRegistry(client.logger, registry)
if err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, fmt.Errorf("invalid registry %s: %q", registry, err)
}
registryBp, err := registryCache.LocateBuildpack(name)
if err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, fmt.Errorf("unable to find %s in registry: %q", style.Symbol(name), err)
}
buildpackMd, layersMd, err = metadataFromImage(client, registryBp.Address, false)
if err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, fmt.Errorf("error pulling registry specified image: %s", err)
}
return buildpackMd, layersMd, nil
}
func metadataFromArchive(downloader Downloader, path string) (buildpackMd buildpackage.Metadata, layersMd dist.BuildpackLayers, err error) {
imgBlob, err := downloader.Download(context.Background(), path)
if err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, fmt.Errorf("unable to download archive: %q", err)
}
config, err := buildpackage.ConfigFromOCILayoutBlob(imgBlob)
if err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, fmt.Errorf("unable to fetch config from buildpack blob: %q", err)
}
wrapper := ImgWrapper{config}
if _, err := dist.GetLabel(wrapper, dist.BuildpackLayersLabel, &layersMd); err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, err
}
if _, err := dist.GetLabel(wrapper, buildpackage.MetadataLabel, &buildpackMd); err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, err
}
return buildpackMd, layersMd, nil
}
func metadataFromImage(client *Client, name string, daemon bool) (buildpackMd buildpackage.Metadata, layersMd dist.BuildpackLayers, err error) {
imageName := buildpack.ParsePackageLocator(name)
img, err := client.imageFetcher.Fetch(context.Background(), imageName, daemon, config.PullNever)
if err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, err
}
if _, err := dist.GetLabel(img, dist.BuildpackLayersLabel, &layersMd); err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, fmt.Errorf("unable to get image label %s: %q", dist.BuildpackLayersLabel, err)
}
if _, err := dist.GetLabel(img, buildpackage.MetadataLabel, &buildpackMd); err != nil {
return buildpackage.Metadata{}, dist.BuildpackLayers{}, fmt.Errorf("unable to get image label %s: %q", buildpackage.MetadataLabel, err)
}
return buildpackMd, layersMd, nil
}
func extractOrder(buildpackMd buildpackage.Metadata) dist.Order {
return dist.Order{
{
Group: []dist.BuildpackRef{
{
BuildpackInfo: buildpackMd.BuildpackInfo,
},
},
},
}
}
func extractBuildpacks(layersMd dist.BuildpackLayers) []dist.BuildpackInfo {
result := []dist.BuildpackInfo{}
buildpackSet := map[*dist.BuildpackInfo]bool{}
for buildpackID, buildpackMap := range layersMd {
for version, layerInfo := range buildpackMap {
bp := dist.BuildpackInfo{
ID: buildpackID,
Version: version,
Homepage: layerInfo.Homepage,
}
buildpackSet[&bp] = true
}
}
for currentBuildpack := range buildpackSet {
result = append(result, *currentBuildpack)
}
sort.Slice(result, func(i int, j int) bool {
switch {
case result[i].ID < result[j].ID:
return true
case result[i].ID == result[j].ID:
return result[i].Version < result[j].Version
default:
return false
}
})
return result
}