Skip to content

Commit

Permalink
state: add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
3u13r committed Nov 13, 2023
1 parent 5af6ee0 commit 619f679
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cli/internal/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ The control flow is as follows:
└────────────────────┘
*/
func (a *applyCmd) apply(cmd *cobra.Command, configFetcher attestationconfigapi.Fetcher, upgradeDir string) error {
// Migrate state file
stateFile, err := state.ReadFromFile(a.fileHandler, constants.StateFilename)
if err != nil {
return err
}
if err := stateFile.Migrate(); err != nil {
return err
}

// Validate inputs
conf, stateFile, err := a.validateInputs(cmd, configFetcher)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cli/internal/state/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ go_library(
visibility = ["//cli:__subpackages__"],
deps = [
"//internal/cloud/cloudprovider",
"//internal/constants",
"//internal/file",
"//internal/validation",
"@cat_dario_mergo//:mergo",
"@com_github_siderolabs_talos_pkg_machinery//config/encoder",
"@org_golang_x_mod//semver",
],
)

Expand Down
19 changes: 19 additions & 0 deletions cli/internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (

"dario.cat/mergo"
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file"
"github.com/edgelesssys/constellation/v2/internal/validation"
"golang.org/x/mod/semver"
)

const (
Expand Down Expand Up @@ -555,6 +557,23 @@ func (s *State) Constraints() []*validation.Constraint {
return []*validation.Constraint{}
}

// Migrate migrates the state to the current version.
// This is mostly done to pass the validation of the current version.
// The infrastructure will be overwritten by the terraform outputs after the validation.
func (s *State) Migrate() error {
// In v2.13.0 the ClusterEndpoint and InClusterEndpoint fields were added.
// So they are expected to be empty when upgrading to this version.
if semver.Compare(constants.BinaryVersion().String(), "v2.13.0") != -1 {
if s.Infrastructure.InClusterEndpoint == "" {
s.Infrastructure.InClusterEndpoint = s.Infrastructure.ClusterEndpoint
}
if s.Infrastructure.IPCidrNode == "" {
s.Infrastructure.IPCidrNode = "192.168.2.1"
}
}
return nil
}

// HexBytes is a byte slice that is marshalled to and from a hex string.
type HexBytes []byte

Expand Down

0 comments on commit 619f679

Please sign in to comment.