-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support resource sum. (#463)
* feat: add support resource sum. * feat: add pure SubjectAccessReview check * feat: add cluster list. * fix: rbac RequestSubjectAccessReview. * fix: sum resource list. * fix: update resource. * fix: pre alloc silice.
- Loading branch information
Showing
5 changed files
with
260 additions
and
4 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,18 @@ | ||
/* | ||
Copyright 2023 The Katanomi Authors. | ||
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 resource provides some common operations on ResourceRequirements resources. | ||
package resource |
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,59 @@ | ||
/* | ||
Copyright 2023 The Katanomi Authors. | ||
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 resource | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// SumResources sums many ResourceRequirements resources. | ||
// Note: No operations are performed on Claims. only the sum of Limits and Requests is completed. | ||
func SumResources(resources ...corev1.ResourceRequirements) corev1.ResourceRequirements { | ||
if len(resources) == 0 { | ||
return corev1.ResourceRequirements{} | ||
} | ||
|
||
sumLimits := make([]corev1.ResourceList, 0, len(resources)) | ||
sumRequests := make([]corev1.ResourceList, 0, len(resources)) | ||
for _, item := range resources { | ||
sumLimits = append(sumLimits, item.Limits) | ||
sumRequests = append(sumRequests, item.Requests) | ||
} | ||
|
||
return corev1.ResourceRequirements{ | ||
Limits: SumResourceList(sumLimits...), | ||
Requests: SumResourceList(sumRequests...), | ||
} | ||
} | ||
|
||
// SumResourceList sums many ResourceList resources. | ||
func SumResourceList(list ...corev1.ResourceList) corev1.ResourceList { | ||
result := corev1.ResourceList{} | ||
for _, item := range list { | ||
for key, itemQuantity := range item { | ||
tmpQuantity := itemQuantity | ||
if v, ok := result[key]; ok { | ||
v.Add(itemQuantity) | ||
result[key] = v | ||
} else { | ||
result[key] = tmpQuantity | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} |
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,151 @@ | ||
/* | ||
Copyright 2023 The Katanomi Authors. | ||
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 resource | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func TestSumResources(t *testing.T) { | ||
tests := map[string]struct { | ||
param1 corev1.ResourceRequirements | ||
param2 corev1.ResourceRequirements | ||
want corev1.ResourceRequirements | ||
}{ | ||
"param1 and param2 is empty": {}, | ||
"param1 is empty": { | ||
param2: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
}, | ||
want: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
}, | ||
}, | ||
"param2 is empty": { | ||
param1: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
}, | ||
want: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
}, | ||
}, | ||
"param1 and param2 with value": { | ||
param2: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
}, | ||
param1: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
}, | ||
want: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("4"), | ||
corev1.ResourceMemory: resource.MustParse("4Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("4"), | ||
corev1.ResourceMemory: resource.MustParse("4Gi"), | ||
}, | ||
}, | ||
}, | ||
"just has cpu or memory": { | ||
param2: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
}, | ||
param1: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("4"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceMemory: resource.MustParse("4Gi"), | ||
}, | ||
}, | ||
want: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("6"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("2"), | ||
corev1.ResourceMemory: resource.MustParse("6Gi"), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
g := NewGomegaWithT(t) | ||
for name, tt := range tests { | ||
got := SumResources(tt.param1, tt.param2) | ||
g.Expect(got.Limits.Cpu().Value()).Should(Equal(tt.want.Limits.Cpu().Value()), fmt.Sprintf("%s: Limits.Cpu", name)) | ||
g.Expect(got.Limits.Memory().Value()).Should(Equal(tt.want.Limits.Memory().Value()), fmt.Sprintf("%s: Limits.Memory", name)) | ||
g.Expect(got.Requests.Cpu().Value()).Should(Equal(tt.want.Requests.Cpu().Value()), fmt.Sprintf("%s: Requests.Cpu", name)) | ||
g.Expect(got.Requests.Memory().Value()).Should(Equal(tt.want.Requests.Memory().Value()), fmt.Sprintf("%s: Requests.Memory", name)) | ||
} | ||
} |