Skip to content

Commit

Permalink
add port suggest system and port change system
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-kod committed Aug 16, 2022
1 parent 95c6bc8 commit d9a2d2d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
Binary file modified __pycache__/k8s.cpython-38.pyc
Binary file not shown.
Binary file modified __pycache__/main.cpython-38.pyc
Binary file not shown.
25 changes: 21 additions & 4 deletions k8s.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time

import subprocess, sys
import subprocess, sys, 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)
Expand All @@ -13,6 +14,22 @@ def get_pod():
cp = subprocess.run(['helm', 'list', '--short'], encoding='utf-8', stdout=subprocess.PIPE)
return cp.stdout.split('\n')

def delete_pod(pod_name):
cp = subprocess.run(['helm', 'uninstall', '--purge'], encoding='utf-8', stdout=subprocess.PIPE)
return cp.stdout
def delete_pod(name):
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)
ports = cp.stdout[1:-2].split('\n')
print(f"return:{ports}")
unusedport = []
while(len(unusedport) < 4):
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):
cp = subprocess.run(['kubectl' 'patch' 'service' name '--type=\'json\'' '-p=\'[{"op": "replace", "path": "/spec/ports/0/nodePort", "value": 31600}]\''], encoding='utf-8', stdout=subprocess.PIPE)
return cp.stdout
20 changes: 13 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Pod(BaseModel):

class Service(BaseModel):
port: int
pod_name: str
name: str

@app.get("/")
def read_root():
Expand All @@ -27,21 +27,27 @@ def create_pod(pod: Pod):
callback = k8s.make_pod()


return {"pod_name": callback}
return {"name": callback}

@app.get("/pods/")
def get_pod():
return {"output": k8s.get_pod()}

@app.post("/services/")
def create_service(service: Service):
return {"service_name": "test_name"}
callback = k8s.change_port(name)
return {"result": callback}

@app.delete("/pods/{pod_name}")
@app.get("/ports_suggest/")
def get_unused_port():
ports = k8s.get_used_port()
return {"sugessted_port":ports}

@app.delete("/pods/{name}")
def delete_pod():
callback = k8s.delete_pod(pod_name)
callback = k8s.delete_pod(name)
return {"deleted": callback}

@app.delete("/services/{service_name}")
def delete_pod(service_name: str):
@app.delete("/services/{name}")
def delete_pod(name: str):
return {"status": "test"}

0 comments on commit d9a2d2d

Please sign in to comment.