Python Script for Storing ECR Login Information as Kubernetes Secrets

2020-07-13 20:39 (4 years ago) ytyng
#!/usr/bin/env python3

import subprocess
import re

namespace = 'my-namespace'
secret_name = 'ecr-credeintial'
aws_region = 'ap-northeast-1'
docker_server = 'https://000000000000.dkr.ecr.ap-northeast-1.amazonaws.com'

def main():
  output = subprocess.check_output([
    'aws', 'ecr', 'get-login',
    '--no-include-email', '--region', aws_region,
  ]).decode()
  words = output.split()
  username = words[words.index('-u') + 1]
  password = words[words.index('-p') + 1]

  subprocess.run([
    'kubectl', '-n', namespace, 'delete', 'secret', secret_name])

  subprocess.run([
    'kubectl', '-n', namespace, 'create', 'secret',
    'docker-registry', secret_name,
    f'--docker-username={username}',
    f'--docker-password={password}',
    f'--docker-server={docker_server}'
  ])

if __name__ == '__main__':
  main()
Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011