import os
import sys

EXTERNAL_IP = "143.205.173.225"

# apply or delete config
kube_command = 'apply'
if len(sys.argv) == 2:
    kube_command = sys.argv[1]

paths = []
for p, _, f in os.walk('./'):
    for file in f:
        if 'deployment.yml' == file:
            paths.append(os.path.normpath(p))

for path in paths:
    os.system(f"kubectl {kube_command} -f {path}")
    
    # if kube_command == 'apply':
    #     # apply external ip
    #     service_name = path.split(os.path.normpath('/'))[-2] # -1 is deployment folder
    #     if service_name.split('-')[-1] == 'microservice':
    #         # remove microservice suffix
    #         service_name = service_name[:len(service_name)-len('-microservice')]
        
    #     os.system(f"kubectl patch service {service_name} -p '{{\"spec\":{{\"externalIPs\":[\"{EXTERNAL_IP}\"]}}}}'")