kubernetes-client / c

Official C client library for Kubernetes
Apache License 2.0
144 stars 46 forks source link

how to config a created pod to a Node? Can you help me to check code? #57

Closed HaunHsiung closed 3 years ago

HaunHsiung commented 3 years ago

list_t nSelector = list_create(); char node = strdup("ssd"); // IP address list_addElement(nSelector, node); // v1_node_selector_t *nodeselector = calloc(1, sizeof(v1_node_selector_t)); // nodeselector->node_selector_terms = nSelector;

v1_node_selector_t *nodeselector = v1_node_selector_create(nSelector);
list_t *nodeSelectorCreator = list_create();
list_addElement(nodeSelectorCreator, nodeselector);
podinfo->spec->node_selector = nodeSelectorCreator;
podinfo->spec->node_name = strdup("szvphicprd10133");
ityuhui commented 3 years ago

Please see below source code and swagger doc to have a try.

https://github.com/kubernetes-client/c/blob/master/kubernetes/model/v1_node_selector.h

typedef struct v1_node_selector_t {
    list_t *node_selector_terms; //nonprimitive container

} v1_node_selector_t;

https://github.com/kubernetes-client/c/blob/master/kubernetes/model/v1_node_selector_term.h

typedef struct v1_node_selector_term_t {
    list_t *match_expressions; //nonprimitive container
    list_t *match_fields; //nonprimitive container

} v1_node_selector_term_t;

https://github.com/kubernetes-client/c/blob/master/kubernetes/model/v1_node_selector_requirement.h

https://github.com/kubernetes-client/c/blob/master/kubernetes/swagger.json

    "v1.NodeSelectorTerm": {
      "description": "A null or empty node selector term matches no objects. The requirements of them are ANDed. The TopologySelectorTerm type implements a subset of the NodeSelectorTerm.",
      "properties": {
        "matchExpressions": {
          "description": "A list of node selector requirements by node's labels.",
          "items": {
            "$ref": "#/definitions/v1.NodeSelectorRequirement"
          },
          "type": "array"
        },
        "matchFields": {
          "description": "A list of node selector requirements by node's fields.",
          "items": {
            "$ref": "#/definitions/v1.NodeSelectorRequirement"
          },
          "type": "array"
        }
      },
      "type": "object"
    },

    "v1.NodeSelectorRequirement": {
      "description": "A node selector requirement is a selector that contains values, a key, and an operator that relates the key and values.",
      "properties": {
        "key": {
          "description": "The label key that the selector applies to.",
          "type": "string"
        },
        "operator": {
          "description": "Represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists, DoesNotExist. Gt, and Lt.",
          "type": "string"
        },
        "values": {
          "description": "An array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. If the operator is Gt or Lt, the values array must have a single element, which will be interpreted as an integer. This array is replaced during a strategic merge patch.",
          "items": {
            "type": "string"
          },
          "type": "array"
        }
      },
      "required": [
        "key",
        "operator"
      ],
      "type": "object"
    },

I think the API server accepts matchExpressions and matchFields

https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/

brendandburns commented 3 years ago

I think this question is answered. Shall we close the issue?

HaunHsiung commented 3 years ago

I have solved this problem by using affinity character, it seems unable to simply use node selector in this API.

ityuhui commented 3 years ago

I have solved this problem by using affinity character, it seems unable to simply use node selector in this API.

It's very good to meet your requirement using affinity. But I still think node selector is OK in this API (just a bit complicated, need prepare matchExpressions or match_fieldskey-value list)

brendandburns commented 3 years ago

Closing the issue since it is resolved.