Last active
February 2, 2024 17:35
-
-
Save theycallmeloki/aa4df404c3df85c31dac91216e22f678 to your computer and use it in GitHub Desktop.
grab pachyderm config and kubeconfigs from root minikube user and provision them under the regular user (RUN THIS WITH SUDO)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import yaml | |
import base64 | |
import shutil | |
import pwd | |
from pathlib import Path | |
# Update these variables with your actual username and paths | |
username = "your_username" | |
home_dir = Path(f"/home/{username}") | |
pachyderm_config_src = Path("/root/.pachyderm/config.json") | |
pachyderm_config_dest = home_dir / ".pachyderm/config.json" | |
kubectl_config_src = Path("/root/.kube/config") | |
kubectl_config_dest = home_dir / ".kube/config" | |
def copy_and_change_ownership(src, dest): | |
shutil.copy(src, dest) | |
uid = pwd.getpwnam(username).pw_uid | |
gid = pwd.getpwnam(username).pw_gid | |
os.chown(dest, uid, gid) | |
def encode_file_to_base64(path): | |
with open(path, 'rb') as file: | |
return base64.b64encode(file.read()).decode('utf-8') | |
def process_kubectl_config(): | |
with open(kubectl_config_src, 'r') as file: | |
config = yaml.safe_load(file) | |
for cluster in config.get('clusters', []): | |
ca_path = cluster['cluster'].get('certificate-authority') | |
if ca_path and Path(ca_path).exists(): | |
cluster['cluster']['certificate-authority-data'] = encode_file_to_base64(ca_path) | |
cluster['cluster'].pop('certificate-authority', None) | |
for user in config.get('users', []): | |
cert_path = user['user'].get('client-certificate') | |
key_path = user['user'].get('client-key') | |
if cert_path and Path(cert_path).exists(): | |
user['user']['client-certificate-data'] = encode_file_to_base64(cert_path) | |
user['user'].pop('client-certificate', None) | |
if key_path and Path(key_path).exists(): | |
user['user']['client-key-data'] = encode_file_to_base64(key_path) | |
user['user'].pop('client-key', None) | |
with open(kubectl_config_dest, 'w') as file: | |
yaml.safe_dump(config, file) | |
def main(): | |
# Copy Pachyderm configuration and change ownership | |
copy_and_change_ownership(pachyderm_config_src, pachyderm_config_dest) | |
# Process kubectl configuration | |
process_kubectl_config() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment