Skip to content

Commit bd65882

Browse files
kasimekaytsssun
authored andcommitted
host-ctr: add --command flag for custom container entrypoint
Add --command flag that accepts a JSON array to override container entrypoint using OCI's WithProcessArgs spec option. Signed-off-by: Yutong Sun <[email protected]>
1 parent c7f01c2 commit bd65882

File tree

1 file changed

+29
-3
lines changed
  • sources/host-ctr/cmd/host-ctr

1 file changed

+29
-3
lines changed

sources/host-ctr/cmd/host-ctr/main.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"encoding/base64"
6+
"encoding/json"
67
"fmt"
78
"io"
89
"math/rand"
@@ -106,6 +107,7 @@ func App() *cli.App {
106107
registryConfig string
107108
cType string
108109
useCachedImage bool
110+
command string
109111
)
110112

111113
app := cli.NewApp()
@@ -171,9 +173,29 @@ func App() *cli.App {
171173
Destination: &useCachedImage,
172174
Value: false,
173175
},
176+
&cli.StringFlag{
177+
Name: "command",
178+
Usage: "a JSON array of commands and arguments to run as the container's entrypoint",
179+
Destination: &command,
180+
Value: "[]",
181+
},
174182
},
175183
Action: func(_ *cli.Context) error {
176-
return runCtr(containerdSocket, namespace, containerID, source, superpowered, registryConfig, containerType(cType), useCachedImage)
184+
var commandParts []string
185+
if err := json.Unmarshal([]byte(command), &commandParts); err != nil {
186+
return fmt.Errorf("failed to parse entrypoint command: %w", err)
187+
}
188+
return runCtr(
189+
containerdSocket,
190+
namespace,
191+
containerID,
192+
source,
193+
superpowered,
194+
registryConfig,
195+
containerType(cType),
196+
useCachedImage,
197+
commandParts,
198+
)
177199
},
178200
},
179201
{
@@ -284,7 +306,7 @@ func SliceContains(s []string, v string) bool {
284306
return false
285307
}
286308

287-
func runCtr(containerdSocket string, namespace string, containerID string, source string, superpowered bool, registryConfigPath string, cType containerType, useCachedImage bool) error {
309+
func runCtr(containerdSocket string, namespace string, containerID string, source string, superpowered bool, registryConfigPath string, cType containerType, useCachedImage bool, command []string) error {
288310
// Check if the containerType provided is valid
289311
if !cType.IsValid() {
290312
return errors.New("Invalid container type")
@@ -380,6 +402,11 @@ func runCtr(containerdSocket string, namespace string, containerID string, sourc
380402
specOpts = append(specOpts, withDefault())
381403
}
382404

405+
// Override the entrypoint command, regardless of container type or other options
406+
if len(command) > 0 {
407+
specOpts = append(specOpts, oci.WithProcessArgs(command...))
408+
}
409+
383410
ctrOpts := containerd.WithNewSpec(specOpts...)
384411

385412
// Create the container.
@@ -733,7 +760,6 @@ func fetchECRRef(ctx context.Context, input string, specialRegions specialRegion
733760
// if a valid ECR ref has not yet been returned
734761
log.G(ctx).WithError(err).WithField("source", input).Error("failed to parse special ECR reference")
735762
return ecr.ECRSpec{}, errors.Wrap(err, "could not parse ECR reference for special regions")
736-
737763
}
738764

739765
// fetchECRImage does some additional conversions before resolving the image reference and fetches the image.

0 commit comments

Comments
 (0)