Skip to content

Commit 5fc0962

Browse files
authored
feat(v2): restore command, deduplicate code (#1760)
* feat(v2): restore command * feat(v2): restore command
1 parent 509e9d7 commit 5fc0962

File tree

6 files changed

+434
-449
lines changed

6 files changed

+434
-449
lines changed

cmd/installer/cli/cidr.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
func addCIDRFlags(cmd *cobra.Command) {
13+
func addCIDRFlags(cmd *cobra.Command) error {
1414
cmd.Flags().String("pod-cidr", k0sv1beta1.DefaultNetwork().PodCIDR, "IP address range for Pods")
15-
cmd.Flags().MarkHidden("pod-cidr")
15+
if err := cmd.Flags().MarkHidden("pod-cidr"); err != nil {
16+
return err
17+
}
1618
cmd.Flags().String("service-cidr", k0sv1beta1.DefaultNetwork().ServiceCIDR, "IP address range for Services")
17-
cmd.Flags().MarkHidden("service-cidr")
19+
if err := cmd.Flags().MarkHidden("service-cidr"); err != nil {
20+
return err
21+
}
1822
cmd.Flags().String("cidr", ecv1beta1.DefaultNetworkCIDR, "CIDR block of available private IP addresses (/16 or larger)")
23+
24+
return nil
1925
}
2026

2127
func validateCIDRFlags(cmd *cobra.Command) error {

cmd/installer/cli/install2.go

Lines changed: 114 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -71,64 +71,10 @@ func Install2Cmd(ctx context.Context, name string) *cobra.Command {
7171
SilenceUsage: true,
7272
SilenceErrors: true,
7373
PreRunE: func(cmd *cobra.Command, args []string) error {
74-
if os.Getuid() != 0 {
75-
return fmt.Errorf("install command must be run as root")
76-
}
77-
78-
if flags.skipHostPreflights {
79-
logrus.Warnf("Warning: --skip-host-preflights is deprecated and will be removed in a later version. Use --ignore-host-preflights instead.")
80-
}
81-
82-
p, err := parseProxyFlags(cmd)
83-
if err != nil {
84-
return err
85-
}
86-
flags.proxy = p
87-
88-
if err := validateCIDRFlags(cmd); err != nil {
74+
if err := preRunInstall2(cmd, args, &flags); err != nil {
8975
return err
9076
}
9177

92-
// parse the various cidr flags to make sure we have exactly what we want
93-
cidrCfg, err := getCIDRConfig(cmd)
94-
if err != nil {
95-
return fmt.Errorf("unable to determine pod and service CIDRs: %w", err)
96-
}
97-
flags.cidrCfg = cidrCfg
98-
99-
// if a network interface flag was not provided, attempt to discover it
100-
if flags.networkInterface == "" {
101-
autoInterface, err := determineBestNetworkInterface()
102-
if err != nil {
103-
flags.autoSelectNetworkInterfaceErr = err
104-
} else {
105-
flags.isAutoSelectedNetworkInterface = true
106-
flags.networkInterface = autoInterface
107-
}
108-
}
109-
110-
// validate the the license is indeed a license file
111-
l, err := helpers.ParseLicense(flags.licenseFile)
112-
if err != nil {
113-
if err == helpers.ErrNotALicenseFile {
114-
return fmt.Errorf("license file is not a valid license file")
115-
}
116-
117-
return fmt.Errorf("unable to parse license file: %w", err)
118-
}
119-
flags.license = l
120-
121-
runtimeconfig.ApplyFlags(cmd.Flags())
122-
os.Setenv("TMPDIR", runtimeconfig.EmbeddedClusterTmpSubDir())
123-
124-
if err := runtimeconfig.WriteToDisk(); err != nil {
125-
return fmt.Errorf("unable to write runtime config to disk: %w", err)
126-
}
127-
128-
if os.Getenv("DISABLE_TELEMETRY") != "" {
129-
metrics.DisableMetrics()
130-
}
131-
13278
return nil
13379
},
13480
PostRun: func(cmd *cobra.Command, args []string) {
@@ -140,36 +86,120 @@ func Install2Cmd(ctx context.Context, name string) *cobra.Command {
14086
}
14187

14288
return nil
143-
14489
},
14590
}
14691

92+
if err := addInstallFlags(cmd, &flags); err != nil {
93+
panic(err)
94+
}
95+
14796
cmd.Flags().StringVar(&flags.adminConsolePassword, "admin-console-password", "", "Password for the Admin Console")
14897
cmd.Flags().IntVar(&flags.adminConsolePort, "admin-console-port", ecv1beta1.DefaultAdminConsolePort, "Port on which the Admin Console will be served")
98+
cmd.Flags().StringVarP(&flags.licenseFile, "license", "l", "", "Path to the license file")
99+
if err := cmd.MarkFlagRequired("license"); err != nil {
100+
panic(err)
101+
}
102+
cmd.Flags().StringVar(&flags.configValues, "config-values", "", "Path to the config values to use when installing")
103+
104+
return cmd
105+
}
106+
107+
func addInstallFlags(cmd *cobra.Command, flags *Install2CmdFlags) error {
149108
cmd.Flags().StringVar(&flags.airgapBundle, "airgap-bundle", "", "Path to the air gap bundle. If set, the installation will complete without internet access.")
150109
cmd.Flags().StringVar(&flags.dataDir, "data-dir", ecv1beta1.DefaultDataDir, "Path to the data directory")
151-
cmd.Flags().StringVarP(&flags.licenseFile, "license", "l", "", "Path to the license file")
152110
cmd.Flags().IntVar(&flags.localArtifactMirrorPort, "local-artifact-mirror-port", ecv1beta1.DefaultLocalArtifactMirrorPort, "Port on which the Local Artifact Mirror will be served")
153111
cmd.Flags().StringVar(&flags.networkInterface, "network-interface", "", "The network interface to use for the cluster")
154112
cmd.Flags().BoolVarP(&flags.assumeYes, "yes", "y", false, "Assume yes to all prompts.")
113+
cmd.Flags().SetNormalizeFunc(normalizeNoPromptToYes)
155114

156115
cmd.Flags().StringVar(&flags.overrides, "overrides", "", "File with an EmbeddedClusterConfig object to override the default configuration")
157-
cmd.Flags().MarkHidden("overrides")
116+
if err := cmd.Flags().MarkHidden("overrides"); err != nil {
117+
return err
118+
}
158119

159120
cmd.Flags().StringSliceVar(&flags.privateCAs, "private-ca", []string{}, "Path to a trusted private CA certificate file")
160121

161-
cmd.Flags().BoolVar(&flags.skipHostPreflights, "skip-host-preflights", false, "Skip host preflight checks. This is not recommended and has been deprecated.")
162-
cmd.Flags().MarkHidden("skip-host-preflights")
163-
cmd.Flags().MarkDeprecated("skip-host-preflights", "This flag is deprecated and will be removed in a future version. Use --ignore-host-preflights instead.")
122+
if err := addProxyFlags(cmd); err != nil {
123+
return err
124+
}
125+
if err := addCIDRFlags(cmd); err != nil {
126+
return err
127+
}
164128

129+
cmd.Flags().BoolVar(&flags.skipHostPreflights, "skip-host-preflights", false, "Skip host preflight checks. This is not recommended and has been deprecated.")
130+
if err := cmd.Flags().MarkHidden("skip-host-preflights"); err != nil {
131+
return err
132+
}
133+
if err := cmd.Flags().MarkDeprecated("skip-host-preflights", "This flag is deprecated and will be removed in a future version. Use --ignore-host-preflights instead."); err != nil {
134+
return err
135+
}
165136
cmd.Flags().BoolVar(&flags.ignoreHostPreflights, "ignore-host-preflights", false, "Allow bypassing host preflight failures")
166-
cmd.Flags().StringVar(&flags.configValues, "config-values", "", "Path to the config values to use when installing")
167137

168-
addProxyFlags(cmd)
169-
addCIDRFlags(cmd)
170-
cmd.Flags().SetNormalizeFunc(normalizeNoPromptToYes)
138+
return nil
139+
}
171140

172-
return cmd
141+
func preRunInstall2(cmd *cobra.Command, args []string, flags *Install2CmdFlags) error {
142+
if os.Getuid() != 0 {
143+
return fmt.Errorf("install command must be run as root")
144+
}
145+
146+
if flags.skipHostPreflights {
147+
logrus.Warnf("Warning: --skip-host-preflights is deprecated and will be removed in a later version. Use --ignore-host-preflights instead.")
148+
}
149+
150+
p, err := parseProxyFlags(cmd)
151+
if err != nil {
152+
return err
153+
}
154+
flags.proxy = p
155+
156+
if err := validateCIDRFlags(cmd); err != nil {
157+
return err
158+
}
159+
160+
// parse the various cidr flags to make sure we have exactly what we want
161+
cidrCfg, err := getCIDRConfig(cmd)
162+
if err != nil {
163+
return fmt.Errorf("unable to determine pod and service CIDRs: %w", err)
164+
}
165+
flags.cidrCfg = cidrCfg
166+
167+
// if a network interface flag was not provided, attempt to discover it
168+
if flags.networkInterface == "" {
169+
autoInterface, err := determineBestNetworkInterface()
170+
if err != nil {
171+
flags.autoSelectNetworkInterfaceErr = err
172+
} else {
173+
flags.isAutoSelectedNetworkInterface = true
174+
flags.networkInterface = autoInterface
175+
}
176+
}
177+
178+
if flags.licenseFile != "" {
179+
// validate the the license is indeed a license file
180+
l, err := helpers.ParseLicense(flags.licenseFile)
181+
if err != nil {
182+
if err == helpers.ErrNotALicenseFile {
183+
return fmt.Errorf("license file is not a valid license file")
184+
}
185+
186+
return fmt.Errorf("unable to parse license file: %w", err)
187+
}
188+
flags.license = l
189+
}
190+
191+
runtimeconfig.ApplyFlags(cmd.Flags())
192+
os.Setenv("TMPDIR", runtimeconfig.EmbeddedClusterTmpSubDir())
193+
194+
if err := runtimeconfig.WriteToDisk(); err != nil {
195+
return fmt.Errorf("unable to write runtime config to disk: %w", err)
196+
}
197+
198+
if os.Getenv("DISABLE_TELEMETRY") != "" {
199+
metrics.DisableMetrics()
200+
}
201+
202+
return nil
173203
}
174204

175205
func runInstall2(cmd *cobra.Command, args []string, name string, flags Install2CmdFlags) error {
@@ -318,17 +348,9 @@ func runInstallVerifyAndPrompt(ctx context.Context, name string, flags *Install2
318348
os.Exit(1)
319349
}
320350

321-
channelRelease, err := release.GetChannelRelease()
351+
_, err = getChannelReleaseAndVerify(ctx, flags)
322352
if err != nil {
323-
return fmt.Errorf("unable to read channel release data: %w", err)
324-
}
325-
326-
if channelRelease != nil && channelRelease.Airgap && flags.airgapBundle == "" && !flags.assumeYes {
327-
logrus.Warnf("You downloaded an air gap bundle but didn't provide it with --airgap-bundle.")
328-
logrus.Warnf("If you continue, the installation will not use an air gap bundle and will connect to the internet.")
329-
if !prompts.New().Confirm("Do you want to proceed with an online installation?", false) {
330-
return ErrNothingElseToAdd
331-
}
353+
return err
332354
}
333355

334356
metrics.ReportApplyStarted(ctx, flags.licenseFile)
@@ -399,6 +421,22 @@ func runInstallVerifyAndPrompt(ctx context.Context, name string, flags *Install2
399421
return nil
400422
}
401423

424+
func getChannelReleaseAndVerify(ctx context.Context, flags *Install2CmdFlags) (*release.ChannelRelease, error) {
425+
channelRelease, err := release.GetChannelRelease()
426+
if err != nil {
427+
return nil, fmt.Errorf("unable to read channel release data: %w", err)
428+
}
429+
430+
if channelRelease != nil && channelRelease.Airgap && flags.airgapBundle == "" && !flags.assumeYes {
431+
logrus.Warnf("You downloaded an air gap bundle but didn't provide it with --airgap-bundle.")
432+
logrus.Warnf("If you continue, the installation will not use an air gap bundle and will connect to the internet.")
433+
if !prompts.New().Confirm("Do you want to proceed with an online installation?", false) {
434+
return nil, ErrNothingElseToAdd
435+
}
436+
}
437+
return channelRelease, nil
438+
}
439+
402440
func installAndStartCluster(ctx context.Context, networkInterface string, airgapBundle string, license *kotsv1beta1.License, proxy *ecv1beta1.ProxySpec, cidrCfg *CIDRConfig, overrides string) (*k0sconfig.ClusterConfig, error) {
403441
loading := spinner.Start()
404442
defer loading.Close()

cmd/installer/cli/proxy.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16-
func addProxyFlags(cmd *cobra.Command) {
16+
func addProxyFlags(cmd *cobra.Command) error {
1717
cmd.Flags().String("http-proxy", "", "HTTP proxy to use for the installation")
1818
cmd.Flags().String("https-proxy", "", "HTTPS proxy to use for the installation")
1919
cmd.Flags().String("no-proxy", "", "Comma-separated list of hosts for which not to use a proxy")
2020

2121
// the "proxy" flag is hidden and used in tests to detect the proxy from the env
2222
cmd.Flags().Bool("proxy", false, "Use the system proxy settings for the install operation. These variables are currently only passed through to Velero and the Admin Console.")
23-
cmd.Flags().MarkHidden("proxy")
23+
if err := cmd.Flags().MarkHidden("proxy"); err != nil {
24+
return err
25+
}
26+
27+
return nil
2428
}
2529

2630
func parseProxyFlags(cmd *cobra.Command) (*ecv1beta1.ProxySpec, error) {

cmd/installer/cli/proxy_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ package cli
33
import (
44
"testing"
55

6-
k0sconfig "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
76
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
87
"github.com/spf13/cobra"
98
"github.com/spf13/pflag"
109
"github.com/stretchr/testify/assert"
1110
)
1211

1312
func Test_getProxySpecFromFlags(t *testing.T) {
14-
type args struct {
15-
cfg *k0sconfig.ClusterConfig
16-
}
1713
tests := []struct {
1814
name string
1915
init func(t *testing.T, flagSet *pflag.FlagSet)

0 commit comments

Comments
 (0)