Skip to content
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

Add Verification of OneAgent Version #3405

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading