@@ -25,9 +25,8 @@ import (
25
25
cnb "github.com/buildpacks/lifecycle"
26
26
cnbl "github.com/buildpacks/lifecycle/launch"
27
27
shell "github.com/kballard/go-shellquote"
28
- v1 "k8s.io/api/core/v1"
29
28
30
- "github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug/annotations "
29
+ "github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug/types "
31
30
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
32
31
)
33
32
@@ -51,16 +50,16 @@ func init() {
51
50
// CNB images use a special launcher as the entrypoint. In CNB Platform API 0.3,
52
51
// this was always `/cnb/lifecycle/launcher`, but Platform API 0.4 (introduced in pack 0.13)
53
52
// allows using a symlink to a file in `/cnb/process/<type>`. More below.
54
- func isCNBImage (ic imageConfiguration ) bool {
55
- if _ , found := ic .labels ["io.buildpacks.stack.id" ]; ! found {
53
+ func isCNBImage (ic ImageConfiguration ) bool {
54
+ if _ , found := ic .Labels ["io.buildpacks.stack.id" ]; ! found {
56
55
return false
57
56
}
58
- return len (ic .entrypoint ) == 1 && (ic .entrypoint [0 ] == cnbLauncher || strings .HasPrefix (ic .entrypoint [0 ], cnbProcessLauncherPrefix ))
57
+ return len (ic .Entrypoint ) == 1 && (ic .Entrypoint [0 ] == cnbLauncher || strings .HasPrefix (ic .Entrypoint [0 ], cnbProcessLauncherPrefix ))
59
58
}
60
59
61
60
// hasCNBLauncherEntrypoint returns true if the entrypoint is the cnbLauncher.
62
- func hasCNBLauncherEntrypoint (ic imageConfiguration ) bool {
63
- return len (ic .entrypoint ) == 1 && ic .entrypoint [0 ] == cnbLauncher
61
+ func hasCNBLauncherEntrypoint (ic ImageConfiguration ) bool {
62
+ return len (ic .Entrypoint ) == 1 && ic .Entrypoint [0 ] == cnbLauncher
64
63
}
65
64
66
65
// updateForCNBImage normalizes a CNB image by rewriting the CNB launch configuration into
@@ -106,43 +105,44 @@ func hasCNBLauncherEntrypoint(ic imageConfiguration) bool {
106
105
// the default process type. `CNB_PROCESS_TYPE` is ignored in this situation. A different process
107
106
// can be used by overriding the image entrypoint. Direct and script launches are supported by
108
107
// setting the entrypoint to `/cnb/lifecycle/launcher` and providing the appropriate arguments.
109
- func updateForCNBImage (container * v1. Container , ic imageConfiguration , transformer func (container * v1. Container , ic imageConfiguration ) (annotations .ContainerDebugConfiguration , string , error )) (annotations .ContainerDebugConfiguration , string , error ) {
108
+ func updateForCNBImage (adapter types. ContainerAdapter , ic ImageConfiguration , transformer func (adapter types. ContainerAdapter , ic ImageConfiguration ) (types .ContainerDebugConfiguration , string , error )) (types .ContainerDebugConfiguration , string , error ) {
110
109
// buildpacks/lifecycle 0.6.0 embeds the process definitions into a special image label.
111
110
// The build metadata isn't absolutely required as the image args could be
112
111
// a command line (e.g., `python xxx`) but it likely indicates the
113
112
// image was built with an older lifecycle.
114
- metadataJSON , found := ic .labels ["io.buildpacks.build.metadata" ]
113
+ metadataJSON , found := ic .Labels ["io.buildpacks.build.metadata" ]
115
114
if ! found {
116
- return annotations .ContainerDebugConfiguration {}, "" , fmt .Errorf ("image is missing buildpacks metadata; perhaps built with older lifecycle?" )
115
+ return types .ContainerDebugConfiguration {}, "" , fmt .Errorf ("image is missing buildpacks metadata; perhaps built with older lifecycle?" )
117
116
}
118
117
m := cnb.BuildMetadata {}
119
118
if err := json .Unmarshal ([]byte (metadataJSON ), & m ); err != nil {
120
- return annotations .ContainerDebugConfiguration {}, "" , fmt .Errorf ("unable to parse image buildpacks metadata" )
119
+ return types .ContainerDebugConfiguration {}, "" , fmt .Errorf ("unable to parse image buildpacks metadata" )
121
120
}
122
121
if len (m .Processes ) == 0 {
123
- return annotations .ContainerDebugConfiguration {}, "" , fmt .Errorf ("buildpacks metadata has no processes" )
122
+ return types .ContainerDebugConfiguration {}, "" , fmt .Errorf ("buildpacks metadata has no processes" )
124
123
}
125
124
126
- needsCnbLauncher := ic .entrypoint [0 ] != cnbLauncher
125
+ needsCnbLauncher := ic .Entrypoint [0 ] != cnbLauncher
127
126
// Rewrites the command-line with cnbLauncher as the entrypoint
128
127
ic , rewriter := adjustCommandLine (m , ic )
129
128
130
129
// The CNB launcher uses CNB_APP_DIR (defaults to /workspace) and ignores the image's working directory.
131
- if appDir := ic .env ["CNB_APP_DIR" ]; appDir != "" {
132
- ic .workingDir = appDir
130
+ if appDir := ic .Env ["CNB_APP_DIR" ]; appDir != "" {
131
+ ic .WorkingDir = appDir
133
132
} else {
134
- ic .workingDir = "/workspace"
133
+ ic .WorkingDir = "/workspace"
135
134
}
136
135
137
- c , img , err := transformer (container , ic )
136
+ c , img , err := transformer (adapter , ic )
138
137
if err != nil {
139
138
return c , img , err
140
139
}
141
140
// must explicitly modify the working dir as the imageConfig is lost after we return
142
141
if c .WorkingDir == "" {
143
- c .WorkingDir = ic .workingDir
142
+ c .WorkingDir = ic .WorkingDir
144
143
}
145
144
145
+ container := adapter .GetContainer ()
146
146
if container .Args != nil && rewriter != nil {
147
147
// Only rewrite the container if the arguments were changed: some transforms only alter
148
148
// env vars, and the image's arguments are not changed.
@@ -158,11 +158,11 @@ func updateForCNBImage(container *v1.Container, ic imageConfiguration, transform
158
158
// in a form suitable for the normal `skaffold debug` transformations. It returns an
159
159
// amended configuration with a function to re-transform the command-line to the form
160
160
// expected by cnbLauncher.
161
- func adjustCommandLine (m cnb.BuildMetadata , ic imageConfiguration ) (imageConfiguration , func ([]string ) []string ) {
161
+ func adjustCommandLine (m cnb.BuildMetadata , ic ImageConfiguration ) (ImageConfiguration , func ([]string ) []string ) {
162
162
// check for direct exec
163
- if hasCNBLauncherEntrypoint (ic ) && len (ic .arguments ) > 0 && ic .arguments [0 ] == "--" {
163
+ if hasCNBLauncherEntrypoint (ic ) && len (ic .Arguments ) > 0 && ic .Arguments [0 ] == "--" {
164
164
// strip and then restore the "--"
165
- ic .arguments = ic .arguments [1 :]
165
+ ic .Arguments = ic .Arguments [1 :]
166
166
return ic , func (transformed []string ) []string {
167
167
return append ([]string {"--" }, transformed ... )
168
168
}
@@ -180,19 +180,19 @@ func adjustCommandLine(m cnb.BuildMetadata, ic imageConfiguration) (imageConfigu
180
180
} else {
181
181
args := append ([]string {p .Command }, p .Args ... )
182
182
args = append (args , clArgs ... )
183
- ic .entrypoint = []string {cnbLauncher }
184
- ic .arguments = args
183
+ ic .Entrypoint = []string {cnbLauncher }
184
+ ic .Arguments = args
185
185
return ic , func (transformed []string ) []string {
186
186
return append ([]string {"--" }, transformed ... )
187
187
}
188
188
}
189
189
}
190
190
// Script type: split p.Command, pass it through the transformer, and then reassemble in the rewriter.
191
- ic .entrypoint = []string {cnbLauncher }
191
+ ic .Entrypoint = []string {cnbLauncher }
192
192
if args , err := shell .Split (p .Command ); err == nil {
193
- ic .arguments = args
193
+ ic .Arguments = args
194
194
} else {
195
- ic .arguments = []string {p .Command }
195
+ ic .Arguments = []string {p .Command }
196
196
}
197
197
return ic , func (transformed []string ) []string {
198
198
// reassemble back into a script with arguments
@@ -202,16 +202,16 @@ func adjustCommandLine(m cnb.BuildMetadata, ic imageConfiguration) (imageConfigu
202
202
}
203
203
}
204
204
205
- if len (ic .arguments ) == 0 {
206
- log .Entry (context .TODO ()).Warnf ("no CNB launch found for %s" , ic .artifact )
205
+ if len (ic .Arguments ) == 0 {
206
+ log .Entry (context .TODO ()).Warnf ("no CNB launch found for %s" , ic .Artifact )
207
207
return ic , nil
208
208
}
209
209
210
210
// ic.arguments[0] is a shell script: split it, pass it through the transformer, and then reassemble in the rewriter.
211
211
// If it can't be split, then we return it untouched, to be handled by the normal debug process.
212
- if cmdline , err := shell .Split (ic .arguments [0 ]); err == nil {
213
- positionals := ic .arguments [1 :] // save aside the script positional arguments
214
- ic .arguments = cmdline
212
+ if cmdline , err := shell .Split (ic .Arguments [0 ]); err == nil {
213
+ positionals := ic .Arguments [1 :] // save aside the script positional arguments
214
+ ic .Arguments = cmdline
215
215
return ic , func (transformed []string ) []string {
216
216
// reassemble back into a script with the positional arguments
217
217
return append ([]string {shJoin (transformed )}, positionals ... )
@@ -222,11 +222,11 @@ func adjustCommandLine(m cnb.BuildMetadata, ic imageConfiguration) (imageConfigu
222
222
223
223
// findCNBProcess tries to resolve a CNB process definition given the image configuration.
224
224
// It is assumed that the image is a CNB image.
225
- func findCNBProcess (ic imageConfiguration , m cnb.BuildMetadata ) (cnbl.Process , []string , bool ) {
226
- if hasCNBLauncherEntrypoint (ic ) && len (ic .arguments ) > 0 {
225
+ func findCNBProcess (ic ImageConfiguration , m cnb.BuildMetadata ) (cnbl.Process , []string , bool ) {
226
+ if hasCNBLauncherEntrypoint (ic ) && len (ic .Arguments ) > 0 {
227
227
// the launcher accepts the first argument as a process type
228
- if len (ic .arguments ) == 1 {
229
- processType := ic .arguments [0 ]
228
+ if len (ic .Arguments ) == 1 {
229
+ processType := ic .Arguments [0 ]
230
230
for _ , p := range m .Processes {
231
231
if p .Type == processType {
232
232
return p , nil , true // drop the argument
@@ -238,20 +238,20 @@ func findCNBProcess(ic imageConfiguration, m cnb.BuildMetadata) (cnbl.Process, [
238
238
239
239
// determine process-type
240
240
processType := "web" // default buildpacks process type
241
- platformAPI := ic .env ["CNB_PLATFORM_API" ]
241
+ platformAPI := ic .Env ["CNB_PLATFORM_API" ]
242
242
if platformAPI == "0.4" {
243
243
// Platform API 0.4-style /cnb/process/xxx
244
- if ! strings .HasPrefix (ic .entrypoint [0 ], cnbProcessLauncherPrefix ) {
244
+ if ! strings .HasPrefix (ic .Entrypoint [0 ], cnbProcessLauncherPrefix ) {
245
245
return cnbl.Process {}, nil , false
246
246
}
247
- processType = ic .entrypoint [0 ][len (cnbProcessLauncherPrefix ):]
248
- } else if len (ic .env ["CNB_PROCESS_TYPE" ]) > 0 {
249
- processType = ic .env ["CNB_PROCESS_TYPE" ]
247
+ processType = ic .Entrypoint [0 ][len (cnbProcessLauncherPrefix ):]
248
+ } else if len (ic .Env ["CNB_PROCESS_TYPE" ]) > 0 {
249
+ processType = ic .Env ["CNB_PROCESS_TYPE" ]
250
250
}
251
251
252
252
for _ , p := range m .Processes {
253
253
if p .Type == processType {
254
- return p , ic .arguments , true
254
+ return p , ic .Arguments , true
255
255
}
256
256
}
257
257
return cnbl.Process {}, nil , false
0 commit comments