如何在kubernetes go client pod spec中提供资源限制?

时间:2018-10-25 06:57:51

标签: kubernetes kubernetes-go-client

    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,
        },

我想提供相应的资源限制,例如:

resources:
  limits:
    cpu: "1"
  requests:
    cpu: "0.5"
args:
- -cpus
- "2"

我该怎么做。我尝试添加Limits及其映射键值对,但这似乎是一个嵌套的结构。关于如何在kube client go中提供资源似乎没有任何示例。

2 个答案:

答案 0 :(得分:3)

创建状态集时,我一直在努力。也许我的代码被窃取将帮助您:

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),
                            },
                        },

vars cpuReq,memReq,cpuLimit和memLimit应该是字符串

答案 1 :(得分:0)

Here,您可以找到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,您可以找到使用示例的测试文件。

Sourcegraph的Crome或Firefox插件对在GitHub上使用源代码很有帮助。