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: added --post-apply-phases flag #514

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (

const WrappedErrMessage = "%w: %s"

var ErrDownloadDependenciesFailed = errors.New("dependencies download failed")
var (
ErrDownloadDependenciesFailed = errors.New("dependencies download failed")
ErrPhaseInvalid = errors.New("phase is not valid")
)

type Timeouts struct {
ProcessTimeout int
Expand Down Expand Up @@ -60,6 +63,7 @@ type ClusterCmdFlags struct {
UpgradePathLocation string
UpgradeNode string
DistroPatchesLocation string
PostApplyPhases []string
ClusterSkipsCmdFlags
}

Expand Down Expand Up @@ -222,6 +226,7 @@ func NewApplyCommand(tracker *analytics.Tracker) *cobra.Command {
flags.Upgrade,
flags.UpgradePathLocation,
flags.UpgradeNode,
flags.PostApplyPhases,
)
if err != nil {
cmdEvent.AddErrorMessage(err)
Expand Down Expand Up @@ -414,6 +419,15 @@ func getCreateClusterCmdFlags(cmd *cobra.Command, tracker *analytics.Tracker, cm
return ClusterCmdFlags{}, fmt.Errorf("%w: %s", ErrParsingFlag, "distro-patches")
}

postApplyPhases, err := cmdutil.StringSliceFlag(cmd, "post-apply-phases", tracker, cmdEvent)
if err != nil {
return ClusterCmdFlags{}, fmt.Errorf("%w: %s", ErrParsingFlag, "post-apply-phases")
}

if err := validatePostApplyPhasesFlag(postApplyPhases); err != nil {
return ClusterCmdFlags{}, fmt.Errorf("%w: %s %w", ErrParsingFlag, "post-apply-phases", err)
}

return ClusterCmdFlags{
Debug: debug,
FuryctlPath: furyctlPath,
Expand All @@ -436,9 +450,20 @@ func getCreateClusterCmdFlags(cmd *cobra.Command, tracker *analytics.Tracker, cm
UpgradeNode: upgradeNode,
DistroPatchesLocation: distroPatchesLocation,
ClusterSkipsCmdFlags: skips,
PostApplyPhases: postApplyPhases,
}, nil
}

func validatePostApplyPhasesFlag(phases []string) error {
for _, phase := range phases {
if err := cluster.ValidateMainPhases(phase); err != nil {
return fmt.Errorf("%w: %s", ErrPhaseInvalid, phase)
}
}

return nil
}

func setupCreateClusterCmdFlags(cmd *cobra.Command) {
cmd.Flags().StringP(
"config",
Expand Down Expand Up @@ -532,6 +557,12 @@ func setupCreateClusterCmdFlags(cmd *cobra.Command) {
"WARNING: furyctl won't ask for confirmation and will proceed applying upgrades and reducers. Options are: all, upgrades, migrations, pods-running-check",
)

cmd.Flags().StringSlice(
"post-apply-phases",
[]string{},
"Phases to run after the apply command. Options are: infrastructure, kubernetes, distribution, plugins",
)

//nolint:gomnd,revive // ignore magic number linters
cmd.Flags().Int(
"timeout",
Expand Down
1 change: 1 addition & 0 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func getPhasePath(
true,
upgradePathLocation,
"",
[]string{},
)
if err != nil {
return "", fmt.Errorf("error while initializing cluster creator: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/kfd/v1alpha2/ekscluster/create/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ func (d *Distribution) runReducers(
return nil
}

func (d *Distribution) SetUpgrade(upgradeEnabled bool) {
d.upgrade.Enabled = upgradeEnabled
}

func (d *Distribution) Stop() error {
errCh := make(chan error)
doneCh := make(chan bool)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ func (i *Infrastructure) postInfrastructure(
return nil
}

func (i *Infrastructure) SetUpgrade(upgradeEnabled bool) {
i.upgrade.Enabled = upgradeEnabled
}

func (i *Infrastructure) Stop() error {
logrus.Debug("Stopping terraform...")

Expand Down
4 changes: 4 additions & 0 deletions internal/apis/kfd/v1alpha2/ekscluster/create/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ func (k *Kubernetes) postKubernetes(
return nil
}

func (k *Kubernetes) SetUpgrade(upgradeEnabled bool) {
k.upgrade.Enabled = upgradeEnabled
}

func (k *Kubernetes) Stop() error {
errCh := make(chan error)
doneCh := make(chan bool)
Expand Down
60 changes: 60 additions & 0 deletions internal/apis/kfd/v1alpha2/ekscluster/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type ClusterCreator struct {
force []string
upgrade bool
externalUpgradesPath string
postApplyPhases []string
}

type Phases struct {
Expand Down Expand Up @@ -160,6 +161,11 @@ func (v *ClusterCreator) SetProperty(name string, value any) {
if s, ok := value.(string); ok {
v.externalUpgradesPath = s
}

case cluster.CreatorPropertyPostApplyPhases:
if s, ok := value.([]string); ok {
v.postApplyPhases = s
}
}
}

Expand Down Expand Up @@ -639,6 +645,14 @@ func (v *ClusterCreator) allPhases(
return err
}

if len(v.postApplyPhases) > 0 {
logrus.Info("Executing extra phases...")

if err := v.extraPhases(phases, upgradeState, upgr); err != nil {
return fmt.Errorf("error while executing extra phases: %w", err)
}
}

if v.dryRun {
logrus.Info("Kubernetes Fury cluster created successfully (dry-run mode)")

Expand Down Expand Up @@ -672,6 +686,52 @@ func (v *ClusterCreator) allPhases(
return nil
}

func (v *ClusterCreator) extraPhases(phases *Phases, upgradeState *upgrade.State, upgr *upgrade.Upgrade) error {
initialUpgrade := upgr.Enabled

defer func() {
upgr.Enabled = initialUpgrade
}()

for _, phase := range v.postApplyPhases {
switch phase {
case cluster.OperationPhaseInfrastructure:
phases.Infrastructure.SetUpgrade(false)

if err := phases.Infrastructure.Exec(StartFromFlagNotSet, upgradeState); err != nil {
return fmt.Errorf("error while executing post infrastructure phase: %w", err)
}

case cluster.OperationPhaseKubernetes:
phases.Kubernetes.SetUpgrade(false)

if err := phases.Kubernetes.Exec(StartFromFlagNotSet, upgradeState); err != nil {
return fmt.Errorf("error while executing post kubernetes phase: %w", err)
}

case cluster.OperationPhaseDistribution:
phases.Distribution.SetUpgrade(false)

if err := phases.Distribution.Exec(
v1alpha2.Reducers{},
StartFromFlagNotSet,
upgradeState,
); err != nil {
return fmt.Errorf("error while executing post distribution phase: %w", err)
}

case cluster.OperationPhasePlugins:
if distribution.HasFeature(v.kfdManifest, distribution.FeaturePlugins) {
if err := phases.Plugins.Exec(); err != nil {
return fmt.Errorf("error while executing plugins phase: %w", err)
}
}
}
}

return nil
}

func (v *ClusterCreator) allPhasesExec(
startFrom string,
phases *Phases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ func (d *Distribution) Stop() error {
return nil
}

func (d *Distribution) SetUpgrade(upgradeEnabled bool) {
d.upgrade.Enabled = upgradeEnabled
}

func (d *Distribution) runReducers(
reducers v1alpha2.Reducers,
cfg template.Config,
Expand Down
47 changes: 47 additions & 0 deletions internal/apis/kfd/v1alpha2/kfddistribution/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type ClusterCreator struct {
force []string
upgrade bool
externalUpgradesPath string
postApplyPhases []string
}

func (c *ClusterCreator) SetProperties(props []cluster.CreatorProperty) {
Expand Down Expand Up @@ -129,6 +130,11 @@ func (c *ClusterCreator) SetProperty(name string, value any) {
if s, ok := value.(string); ok {
c.externalUpgradesPath = s
}

case cluster.CreatorPropertyPostApplyPhases:
if s, ok := value.([]string); ok {
c.postApplyPhases = s
}
}
}

Expand Down Expand Up @@ -368,6 +374,47 @@ func (c *ClusterCreator) allPhases(
}
}

if len(c.postApplyPhases) > 0 {
logrus.Info("Executing extra phases...")

if err := c.extraPhases(distributionPhase, pluginsPhase, upgradeState, upgr); err != nil {
return fmt.Errorf("error while executing extra phases: %w", err)
}
}

return nil
}

func (c *ClusterCreator) extraPhases(
distributionPhase upgrade.ReducersOperatorPhase[v1alpha2.Reducers],
pluginsPhase *commcreate.Plugins,
upgradeState *upgrade.State,
upgr *upgrade.Upgrade,
) error {
initialUpgrade := upgr.Enabled

defer func() {
upgr.Enabled = initialUpgrade
}()

for _, phase := range c.postApplyPhases {
switch phase {
case cluster.OperationPhaseDistribution:
distributionPhase.SetUpgrade(false)

if err := distributionPhase.Exec(v1alpha2.Reducers{}, StartFromFlagNotSet, upgradeState); err != nil {
return fmt.Errorf("error while executing distribution phase: %w", err)
}

case cluster.OperationPhasePlugins:
if distribution.HasFeature(c.kfdManifest, distribution.FeaturePlugins) {
if err := pluginsPhase.Exec(); err != nil {
return fmt.Errorf("error while executing plugins phase: %w", err)
}
}
}
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/apis/kfd/v1alpha2/onpremises/create/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ func (d *Distribution) injectStoredConfig(cfg template.Config) (template.Config,
return cfg, nil
}

func (d *Distribution) SetUpgrade(upgradeEnabled bool) {
d.upgrade.Enabled = upgradeEnabled
}

func NewDistribution(
furyctlConf public.OnpremisesKfdV1Alpha2,
kfdManifest config.KFD,
Expand Down
4 changes: 4 additions & 0 deletions internal/apis/kfd/v1alpha2/onpremises/create/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ func (k *Kubernetes) postKubernetes(
return nil
}

func (k *Kubernetes) SetUpgrade(upgradeEnabled bool) {
k.upgrade.Enabled = upgradeEnabled
}

func NewKubernetes(
furyctlConf public.OnpremisesKfdV1Alpha2,
kfdManifest config.KFD,
Expand Down
61 changes: 61 additions & 0 deletions internal/apis/kfd/v1alpha2/onpremises/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type ClusterCreator struct {
upgrade bool
externalUpgradesPath string
upgradeNode string
postApplyPhases []string
}

func (c *ClusterCreator) SetProperties(props []cluster.CreatorProperty) {
Expand Down Expand Up @@ -142,6 +143,11 @@ func (c *ClusterCreator) SetProperty(name string, value any) {
if s, ok := value.(string); ok {
c.upgradeNode = s
}

case cluster.CreatorPropertyPostApplyPhases:
if s, ok := value.([]string); ok {
c.postApplyPhases = s
}
}
}

Expand Down Expand Up @@ -429,6 +435,61 @@ func (c *ClusterCreator) allPhases(
}
}

if len(c.postApplyPhases) > 0 {
logrus.Info("Executing extra phases...")

if err := c.extraPhases(
kubernetesPhase,
distributionPhase,
pluginsPhase,
upgr,
upgradeState,
); err != nil {
return fmt.Errorf("error while executing extra phases: %w", err)
}
}

return nil
}

func (c *ClusterCreator) extraPhases(
kubernetesPhase upgrade.OperatorPhase,
distributionPhase upgrade.ReducersOperatorPhase[v1alpha2.Reducers],
pluginsPhase *commcreate.Plugins,
upgr *upgrade.Upgrade,
upgradeState *upgrade.State,
) error {
initialUpgrade := upgr.Enabled

defer func() {
upgr.Enabled = initialUpgrade
}()

for _, phase := range c.postApplyPhases {
switch phase {
case cluster.OperationPhaseKubernetes:
kubernetesPhase.SetUpgrade(false)

if err := kubernetesPhase.Exec(StartFromFlagNotSet, upgradeState); err != nil {
return fmt.Errorf("error while executing kubernetes phase: %w", err)
}

case cluster.OperationPhaseDistribution:
distributionPhase.SetUpgrade(false)

if err := distributionPhase.Exec(nil, StartFromFlagNotSet, upgradeState); err != nil {
return fmt.Errorf("error while executing distribution phase: %w", err)
}

case cluster.OperationPhasePlugins:
if distribution.HasFeature(c.kfdManifest, distribution.FeaturePlugins) {
if err := pluginsPhase.Exec(); err != nil {
return fmt.Errorf("error while executing plugins phase: %w", err)
}
}
}
}

return nil
}

Expand Down
Loading