forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_image.go
175 lines (148 loc) · 5.16 KB
/
inspect_image.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
169
170
171
172
173
174
175
package pack
import (
"context"
"strings"
"github.com/Masterminds/semver"
"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/launch"
"github.com/pkg/errors"
"github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/image"
)
// ImageInfo is a collection of metadata describing
// an app image built using Cloud Native Buildpacks.
type ImageInfo struct {
// Stack Identifier used when building this image
StackID string
// List of buildpacks that passed detection, ran their build
// phases and made a contribution to this image.
Buildpacks []lifecycle.GroupBuildpack
// Base includes two references to the run image,
// - the Run Image ID,
// - the hash of the last layer in the app image that belongs to the run image.
// A way to visualize this is given an image with n layers:
//
// last layer in run image
// v
// [1, ..., k, k+1, ..., n]
// ^
// first layer added by buildpacks
//
// the first 1 to k layers all belong to the run image,
// the last k+1 to n layers are added by buildpacks.
// the sum of all of these is our app image.
Base lifecycle.RunImageMetadata
// BOM or Bill of materials, contains dependency and
// version information provided by each buildpack.
BOM []lifecycle.BOMEntry
// Stack includes the run image name, and a list of image mirrors,
// where the run image is hosted.
Stack lifecycle.StackMetadata
// Processes lists all processes contributed by buildpacks.
Processes ProcessDetails
}
// ProcessDetails is a collection of all start command metadata
// on an image.
type ProcessDetails struct {
// An Images default start command.
DefaultProcess *launch.Process
// List of all start commands contributed by buildpacks.
OtherProcesses []launch.Process
}
// Deserialize just the subset of fields we need to avoid breaking changes
type layersMetadata struct {
RunImage lifecycle.RunImageMetadata `json:"runImage" toml:"run-image"`
Stack lifecycle.StackMetadata `json:"stack" toml:"stack"`
}
const (
platformAPIEnv = "CNB_PLATFORM_API"
cnbProcessEnv = "CNB_PROCESS_TYPE"
launcherEntrypoint = "/cnb/lifecycle/launcher"
windowsLauncherEntrypoint = `c:\cnb\lifecycle\launcher.exe`
entrypointPrefix = "/cnb/process/"
windowsEntrypointPrefix = `c:\cnb\process\`
defaultProcess = "web"
fallbackPlatformAPI = "0.3"
windowsPrefix = "c:"
)
// InspectImage reads the Label metadata of an image. It initializes a ImageInfo object
// using this metadata, and returns it.
// If daemon is true, first the local registry will be searched for the image.
// Otherwise it assumes the image is remote.
func (c *Client) InspectImage(name string, daemon bool) (*ImageInfo, error) {
img, err := c.imageFetcher.Fetch(context.Background(), name, daemon, config.PullNever)
if err != nil {
if errors.Cause(err) == image.ErrNotFound {
return nil, nil
}
return nil, err
}
var layersMd layersMetadata
if _, err := dist.GetLabel(img, lifecycle.LayerMetadataLabel, &layersMd); err != nil {
return nil, err
}
var buildMD lifecycle.BuildMetadata
if _, err := dist.GetLabel(img, lifecycle.BuildMetadataLabel, &buildMD); err != nil {
return nil, err
}
minimumBaseImageReferenceVersion := semver.MustParse("0.5.0")
actualLauncherVersion, err := semver.NewVersion(buildMD.Launcher.Version)
if err == nil && actualLauncherVersion.LessThan(minimumBaseImageReferenceVersion) {
layersMd.RunImage.Reference = ""
}
stackID, err := img.Label(lifecycle.StackIDLabel)
if err != nil {
return nil, err
}
platformAPI, err := img.Env(platformAPIEnv)
if err != nil {
return nil, errors.Wrap(err, "reading platform api")
}
if platformAPI == "" {
platformAPI = fallbackPlatformAPI
}
platformAPIVersion, err := semver.NewVersion(platformAPI)
if err != nil {
return nil, errors.Wrap(err, "parsing platform api version")
}
var defaultProcessType string
if platformAPIVersion.LessThan(semver.MustParse("0.4")) {
defaultProcessType, err = img.Env(cnbProcessEnv)
if err != nil || defaultProcessType == "" {
defaultProcessType = defaultProcess
}
} else {
entrypoint, err := img.Entrypoint()
if err != nil {
return nil, errors.Wrap(err, "reading entrypoint")
}
if len(entrypoint) > 0 && entrypoint[0] != launcherEntrypoint && entrypoint[0] != windowsLauncherEntrypoint {
process := entrypoint[0]
if strings.HasPrefix(process, windowsPrefix) {
process = strings.TrimPrefix(process, windowsEntrypointPrefix)
process = strings.TrimSuffix(process, ".exe") // Trim .exe for Windows support
} else {
process = strings.TrimPrefix(process, entrypointPrefix)
}
defaultProcessType = process
}
}
var processDetails ProcessDetails
for _, proc := range buildMD.Processes {
proc := proc
if proc.Type == defaultProcessType {
processDetails.DefaultProcess = &proc
continue
}
processDetails.OtherProcesses = append(processDetails.OtherProcesses, proc)
}
return &ImageInfo{
StackID: stackID,
Stack: layersMd.Stack,
Base: layersMd.RunImage,
BOM: buildMD.BOM,
Buildpacks: buildMD.Buildpacks,
Processes: processDetails,
}, nil
}