11
Spec: v1.PodSpec{
    Containers: []v1.Container{
        v1.Container{
            Name:            podName,
            Image:           deploymentName,
            ImagePullPolicy: "IfNotPresent",
            Ports:           []v1.ContainerPort{},
            Env: []v1.EnvVar{
                v1.EnvVar{
                    Name:  "RASA_NLU_CONFIG",
                    Value: os.Getenv("RASA_NLU_CONFIG"),
                },
                v1.EnvVar{
                    Name:  "RASA_NLU_DATA",
                    Value: os.Getenv("RASA_NLU_DATA"),
                },
            },
            Resources: v1.ResourceRequirements{},
        },
    },
    RestartPolicy: v1.RestartPolicyOnFailure,
},

I want to provide resource limits as corresponding like :

resources:
  limits:
    cpu: "1"
  requests:
    cpu: "0.5"

How do I go on to do that. I tried adding Limits and its map key value pair but it seems to be quite a nested structure. There doesnt seem to be any example as to how to provide resources in kube client go.

Yusuf Tarık Günaydın
  • 3,016
  • 2
  • 27
  • 41
mohd.gadi
  • 495
  • 1
  • 7
  • 15

2 Answers2

19

I struggled with the same when i was creating a statefulset. Maybe my codesnipped will help you:

Resources: apiv1.ResourceRequirements{
    Limits: apiv1.ResourceList{
        "cpu":    resource.MustParse(cpuLimit),
        "memory": resource.MustParse(memLimit),
    },
    Requests: apiv1.ResourceList{
        "cpu":    resource.MustParse(cpuReq),
        "memory": resource.MustParse(memReq),
    },
},

the vars cpuReq, memReq, cpuLimit and memLimit are supposed to be strings

Yusuf Tarık Günaydın
  • 3,016
  • 2
  • 27
  • 41
Louis Baumann
  • 402
  • 2
  • 6
-1

Here you can find definition of v1.ResourceRequirements{}:

// ResourceRequirements describes the compute resource requirements.
type ResourceRequirements struct {
    // Limits describes the maximum amount of compute resources allowed.
    // More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
    // +optional
    Limits ResourceList `json:"limits,omitempty" protobuf:"bytes,1,rep,name=limits,casttype=ResourceList,castkey=ResourceName"`
    // Requests describes the minimum amount of compute resources required.
    // If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
    // otherwise to an implementation-defined value.
    // More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
    // +optional
    Requests ResourceList `json:"requests,omitempty" protobuf:"bytes,2,rep,name=requests,casttype=ResourceList,castkey=ResourceName"`
}

ResourceList:

// ResourceList is a set of (resource name, quantity) pairs.
type ResourceList map[ResourceName]resource.Quantity

Here you can find test file with example of use.

Sourcegraph plugin for Crome or Firefox could be very helpful to work with a source code on GitHub.

VAS
  • 8,538
  • 1
  • 28
  • 39
  • to be honest, your answer doesnt help, the OP showed that we are following and its a question of the {{ }} when doing the JSON in GOLANG, perhaps it would be more helpful; you know how to do the syntax to show it? – Daniel Smith Feb 12 '21 at 07:50