def check_python_kubernetes(self): #configuration = kubernetes.client.Configuration() core_api_instance = client.CoreApi() try_count = 0 while True: try: self.logger.info( "Try to access to the target kubernetes cluster") config.load_kube_config( config_file=self.KUBE_CONFIG_DEFAULT_LOCATION) api_response = core_api_instance.get_api_versions() self.logger.info(str(api_response)) break except ApiException as e: self.logger.error("Failed connect to k8s with python client.") try_count = try_count + 1 if try_count == 3: self.logger.error( "All 3 tries of connecting k8s with python client fails.") sys.exit(1) time.sleep(5) self.logger.info( "CHECKING SUCCESSFULLY: Successfully access kubernetes through python client. " )
def api_discovery(): print("Supported APIs (* is preferred version):") print("%-40s %s" % ("core", ",".join(client.CoreApi().get_api_versions().versions))) for api in client.ApisApi().get_api_versions().groups: versions = [] for v in api.versions: name = "" if v.version == api.preferred_version.version and len( api.versions) > 1: name += "*" name += v.version versions.append(name) print("%-40s %s" % (api.name, ",".join(versions)))
def main(): config.load_kube_config() print("List of APIs... maybe...") print("Supported APIs (* is preferred version):") print("%-40s %s" % ("core", ",".join(client.CoreApi().get_api_versions().versions))) for api in client.ApisApi().get_api_versions().groups: versions = [] for v in api.versions: name = "" if v.version == api.preferred_version.version and len( api.versions) > 1: name += "*" name += v.version versions.append(name) print("%-40s %s" % (api.name, ",".join(versions)))
def __init__(self): """ Kube config file should be placed in /root/.kube folder with config as name for linux """ self.configuration = client.Configuration() config.load_kube_config() print("Supported APIs (* is preferred version):") print("%-40s %s" % ("core", ",".join(client.CoreApi().get_api_versions().versions))) for api in client.ApisApi().get_api_versions().groups: versions = [] for v in api.versions: name = "" if v.version == api.preferred_version.version and len( api.versions) > 1: name += "*" name += v.version versions.append(name) print("%-40s %s" % (api.name, ",".join(versions)))
def main(): # Configs can be set in Configuration class directly or using helper # utility config.load_kube_config(os.environ["HOME"] + '/.kube/config') print("Supported APIs (* is preferred version):") print("%-20s %s" % ("core", ",".join(client.CoreApi().get_api_versions().versions))) for api in client.ApisApi().get_api_versions().groups: versions = [] for v in api.versions: name = "" if v.version == api.preferred_version.version and len( api.versions) > 1: name += "*" name += v.version versions.append(name) print("%-40s %s" % (api.name, ",".join(versions)))
def auth_check(auth_type, token): load_auth(auth_type, token) try: core_api = client.CoreApi() core_api.get_api_versions() code = 0 msg = '登录成功' except client.exceptions.ApiException as e: print(e.status) if auth_type == 'kube_config': msg = '认证文件无效' elif auth_type == 'token': msg = 'token无效' else: msg = '请使用认证文件或者token认证' code = 1 result = {'code': code, 'msg': msg} return result
def main(): # Configs can be set in Configuration class directly or using helper # utility. If no argument provided, the config will be loaded from # default location. config.load_kube_config() print("Supported APIs (* is preferred version):") print("%-40s %s" % ("core", ",".join(client.CoreApi().get_api_versions().versions))) for api in client.ApisApi().get_api_versions().groups: versions = [] for v in api.versions: name = "" if v.version == api.preferred_version.version and len( api.versions) > 1: name += "*" name += v.version versions.append(name) print("%-40s %s" % (api.name, ",".join(versions)))
def cli(ctx, verbose, kubeconfig, context): config.load_kube_config(config_file=kubeconfig, context=context) handler = { "sink": sys.stderr, "format": "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> " "<level>{level}</level> {message}", "level": "TRACE" if verbose else "INFO" } logger.configure(handlers=[handler]) try: logger.info("Connecting to Kubernetes master at {}", client.Configuration().host) api = client.CoreApi() api.get_api_versions() except Exception: logger.error("Failed connecting to Kubernetes master") ctx.abort()
def discoveryApi(): """ :return: Supported APIs (* is preferred version): core v1 apiregistration.k8s.io *v1,v1beta1 extensions v1beta1 apps v1 events.k8s.io v1beta1 authentication.k8s.io *v1,v1beta1 authorization.k8s.io *v1,v1beta1 autoscaling *v1,v2beta1,v2beta2 batch *v1,v1beta1 certificates.k8s.io v1beta1 networking.k8s.io *v1,v1beta1 policy v1beta1 rbac.authorization.k8s.io *v1,v1beta1 storage.k8s.io *v1,v1beta1 admissionregistration.k8s.io *v1,v1beta1 apiextensions.k8s.io *v1,v1beta1 scheduling.k8s.io *v1,v1beta1 coordination.k8s.io *v1,v1beta1 node.k8s.io v1beta1 """ print("Supported APIs (* is preferred version):") print("%-40s %s" % ("core", ",".join(client.CoreApi().get_api_versions().versions))) for api in client.ApisApi().get_api_versions().groups: versions = [] for v in api.versions: name = "" if v.version == api.preferred_version.version and len( api.versions) > 1: name += "*" name += v.version versions.append(name) print("%-40s %s" % (api.name, ",".join(versions)))
def get_core_api_client(self, auth): k8s_client = self.get_k8s_client(auth_plugin=auth) return client.CoreApi(api_client=k8s_client)
def initialize_CoreApiClient(self, auth): k8s_client = self.get_k8sClient(auth_plugin=auth) return client.CoreApi(api_client=k8s_client)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Reads the list of available API versions and prints them. Similar to running `kubectl api-versions`. """ from kubernetes import client, config """ client.CoreApi() client.ApisApi() """ def main(): # Configs can be set in Configuration class directly or using helper # utility. If no argument provided, the config will be loaded from # default location. config.load_kube_config() # 将 ~/.kube 的config文件的内容复制到本地目录,保存为文件kubeconfig.yaml # config.kube_config.load_kube_config(config_file="kubeconfig.yaml") print("Supported APIs (* is preferred version):") print("%-40s %s" %
def get_api_class(self, api_client): resp = client.CoreApi(api_client).get_api_versions() version = resp.versions[0] return f"Core{version.capitalize()}Api"
def Url(self, item): configuration = client.Configuration() api_instance = client.CoreApi(client.ApiClient(configuration)) self.URL = api_instance.get_api_versions( ).server_address_by_client_cid_rs[0].server_address
def initialize_CoreApiClient(self, **kwargs): k8s_client = self.get_k8sClient(**kwargs) return client.CoreApi(api_client=k8s_client)
import requests from ast import literal_eval from urllib.parse import urljoin from kubernetes.client.rest import ApiException ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) CONFIG_PATH = os.path.join(ROOT_DIR, 'k8s_config.ini') KUBECONFIG_PATH = os.path.join(ROOT_DIR, 'KUBECONFIG') config = configparser.ConfigParser(os.environ) config.read(CONFIG_PATH) node_ip = config.get('node', 'node_ip') base_url = config.get('node', 'base_url') def_header = config.get('constants', 'default_header') def_header = literal_eval(def_header) k8_config.load_kube_config(config_file=KUBECONFIG_PATH) v = client.CoreApi() v1 = client.CoreV1Api() v1_apps = client.AppsV1Api() class kubernetes_library: """ Kuberntes library contains methods that will connect to a kubernetes cluster and performs fetching or manipulation of kubernetes objects running in the cluster. """ def get_pods_details_in_default_namespace(self): """ This method fetches all the pod name, pod ip and pod status in the default namespace. currently I am printing the output, In a real environment we can return this information. :return: None """ try:
def health(): core = client.CoreApi() ret = core.get_api_versions() return jsonify({'versions': ret.versions})
def getCoreApi(self): return client.CoreApi(api_client=self.k8s_client)
def get_api_cls_list(self, api_client): versions = client.CoreApi(api_client).get_api_versions() return [f"Core{ver.capitalize()}Api" for ver in versions.versions]