Skip to content

Commit

Permalink
Merge pull request #1 from cloud-native-dojo/tomoyk/patch
Browse files Browse the repository at this point in the history
Code Refacotoring
  • Loading branch information
tomoyk committed Aug 17, 2022
2 parents 3e6625d + b5b768f commit f014525
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions k8s.py
@@ -1,35 +1,47 @@
import time

import subprocess, sys, shlex
import subprocess
import sys
import shlex
from random import randint


def make_pod():
cp = subprocess.run(['C:/Program Files/helm/helm','--set', 'service.type=NodePort,persistence.storageClass=longhorn', 'install', 'bitnami/wordpress', '--generate-name'], encoding='utf-8', stdout=subprocess.PIPE)
cp = subprocess.run(['helm', '--set', 'service.type=NodePort,persistence.storageClass=longhorn',
'install', 'bitnami/wordpress', '--generate-name'], encoding='utf-8', stdout=subprocess.PIPE)
if cp.returncode != 0:
print('ls failed.', file=sys.stderr)
sys.exit(1)
return cp.stdout.split('\n')[0].replace('NAME: ', '')


def get_pod():
cp = subprocess.run(['helm', 'list', '--short'], encoding='utf-8', stdout=subprocess.PIPE)
cp = subprocess.run(['helm', 'list', '--short'],
encoding='utf-8', stdout=subprocess.PIPE)
return cp.stdout.split('\n')


def delete_pod(name):
cp = subprocess.run(['helm', 'uninstall', name], encoding='utf-8', stdout=subprocess.PIPE)
cp = subprocess.run(['helm', 'uninstall', name],
encoding='utf-8', stdout=subprocess.PIPE)
return cp.stdout


def get_used_port():
cp = subprocess.run(['C:/Program Files/Docker/Docker/resources/bin/kubectl', 'get', 'svc', '--all-namespaces', '-o', 'go-template=\'{{range .items}}{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\\n"}}{{end}}{{end}}{{end}}\''], encoding='utf-8', stdout=subprocess.PIPE)
cp = subprocess.run(['kubectl', 'get', 'svc', '--all-namespaces', '-o',
'go-template=\'{{range .items}}{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\\n"}}{{end}}{{end}}{{end}}\''], encoding='utf-8', stdout=subprocess.PIPE)
ports = cp.stdout[1:-2].split('\n')
print(f"return:{ports}")
unusedport = []
while(len(unusedport) < 4):
num = randint(30000,32768)
num = randint(30000, 32768)
if num not in ports and num not in unusedport:
unusedport.append(num)
print(unusedport)
return unusedport


def update_port(name, port):
cp = subprocess.run(['kubectl', 'patch', 'service', name, '--type=\'json\'', '-p=\'[{"op": "replace", "path": "/spec/ports/0/nodePort", "value": {}}]\''.format(port)], encoding='utf-8', stdout=subprocess.PIPE)
cp = subprocess.run(['kubectl', 'patch', 'service', name, '--type=\'json\'',
'-p=\'[{"op": "replace", "path": "/spec/ports/0/nodePort", "value": {}}]\''.format(port)], encoding='utf-8', stdout=subprocess.PIPE)
return cp.stdout

0 comments on commit f014525

Please sign in to comment.