-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from IBM/issue13
Use Kubernetes API Instead of Calling Kubectl Through the Subproccess Module
- Loading branch information
Showing
3 changed files
with
93 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,90 @@ | ||
import requests | ||
import os | ||
import subprocess | ||
import json | ||
from src.common.functions import Color as Color | ||
|
||
class IngressCreator: | ||
def __init__(self, clusterNameOrID, resourceGroupID, namespace, secretName, serviceName, servicePort, accessToken, refreshToken, ingressSubdomain ): | ||
def __init__(self, clusterNameOrID, resourceGroupID, namespace, secretName, serviceName, servicePort, accessToken, refreshToken, ingressSubdomain, iks_master_url): | ||
self.clusterNameOrID=clusterNameOrID | ||
self.resourceGroupID=resourceGroupID | ||
self.namespace=namespace | ||
self.secretName=secretName | ||
self.serviceName=serviceName | ||
self.servicePort=servicePort | ||
self.accessToken= accessToken | ||
self.accessToken=accessToken | ||
self.refreshToken=refreshToken | ||
self.ingressSubdomain=ingressSubdomain | ||
|
||
def getKubeConfig(self): | ||
|
||
url = "https://containers.cloud.ibm.com/global/v2/getKubeconfig?cluster="+self.clusterNameOrID | ||
|
||
payload = "" | ||
headers = { | ||
'X-Auth-Resource-Group': self.resourceGroupID, | ||
'X-Auth-Refresh-Token': self.refreshToken, | ||
'Authorization': self.accessToken, | ||
'accept': 'application/json' | ||
} | ||
try: | ||
response = requests.request("GET", url, headers=headers, data=payload) | ||
text_file = open("config.txt", "w") | ||
n = text_file.write(response.text) | ||
text_file.close() | ||
os.environ['KUBECONFIG'] = "config.txt" | ||
print(Color.GREEN+"SUCCESS: Installed kube config file"+Color.END) | ||
|
||
except: | ||
print(Color.RED+"ERROR: Unable to Install kube config file"+Color.END) | ||
self.iks_master_url=iks_master_url | ||
|
||
def create_ingress(self): | ||
if self.iks_master_url =="": | ||
print(Color.RED+"ERROR: Public service endpoint for IKS Cluster is not enabled"+Color.END) | ||
#1. get id token to make kubernetes API calls | ||
url = "https://iam.cloud.ibm.com/identity/token" | ||
|
||
#1. setting config | ||
self.getKubeConfig() | ||
payload="grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&apikey="+os.getenv("CIS_SERVICES_APIKEY") | ||
headers = { | ||
'content-type': 'application/x-www-form-urlencoded', | ||
'Authorization': 'Basic a3ViZTprdWJl', | ||
'cache-control': 'no-cache' | ||
} | ||
try: | ||
response = requests.request("POST", url, headers=headers, data=payload) | ||
data=json.loads(response.text) | ||
idToken=data["id_token"] | ||
except: | ||
print(Color.RED+"ERROR: Unable to get id token"+Color.END) | ||
|
||
#2. getting ingressSubdomain | ||
ingressFileName="ingress.yaml" | ||
|
||
|
||
|
||
#2. apply yaml file through kubernetes API | ||
url = self.iks_master_url+"/apis/networking.k8s.io/v1beta1/namespaces/"+self.namespace+"/ingresses" | ||
payload = json.dumps({ | ||
"apiVersion": "networking.k8s.io/v1beta1", | ||
"kind": "Ingress", | ||
"metadata": { | ||
"name": "cis-cert", | ||
"annotations": { | ||
"nginx.ingress.kubernetes.io/ssl-redirect": "false" | ||
} | ||
}, | ||
"spec": { | ||
"tls": [ | ||
{ | ||
"hosts": [ | ||
self.ingressSubdomain | ||
], | ||
"secretName": self.secretName | ||
} | ||
], | ||
"rules": [ | ||
{ | ||
"host": self.ingressSubdomain, | ||
"http": { | ||
"paths": [ | ||
{ | ||
"path": "/", | ||
"backend": { | ||
"serviceName": self.serviceName, | ||
"servicePort": int(self.servicePort) | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}) | ||
headers = { | ||
'Authorization': 'bearer'+' '+idToken, | ||
'Content-Type': 'application/json' | ||
} | ||
try: | ||
response = requests.request("POST", url, headers=headers, data=payload, verify=False) | ||
print(Color.GREEN+"SUCCESS: Created ingress file"+Color.END) | ||
|
||
yaml='''apiVersion: networking.k8s.io/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: cis-ingress | ||
annotations: | ||
nginx.ingress.kubernetes.io/ssl-redirect: "false" | ||
spec: | ||
tls: | ||
- hosts: | ||
- '''+self.ingressSubdomain+''' | ||
secretName: '''+self.secretName+''' | ||
rules: | ||
- host: '''+self.ingressSubdomain+''' | ||
http: | ||
paths: | ||
- path: / | ||
backend: | ||
serviceName: '''+self.serviceName+''' | ||
servicePort: '''+self.servicePort | ||
except: | ||
print(Color.RED+"ERROR: Unable to create ingress file"+Color.END) | ||
|
||
#3. create yaml file | ||
text_file = open(ingressFileName, "w") | ||
n = text_file.write(yaml) | ||
text_file.close() | ||
|
||
#4. kubectl sanity check | ||
try: | ||
subprocess.check_output(["kubectl", "--help"], stderr=subprocess.STDOUT) | ||
except: | ||
print(Color.RED+"ERROR: Kubectl is not installed"+Color.END) | ||
|
||
#5. apply yaml | ||
try: | ||
|
||
subprocess.check_output(["kubectl", "apply", "-f", ingressFileName, "--namespace="+self.namespace], stderr=subprocess.STDOUT) | ||
print(Color.GREEN+"SUCCESS: Ingress was created"+Color.END) | ||
|
||
except subprocess.CalledProcessError as e: | ||
print(Color.RED+"ERROR: Ingress was not created"+Color.END) | ||
#6 delete ingress yaml and kube config file | ||
os.remove(ingressFileName) | ||
os.remove("config.txt") | ||
#future enchancements: use kubectl api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters