Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: April improvements #507

Merged
merged 8 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 106 additions & 64 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,32 @@ type Timeouts struct {
PodRunningCheckTimeout int
}

type ClusterSkipsCmdFlags struct {
SkipVpn bool
SkipDepsDownload bool
SkipDepsValidation bool
SkipNodesUpgrade bool
}

type ClusterCmdFlags struct {
Timeouts
Debug bool
FuryctlPath string
DistroLocation string
Phase string
StartFrom string
BinPath string
SkipVpn bool
VpnAutoConnect bool
DryRun bool
SkipDepsDownload bool
SkipDepsValidation bool
SkipNodesUpgrade bool
NoTTY bool
GitProtocol git.Protocol
Force []string
Outdir string
Upgrade bool
UpgradePathLocation string
UpgradeNode string
Debug bool
FuryctlPath string
DistroLocation string
Phase string
StartFrom string
BinPath string
VpnAutoConnect bool
DryRun bool
NoTTY bool
GitProtocol git.Protocol
Force []string
Outdir string
Upgrade bool
UpgradePathLocation string
UpgradeNode string
DistroPatchesLocation string
ClusterSkipsCmdFlags
}

func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
Expand All @@ -74,9 +79,10 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
return err
}

outDir := flags.Outdir

// Get home dir.
logrus.Debug("Getting Home Directory Path...")
outDir := flags.Outdir

homeDir, err := os.UserHomeDir()
if err != nil {
Expand All @@ -98,6 +104,14 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
logrus.Info("Dry run mode enabled, no changes will be applied")
}

absDistroPatchesLocation, err := filepath.Abs(flags.DistroPatchesLocation)
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting absolute path of distro patches location: %w", err)
}

var distrodl *distribution.Downloader

// Init first half of collaborators.
Expand All @@ -106,9 +120,9 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
depsvl := dependencies.NewValidator(executor, flags.BinPath, flags.FuryctlPath, flags.VpnAutoConnect)

if flags.DistroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, flags.GitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, flags.GitProtocol, absDistroPatchesLocation)
} else {
distrodl = distribution.NewDownloader(client, flags.GitProtocol)
distrodl = distribution.NewDownloader(client, flags.GitProtocol, absDistroPatchesLocation)
}

// Init packages.
Expand Down Expand Up @@ -142,7 +156,7 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
basePath := filepath.Join(outDir, ".furyctl", res.MinimalConf.Metadata.Name)

// Init second half of collaborators.
depsdl := dependencies.NewCachingDownloader(client, basePath, flags.BinPath, flags.GitProtocol)
depsdl := dependencies.NewCachingDownloader(client, outDir, basePath, flags.BinPath, flags.GitProtocol)

// Validate the furyctl.yaml file.
logrus.Info("Validating configuration file...")
Expand Down Expand Up @@ -239,7 +253,41 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
return cmd
}

func getSkipsClusterCmdFlags(cmd *cobra.Command, tracker *analytics.Tracker, cmdEvent analytics.Event) (ClusterSkipsCmdFlags, error) {
skipDepsDownload, err := cmdutil.BoolFlag(cmd, "skip-deps-download", tracker, cmdEvent)
if err != nil {
return ClusterSkipsCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "skip-deps-download")
}

skipDepsValidation, err := cmdutil.BoolFlag(cmd, "skip-deps-validation", tracker, cmdEvent)
if err != nil {
return ClusterSkipsCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "skip-deps-validation")
}

skipNodesUpgrade, err := cmdutil.BoolFlag(cmd, "skip-nodes-upgrade", tracker, cmdEvent)
if err != nil {
return ClusterSkipsCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "skip-nodes-upgrade")
}

skipVpn, err := cmdutil.BoolFlag(cmd, "skip-vpn-confirmation", tracker, cmdEvent)
if err != nil {
return ClusterSkipsCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "skip-vpn-confirmation")
}

