Skip to content

Commit 42c0bdb

Browse files
committed
feat: add provisioner flag to images default command
Fixes #11925 Signed-off-by: Mateusz Urbanek <mateusz.urbanek@siderolabs.com>
1 parent 6bc0b1b commit 42c0bdb

File tree

6 files changed

+89
-2
lines changed

6 files changed

+89
-2
lines changed

cmd/talosctl/cmd/talos/image.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/dustin/go-humanize"
2020
"github.com/spf13/cobra"
21+
"github.com/spf13/pflag"
2122

2223
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
2324
"github.com/siderolabs/talos/pkg/imager/cache"
@@ -149,7 +150,15 @@ var imageDefaultCmd = &cobra.Command{
149150
fmt.Printf("%s\n", images.KubeScheduler)
150151
fmt.Printf("%s\n", images.KubeProxy)
151152
fmt.Printf("%s\n", images.Kubelet)
152-
fmt.Printf("%s\n", images.Installer)
153+
154+
if slices.Contains([]string{provisionerInstaller, provisionerAll}, imageDefaultCmdFlags.provisioner.String()) {
155+
fmt.Printf("%s\n", images.Installer)
156+
}
157+
158+
if slices.Contains([]string{provisionerDocker, provisionerAll}, imageDefaultCmdFlags.provisioner.String()) {
159+
fmt.Printf("%s\n", images.Talos)
160+
}
161+
153162
fmt.Printf("%s\n", images.Pause)
154163

155164
return nil
@@ -305,11 +314,25 @@ var imageCacheCreateCmdFlags struct {
305314
force bool
306315
}
307316

317+
const (
318+
provisionerDocker = "docker"
319+
provisionerInstaller = "installer"
320+
provisionerAll = "all"
321+
)
322+
323+
var imageDefaultCmdFlags = struct {
324+
provisioner pflag.Value
325+
}{
326+
provisioner: helpers.StringChoice(provisionerInstaller, provisionerDocker, provisionerAll),
327+
}
328+
308329
func init() {
309330
imageCmd.PersistentFlags().StringVar(&imageCmdFlags.namespace, "namespace", "cri", "namespace to use: `system` (etcd and kubelet images) or `cri` for all Kubernetes workloads")
310331
addCommand(imageCmd)
311332

312333
imageCmd.AddCommand(imageDefaultCmd)
334+
imageDefaultCmd.PersistentFlags().Var(imageDefaultCmdFlags.provisioner, "provisioner", "include provisioner specific images")
335+
313336
imageCmd.AddCommand(imageListCmd)
314337
imageCmd.AddCommand(imagePullCmd)
315338
imageCmd.AddCommand(imageCacheCreateCmd)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package helpers
6+
7+
import (
8+
"fmt"
9+
"slices"
10+
11+
"github.com/spf13/pflag"
12+
)
13+
14+
// choiceValue implements the [pflag.Value] interface.
15+
type choiceValue struct {
16+
value string
17+
validate func(string) error
18+
}
19+
20+
// Set sets the value of the choice.
21+
func (f *choiceValue) Set(s string) error {
22+
err := f.validate(s)
23+
if err != nil {
24+
return err
25+
}
26+
27+
f.value = s
28+
29+
return nil
30+
}
31+
32+
// Type returns the type of the choice, which must be "string" for [pflag.FlagSet.GetString].
33+
func (f *choiceValue) Type() string { return "string" }
34+
35+
// String returns the current value of the choice.
36+
func (f *choiceValue) String() string { return f.value }
37+
38+
// StringChoice returns a [choiceValue] that validates the value against a set
39+
// of choices. Only the last value will be used if multiple values are set.
40+
func StringChoice(defaultValue string, otherChoices ...string) pflag.Value {
41+
return &choiceValue{
42+
value: defaultValue,
43+
validate: func(s string) error {
44+
choices := slices.Concat(otherChoices, []string{defaultValue})
45+
46+
if slices.Contains(choices, s) {
47+
return nil
48+
}
49+
50+
return fmt.Errorf("must be one of %v", choices)
51+
},
52+
}
53+
}

hack/cloud-image-uploader/factory.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"fmt"
1010
"io"
11+
"log"
1112
"net/http"
1213
"net/url"
1314
"os"
@@ -64,6 +65,8 @@ func (f *FactoryDownloader) getArtifact(ctx context.Context, name string) (io.Re
6465
return nil, fmt.Errorf("failed to create request: %w", err)
6566
}
6667

68+
log.Printf("requesting artifact: %s", url)
69+
6770
resp, err := http.DefaultClient.Do(req)
6871
if err != nil {
6972
return nil, fmt.Errorf("failed to download image from %q: %w", url, err)
@@ -90,6 +93,8 @@ func (f *FactoryDownloader) saveArtifact(name string, r io.Reader) error {
9093
}
9194
defer of.Close() //nolint:errcheck
9295

96+
log.Printf("saving artifact: %s", artifact)
97+
9398
_, err = io.Copy(of, r)
9499
if err != nil {
95100
return fmt.Errorf("failed to write image to file %q: %w", artifact, err)

pkg/images/images.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ var (
3131
// DefaultTalosImageRepository is the default container repository for
3232
// the talos image.
3333
DefaultTalosImageRepository = Registry + "/" + Username + "/" + "talos"
34+
35+
// DefaultTalosImage is the default talos image.
36+
DefaultTalosImage = DefaultTalosImageRepository + ":" + version.Tag
3437
)

pkg/images/list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Versions struct {
2424
KubeScheduler string
2525

2626
Installer string
27+
Talos string
2728

2829
Pause string
2930
}
@@ -47,6 +48,7 @@ func List(config config.Config) Versions {
4748
images.KubeScheduler = config.Cluster().Scheduler().Image()
4849

4950
images.Installer = DefaultInstallerImage
51+
images.Talos = DefaultTalosImage
5052

5153
images.Pause = DefaultSandboxImage
5254

website/content/v1.12/reference/cli.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,8 @@ talosctl image default [flags]
19001900
### Options
19011901

19021902
```
1903-
-h, --help help for default
1903+
-h, --help help for default
1904+
--provisioner string include provisioner specific images (default "installer")
19041905
```
19051906

19061907
### Options inherited from parent commands

0 commit comments

Comments
 (0)