-
Notifications
You must be signed in to change notification settings - Fork 825
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
Fix wrong condition check for Memory limit #1418
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2020 Google LLC All Rights Reserved. | ||
// | ||
// Licensed 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 main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func TestControllerConfigValidation(t *testing.T) { | ||
t.Parallel() | ||
|
||
c := config{MinPort: 10, MaxPort: 2} | ||
errs := c.validate() | ||
assert.Len(t, errs, 1) | ||
errorsContainString(t, errs, "max Port cannot be set less that the Min Port") | ||
|
||
c.SidecarMemoryRequest = resource.MustParse("2Gi") | ||
c.SidecarMemoryLimit = resource.MustParse("1Gi") | ||
errs = c.validate() | ||
assert.Len(t, errs, 2) | ||
errorsContainString(t, errs, "Request must be less than or equal to memory limit") | ||
|
||
c.SidecarMemoryLimit = resource.MustParse("2Gi") | ||
c.SidecarCPURequest = resource.MustParse("2m") | ||
c.SidecarCPULimit = resource.MustParse("1m") | ||
errs = c.validate() | ||
assert.Len(t, errs, 2) | ||
errorsContainString(t, errs, "Request must be less than or equal to cpu limit") | ||
|
||
c.SidecarMemoryLimit = resource.MustParse("2Gi") | ||
c.SidecarCPURequest = resource.MustParse("-2m") | ||
c.SidecarCPULimit = resource.MustParse("2m") | ||
errs = c.validate() | ||
assert.Len(t, errs, 2) | ||
errorsContainString(t, errs, "Resource cpu request value must be non negative") | ||
} | ||
|
||
func errorsContainString(t *testing.T, errs []error, expected string) { | ||
found := false | ||
for _, v := range errs { | ||
if expected == v.Error() { | ||
found = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can safely break the loop here because there is no point to check other elements of the slice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added break |
||
} | ||
} | ||
assert.True(t, found, "Was not able to find '%s'", expected) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import ( | |
"github.com/pkg/errors" | ||
admregv1b "k8s.io/api/admissionregistration/v1beta1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
) | ||
|
@@ -456,6 +457,24 @@ func (gss *GameServerSpec) Validate(devAddress string) ([]metav1.StatusCause, bo | |
return causes, len(causes) == 0 | ||
} | ||
|
||
// ValidateResource validates limit or Memory CPU resources used for containers in pods | ||
// If a GameServer is invalid there will be > 0 values in | ||
// the returned array | ||
func ValidateResource(request resource.Quantity, limit resource.Quantity, resourceName corev1.ResourceName) []error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have placed ValidateResource() here so we will check PodTemplate in a Fleet and GameServer using exact same function. Which is part of #1298 |
||
validationErrors := make([]error, 0) | ||
if !limit.IsZero() && request.Cmp(limit) > 0 { | ||
validationErrors = append(validationErrors, errors.New(fmt.Sprintf("Request must be less than or equal to %s limit", resourceName))) | ||
} | ||
if request.Cmp(resource.Quantity{}) < 0 { | ||
validationErrors = append(validationErrors, errors.New(fmt.Sprintf("Resource %s request value must be non negative", resourceName))) | ||
} | ||
if limit.Cmp(resource.Quantity{}) < 0 { | ||
validationErrors = append(validationErrors, errors.New(fmt.Sprintf("Resource %s limit value must be non negative", resourceName))) | ||
} | ||
|
||
return validationErrors | ||
} | ||
|
||
// Validate validates the GameServer configuration. | ||
// If a GameServer is invalid there will be > 0 values in | ||
// the returned array | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this. 👍