|
| 1 | +from linode_api4.objects import ( |
| 2 | + Base, DerivedBase, Property, Region, Type, Instance, MappedObject |
| 3 | +) |
| 4 | + |
| 5 | +class KubeVersion(Base): |
| 6 | + """ |
| 7 | + A KubeVersion is a version of Kubernetes that can be deployed on LKE. |
| 8 | + """ |
| 9 | + api_endpoint = "/lke/versions" |
| 10 | + |
| 11 | + properties = { |
| 12 | + "id": Property(identifier=True), |
| 13 | + } |
| 14 | + |
| 15 | + |
| 16 | +class LKENodePoolNode(): |
| 17 | + """ |
| 18 | + AN LKE Node Pool Node is a helper class that is used to populate the "nodes" |
| 19 | + array of an LKE Node Pool, and set up an automatic relationship with the |
| 20 | + Linode Instance the Node represented. |
| 21 | + """ |
| 22 | + def __init__(self, client, json): |
| 23 | + """ |
| 24 | + Creates this NodePoolNode |
| 25 | + """ |
| 26 | + #: The ID of this Node Pool Node |
| 27 | + self.id = json.get("id") # why do these have an ID if they don't have an endpoint of their own? |
| 28 | + |
| 29 | + #: The ID of the Linode Instance this Node represents |
| 30 | + self.instance_id = json.get("instance_id") |
| 31 | + |
| 32 | + #: The Instance object backing this Node Pool Node |
| 33 | + self.instance = Instance(client, self.instance_id) |
| 34 | + |
| 35 | + #: The Status of this Node Pool Node |
| 36 | + self.status = json.get("status") |
| 37 | + |
| 38 | +class LKENodePool(DerivedBase): |
| 39 | + """ |
| 40 | + An LKE Node Pool describes a pool of Linode Instances that exist within an |
| 41 | + LKE Cluster. |
| 42 | + """ |
| 43 | + api_endpoint = "/lke/clustters/{cluster_id}/pools/{id}" |
| 44 | + derived_url_path = 'pools' |
| 45 | + parent_id = "linode_id" |
| 46 | + |
| 47 | + properties = { |
| 48 | + "id": Property(identifier=True), |
| 49 | + "cluster_id": Property(identifier=True), |
| 50 | + "type": Property(slug_relationship=Type), |
| 51 | + "disks": Property(), |
| 52 | + "count": Property(mutable=True), |
| 53 | + "nodes": Property(volatile=True), # this is formatted in _populate below |
| 54 | + } |
| 55 | + |
| 56 | + def _populate(self, json): |
| 57 | + """ |
| 58 | + Parse Nodes into more useful LKENodePoolNode objects |
| 59 | + """ |
| 60 | + if json is not None: |
| 61 | + new_nodes = [ |
| 62 | + LKENodePoolNode(self._client, c) for c in json["nodes"] |
| 63 | + ] |
| 64 | + json["nodes"] = new_nodes |
| 65 | + |
| 66 | + super()._populate(json) |
| 67 | + |
| 68 | + def recycle(self): |
| 69 | + """ |
| 70 | + Deleted and recreates all Linodes in this Node Pool in a rolling fashion. |
| 71 | + Completing this operation may take several minutes. This operation will |
| 72 | + cause all local data on Linode Instances in this pool to be lost. |
| 73 | + """ |
| 74 | + self._client.post("{}/recycle".format(LKENodePool.api_endpoint), model=self) |
| 75 | + self.invalidate() |
| 76 | + |
| 77 | + |
| 78 | +class LKECluster(Base): |
| 79 | + """ |
| 80 | + An LKE Cluster is a single k8s cluster deployed via Linode Kubernetes Engine. |
| 81 | + """ |
| 82 | + api_endpoint = "/lke/clusters/{id}" |
| 83 | + |
| 84 | + properties = { |
| 85 | + "id": Property(identifier=True), |
| 86 | + "created": Property(is_datetime=True), |
| 87 | + "label": Property(mutable=True), |
| 88 | + "tags": Property(mutable=True), |
| 89 | + "updated": Property(is_datetime=True), |
| 90 | + "region": Property(slug_relationship=Region), |
| 91 | + "k8s_version": Property(slug_relationship=KubeVersion), |
| 92 | + "pools": Property(derived_class=LKENodePool), |
| 93 | + } |
| 94 | + |
| 95 | + @property |
| 96 | + def api_endpoints(self): |
| 97 | + """ |
| 98 | + A list of API Endpoints for this Cluster. |
| 99 | + """ |
| 100 | + # This result appears to be a PaginatedList, but objects in the list don't |
| 101 | + # have IDs and can't be retrieved on their own, and it doesn't accept normal |
| 102 | + # pagination properties, so we're converting this to a list of strings. |
| 103 | + if not hasattr(self, "_api_endpoints"): |
| 104 | + results = self._client.get("{}/api-endpoints".format(LKECluster.api_endpoint), model=self) |
| 105 | + |
| 106 | + self._api_endpoints = [MappedObject(**c) for c in results["data"]] |
| 107 | + |
| 108 | + return self._api_endpoints |
| 109 | + |
| 110 | + @property |
| 111 | + def kubeconfig(self): |
| 112 | + """ |
| 113 | + The administrative Kubernetes Config used to access this cluster, encoded |
| 114 | + in base64. Note that this config contains sensitive credentials to your |
| 115 | + cluster. |
| 116 | +
|
| 117 | + To convert this config into a readable form, use python's `base64` module: |
| 118 | +
|
| 119 | + import base64 |
| 120 | +
|
| 121 | + config = my_cluster.kubeconfig |
| 122 | + yaml_config = base64.b64decode(config) |
| 123 | +
|
| 124 | + # write this config out to disk |
| 125 | + with open("/path/to/target/kubeconfig.yaml", "w") as f: |
| 126 | + f.write(yaml_config.decode()) |
| 127 | +
|
| 128 | + It may take a few minutes for a config to be ready when creating a new |
| 129 | + cluster; during that time this request may fail. |
| 130 | + """ |
| 131 | + if not hasattr(self, "_kubeconfig"): |
| 132 | + result = self._client.get("{}/kubeconfig".format(LKECluster.api_endpoint), model=self) |
| 133 | + |
| 134 | + self._kubeconfig = result["kubeconfig"] |
| 135 | + |
| 136 | + return self._kubeconfig |
| 137 | + |
| 138 | + def node_pool_create(self, node_type, node_count, **kwargs): |
| 139 | + """ |
| 140 | + Creates a new :any:`LKENodePool` for this cluster. |
| 141 | +
|
| 142 | + :param node_type: The type of nodes to create in this pool. |
| 143 | + :type node_type: :any:`Type` or str |
| 144 | + :param node_count: The number of nodes to create in this pool. |
| 145 | + :type node_count: int |
| 146 | + :param kwargs: Any other arguments to pass to the API. See the API docs |
| 147 | + for possible values. |
| 148 | +
|
| 149 | + :returns: The new Node Pool |
| 150 | + :rtype: LKENodePool |
| 151 | + """ |
| 152 | + params = { |
| 153 | + "type": node_type, |
| 154 | + "count": node_count, |
| 155 | + } |
| 156 | + params.update(kwargs) |
| 157 | + |
| 158 | + result = self._client.post("{}/pools".format(LKECluster.api_endpoint), model=self, data=params) |
| 159 | + self.invalidate() |
| 160 | + |
| 161 | + if not 'id' in result: |
| 162 | + raise UnexpectedResponseError('Unexpected response creating node pool!', json=result) |
| 163 | + |
| 164 | + return LKENodePool(self._client, result["id"], self.id, result) |
0 commit comments