this is my very first question here :)
I am trying to write something in Go which for the moment is just getting ResourceQuotas. While testing I noticed that when importing k8s.io/api/core/v1 like this:
coreV1 "k8s.io/api/core/v1"
And having a variable like this, with the type coming from coreV1:
rq := &coreV1.ResourceQuota{
TypeMeta: v1.TypeMeta{},
ObjectMeta: v1.ObjectMeta{},
Spec: coreV1.ResourceQuotaSpec{
Hard: map[coreV1.ResourceName]resources.Quantity{},
Scopes: []coreV1.ResourceQuotaScope{},
ScopeSelector: &coreV1.ScopeSelector{},
},
Status: coreV1.ResourceQuotaStatus{},
}
I am not able to e.g. set a ResourceQuota's requests.cpu or limits.cpu, only this is possible:
CPULimts := rq.Spec.Hard.Cpu()
fmt.Println(CPULimts)
Am I using it wrong or missing something? ResourceQuotas like this: https://kubernetes.io/docs/concepts/policy/resource-quotas/#viewing-and-setting-quotas are working just perfectly fine in many of the clusters I manage.
Do I maybe need to use some other package?
Thank you in advance!
CodePudding user response:
I think you're misreading your struct: Spec.Hard
is a map of corev1.ResourceName
to quotas, where corev1.ResourceName
is a set of constant strings that includes things like requests.cpu
(see the documentation for that package).
So we can write:
package main
import (
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func main() {
rq := &corev1.ResourceQuota{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{},
Scopes: []corev1.ResourceQuotaScope{},
ScopeSelector: &corev1.ScopeSelector{},
},
Status: corev1.ResourceQuotaStatus{},
}
rq.Spec.Hard[corev1.ResourceRequestsCPU] = resource.MustParse("500m")
rq.Spec.Hard[corev1.ResourceStorage] = resource.MustParse("10G")
content, err := json.Marshal(rq.Spec)
if err != nil {
panic(err)
}
fmt.Print(string(content))
}
Which will output:
{
"hard": {
"requests.cpu": "500m",
"storage": "10G"
},
"scopeSelector": {}
}