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

Adds warning for missing memory limit for activeGate #538

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions src/api/v1beta1/activegate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ var (
)

var ActiveGateDisplayNames = map[CapabilityDisplayName]struct{}{
RoutingCapability.DisplayName: struct{}{},
KubeMonCapability.DisplayName: struct{}{},
MetricsIngestCapability.DisplayName: struct{}{},
DynatraceApiCapability.DisplayName: struct{}{},
RoutingCapability.DisplayName: {},
KubeMonCapability.DisplayName: {},
MetricsIngestCapability.DisplayName: {},
DynatraceApiCapability.DisplayName: {},
}

type ActiveGateSpec struct {
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions src/webhook/validation/activegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/src/api/v1beta1"
corev1 "k8s.io/api/core/v1"
)

const (
Expand All @@ -17,6 +18,7 @@ Make sure you correctly specify the ActiveGate capabilities in your custom resou
errorDuplicateActiveGateCapability = `The DynaKube's specification tries to specify duplicate capabilities in the ActiveGate section, duplicate capability=%s.
Make sure you don't duplicate an Activegate capability in your custom resource.
`
warningMissingActiveGateMemoryLimit = `ActiveGate specification missing memory limits. Can cause excess memory usage.`
)

func conflictingActiveGateConfiguration(dv *dynakubeValidator, dynakube *dynatracev1beta1.DynaKube) string {
Expand Down Expand Up @@ -54,3 +56,16 @@ func invalidActiveGateCapabilities(dv *dynakubeValidator, dynakube *dynatracev1b
}
return ""
}

func missingActiveGateMemoryLimit(dv *dynakubeValidator, dynakube *dynatracev1beta1.DynaKube) string {
if dynakube.ActiveGateMode() {
if !memoryLimitSet(dynakube.Spec.ActiveGate.Resources) {
return warningMissingActiveGateMemoryLimit
}
}
return ""
}

func memoryLimitSet(resources corev1.ResourceRequirements) bool {
return resources.Limits != nil && resources.Limits.Memory() != nil
}
47 changes: 45 additions & 2 deletions src/webhook/validation/activegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/src/api/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

func TestConflictingActiveGateConfiguration(t *testing.T) {
Expand All @@ -23,7 +25,7 @@ func TestConflictingActiveGateConfiguration(t *testing.T) {
},
})

assertAllowedResponseWithoutWarnings(t, &dynatracev1beta1.DynaKube{
assertAllowedResponseWithWarnings(t, 1, &dynatracev1beta1.DynaKube{
ObjectMeta: defaultDynakubeObjectMeta,
Spec: dynatracev1beta1.DynaKubeSpec{
APIURL: testApiUrl,
Expand All @@ -36,7 +38,7 @@ func TestConflictingActiveGateConfiguration(t *testing.T) {
},
})

assertAllowedResponseWithWarnings(t, 2, &dynatracev1beta1.DynaKube{
assertAllowedResponseWithWarnings(t, 3, &dynatracev1beta1.DynaKube{
ObjectMeta: defaultDynakubeObjectMeta,
Spec: dynatracev1beta1.DynaKubeSpec{
APIURL: testApiUrl,
Expand Down Expand Up @@ -106,3 +108,44 @@ func TestInvalidActiveGateCapabilities(t *testing.T) {
})
})
}

func TestMissingActiveGateMemoryLimit(t *testing.T) {
t.Run(`memory warning in activeGate mode`, func(t *testing.T) {
assertAllowedResponseWithWarnings(t, 1,
&dynatracev1beta1.DynaKube{
ObjectMeta: defaultDynakubeObjectMeta,
Spec: dynatracev1beta1.DynaKubeSpec{
APIURL: testApiUrl,
ActiveGate: dynatracev1beta1.ActiveGateSpec{
Capabilities: []dynatracev1beta1.CapabilityDisplayName{
dynatracev1beta1.RoutingCapability.DisplayName,
},
CapabilityProperties: dynatracev1beta1.CapabilityProperties{
Resources: corev1.ResourceRequirements{},
},
},
},
})
})
t.Run(`no memory warning in activeGate mode with memory limit`, func(t *testing.T) {
assertAllowedResponseWithoutWarnings(t,
&dynatracev1beta1.DynaKube{
ObjectMeta: defaultDynakubeObjectMeta,
Spec: dynatracev1beta1.DynaKubeSpec{
APIURL: testApiUrl,
ActiveGate: dynatracev1beta1.ActiveGateSpec{
Capabilities: []dynatracev1beta1.CapabilityDisplayName{
dynatracev1beta1.RoutingCapability.DisplayName,
},
CapabilityProperties: dynatracev1beta1.CapabilityProperties{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceLimitsMemory: *resource.NewMilliQuantity(1, ""),
},
},
},
},
},
})
})
}
1 change: 1 addition & 0 deletions src/webhook/validation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ var validators = []validator{
var warnings = []validator{
oneAgentModePreviewWarning,
metricIngestPreviewWarning,
missingActiveGateMemoryLimit,
}
14 changes: 13 additions & 1 deletion src/webhook/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strings"

dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/src/api/v1beta1"
"github.com/Dynatrace/dynatrace-operator/src/scheme"
Expand Down Expand Up @@ -53,7 +54,9 @@ func (validator *dynakubeValidator) Handle(_ context.Context, request admission.
}
warningMessages := validator.runValidators(warnings, dynakube)
if len(warningMessages) > 0 {
warningMessages = append(warningMessages, basePreviewWarning)
if hasPreviewWarning(warningMessages) {
warningMessages = append(warningMessages, basePreviewWarning)
}
response = response.WithWarnings(warningMessages...)
}
return response
Expand Down Expand Up @@ -89,3 +92,12 @@ func decodeRequestToDynakube(request admission.Request, dynakube *dynatracev1bet
}
return nil
}

func hasPreviewWarning(warnings []string) bool {
for _, warning := range warnings {
if strings.Contains(warning, "PREVIEW") {
return true
}
}
return false
}
6 changes: 3 additions & 3 deletions src/webhook/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var dummyNamespace2 = corev1.Namespace{
func TestDynakubeValidator_Handle(t *testing.T) {
t.Run(`valid dynakube specs`, func(t *testing.T) {

assertAllowedResponseWithWarnings(t, 3, &dynatracev1beta1.DynaKube{
assertAllowedResponseWithWarnings(t, 4, &dynatracev1beta1.DynaKube{
ObjectMeta: defaultDynakubeObjectMeta,
Spec: dynatracev1beta1.DynaKubeSpec{
APIURL: testApiUrl,
Expand Down Expand Up @@ -191,12 +191,12 @@ func assertDeniedResponse(t *testing.T, errMessages []string, dynakube *dynatrac

func assertAllowedResponseWithoutWarnings(t *testing.T, dynakube *dynatracev1beta1.DynaKube, other ...client.Object) {
response := assertAllowedResponse(t, dynakube, other...)
assert.Equal(t, len(response.Warnings), 0)
assert.Equal(t, 0, len(response.Warnings))
}

func assertAllowedResponseWithWarnings(t *testing.T, warningAmount int, dynakube *dynatracev1beta1.DynaKube, other ...client.Object) {
response := assertAllowedResponse(t, dynakube, other...)
assert.Equal(t, len(response.Warnings), warningAmount)
assert.Equal(t, warningAmount, len(response.Warnings))
}

func assertAllowedResponse(t *testing.T, dynakube *dynatracev1beta1.DynaKube, other ...client.Object) admission.Response {
Expand Down