return ClusterSkipsCmdFlags{
SkipVpn: skipVpn,
SkipDepsDownload: skipDepsDownload,
SkipDepsValidation: skipDepsValidation,
SkipNodesUpgrade: skipNodesUpgrade,
}, nil
}

func getCreateClusterCmdFlags(cmd *cobra.Command, tracker *analytics.Tracker, cmdEvent analytics.Event) (ClusterCmdFlags, error) {
skips, err := getSkipsClusterCmdFlags(cmd, tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, err
}

debug, err := cmdutil.BoolFlag(cmd, "debug", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "debug")
Expand Down Expand Up @@ -285,17 +333,12 @@ func getCreateClusterCmdFlags(cmd *cobra.Command, tracker *analytics.Tracker, cm

binPath := cmdutil.StringFlagOptional(cmd, "bin-path")

skipVpn, err := cmdutil.BoolFlag(cmd, "skip-vpn-confirmation", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "skip-vpn-confirmation")
}

vpnAutoConnect, err := cmdutil.BoolFlag(cmd, "vpn-auto-connect", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "vpn-auto-connect")
}

if skipVpn && vpnAutoConnect {
if skips.SkipVpn && vpnAutoConnect {
return ClusterCmdFlags{}, fmt.Errorf(
"%w: %s: cannot use together with skip-vpn flag",
ErrParsingFlag,
Expand All @@ -318,21 +361,6 @@ func getCreateClusterCmdFlags(cmd *cobra.Command, tracker *analytics.Tracker, cm
return ClusterCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "force")
}

skipDepsDownload, err := cmdutil.BoolFlag(cmd, "skip-deps-download", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "skip-deps-download")
}

skipDepsValidation, err := cmdutil.BoolFlag(cmd, "skip-deps-validation", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "skip-deps-validation")
}

skipNodesUpgrade, err := cmdutil.BoolFlag(cmd, "skip-nodes-upgrade", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "skip-nodes-upgrade")
}

gitProtocol, err := cmdutil.StringFlag(cmd, "git-protocol", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf(WrappedErrMessage, ErrParsingFlag, "git-protocol")
Expand Down Expand Up @@ -381,30 +409,33 @@ func getCreateClusterCmdFlags(cmd *cobra.Command, tracker *analytics.Tracker, cm
)
}

distroPatchesLocation, err := cmdutil.StringFlag(cmd, "distro-patches", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf("%w: %s", ErrParsingFlag, "distro-patches")
}

return ClusterCmdFlags{
Debug: debug,
FuryctlPath: furyctlPath,
DistroLocation: distroLocation,
Phase: phase,
StartFrom: startFrom,
BinPath: binPath,
SkipVpn: skipVpn,
VpnAutoConnect: vpnAutoConnect,
DryRun: dryRun,
SkipDepsDownload: skipDepsDownload,
SkipDepsValidation: skipDepsValidation,
SkipNodesUpgrade: skipNodesUpgrade,
NoTTY: noTTY,
Force: force,
GitProtocol: typedGitProtocol,
Debug: debug,
FuryctlPath: furyctlPath,
DistroLocation: distroLocation,
Phase: phase,
StartFrom: startFrom,
BinPath: binPath,
VpnAutoConnect: vpnAutoConnect,
DryRun: dryRun,
NoTTY: noTTY,
Force: force,
GitProtocol: typedGitProtocol,
Timeouts: Timeouts{
ProcessTimeout: timeout,
PodRunningCheckTimeout: podRunningCheckTimeout,
},
Outdir: outdir,
Upgrade: upgrade,
UpgradePathLocation: upgradePathLocation,
UpgradeNode: upgradeNode,
Outdir: outdir,
Upgrade: upgrade,
UpgradePathLocation: upgradePathLocation,
UpgradeNode: upgradeNode,
DistroPatchesLocation: distroPatchesLocation,
ClusterSkipsCmdFlags: skips,
}, nil
}

Expand Down Expand Up @@ -437,7 +468,18 @@ func setupCreateClusterCmdFlags(cmd *cobra.Command) {
"Location where to download schemas, defaults and the distribution manifests from. "+
"It can either be a local path (eg: /path/to/fury/distribution) or "+
"a remote URL (eg: git::[email protected]:sighupio/fury-distribution?depth=1&ref=BRANCH_NAME). "+
"Any format supported by hashicorp/go-getter can be used.",
cmdutil.AnyGoGetterFormatStr,
)

cmd.Flags().String(
"distro-patches",
"",
"Location where the distribution's user-made patches can be downloaded from. "+
"This can be either a local path (eg: /path/to/distro-patches) or "+
"a remote URL (eg: git::[email protected]:your-org/distro-patches?depth=1&ref=BRANCH_NAME). "+
cmdutil.AnyGoGetterFormatStr+
" Patches within this location must be in a folder named after the distribution version (eg: v1.29.0) and "+
"must have the same structure as the distribution's repository.",
)

cmd.Flags().StringP(
Expand Down
45 changes: 42 additions & 3 deletions cmd/create/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewConfigCommand(tracker *analytics.Tracker) *cobra.Command {
if err != nil {
return fmt.Errorf("%w: version", ErrParsingFlag)
}

if version == "" {
return fmt.Errorf("%w: version", ErrMandatoryFlag)
}
Expand Down Expand Up @@ -94,14 +95,36 @@ func NewConfigCommand(tracker *analytics.Tracker) *cobra.Command {
return fmt.Errorf("%w: %w", ErrParsingFlag, err)
}

distroPatchesLocation, err := cmdutil.StringFlag(cmd, "distro-patches", tracker, cmdEvent)
if err != nil {
return fmt.Errorf("%w: %s", ErrParsingFlag, "distro-patches")
}

outDir, err := cmdutil.StringFlag(cmd, "outdir", tracker, cmdEvent)
if err != nil {
return fmt.Errorf("%w: outdir", ErrParsingFlag)
}

homeDir, err := os.UserHomeDir()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
}

if outDir == "" {
outDir = homeDir
}

minimalConf := distroconf.Furyctl{
APIVersion: apiVersion,
Kind: kind,
Metadata: distroconf.FuryctlMeta{
Name: name,
},
Spec: distroconf.FuryctlSpec{
DistributionVersion: semver.EnsurePrefix(version),
DistributionVersion: version,
},
}

Expand All @@ -113,9 +136,9 @@ func NewConfigCommand(tracker *analytics.Tracker) *cobra.Command {
depsvl := dependencies.NewValidator(executor, "", "", false)

if distroLocation == "" {
distrodl = distribution.NewCachingDownloader(client, typedGitProtocol)
distrodl = distribution.NewCachingDownloader(client, outDir, typedGitProtocol, distroPatchesLocation)
} else {
distrodl = distribution.NewDownloader(client, typedGitProtocol)
distrodl = distribution.NewDownloader(client, typedGitProtocol, distroPatchesLocation)
}

// Init packages.
Expand Down Expand Up @@ -174,6 +197,15 @@ func NewConfigCommand(tracker *analytics.Tracker) *cobra.Command {
return fmt.Errorf("failed to create configuration file: %w", err)
}

if err := config.Validate(furyctlPath, res.RepoPath); err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

_ = os.Remove(furyctlPath)

return fmt.Errorf("error while validating configuration file: %w", err)
}

logrus.Infof("Configuration file created successfully at: %s", out.Name())

cmdEvent.AddSuccessMessage(fmt.Sprintf("Configuration file created successfully at: %s", out.Name()))
Expand All @@ -200,6 +232,13 @@ func NewConfigCommand(tracker *analytics.Tracker) *cobra.Command {
"Any format supported by hashicorp/go-getter can be used.",
)

cmd.Flags().String(
"distro-patches",
"",
"Location where to download distribution's user-made patches from. "+
cmdutil.AnyGoGetterFormatStr,
)

cmd.Flags().StringP(
"version",
"v",
Expand Down
Loading