Skip to content

Commit

Permalink
[PLAT-4712] #135 NamedTemporaryFile causes issues on Windows platform
Browse files Browse the repository at this point in the history
Summary: The flush of the named temporary file doesn't seem to cause the secret file to be written out properly on Windows, as the associated bug explains. Instead, we can write to a temporary dir and close the file properly instead

Test Plan: Run on linux and Windows platforms

Reviewers: ysharma-contractor, bgandhi

Reviewed By: bgandhi

Subscribers: yugaware

Differential Revision: https://phabricator.dev.yugabyte.com/D18507
  • Loading branch information
iSignal committed Jul 25, 2022
1 parent e8330c4 commit bf387e3
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions stable/yugabyte/generate_kubeconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import json
import base64
import tempfile

import os.path

def run_command(command_args, namespace=None, as_json=True):
command = ['kubectl']
Expand Down Expand Up @@ -66,17 +66,20 @@ def run_command(command_args, namespace=None, as_json=True):
if not kube_config:
kube_config = '/tmp/{}.conf'.format(args['service_account'])

with tempfile.NamedTemporaryFile() as ca_crt_file:
ca_crt = base64.b64decode(secret_data['data']['ca.crt'])
ca_crt_file.write(ca_crt)
ca_crt_file.flush()
# create kubeconfig entry
set_cluster_cmd = ['config', 'set-cluster', cluster_name,
'--kubeconfig={}'.format(kube_config),
'--server={}'.format(endpoint.strip('"')),
'--embed-certs=true',
'--certificate-authority={}'.format(ca_crt_file.name)]
run_command(set_cluster_cmd, as_json=False)

tmpdir = tempfile.TemporaryDirectory()
ca_crt_file_name = os.path.join(tmpdir.name, "ca.crt")
ca_crt_file = open(ca_crt_file_name, "wb")
ca_crt_file.write(base64.b64decode(secret_data['data']['ca.crt']))
ca_crt_file.close()

# create kubeconfig entry
set_cluster_cmd = ['config', 'set-cluster', cluster_name,
'--kubeconfig={}'.format(kube_config),
'--server={}'.format(endpoint.strip('"')),
'--embed-certs=true',
'--certificate-authority={}'.format(ca_crt_file_name)]
run_command(set_cluster_cmd, as_json=False)

user_token = base64.b64decode(secret_data['data']['token']).decode('utf-8')
set_credentials_cmd = ['config', 'set-credentials', context_name,
Expand Down

0 comments on commit bf387e3

Please sign in to comment.