|
| 1 | +# Copyright 2018 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# This example demonstrate communication with a remote Kube cluster from a |
| 16 | +# server outside of the cluster without kube client installed on it. |
| 17 | +# The communication is secured with the use of Bearer token. |
| 18 | + |
| 19 | +from kubernetes import client, config |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + # Define the barer token we are going to use to authenticate. |
| 24 | + # See here to create the token: |
| 25 | + # https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/ |
| 26 | + aToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 27 | + |
| 28 | + # Create a configuration object |
| 29 | + aConfiguration = client.Configuration() |
| 30 | + |
| 31 | + # Specify the endpoint of your Kube cluster |
| 32 | + aConfiguration.host = "https://XXX.XXX.XXX.XXX:443" |
| 33 | + |
| 34 | + # Security part. |
| 35 | + # In this simple example we are not going to verify the SSL certificate of |
| 36 | + # the remote cluster (for simplicity reason) |
| 37 | + aConfiguration.verify_ssl = False |
| 38 | + # Nevertheless if you want to do it you can with these 2 parameters |
| 39 | + # configuration.verify_ssl=True |
| 40 | + # ssl_ca_cert is the filepath to the file that contains the certificate. |
| 41 | + # configuration.ssl_ca_cert="certificate" |
| 42 | + |
| 43 | + aConfiguration.api_key = {"authorization": "Bearer " + aToken} |
| 44 | + |
| 45 | + # Create a ApiClient with our config |
| 46 | + aApiClient = client.ApiClient(aConfiguration) |
| 47 | + |
| 48 | + # Do calls |
| 49 | + v1 = client.CoreV1Api(aApiClient) |
| 50 | + print("Listing pods with their IPs:") |
| 51 | + ret = v1.list_pod_for_all_namespaces(watch=False) |
| 52 | + for i in ret.items: |
| 53 | + print("%s\t%s\t%s" % |
| 54 | + (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + main() |
0 commit comments