-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support version contract for Talos config generation
This allows to generating current version Talos configs (by default) or backwards compatible configuration (e.g. for Talos 0.8). `talosctl gen config` defaults to current version, but explicit version can be passed to the command via flags. `talosctl cluster create` defaults to install/container image version, but that can be overridden. This makes `talosctl cluster create` now compatible with 0.8.1 images out of the box. Upgrade tests use contract based on source version in the test. When used as a library, `VersionContract` can be omitted (defaults to current version) or passed explicitly. `VersionContract` can be convienietly parsed from Talos version string or specified as one of the constants. Fixes #3130 Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
- Loading branch information
Showing
12 changed files
with
259 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package config | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
// VersionContract describes Talos version to generate config for. | ||
// | ||
// Config generation only supports backwards compatibility (e.g. Talos 0.9 can generate configs for Talos 0.9 and 0.8). | ||
// Matching version of the machinery package is required to generate configs for the current version of Talos. | ||
// | ||
// Nil value of *VersionContract always describes current version of Talos. | ||
type VersionContract struct { | ||
Major int | ||
Minor int | ||
} | ||
|
||
// Well-known Talos version contracts. | ||
var ( | ||
TalosVersionCurrent = (*VersionContract)(nil) | ||
TalosVersion0_9 = &VersionContract{0, 9} | ||
TalosVersion0_8 = &VersionContract{0, 8} | ||
) | ||
|
||
var versionRegexp = regexp.MustCompile(`^v(\d+)\.(\d+)($|\.)`) | ||
|
||
// ParseContractFromVersion parses Talos version into VersionContract. | ||
func ParseContractFromVersion(version string) (*VersionContract, error) { | ||
matches := versionRegexp.FindStringSubmatch(version) | ||
if len(matches) < 3 { | ||
return nil, fmt.Errorf("error parsing version %q", version) | ||
} | ||
|
||
var contract VersionContract | ||
|
||
contract.Major, _ = strconv.Atoi(matches[1]) //nolint: errcheck | ||
contract.Minor, _ = strconv.Atoi(matches[2]) //nolint: errcheck | ||
|
||
return &contract, nil | ||
} | ||
|
||
// Greater compares contract to another contract. | ||
func (contract *VersionContract) Greater(other *VersionContract) bool { | ||
if contract == nil { | ||
return other != nil | ||
} | ||
|
||
if other == nil { | ||
return false | ||
} | ||
|
||
return contract.Major > other.Major || (contract.Major == other.Major && contract.Minor > other.Minor) | ||
} | ||
|
||
// SupportsECDSAKeys returns true if version of Talos supports ECDSA keys (vs. RSA keys). | ||
func (contract *VersionContract) SupportsECDSAKeys() bool { | ||
return contract.Greater(TalosVersion0_8) | ||
} | ||
|
||
// SupportsAggregatorCA returns true if version of Talos supports AggregatorCA in the config. | ||
func (contract *VersionContract) SupportsAggregatorCA() bool { | ||
return contract.Greater(TalosVersion0_8) | ||
} | ||
|
||
// SupportsServiceAccount returns true if version of Talos supports ServiceAccount in the config. | ||
func (contract *VersionContract) SupportsServiceAccount() bool { | ||
return contract.Greater(TalosVersion0_8) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/talos-systems/talos/pkg/machinery/config" | ||
) | ||
|
||
func TestContractGreater(t *testing.T) { | ||
assert.True(t, config.TalosVersion0_9.Greater(config.TalosVersion0_8)) | ||
assert.True(t, config.TalosVersionCurrent.Greater(config.TalosVersion0_8)) | ||
assert.True(t, config.TalosVersionCurrent.Greater(config.TalosVersion0_9)) | ||
|
||
assert.False(t, config.TalosVersion0_8.Greater(config.TalosVersion0_9)) | ||
assert.False(t, config.TalosVersion0_8.Greater(config.TalosVersion0_8)) | ||
assert.False(t, config.TalosVersionCurrent.Greater(config.TalosVersionCurrent)) | ||
} | ||
|
||
func TestContractParseVersion(t *testing.T) { | ||
contract, err := config.ParseContractFromVersion("v0.8") | ||
assert.NoError(t, err) | ||
assert.Equal(t, config.TalosVersion0_8, contract) | ||
|
||
contract, err = config.ParseContractFromVersion("v0.8.1") | ||
assert.NoError(t, err) | ||
assert.Equal(t, config.TalosVersion0_8, contract) | ||
|
||
contract, err = config.ParseContractFromVersion("v0.88") | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, config.TalosVersion0_8, contract) | ||
|
||
contract, err = config.ParseContractFromVersion("v0.8.3-alpha.4") | ||
assert.NoError(t, err) | ||
assert.Equal(t, config.TalosVersion0_8, contract) | ||
} | ||
|
||
func TestContractCurrent(t *testing.T) { | ||
assert.True(t, config.TalosVersionCurrent.SupportsAggregatorCA()) | ||
assert.True(t, config.TalosVersionCurrent.SupportsECDSAKeys()) | ||
assert.True(t, config.TalosVersionCurrent.SupportsServiceAccount()) | ||
} | ||
|
||
func TestContract0_9(t *testing.T) { | ||
assert.True(t, config.TalosVersion0_9.SupportsAggregatorCA()) | ||
assert.True(t, config.TalosVersion0_9.SupportsECDSAKeys()) | ||
assert.True(t, config.TalosVersion0_9.SupportsServiceAccount()) | ||
} | ||
|
||
func TestContract0_8(t *testing.T) { | ||
assert.False(t, config.TalosVersion0_8.SupportsAggregatorCA()) | ||
assert.False(t, config.TalosVersion0_8.SupportsECDSAKeys()) | ||
assert.False(t, config.TalosVersion0_8.SupportsServiceAccount()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.