-
Notifications
You must be signed in to change notification settings - Fork 536
iserver-test: Overhaul upgrade config #19020
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
Open
ericywl
wants to merge
6
commits into
main
Choose a base branch
from
iserver-test-uprade-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+559
−107
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
79f9519
Add new upgrade config
ericywl 2c3f122
Make update
ericywl 7e97f1e
Refactor lazy-rollover-exceptions config structure
ericywl b5f66e3
Merge branch 'main' into iserver-test-uprade-config
ericywl 7cf1764
Apply suggestions from code review
ericywl addbaa8
Add validation for special wildcard x
ericywl 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,304 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package integrationservertest | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/elastic/apm-server/integrationservertest/internal/ech" | ||
) | ||
|
||
const ( | ||
upgradeConfigFilename = "upgrade-config.yaml" | ||
dockerImageOverrideFilename = "docker-image-override.yaml" | ||
) | ||
|
||
type upgradeTestConfig struct { | ||
DataStreamLifecycle map[string]string | ||
LazyRolloverExceptions []lazyRolloverException | ||
} | ||
|
||
// ExpectedLifecycle returns the lifecycle management that is expected of the provided version. | ||
func (cfg upgradeTestConfig) ExpectedLifecycle(version ech.Version) string { | ||
lifecycle, ok := cfg.DataStreamLifecycle[version.MajorMinor()] | ||
if !ok { | ||
return managedByILM | ||
} | ||
if strings.EqualFold(lifecycle, "DSL") { | ||
return managedByDSL | ||
} | ||
return managedByILM | ||
} | ||
|
||
// LazyRollover checks if the upgrade path is expected to have lazy rollover. | ||
func (cfg upgradeTestConfig) LazyRollover(from, to ech.Version) bool { | ||
for _, e := range cfg.LazyRolloverExceptions { | ||
if e.matchVersions(from, to) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func parseConfigFile(filename string) (upgradeTestConfig, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return upgradeTestConfig{}, fmt.Errorf("failed to open %s: %w", filename, err) | ||
} | ||
|
||
defer f.Close() | ||
return parseConfig(f) | ||
} | ||
|
||
func parseConfig(reader io.Reader) (upgradeTestConfig, error) { | ||
type lazyRolloverExceptionYAML struct { | ||
From string `yaml:"from"` | ||
To string `yaml:"to"` | ||
} | ||
|
||
type upgradeTestConfigYAML struct { | ||
DataStreamLifecycle map[string]string `yaml:"data-stream-lifecycle"` | ||
LazyRolloverExceptions []lazyRolloverExceptionYAML `yaml:"lazy-rollover-exceptions"` | ||
} | ||
|
||
b, err := io.ReadAll(reader) | ||
if err != nil { | ||
return upgradeTestConfig{}, fmt.Errorf("failed to read config: %w", err) | ||
} | ||
|
||
configYAML := upgradeTestConfigYAML{} | ||
if err := yaml.Unmarshal(b, &configYAML); err != nil { | ||
return upgradeTestConfig{}, fmt.Errorf("failed to unmarshal upgrade test config: %w", err) | ||
} | ||
|
||
config := upgradeTestConfig{ | ||
DataStreamLifecycle: configYAML.DataStreamLifecycle, | ||
} | ||
|
||
for _, e := range configYAML.LazyRolloverExceptions { | ||
from, err := parseLazyRolloverExceptionVersionRange(e.From) | ||
if err != nil { | ||
return upgradeTestConfig{}, fmt.Errorf( | ||
"failed to parse lazy-rollover-exception version '%s': %w", e.From, err) | ||
} | ||
to, err := parseLazyRolloverExceptionVersionRange(e.To) | ||
if err != nil { | ||
return upgradeTestConfig{}, fmt.Errorf( | ||
"failed to parse lazy-rollover-exception version '%s': %w", e.From, err) | ||
ericywl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
config.LazyRolloverExceptions = append(config.LazyRolloverExceptions, lazyRolloverException{ | ||
From: from, | ||
To: to, | ||
}) | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
type lazyRolloverException struct { | ||
From lreVersion | ||
To lreVersion | ||
} | ||
|
||
func (e lazyRolloverException) matchVersions(from, to ech.Version) bool { | ||
// Either version not in range. | ||
if !e.From.matchVersion(from) || !e.To.matchVersion(to) { | ||
return false | ||
} | ||
// If both pattern have minor x, check if both version minors are the same. | ||
if e.From.isSingularWithMinorX() && e.To.isSingularWithMinorX() { | ||
return from.Minor == to.Minor | ||
} | ||
return true | ||
} | ||
|
||
type lreVersion struct { | ||
Singular *wildcardVersion // Nil if range. | ||
Range *lreVersionRange // Nil if singular version. | ||
} | ||
|
||
func (r lreVersion) matchVersion(version ech.Version) bool { | ||
// Range of versions. | ||
if r.Range != nil { | ||
return r.Range.inRange(version) | ||
} | ||
|
||
// Singular version only. | ||
if r.Singular.Major != version.Major { | ||
return false | ||
} | ||
// Both wildcards, return true since major is equal. | ||
if r.Singular.Minor.isWildcard() && r.Singular.Patch.isWildcard() { | ||
return true | ||
} | ||
// Only patch is wildcard, check minor. | ||
if r.Singular.Patch.isWildcard() { | ||
return *r.Singular.Minor.Num == version.Minor | ||
} | ||
// Only minor is wildcard, check patch. | ||
return *r.Singular.Patch.Num == version.Patch | ||
} | ||
|
||
func (r lreVersion) isSingularWithMinorX() bool { | ||
return r.Singular != nil && r.Singular.Minor.isX() | ||
} | ||
|
||
type lreVersionRange struct { | ||
Start ech.Version | ||
InclusiveStart bool | ||
End ech.Version | ||
InclusiveEnd bool | ||
} | ||
|
||
func (r lreVersionRange) inRange(version ech.Version) bool { | ||
cmpStart := r.Start.Compare(version) < 0 | ||
if r.InclusiveStart { | ||
cmpStart = r.Start.Compare(version) <= 0 | ||
} | ||
cmpEnd := r.End.Compare(version) > 0 | ||
if r.InclusiveEnd { | ||
cmpEnd = r.End.Compare(version) >= 0 | ||
} | ||
return cmpStart && cmpEnd | ||
} | ||
|
||
type wildcardVersion struct { | ||
Major uint64 | ||
Minor numberOrWildcard | ||
Patch numberOrWildcard | ||
} | ||
|
||
type numberOrWildcard struct { | ||
Num *uint64 // Not nil if Str is a number. | ||
Str string | ||
} | ||
|
||
func (n numberOrWildcard) isWildcard() bool { | ||
return n.Num == nil | ||
} | ||
|
||
func (n numberOrWildcard) isX() bool { | ||
return n.Str == "x" | ||
} | ||
|
||
func (n numberOrWildcard) isStar() bool { | ||
return n.Str == "*" | ||
} | ||
|
||
func parseNumberOrWildcard(s string) (numberOrWildcard, error) { | ||
if s == "*" || s == "x" { | ||
return numberOrWildcard{Str: s}, nil | ||
} | ||
|
||
num, err := strconv.ParseUint(s, 10, 64) | ||
if err != nil { | ||
return numberOrWildcard{}, err | ||
} | ||
return numberOrWildcard{Num: &num, Str: s}, nil | ||
} | ||
|
||
var ( | ||
lazyRolloverVersionRg = regexp.MustCompile(`^(\d+).(\d+|\*|x).(\d+|\*)$`) | ||
lazyRolloverVersionRangeRg = regexp.MustCompile(`^([\[(])\s*(\d+.\d+.\d+)\s*-\s*(\d+.\d+.\d+)\s*([])])$`) | ||
) | ||
|
||
func parseLazyRolloverExceptionVersionRange(s string) (lreVersion, error) { | ||
// Range of versions. | ||
matches := lazyRolloverVersionRangeRg.FindStringSubmatch(s) | ||
if len(matches) > 0 { | ||
inclusiveStart := matches[1] == "[" | ||
inclusiveEnd := matches[4] == "]" | ||
start, err := ech.NewVersionFromString(matches[2]) | ||
if err != nil { | ||
return lreVersion{}, fmt.Errorf("failed to parse '%s' start: %w", s, err) | ||
} | ||
end, err := ech.NewVersionFromString(matches[3]) | ||
if err != nil { | ||
return lreVersion{}, fmt.Errorf("failed to parse '%s' end: %w", s, err) | ||
} | ||
return lreVersion{ | ||
Range: &lreVersionRange{ | ||
Start: start, | ||
InclusiveStart: inclusiveStart, | ||
End: end, | ||
InclusiveEnd: inclusiveEnd, | ||
}, | ||
}, nil | ||
} | ||
|
||
// Singular version only. | ||
matches = lazyRolloverVersionRg.FindStringSubmatch(s) | ||
if len(matches) > 0 { | ||
major, err := strconv.ParseUint(matches[1], 10, 64) | ||
if err != nil { | ||
return lreVersion{}, fmt.Errorf("failed to parse '%s' major: %w", s, err) | ||
} | ||
minor, err := parseNumberOrWildcard(matches[2]) | ||
if err != nil { | ||
return lreVersion{}, fmt.Errorf("failed to parse '%s' minor: %w", s, err) | ||
} | ||
patch, err := parseNumberOrWildcard(matches[3]) | ||
if err != nil { | ||
return lreVersion{}, fmt.Errorf("failed to parse '%s' patch: %w", s, err) | ||
} | ||
return lreVersion{ | ||
Singular: &wildcardVersion{ | ||
Major: major, | ||
Minor: minor, | ||
Patch: patch, | ||
}, | ||
}, nil | ||
} | ||
|
||
return lreVersion{}, fmt.Errorf("invalid version pattern '%s'", s) | ||
} | ||
|
||
func parseDockerImageOverride(filename string) (map[ech.Version]*dockerImageOverrideConfig, error) { | ||
b, err := os.ReadFile(filename) | ||
if err != nil { | ||
// File does not exist, fallback to no overrides. | ||
if errors.Is(err, os.ErrNotExist) { | ||
return map[ech.Version]*dockerImageOverrideConfig{}, nil | ||
} | ||
return nil, fmt.Errorf("failed to read %s: %w", filename, err) | ||
} | ||
|
||
config := map[string]*dockerImageOverrideConfig{} | ||
if err = yaml.Unmarshal(b, &config); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal docker image override config: %w", err) | ||
} | ||
|
||
result := map[ech.Version]*dockerImageOverrideConfig{} | ||
for k, v := range config { | ||
version, err := ech.NewVersionFromString(k) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid version in docker image override config: %w", err) | ||
} | ||
result[version] = v | ||
} | ||
|
||
return result, nil | ||
} |
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.