Skip to content

Commit

Permalink
Add Verification of OneAgent Version (#3405) (#3459)
Browse files Browse the repository at this point in the history
Co-authored-by: Werner Putschögl <[email protected]>
Co-authored-by: adam.orcholski <[email protected]>
  • Loading branch information
3 people authored Jul 12, 2024
1 parent 8dc6386 commit 064c700
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/api/v1beta2/dynakube/validation/oneagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 ""
}
38 changes: 38 additions & 0 deletions pkg/api/v1beta2/dynakube/validation/oneagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
})
})
}
}
1 change: 1 addition & 0 deletions pkg/api/v1beta2/dynakube/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
nameTooLong,
namespaceSelectorViolateLabelSpec,
imageFieldHasTenantImage,
validateOneAgentVersionIsSemVerCompliant,
}
validatorWarningFuncs = []validatorFunc{
missingActiveGateMemoryLimit,
Expand Down

0 comments on commit 064c700

Please sign in to comment.