detached 9f503cc

docker: check if the user submitted an image ID or Name

commit
9f503cc17c90295ef3823ef9e7b5fc6b635dbf81
author
bnema · · 2024-04-21 21:26 UTC
parent
b485310
signature
unsigned
8 files · +230 -9
M go.mod +35 -0
@@ -8,6 +8,7 @@ require (
88 github.com/charmbracelet/bubbles v0.16.1
99 github.com/charmbracelet/bubbletea v0.24.1
1010 github.com/charmbracelet/lipgloss v0.7.1
11+ github.com/containerd/containerd v1.7.15
1112 github.com/docker/docker v24.0.6+incompatible
1213 github.com/docker/go-connections v0.4.0
1314 github.com/fatih/color v1.15.0
@@ -23,27 +24,61 @@ require (
2324 )
2425
2526 require (
27+ github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
28+ github.com/AdamKorcz/go-118-fuzz-build v0.0.0-20230306123547-8075edf89bb0 // indirect
29+ github.com/Microsoft/hcsshim v0.11.4 // indirect
2630 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
2731 github.com/charmbracelet/harmonica v0.2.0 // indirect
32+ github.com/containerd/cgroups v1.1.0 // indirect
2833 github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
34+ github.com/containerd/continuity v0.4.2 // indirect
35+ github.com/containerd/fifo v1.1.0 // indirect
36+ github.com/containerd/log v0.1.0 // indirect
37+ github.com/containerd/ttrpc v1.2.3 // indirect
38+ github.com/containerd/typeurl/v2 v2.1.1 // indirect
2939 github.com/creack/pty v1.1.18 // indirect
40+ github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
3041 github.com/dustin/go-humanize v1.0.1 // indirect
42+ github.com/felixge/httpsnoop v1.0.3 // indirect
43+ github.com/go-logr/logr v1.2.4 // indirect
44+ github.com/go-logr/stdr v1.2.2 // indirect
45+ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
46+ github.com/golang/protobuf v1.5.4 // indirect
47+ github.com/google/go-cmp v0.5.9 // indirect
3148 github.com/inconshreveable/mousetrap v1.1.0 // indirect
3249 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
50+ github.com/klauspost/compress v1.16.0 // indirect
3351 github.com/kr/text v0.2.0 // indirect
3452 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
3553 github.com/mattn/go-localereader v0.0.1 // indirect
3654 github.com/mattn/go-runewidth v0.0.15 // indirect
3755 github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
56+ github.com/moby/locker v1.0.1 // indirect
57+ github.com/moby/sys/mountinfo v0.6.2 // indirect
58+ github.com/moby/sys/sequential v0.5.0 // indirect
59+ github.com/moby/sys/signal v0.7.0 // indirect
60+ github.com/moby/sys/user v0.1.0 // indirect
3861 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
3962 github.com/muesli/cancelreader v0.2.2 // indirect
4063 github.com/muesli/reflow v0.3.0 // indirect
4164 github.com/muesli/termenv v0.15.1 // indirect
65+ github.com/opencontainers/runtime-spec v1.1.0 // indirect
66+ github.com/opencontainers/selinux v1.11.0 // indirect
4267 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
4368 github.com/rivo/uniseg v0.4.4 // indirect
69+ github.com/sirupsen/logrus v1.9.3 // indirect
4470 github.com/spf13/pflag v1.0.5 // indirect
71+ go.opencensus.io v0.24.0 // indirect
72+ go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
73+ go.opentelemetry.io/otel v1.19.0 // indirect
74+ go.opentelemetry.io/otel/metric v1.19.0 // indirect
75+ go.opentelemetry.io/otel/trace v1.19.0 // indirect
4576 golang.org/x/sync v0.4.0 // indirect
4677 golang.org/x/term v0.13.0 // indirect
78+ google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a // indirect
79+ google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
80+ google.golang.org/grpc v1.59.0 // indirect
81+ google.golang.org/protobuf v1.33.0 // indirect
4782 lukechampine.com/uint128 v1.3.0 // indirect
4883 modernc.org/cc/v3 v3.41.0 // indirect
4984 modernc.org/ccgo/v3 v3.16.15 // indirect
@@ -8,6 +8,7 @@ require (
8 github.com/charmbracelet/bubbles v0.16.18 github.com/charmbracelet/bubbles v0.16.1
9 github.com/charmbracelet/bubbletea v0.24.19 github.com/charmbracelet/bubbletea v0.24.1
10 github.com/charmbracelet/lipgloss v0.7.110 github.com/charmbracelet/lipgloss v0.7.1
11+ github.com/containerd/containerd v1.7.15
11 github.com/docker/docker v24.0.6+incompatible12 github.com/docker/docker v24.0.6+incompatible
12 github.com/docker/go-connections v0.4.013 github.com/docker/go-connections v0.4.0
13 github.com/fatih/color v1.15.014 github.com/fatih/color v1.15.0
@@ -23,27 +24,61 @@ require (
23 )24 )
24 25
25 require (26 require (
27+ github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
28+ github.com/AdamKorcz/go-118-fuzz-build v0.0.0-20230306123547-8075edf89bb0 // indirect
29+ github.com/Microsoft/hcsshim v0.11.4 // indirect
26 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect30 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
27 github.com/charmbracelet/harmonica v0.2.0 // indirect31 github.com/charmbracelet/harmonica v0.2.0 // indirect
32+ github.com/containerd/cgroups v1.1.0 // indirect
28 github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect33 github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
34+ github.com/containerd/continuity v0.4.2 // indirect
35+ github.com/containerd/fifo v1.1.0 // indirect
36+ github.com/containerd/log v0.1.0 // indirect
37+ github.com/containerd/ttrpc v1.2.3 // indirect
38+ github.com/containerd/typeurl/v2 v2.1.1 // indirect
29 github.com/creack/pty v1.1.18 // indirect39 github.com/creack/pty v1.1.18 // indirect
40+ github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
30 github.com/dustin/go-humanize v1.0.1 // indirect41 github.com/dustin/go-humanize v1.0.1 // indirect
42+ github.com/felixge/httpsnoop v1.0.3 // indirect
43+ github.com/go-logr/logr v1.2.4 // indirect
44+ github.com/go-logr/stdr v1.2.2 // indirect
45+ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
46+ github.com/golang/protobuf v1.5.4 // indirect
47+ github.com/google/go-cmp v0.5.9 // indirect
31 github.com/inconshreveable/mousetrap v1.1.0 // indirect48 github.com/inconshreveable/mousetrap v1.1.0 // indirect
32 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect49 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
50+ github.com/klauspost/compress v1.16.0 // indirect
33 github.com/kr/text v0.2.0 // indirect51 github.com/kr/text v0.2.0 // indirect
34 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect52 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
35 github.com/mattn/go-localereader v0.0.1 // indirect53 github.com/mattn/go-localereader v0.0.1 // indirect
36 github.com/mattn/go-runewidth v0.0.15 // indirect54 github.com/mattn/go-runewidth v0.0.15 // indirect
37 github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect55 github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
56+ github.com/moby/locker v1.0.1 // indirect
57+ github.com/moby/sys/mountinfo v0.6.2 // indirect
58+ github.com/moby/sys/sequential v0.5.0 // indirect
59+ github.com/moby/sys/signal v0.7.0 // indirect
60+ github.com/moby/sys/user v0.1.0 // indirect
38 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect61 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
39 github.com/muesli/cancelreader v0.2.2 // indirect62 github.com/muesli/cancelreader v0.2.2 // indirect
40 github.com/muesli/reflow v0.3.0 // indirect63 github.com/muesli/reflow v0.3.0 // indirect
41 github.com/muesli/termenv v0.15.1 // indirect64 github.com/muesli/termenv v0.15.1 // indirect
65+ github.com/opencontainers/runtime-spec v1.1.0 // indirect
66+ github.com/opencontainers/selinux v1.11.0 // indirect
42 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect67 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
43 github.com/rivo/uniseg v0.4.4 // indirect68 github.com/rivo/uniseg v0.4.4 // indirect
69+ github.com/sirupsen/logrus v1.9.3 // indirect
44 github.com/spf13/pflag v1.0.5 // indirect70 github.com/spf13/pflag v1.0.5 // indirect
71+ go.opencensus.io v0.24.0 // indirect
72+ go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
73+ go.opentelemetry.io/otel v1.19.0 // indirect
74+ go.opentelemetry.io/otel/metric v1.19.0 // indirect
75+ go.opentelemetry.io/otel/trace v1.19.0 // indirect
45 golang.org/x/sync v0.4.0 // indirect76 golang.org/x/sync v0.4.0 // indirect
46 golang.org/x/term v0.13.0 // indirect77 golang.org/x/term v0.13.0 // indirect
78+ google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a // indirect
79+ google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
80+ google.golang.org/grpc v1.59.0 // indirect
81+ google.golang.org/protobuf v1.33.0 // indirect
47 lukechampine.com/uint128 v1.3.0 // indirect82 lukechampine.com/uint128 v1.3.0 // indirect
48 modernc.org/cc/v3 v3.41.0 // indirect83 modernc.org/cc/v3 v3.41.0 // indirect
49 modernc.org/ccgo/v3 v3.16.15 // indirect84 modernc.org/ccgo/v3 v3.16.15 // indirect
M go.sum +149 -0 generated file: collapsed
M internal/cli/cmd/deploy.go +18 -5
@@ -177,21 +177,34 @@ func NewDeployCommand(a *cli.App) *cobra.Command {
177177
178178 // export the docker image to a reader and return its true size
179179 func exportDockerImage(imageName string) (io.ReadCloser, int64, error) {
180- imageID, err := docker.GetImageID(imageName)
180+ // Check if what the user submitted is a valid image ID
181+ exists, err := docker.CheckIfImageExists(imageName)
181182 if err != nil {
182- return nil, 0, fmt.Errorf("error getting image ID: %w", err)
183+ return nil, 0, fmt.Errorf("error while checking image existence: %w", err)
184+ }
185+
186+ var imageID string
187+ if exists {
188+ // What the user submitted is a valid image ID
189+ imageID = imageName
190+ } else {
191+ // What the user submitted is not a valid image ID, search by name
192+ imageID, err = docker.GetImageIDByName(imageName)
193+ if err != nil {
194+ return nil, 0, fmt.Errorf("error while searching for image by name: %w", err)
195+ }
183196 }
184197
185198 actualSize, err := docker.GetImageSizeFromReader(imageID)
186199 if err != nil {
187- return nil, 0, fmt.Errorf("error getting image size: %w", err)
200+ return nil, 0, fmt.Errorf("error while retrieving image size: %w", err)
188201 }
189202
190203 reader, err := docker.ExportImageFromEngine(imageID)
191204 if err != nil {
192- return nil, 0, fmt.Errorf("error exporting image: %w", err)
205+ return nil, 0, fmt.Errorf("error while exporting image: %w", err)
193206 }
194207
195- // Return the wrapped reader.
208+ // Return the reader and the actual size
196209 return reader, actualSize, nil
197210 }
@@ -177,21 +177,34 @@ func NewDeployCommand(a *cli.App) *cobra.Command {
177 177
178 // export the docker image to a reader and return its true size178 // export the docker image to a reader and return its true size
179 func exportDockerImage(imageName string) (io.ReadCloser, int64, error) {179 func exportDockerImage(imageName string) (io.ReadCloser, int64, error) {
180- imageID, err := docker.GetImageID(imageName)180+ // Check if what the user submitted is a valid image ID
181+ exists, err := docker.CheckIfImageExists(imageName)
181 if err != nil {182 if err != nil {
182- return nil, 0, fmt.Errorf("error getting image ID: %w", err)183+ return nil, 0, fmt.Errorf("error while checking image existence: %w", err)
184+ }
185+
186+ var imageID string
187+ if exists {
188+ // What the user submitted is a valid image ID
189+ imageID = imageName
190+ } else {
191+ // What the user submitted is not a valid image ID, search by name
192+ imageID, err = docker.GetImageIDByName(imageName)
193+ if err != nil {
194+ return nil, 0, fmt.Errorf("error while searching for image by name: %w", err)
195+ }
183 }196 }
184 197
185 actualSize, err := docker.GetImageSizeFromReader(imageID)198 actualSize, err := docker.GetImageSizeFromReader(imageID)
186 if err != nil {199 if err != nil {
187- return nil, 0, fmt.Errorf("error getting image size: %w", err)200+ return nil, 0, fmt.Errorf("error while retrieving image size: %w", err)
188 }201 }
189 202
190 reader, err := docker.ExportImageFromEngine(imageID)203 reader, err := docker.ExportImageFromEngine(imageID)
191 if err != nil {204 if err != nil {
192- return nil, 0, fmt.Errorf("error exporting image: %w", err)205+ return nil, 0, fmt.Errorf("error while exporting image: %w", err)
193 }206 }
194 207
195- // Return the wrapped reader.208+ // Return the reader and the actual size
196 return reader, actualSize, nil209 return reader, actualSize, nil
197 }210 }
M internal/httpserve/handler/clientendpoins.go +1 -1
@@ -110,7 +110,7 @@ func PostDeploy(c echo.Context, a *server.App) error {
110110 payload.ImageName = "localhost/" + payload.ImageName
111111
112112 // Get the image ID
113- imageID, err := docker.GetImageID(payload.ImageName)
113+ imageID, err := docker.GetImageIDByName(payload.ImageName)
114114 if err != nil {
115115 return sendJsonError(c, err)
116116 }
@@ -110,7 +110,7 @@ func PostDeploy(c echo.Context, a *server.App) error {
110 payload.ImageName = "localhost/" + payload.ImageName110 payload.ImageName = "localhost/" + payload.ImageName
111 111
112 // Get the image ID112 // Get the image ID
113- imageID, err := docker.GetImageID(payload.ImageName)113+ imageID, err := docker.GetImageIDByName(payload.ImageName)
114 if err != nil {114 if err != nil {
115 return sendJsonError(c, err)115 return sendJsonError(c, err)
116 }116 }
M internal/templating/cmdparams/fromcli.go +1 -1
@@ -13,7 +13,7 @@ import (
1313 // FromYAMLStructToCmdParams converts a YAMLContainerParams struct to a ContainerCommandParams struct
1414 func FromPayloadStructToCmdParams(ppl *common.DeployPayload, a *server.App) (docker.ContainerCommandParams, error) {
1515
16- imageID, err := docker.GetImageID(ppl.ImageName)
16+ imageID, err := docker.GetImageIDByName(ppl.ImageName)
1717 if err != nil {
1818 return docker.ContainerCommandParams{}, err
1919 }
@@ -13,7 +13,7 @@ import (
13 // FromYAMLStructToCmdParams converts a YAMLContainerParams struct to a ContainerCommandParams struct13 // FromYAMLStructToCmdParams converts a YAMLContainerParams struct to a ContainerCommandParams struct
14 func FromPayloadStructToCmdParams(ppl *common.DeployPayload, a *server.App) (docker.ContainerCommandParams, error) {14 func FromPayloadStructToCmdParams(ppl *common.DeployPayload, a *server.App) (docker.ContainerCommandParams, error) {
15 15
16- imageID, err := docker.GetImageID(ppl.ImageName)16+ imageID, err := docker.GetImageIDByName(ppl.ImageName)
17 if err != nil {17 if err != nil {
18 return docker.ContainerCommandParams{}, err18 return docker.ContainerCommandParams{}, err
19 }19 }
M internal/templating/cmdparams/fromyaml.go +1 -1
@@ -22,7 +22,7 @@ type YAMLContainerParams struct {
2222 // FromYAMLStructToCmdParams converts a YAMLContainerParams struct to a ContainerCommandParams struct
2323 func FromYAMLStructToCmdParams(yamlParams YAMLContainerParams) (docker.ContainerCommandParams, error) {
2424 // with the image name find the image id
25- imageID, err := docker.GetImageID(yamlParams.Image)
25+ imageID, err := docker.GetImageIDByName(yamlParams.Image)
2626 if err != nil {
2727 return docker.ContainerCommandParams{}, err
2828 }
@@ -22,7 +22,7 @@ type YAMLContainerParams struct {
22 // FromYAMLStructToCmdParams converts a YAMLContainerParams struct to a ContainerCommandParams struct22 // FromYAMLStructToCmdParams converts a YAMLContainerParams struct to a ContainerCommandParams struct
23 func FromYAMLStructToCmdParams(yamlParams YAMLContainerParams) (docker.ContainerCommandParams, error) {23 func FromYAMLStructToCmdParams(yamlParams YAMLContainerParams) (docker.ContainerCommandParams, error) {
24 // with the image name find the image id24 // with the image name find the image id
25- imageID, err := docker.GetImageID(yamlParams.Image)25+ imageID, err := docker.GetImageIDByName(yamlParams.Image)
26 if err != nil {26 if err != nil {
27 return docker.ContainerCommandParams{}, err27 return docker.ContainerCommandParams{}, err
28 }28 }
M pkg/docker/client.go +2 -0
@@ -2,6 +2,7 @@ package docker
22
33 import (
44 "context"
5+ "crypto/tls"
56 "fmt"
67 "os"
78 "time"
@@ -17,6 +18,7 @@ type ContainerEngineClient interface {
1718 type Config struct {
1819 Sock string
1920 PodmanEnable bool
21+ TLSConfig *tls.Config
2022 }
2123
2224 var (
@@ -2,6 +2,7 @@ package docker
2 2
3 import (3 import (
4 "context"4 "context"
5+ "crypto/tls"
5 "fmt"6 "fmt"
6 "os"7 "os"
7 "time"8 "time"
@@ -17,6 +18,7 @@ type ContainerEngineClient interface {
17 type Config struct {18 type Config struct {
18 Sock string19 Sock string
19 PodmanEnable bool20 PodmanEnable bool
21+ TLSConfig *tls.Config
20 }22 }
21 23
22 var (24 var (
M pkg/docker/images.go +23 -1
@@ -32,7 +32,18 @@ func DeleteContainerImage(imageID string) error {
3232 return nil
3333 }
3434
35-func GetImageID(imageName string) (string, error) {
35+// CheckIfImageExists checks if an image exists in the Docker engine
36+func CheckIfImageExists(imageID string) (bool, error) {
37+ // Get the image information using the Docker client
38+ _, err := GetImageInfo(imageID)
39+ if err != nil {
40+ return false, nil
41+ }
42+
43+ return true, nil
44+}
45+
46+func GetImageIDByName(imageName string) (string, error) {
3647
3748 images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})
3849 if err != nil {
@@ -178,3 +189,14 @@ func GetImageSizeFromReader(imageID string) (int64, error) {
178189
179190 return actualSize, nil
180191 }
192+
193+// list all images
194+func GetAllImages() ([]types.ImageSummary, error) {
195+ // List images using the Docker client
196+ images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})
197+ if err != nil {
198+ return nil, err
199+ }
200+
201+ return images, nil
202+}
@@ -32,7 +32,18 @@ func DeleteContainerImage(imageID string) error {
32 return nil32 return nil
33 }33 }
34 34
35-func GetImageID(imageName string) (string, error) {35+// CheckIfImageExists checks if an image exists in the Docker engine
36+func CheckIfImageExists(imageID string) (bool, error) {
37+ // Get the image information using the Docker client
38+ _, err := GetImageInfo(imageID)
39+ if err != nil {
40+ return false, nil
41+ }
42+
43+ return true, nil
44+}
45+
46+func GetImageIDByName(imageName string) (string, error) {
36 47
37 images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})48 images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})
38 if err != nil {49 if err != nil {
@@ -178,3 +189,14 @@ func GetImageSizeFromReader(imageID string) (int64, error) {
178 189
179 return actualSize, nil190 return actualSize, nil
180 }191 }
192+
193+// list all images
194+func GetAllImages() ([]types.ImageSummary, error) {
195+ // List images using the Docker client
196+ images, err := dockerCli.ImageList(context.Background(), types.ImageListOptions{})
197+ if err != nil {
198+ return nil, err
199+ }
200+
201+ return images, nil
202+}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close