Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions ui/operator_ui/cluster_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ def generate_cluster_id(url: str):
return CLUSTER_ID_INVALID_CHARS.sub('-', url.lower()).strip('-')


class StaticAuthorizationHeaderAuth(AuthBase):
'''Static authentication with given "Authorization" header'''
class KubernetesConfigAuth(AuthBase):
'''Dynamic authentication using the Kubernetes configuration to load the service account token'''

def __init__(self, authorization):
self.authorization = authorization
def __init__(self, config):
self.config = config

def __call__(self, request):
request.headers['Authorization'] = self.authorization
authorization = self.config.get_api_key_with_prefix('authorization')
if authorization:
request.headers['Authorization'] = authorization
return request


Expand Down Expand Up @@ -66,19 +68,20 @@ def __init__(self, api_server_urls: list):

if not api_server_urls:
try:
kubernetes.config.load_incluster_config()
config = kubernetes.client.Configuration()
kubernetes.config.load_incluster_config(config)
except kubernetes.config.ConfigException:
# we are not running inside a cluster
# => assume default kubectl proxy URL
cluster = Cluster(generate_cluster_id(DEFAULT_CLUSTERS), DEFAULT_CLUSTERS)
else:
logger.info("in cluster configuration failed")
config = kubernetes.client.Configuration()
auth = KubernetesConfigAuth(config)
cluster = Cluster(
generate_cluster_id(config.host),
config.host,
ssl_ca_cert=config.ssl_ca_cert,
auth=StaticAuthorizationHeaderAuth(config.api_key['authorization']))
auth=auth)
self._clusters.append(cluster)
else:
for api_server_url in api_server_urls:
Expand Down Expand Up @@ -110,11 +113,7 @@ def get_clusters(self):
continue
config = kubernetes.client.ConfigurationObject()
kubernetes.config.load_kube_config(config_file, context=context['name'], client_configuration=config)
authorization = config.api_key.get('authorization')
if authorization:
auth = StaticAuthorizationHeaderAuth(authorization)
else:
auth = None
auth = KubernetesConfigAuth(config)
cluster = Cluster(
context['name'],
config.host,
Expand Down
2 changes: 1 addition & 1 deletion ui/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ furl==2.1.3
gevent==24.2.1
jq==1.7.0
json_delta>=2.0.2
kubernetes==11.0.0
kubernetes==34.1.0
python-json-logger==2.0.7
requests==2.32.4
stups-tokens>=1.1.19
Expand Down