Skip to content
Open
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ require (
github.com/rancher/fleet/pkg/apis v0.14.0-rc.1 // indirect
github.com/rancher/gke-operator v1.13.0-rc.3 // indirect
//TODO: Replace with a tag during un-rc'ing
github.com/rancher/norman v0.8.0 // indirect
github.com/rancher/norman v0.8.0
github.com/spf13/pflag v1.0.6 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/otel v1.37.0 // indirect
Expand Down
30 changes: 30 additions & 0 deletions pkg/resources/management.cattle.io/v3/project/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ var (
},
Spec: v3.ProjectSpec{
ClusterName: "testcluster",
ResourceQuota: &v3.ProjectResourceQuota{
Limit: v3.ResourceQuotaLimit{
ConfigMaps: "1",
Extended: map[string]string{
"ephemeral-storage": "14",
},
},
},
},
}
emptyProject = func() *v3.Project {
Expand Down Expand Up @@ -74,6 +82,17 @@ func TestAdmit(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "p-abc123",
},
Spec: v3.ProjectSpec{
ClusterName: "testcluster",
ResourceQuota: &v3.ProjectResourceQuota{
Limit: v3.ResourceQuotaLimit{
ConfigMaps: "1",
Extended: map[string]string{
"ephemeral-storage": "14",
},
},
},
},
}
},
oldProject: func() *v3.Project {
Expand All @@ -88,6 +107,17 @@ func TestAdmit(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "p-abc123",
},
Spec: v3.ProjectSpec{
ClusterName: "testcluster",
ResourceQuota: &v3.ProjectResourceQuota{
Limit: v3.ResourceQuotaLimit{
ConfigMaps: "1",
Extended: map[string]string{
"ephemeral-storage": "14",
},
},
},
},
Status: v3.ProjectStatus{
BackingNamespace: "p-abc123",
},
Expand Down
53 changes: 49 additions & 4 deletions pkg/resources/management.cattle.io/v3/project/quota_validate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package project

import (
"fmt"

"github.com/rancher/norman/types/convert"
mgmtv3 "github.com/rancher/rancher/pkg/apis/management.cattle.io/v3"
"github.com/rancher/wrangler/v3/pkg/data/convert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
quotav1 "k8s.io/apiserver/pkg/quota/v1"
Expand All @@ -24,20 +26,63 @@ func quotaFits(resourceListA corev1.ResourceList, resourceListB corev1.ResourceL
return false, failedHard
}

const ExtendedKey = "extended"

var (
resourceQuotaConversion = map[string]string{
"replicationControllers": "replicationcontrollers",
"configMaps": "configmaps",
"persistentVolumeClaims": "persistentvolumeclaims",
"servicesNodePorts": "services.nodeports",
"servicesLoadBalancers": "services.loadbalancers",
"requestsCpu": "requests.cpu",
"requestsMemory": "requests.memory",
"requestsStorage": "requests.storage",
"limitsCpu": "limits.cpu",
"limitsMemory": "limits.memory",
}
)

// convertLimitToResourceList converts a management.cattle.io/v3 ResourceQuotaLimit object to a core/v1 ResourceList,
// which can then be used to compare quotas.
func convertLimitToResourceList(limit *mgmtv3.ResourceQuotaLimit) (corev1.ResourceList, error) {
// TECH DEBT: Any change here has to be reflected in rancher/rancher pkg/resourcequota/quota_validate.go
// until such time as both places are unified in a single function shared between r/r and r/w

toReturn := corev1.ResourceList{}
converted, err := convert.EncodeToMap(limit)
if err != nil {
return nil, err
}

// convert the extended set first, ...
if extended, ok := converted[ExtendedKey]; ok {
delete(converted, ExtendedKey)
for key, value := range extended.(map[string]any) {
resourceName := corev1.ResourceName(key)
resourceQuantity, err := resource.ParseQuantity(value.(string))
if err != nil {
return nil, fmt.Errorf("failed to parse value for key %q: %w", key, err)
}

toReturn[resourceName] = resourceQuantity
}
}

// then place the fixed data. this order ensures that in case of
// conflicts between arbitrary and fixed data the fixed data wins.
for key, value := range converted {
q, err := resource.ParseQuantity(convert.ToString(value))
var resourceName corev1.ResourceName
if val, ok := resourceQuotaConversion[key]; ok {
resourceName = corev1.ResourceName(val)
} else {
resourceName = corev1.ResourceName(key)
}
resourceQuantity, err := resource.ParseQuantity(convert.ToString(value))
if err != nil {
return nil, err
return nil, fmt.Errorf("parsing quantity %q: %w", key, err)
}
toReturn[corev1.ResourceName(key)] = q
toReturn[resourceName] = resourceQuantity
}
return toReturn, nil
}
42 changes: 42 additions & 0 deletions pkg/resources/management.cattle.io/v3/project/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,54 @@ import (
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func TestConvertLimitToResourceList(t *testing.T) {
t.Run("ConvertLimitToResourceList", func(t *testing.T) {
out, err := convertLimitToResourceList(&v3.ResourceQuotaLimit{
ConfigMaps: "1",
LimitsCPU: "2",
LimitsMemory: "3",
PersistentVolumeClaims: "4",
Pods: "5",
ReplicationControllers: "6",
RequestsCPU: "7",
RequestsMemory: "8",
RequestsStorage: "9",
Secrets: "10",
Services: "11",
ServicesLoadBalancers: "12",
ServicesNodePorts: "13",
Extended: map[string]string{
"ephemeral-storage": "14",
},
})
assert.NoError(t, err)
assert.Equal(t, corev1.ResourceList{
"configmaps": resource.MustParse("1"),
"ephemeral-storage": resource.MustParse("14"),
"limits.cpu": resource.MustParse("2"),
"limits.memory": resource.MustParse("3"),
"persistentvolumeclaims": resource.MustParse("4"),
"pods": resource.MustParse("5"),
"replicationcontrollers": resource.MustParse("6"),
"requests.cpu": resource.MustParse("7"),
"requests.memory": resource.MustParse("8"),
"requests.storage": resource.MustParse("9"),
"secrets": resource.MustParse("10"),
"services": resource.MustParse("11"),
"services.loadbalancers": resource.MustParse("12"),
"services.nodeports": resource.MustParse("13"),
}, out)
})
}

func TestProjectValidation(t *testing.T) {
t.Parallel()
type testState struct {
Expand Down