diff --git a/pkg/api/v1beta2/dynakube/validation/oneagent.go b/pkg/api/v1beta2/dynakube/validation/oneagent.go index 9e7958db47..673cadc2d1 100644 --- a/pkg/api/v1beta2/dynakube/validation/oneagent.go +++ b/pkg/api/v1beta2/dynakube/validation/oneagent.go @@ -3,9 +3,11 @@ package validation import ( "context" "fmt" + "strings" "github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta2/dynakube" "github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/env" + "golang.org/x/mod/semver" "k8s.io/apimachinery/pkg/labels" ) @@ -139,3 +141,17 @@ func conflictingHostGroupSettings(_ context.Context, _ *Validator, dk *dynakube. return "" } + +func validateOneAgentVersionIsSemVerCompliant(_ context.Context, _ *Validator, dk *dynakube.DynaKube) string { + agentVersion := dk.CustomOneAgentVersion() + if agentVersion == "" { + return "" + } + + version := "v" + agentVersion + if !(semver.IsValid(version) && semver.Prerelease(version) == "" && semver.Build(version) == "" && len(strings.Split(version, ".")) == 3) { + return "Only semantic versions in the form of major.minor.patch (e.g. 1.0.0) are allowed!" + } + + return "" +} diff --git a/pkg/api/v1beta2/dynakube/validation/oneagent_test.go b/pkg/api/v1beta2/dynakube/validation/oneagent_test.go index be03b03460..5c6495d849 100644 --- a/pkg/api/v1beta2/dynakube/validation/oneagent_test.go +++ b/pkg/api/v1beta2/dynakube/validation/oneagent_test.go @@ -424,3 +424,41 @@ func createDynakubeWithHostGroup(args []string, hostGroup string) *dynakube.Dyna }, } } + +func TestValidateOneAgentVersionIsSemVer(t *testing.T) { + testCasesAcceptedVersions := []string{"", "1.0.0", "1.200.1"} + + testCasesNotAcceptedVersions := []string{"latest", "raw", "1.200.1-raw", "v1.200.1-raw", "1.200.1+build", "v1.200.1+build", "1.200.1-raw+build", "v1.200.1-raw+build", "1.200", "v1.200", "1", "v1", "1.0", "v1.0", "v1.200.0"} + + for _, tc := range testCasesAcceptedVersions { + t.Run("should accept version "+tc, func(t *testing.T) { + assertAllowed(t, &dynakube.DynaKube{ + ObjectMeta: defaultDynakubeObjectMeta, + Spec: dynakube.DynaKubeSpec{ + APIURL: testApiUrl, + OneAgent: dynakube.OneAgentSpec{ + ClassicFullStack: &dynakube.HostInjectSpec{ + Version: tc, + }, + }, + }, + }) + }) + } + + for _, tc := range testCasesNotAcceptedVersions { + t.Run("should accept version "+tc, func(t *testing.T) { + assertDenied(t, []string{"Only semantic versions in the form of major.minor.patch (e.g. 1.0.0) are allowed!"}, &dynakube.DynaKube{ + ObjectMeta: defaultDynakubeObjectMeta, + Spec: dynakube.DynaKubeSpec{ + APIURL: testApiUrl, + OneAgent: dynakube.OneAgentSpec{ + ClassicFullStack: &dynakube.HostInjectSpec{ + Version: tc, + }, + }, + }, + }) + }) + } +} diff --git a/pkg/api/v1beta2/dynakube/validation/validation.go b/pkg/api/v1beta2/dynakube/validation/validation.go index 0071649294..414d1222bf 100644 --- a/pkg/api/v1beta2/dynakube/validation/validation.go +++ b/pkg/api/v1beta2/dynakube/validation/validation.go @@ -37,6 +37,7 @@ var ( nameTooLong, namespaceSelectorViolateLabelSpec, imageFieldHasTenantImage, + validateOneAgentVersionIsSemVerCompliant, } validatorWarningFuncs = []validatorFunc{ missingActiveGateMemoryLimit,