generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
650 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const limitsYAML = ` | ||
MemoryPerCoreLimits: | ||
- NodeSelector: | ||
matchExpressions: | ||
- key: class | ||
operator: DoesNotExist | ||
Limit: 2Gi | ||
` | ||
const limitYAML = ` | ||
MemoryPerCoreLimit: 1Gi | ||
` | ||
|
||
func Test_Config_MemoryPerCoreLimits(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
yaml string | ||
warnings int | ||
limit string | ||
nodeSelector metav1.LabelSelector | ||
}{ | ||
{ | ||
desc: "MemoryPerCoreLimits", | ||
yaml: limitsYAML, | ||
warnings: 0, | ||
limit: "2Gi", | ||
nodeSelector: metav1.LabelSelector{ | ||
MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
{ | ||
Key: "class", | ||
Operator: metav1.LabelSelectorOpDoesNotExist, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
desc: "MemoryPerCoreLimit_Migrate", | ||
yaml: limitYAML, | ||
warnings: 1, | ||
limit: "1Gi", | ||
nodeSelector: metav1.LabelSelector{}, | ||
}, | ||
{ | ||
desc: "MemoryPerCoreLimit_NoMigrateIfMemoryPerCoreLimitsIsSet", | ||
yaml: strings.Join([]string{limitsYAML, limitYAML}, "\n"), | ||
warnings: 0, | ||
limit: "2Gi", | ||
nodeSelector: metav1.LabelSelector{ | ||
MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
{ | ||
Key: "class", | ||
Operator: metav1.LabelSelectorOpDoesNotExist, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tC := range testCases { | ||
t.Run(tC.desc, func(t *testing.T) { | ||
tmp := t.TempDir() | ||
configPath := filepath.Join(tmp, "config.yaml") | ||
require.NoError(t, os.WriteFile(configPath, []byte(tC.yaml), 0o644)) | ||
|
||
c, warnings, err := ConfigFromFile(configPath) | ||
assert.Len(t, warnings, tC.warnings) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, tC.limit, c.MemoryPerCoreLimits[0].Limit.String()) | ||
assert.Equal(t, tC.nodeSelector, c.MemoryPerCoreLimits[0].NodeSelector) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package limits | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
) | ||
|
||
type Limit struct { | ||
NodeSelector metav1.LabelSelector | ||
Limit *resource.Quantity | ||
} | ||
|
||
type Limits []Limit | ||
|
||
// GetLimitForNodeSelector returns the first limit that matches the given node selector. | ||
// If no limit matches, an empty string is returned. | ||
// Limits with invalid node selectors are ignored. | ||
func (l Limits) GetLimitForNodeSelector(nodeSelector map[string]string) *resource.Quantity { | ||
for _, limit := range l { | ||
limitSel, err := metav1.LabelSelectorAsSelector(&limit.NodeSelector) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
if limitSel.Matches(labels.Set(nodeSelector)) { | ||
return limit.Limit | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.