-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
from k8sclient import configuration | ||
|
||
|
||
_SERVICE_HOST_ENV_NAME = "KUBERNETES_SERVICE_HOST" | ||
_SERVICE_PORT_ENV_NAME = "KUBERNETES_SERVICE_PORT" | ||
_SERVICE_TOKEN_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/token" | ||
_SERVICE_CERT_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" | ||
|
||
|
||
class ConfigException(Exception): | ||
pass | ||
|
||
|
||
def _join_host_port(host, port): | ||
"""Adapted golang's net.JoinHostPort""" | ||
if ':' in host or '%' in host: | ||
return "[%s]:%s" % (host, str(port)) | ||
return "%s:%s" % (host, str(port)) | ||
|
||
|
||
def load_incluster_config(): | ||
"""Use the service account kubernetes gives to pods to connect to kubernetes | ||
cluster. It's intended for clients that expect to be running inside a pod | ||
running on kubernetes. It will raise an exception if called from a process | ||
not running in a kubernetes environment.""" | ||
if (_SERVICE_HOST_ENV_NAME not in os.environ or | ||
_SERVICE_PORT_ENV_NAME not in os.environ): | ||
raise ConfigException("Service host/port is not set.") | ||
|
||
configuration.host = ("https://" + | ||
_join_host_port(os.environ[_SERVICE_HOST_ENV_NAME], | ||
os.environ[_SERVICE_PORT_ENV_NAME])) | ||
|
||
if not os.path.isfile(_SERVICE_TOKEN_FILENAME): | ||
raise ConfigException("Service token file does not exists.") | ||
|
||
with open(_SERVICE_TOKEN_FILENAME, 'r') as f: | ||
configuration.api_key['authorization'] = "bearer " + f.read() | ||
|
||
if not os.path.isfile(_SERVICE_CERT_FILENAME): | ||
raise ConfigException("Service certification file does not exists.") | ||
|
||
configuration.ssl_ca_cert = _SERVICE_CERT_FILENAME |