Skip to content

Commit

Permalink
refactor: save kubeconfig as soon as cluster is provisioned
Browse files Browse the repository at this point in the history
  • Loading branch information
jjleng committed May 16, 2024
1 parent 7523643 commit c5f094f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 1 addition & 4 deletions paka/cli/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ def up(
Creates or updates a Kubernetes cluster based on the provided configuration.
"""
cluster_manager = load_cluster_manager(cluster_config)
cluster_manager.ctx.set_should_save_kubeconfig(not no_kubeconfig)
cluster_manager.create()
if not no_kubeconfig:
logger.info("Updating kubeconfig...")
update_kubeconfig(json.loads(cluster_manager.ctx.kubeconfig))
logger.info("Successfully updated kubeconfig.")


@cluster_app.command()
Expand Down
6 changes: 4 additions & 2 deletions paka/cluster/aws/eks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

import os
import json
from typing import List, Optional, cast

import pulumi
Expand All @@ -24,6 +24,7 @@
from paka.cluster.redis import create_redis
from paka.cluster.zipkin import create_zipkin
from paka.config import AwsMixedModelGroup, AwsModelGroup
from paka.k8s.utils import update_kubeconfig
from paka.utils import kubify_name


Expand Down Expand Up @@ -378,7 +379,8 @@ def create_eks_resources(kubeconfig_json: str) -> None:
ctx.set_k8s_provider(k8s_provider)
ctx.set_kubeconfig(kubeconfig_json)

# save_kubeconfig(ctx.cluster_name, kubeconfig_json)
if ctx.should_save_kubeconfig:
update_kubeconfig(json.loads(kubeconfig_json))

# Deploy the metrics server. This is required for the Horizontal Pod Autoscaler to work.
# HPA requires metrics to be available in order to scale the pods.
Expand Down
10 changes: 10 additions & 0 deletions paka/cluster/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Context:
# The kubeconfig str
_kubeconfig: Optional[str]

# Need to lock the access to these fields
_should_save_kubeconfig: bool = False

def __init__(self) -> None:
# Ugly, ideally, we can create these locks dynamically in __getattr__.
# However, __getattr__ is not thread safe either. We need another lock to protect the creation of locks.
Expand Down Expand Up @@ -109,3 +112,10 @@ def set_kubeconfig(self, kubeconfig: str) -> None:
@fasteners.read_locked(lock="_kubeconfig_lock")
def kubeconfig(self) -> Optional[str]:
return self._kubeconfig

def set_should_save_kubeconfig(self, should_save_kubeconfig: bool) -> None:
self._should_save_kubeconfig = should_save_kubeconfig

@property
def should_save_kubeconfig(self) -> bool:
return self._should_save_kubeconfig

0 comments on commit c5f094f

Please sign in to comment.