Skip to content

Commit

Permalink
fix migrations overwriting mutations
Browse files Browse the repository at this point in the history
Signed-off-by: razzle <[email protected]>
  • Loading branch information
Noxsios committed Jan 2, 2024
1 parent 59e769f commit e88fa82
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (ic *ImportChain) Migrate(build types.ZarfBuildData) (warnings []string) {
node = node.next
}
if len(warnings) > 0 {
final := fmt.Sprintf("migrations were performed on the import chain of: %q", ic.head.Name)
final := fmt.Sprintf("Migrations were performed on the import chain of: %q", ic.head.Name)
warnings = append(warnings, final)
}
return warnings
Expand Down
15 changes: 7 additions & 8 deletions src/pkg/packager/deprecated/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var breakingChanges = []BreakingChange{

type migration interface {
name() string
clear() types.ZarfComponent
migrate() (types.ZarfComponent, string)
clear(mc types.ZarfComponent) types.ZarfComponent
run(c types.ZarfComponent) (types.ZarfComponent, string)
}

// MigrateComponent runs all migrations on a component.
Expand All @@ -54,20 +54,19 @@ func MigrateComponent(build types.ZarfBuildData, component types.ZarfComponent)
migratedComponent = component

migrations := []migration{
migrateScriptsToActions{migratedComponent},
migrateSetVariableToSetVariables{migratedComponent},
migrateRequiredToOptional{migratedComponent},
migrateScriptsToActions{},
migrateSetVariableToSetVariables{},
migrateRequiredToOptional{},
}

// Run all migrations
for _, m := range migrations {
// If the component has already been migrated, run the postbuild function.
if slices.Contains(build.Migrations, m.name()) {
migratedComponent = m.clear()
migratedComponent = m.clear(migratedComponent)
} else {
// Otherwise, run the migration.
var warning string
if migratedComponent, warning = m.migrate(); warning != "" {
if migratedComponent, warning = m.run(migratedComponent); warning != "" {
warnings = append(warnings, warning)
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/pkg/packager/deprecated/pluralize-set-variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ import (
"github.com/defenseunicorns/zarf/src/types"
)

type migrateSetVariableToSetVariables struct {
component types.ZarfComponent
}
type migrateSetVariableToSetVariables struct{}

func (m migrateSetVariableToSetVariables) name() string {
return PluralizeSetVariable
}

// If the component has already been migrated, clear the deprecated setVariable.
func (m migrateSetVariableToSetVariables) clear() types.ZarfComponent {
mc := m.component
func (m migrateSetVariableToSetVariables) clear(mc types.ZarfComponent) types.ZarfComponent {
clear := func(actions []types.ZarfComponentAction) []types.ZarfComponentAction {
for i := range actions {
actions[i].DeprecatedSetVariable = ""
Expand Down Expand Up @@ -50,8 +47,7 @@ func (m migrateSetVariableToSetVariables) clear() types.ZarfComponent {
return mc
}

func (m migrateSetVariableToSetVariables) migrate() (types.ZarfComponent, string) {
c := m.component
func (m migrateSetVariableToSetVariables) run(c types.ZarfComponent) (types.ZarfComponent, string) {
hasSetVariable := false

migrate := func(actions []types.ZarfComponentAction) []types.ZarfComponentAction {
Expand Down
13 changes: 4 additions & 9 deletions src/pkg/packager/deprecated/required_to_optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,20 @@ import (
"github.com/defenseunicorns/zarf/src/types"
)

type migrateRequiredToOptional struct {
component types.ZarfComponent
}
type migrateRequiredToOptional struct{}

func (m migrateRequiredToOptional) name() string {
return RequiredToOptional
}

// If the component has already been migrated, clear the deprecated required.
func (m migrateRequiredToOptional) clear() types.ZarfComponent {
mc := m.component
func (m migrateRequiredToOptional) clear(mc types.ZarfComponent) types.ZarfComponent {
mc.DeprecatedRequired = nil
return mc
}

// migrate converts the deprecated required to the new optional
func (m migrateRequiredToOptional) migrate() (types.ZarfComponent, string) {
c := m.component

// run converts the deprecated required to the new optional
func (m migrateRequiredToOptional) run(c types.ZarfComponent) (types.ZarfComponent, string) {
if c.DeprecatedRequired == nil {
return c, ""
}
Expand Down
12 changes: 4 additions & 8 deletions src/pkg/packager/deprecated/scripts-to-actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,26 @@ import (
"github.com/defenseunicorns/zarf/src/types"
)

type migrateScriptsToActions struct {
component types.ZarfComponent
}
type migrateScriptsToActions struct{}

func (m migrateScriptsToActions) name() string {
return ScriptsToActions
}

// If the component has already been migrated, clear the deprecated scripts.
func (m migrateScriptsToActions) clear() types.ZarfComponent {
mc := m.component
func (m migrateScriptsToActions) clear(mc types.ZarfComponent) types.ZarfComponent {
mc.DeprecatedScripts = types.DeprecatedZarfComponentScripts{}
return mc
}

// migrate coverts the deprecated scripts to the new actions
// run coverts the deprecated scripts to the new actions
// The following have no migration:
// - Actions.Create.After
// - Actions.Remove.*
// - Actions.*.OnSuccess
// - Actions.*.OnFailure
// - Actions.*.*.Env
func (m migrateScriptsToActions) migrate() (types.ZarfComponent, string) {
c := m.component
func (m migrateScriptsToActions) run(c types.ZarfComponent) (types.ZarfComponent, string) {
var hasScripts bool

// Convert a script configs to action defaults.
Expand Down
3 changes: 3 additions & 0 deletions src/test/packages/03-deprecated-component-scripts/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ metadata:
components:
# Test that prepare scripts become onCreate actions
- name: 1-test-deprecated-prepare-scripts
optional: true
scripts:
prepare:
# on Windows, touch is replaced with New-Item
- touch test-deprecated-prepare-hook.txt

# Test that deploy scripts become onCreate actions
- name: 2-test-deprecated-deploy-scripts
optional: true
scripts:
before:
- touch test-deprecated-deploy-before-hook.txt
Expand All @@ -21,6 +23,7 @@ components:

# Test that script timeouts still get set
- name: 3-test-deprecated-timeout-scripts
optional: true
scripts:
timeoutSeconds: 1
before:
Expand Down

0 comments on commit e88fa82

Please sign in to comment.