|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + cliflag "k8s.io/component-base/cli/flag" |
| 9 | + logsv1 "k8s.io/component-base/logs/api/v1" |
| 10 | + |
| 11 | + "github.com/lxc/cluster-api-provider-incus/internal/incus" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + rootCmd = &cobra.Command{ |
| 16 | + Use: "image-builder", |
| 17 | + SilenceUsage: true, |
| 18 | + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 19 | + if err := logsv1.ValidateAndApply(logOptions, nil); err != nil { |
| 20 | + return fmt.Errorf("failed to configure logging: %w", err) |
| 21 | + } |
| 22 | + |
| 23 | + switch cfg.instanceType { |
| 24 | + case "container", "virtual-machine": |
| 25 | + default: |
| 26 | + return fmt.Errorf("invalid value for --instance-type argument %q, must be one of [container, virtual-machine]", cfg.instanceType) |
| 27 | + } |
| 28 | + |
| 29 | + switch cfg.ubuntuVersion { |
| 30 | + case "22.04", "24.04": |
| 31 | + default: |
| 32 | + return fmt.Errorf("invalid value for --ubuntu-version argument %q, must be one of [22.04, 24.04]", cfg.ubuntuVersion) |
| 33 | + } |
| 34 | + |
| 35 | + opts, err := incus.NewOptionsFromConfigFile(cfg.configFile, cfg.configRemoteName, false) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("failed to read client credentials: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + client, err = incus.New(gCtx, opts) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("failed to create incus client: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + return nil |
| 46 | + }, |
| 47 | + } |
| 48 | +) |
| 49 | + |
| 50 | +func init() { |
| 51 | + rootCmd.AddGroup(&cobra.Group{ID: "build", Title: "Available Image Types:"}) |
| 52 | + rootCmd.AddCommand(kubeadmCmd, haproxyCmd) |
| 53 | + |
| 54 | + logsv1.AddFlags(logOptions, rootCmd.PersistentFlags()) |
| 55 | + rootCmd.SetGlobalNormalizationFunc(cliflag.WordSepNormalizeFunc) |
| 56 | + rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) |
| 57 | + |
| 58 | + rootCmd.PersistentFlags().StringVar(&cfg.configFile, "config-file", "", |
| 59 | + "Read client configuration from file") |
| 60 | + rootCmd.PersistentFlags().StringVar(&cfg.configRemoteName, "config-remote-name", "", |
| 61 | + "Override remote to use from configuration file") |
| 62 | + |
| 63 | + rootCmd.PersistentFlags().StringVar(&cfg.ubuntuVersion, "ubuntu-version", defaultUbuntuVersion, |
| 64 | + "Ubuntu version to use to launch instance (one of 22.04|24.04)") |
| 65 | + |
| 66 | + rootCmd.PersistentFlags().StringVar(&cfg.instanceName, "instance-name", defaultInstanceName, |
| 67 | + "Name for the builder instance") |
| 68 | + rootCmd.PersistentFlags().StringVar(&cfg.instanceType, "instance-type", defaultInstanceType, |
| 69 | + "Type of image to build (one of container|virtual-machine)") |
| 70 | + rootCmd.PersistentFlags().StringSliceVar(&cfg.instanceProfiles, "instance-profile", defaultInstanceProfiles, |
| 71 | + "Profiles to use to launch the builder instance") |
| 72 | + |
| 73 | + rootCmd.PersistentFlags().StringVar(&cfg.imageAlias, "image-alias", "", |
| 74 | + "Create image with alias. If not specified, a default is used based on config") |
| 75 | + |
| 76 | + rootCmd.PersistentFlags().StringVar(&cfg.outputFile, "output", "image.tar.gz", |
| 77 | + "Output file for exported image") |
| 78 | + |
| 79 | +} |
0 commit comments