Skip to content

Instantly share code, notes, and snippets.

@feisuzhu
Created November 21, 2019 11:02
Show Gist options
  • Save feisuzhu/b1eab704f719178f70f6f438b38b975e to your computer and use it in GitHub Desktop.
Save feisuzhu/b1eab704f719178f70f6f438b38b975e to your computer and use it in GitHub Desktop.
k8s py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -- stdlib --
import json
import os
# -- third party --
import requests
# -- own --
# -- code --
kube = requests.Session()
SA = '/var/run/secrets/kubernetes.io/serviceaccount'
token = open(f'{SA}/token').read().strip()
ns = open(f'{SA}/namespace').read().strip()
kube.headers.update({
'Authorization': 'Bearer ' + token,
'Accept': 'application/json',
})
kube.verify = f'{SA}/ca.crt'
HOST = os.environ['KUBERNETES_SERVICE_HOST']
PORT = os.environ['KUBERNETES_SERVICE_PORT']
deployment = {
'apiVersion': 'apps/v1',
'kind': 'Deployment',
'metadata': {
'name': 'foo-service',
},
'xxx': 'yyy',
}
kube.post(
f'https://{HOST}:{PORT}/apis/apps/v1/namespaces/{ns}/deployments',
data=json.dumps(deployment),
headers={'Content-Type': 'application/json'},
)
name = 'foo-service'
kube.delete(
f'https://{HOST}:{PORT}/apis/apps/v1/namespaces/{ns}/deployments/{name}',
data=json.dumps(),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment