-
Notifications
You must be signed in to change notification settings - Fork 2.1k
automatic upgrades: use default version channel everywhere #35342
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -21,20 +21,60 @@ package automaticupgrades | |
| import ( | ||
| "context" | ||
| "net/url" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "golang.org/x/mod/semver" | ||
|
|
||
| "github.com/gravitational/teleport" | ||
| "github.com/gravitational/teleport/api/client/proto" | ||
| "github.com/gravitational/teleport/integrations/kube-agent-updater/pkg/maintenance" | ||
| "github.com/gravitational/teleport/integrations/kube-agent-updater/pkg/version" | ||
| ) | ||
|
|
||
| const ( | ||
| DefaultChannelName = "default" | ||
| DefaultCloudChannelName = "stable/cloud" | ||
| stableCloudVersionBaseURL = "https://updates.releases.teleport.dev/v1/stable/cloud" | ||
| ) | ||
|
|
||
| // Channels is a map of Channel objects. | ||
| type Channels map[string]*Channel | ||
|
|
||
| // CheckAndSetDefaults checks that every Channel is valid and initializes them. | ||
| func (c Channels) CheckAndSetDefaults() error { | ||
| // It also creates default channels if they are not already present. | ||
| // Cloud must have the `default` and `stable/cloud` channels. | ||
| // Self-hosted with automatic upgrades must have the `default` channel. | ||
| func (c Channels) CheckAndSetDefaults(features proto.Features) error { | ||
| defaultChannel, err := NewDefaultChannel() | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| // If we're on cloud, we need at least "cloud/stable" and "default" | ||
| if features.GetCloud() { | ||
| if _, ok := c[DefaultCloudChannelName]; !ok { | ||
| c[DefaultCloudChannelName] = defaultChannel | ||
| } | ||
| if _, ok := c[DefaultChannelName]; !ok { | ||
| c[DefaultChannelName] = c[DefaultCloudChannelName] | ||
| } | ||
| } | ||
|
|
||
| // If we're on self-hosted with automatic upgrades, we need a "default" channel | ||
| // We don't want to break existing setups so we'll automatically point to the | ||
| // `cloud/stable` channel. | ||
| // TODO: in v15 make this a hard requirement and error if `default` is not set | ||
| // and automatic upgrades are enabled | ||
|
Comment on lines
69
to
70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is for v15 |
||
| if features.GetAutomaticUpgrades() { | ||
| if _, ok := c[DefaultChannelName]; !ok { | ||
| c[DefaultChannelName] = defaultChannel | ||
| } | ||
| } | ||
|
|
||
| var errs []error | ||
| var err error | ||
| for name, channel := range c { | ||
| // Wrapping is not mandatory here, but it adds the channel name in the | ||
| // error, which makes troubleshooting easier. | ||
|
|
@@ -46,6 +86,16 @@ func (c Channels) CheckAndSetDefaults() error { | |
| return trace.NewAggregate(errs...) | ||
| } | ||
|
|
||
| // DefaultVersion returns the version served by the default upgrade channel. | ||
| func (c Channels) DefaultVersion(ctx context.Context) (string, error) { | ||
| channel, ok := c[DefaultChannelName] | ||
| if !ok { | ||
| return "", trace.NotFound("default version channel not found") | ||
| } | ||
| targetVersion, err := channel.GetVersion(ctx) | ||
| return targetVersion, trace.Wrap(err) | ||
| } | ||
|
|
||
| // Channel describes an automatic update channel configuration. | ||
| // It can be configured to serve a static version, or forward version requests | ||
| // to an upstream version server. Forwarded results are cached for 1 minute. | ||
|
|
@@ -61,6 +111,9 @@ type Channel struct { | |
| versionGetter version.Getter | ||
| // criticalTrigger gets the criticality of the channel. It is populated by CheckAndSetDefaults. | ||
| criticalTrigger maintenance.Trigger | ||
| // teleportMajor stores the current teleport major for comparison. | ||
| // This field is initialized during CheckAndSetDefaults. | ||
| teleportMajor int | ||
| } | ||
|
|
||
| // CheckAndSetDefaults checks that the Channel configuration is valid and inits | ||
|
|
@@ -83,17 +136,81 @@ func (c *Channel) CheckAndSetDefaults() error { | |
| default: | ||
| return trace.BadParameter("either ForwardURL or StaticVersion must be set") | ||
| } | ||
|
|
||
| var err error | ||
| c.teleportMajor, err = parseMajorFromVersionString(teleport.Version) | ||
| if err != nil { | ||
| return trace.Wrap(err, "failed to process teleport version") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // GetVersion returns the current version of the channel. If io is involved, | ||
| // this function implements cache and is safe to call frequently. | ||
| // If the target version major is higher than the Teleport version (the one | ||
| // in the Teleport binary, this is usually the proxy version), this function | ||
| // returns the Teleport version instead. | ||
| // If the version source intentionally did not specify a version, a | ||
| // NoNewVersionError is returned. | ||
| func (c *Channel) GetVersion(ctx context.Context) (string, error) { | ||
| return c.versionGetter.GetVersion(ctx) | ||
| targetVersion, err := c.versionGetter.GetVersion(ctx) | ||
| if err != nil { | ||
| return "", trace.Wrap(err) | ||
| } | ||
|
|
||
| targetMajor, err := parseMajorFromVersionString(targetVersion) | ||
| if err != nil { | ||
| return "", trace.Wrap(err, "failed to process target version") | ||
| } | ||
|
|
||
| // The target version is officially incompatible with our version, | ||
| // we prefer returning our version rather than having a broken client | ||
| if targetMajor > c.teleportMajor { | ||
| return teleport.Version, nil | ||
| } | ||
|
|
||
| return targetVersion, nil | ||
| } | ||
|
|
||
| // GetCritical returns the current criticality of the channel. If io is involved, | ||
| // this function implements cache and is safe to call frequently. | ||
| func (c *Channel) GetCritical(ctx context.Context) (bool, error) { | ||
| return c.criticalTrigger.CanStart(ctx, nil) | ||
| } | ||
|
|
||
| // NewDefaultChannel creates a default automatic upgrade channel | ||
| // It looks up the environment variable, and if not found uses the default | ||
| // base URL. This default channel can be used in the proxy (to back its own version server) | ||
| // or in other Teleport process such as integration services deploying and | ||
| // updating teleport agents. | ||
| func NewDefaultChannel() (*Channel, error) { | ||
| return sync.OnceValues[*Channel, error]( | ||
| func() (*Channel, error) { | ||
| forwardURL := GetChannel() | ||
| if forwardURL == "" { | ||
| forwardURL = stableCloudVersionBaseURL | ||
| } | ||
| defaultChannel := &Channel{ | ||
| ForwardURL: forwardURL, | ||
| } | ||
| if err := defaultChannel.CheckAndSetDefaults(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| return defaultChannel, nil | ||
| })() | ||
| } | ||
|
|
||
| func parseMajorFromVersionString(v string) (int, error) { | ||
| v, err := version.EnsureSemver(v) | ||
| if err != nil { | ||
| return 0, trace.Wrap(err, "invalid semver: %s", v) | ||
| } | ||
| majorStr := semver.Major(v) | ||
| if majorStr == "" { | ||
| return 0, trace.BadParameter("cannot detect version major") | ||
| } | ||
|
|
||
| major, err := strconv.Atoi(strings.TrimPrefix(majorStr, "v")) | ||
| return major, trace.Wrap(err, "cannot convert version major to int") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.