Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add default kube config location #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2016
Merged
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
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ pip install kubernetes
list all pods:

```python
import os

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config(os.path.join(os.path.expanduser('~'), '.kube', 'config'))
config.load_kube_config()

v1=client.CoreV1Api()
print("Listing pods with their IPs:")
Expand All @@ -44,12 +42,10 @@ for i in ret.items:
watch on namespace object:

```python
import os

from kubernetes import client, config, watch

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config(os.path.join(os.path.expanduser('~'), '.kube', 'config'))
config.load_kube_config()

v1 = client.CoreV1Api()
count = 10
Expand Down
8 changes: 3 additions & 5 deletions examples/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from kubernetes import client, config


def main():
# Configs can be set in Configuration class directly or using helper
# utility
config.load_kube_config(
os.path.join(os.path.expanduser('~'), '.kube', 'config'))
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
Expand Down
8 changes: 3 additions & 5 deletions examples/example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from kubernetes import client, config, watch


def main():
# Configs can be set in Configuration class directly or using helper
# utility
config.load_kube_config(
os.path.join(os.path.expanduser('~'), '.kube', 'config'))
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()

v1 = client.CoreV1Api()
count = 10
Expand Down
8 changes: 3 additions & 5 deletions examples/example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from kubernetes import client, config


def main():
# Configs can be set in Configuration class directly or using helper
# utility
config.load_kube_config(
os.path.join(os.path.expanduser('~'), '.kube', 'config'))
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()

print("Supported APIs (* is preferred version):")
print("%-20s %s" %
Expand Down
7 changes: 2 additions & 5 deletions examples/example4.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from kubernetes import client, config
from kubernetes.client import configuration


def main():
config_file = os.path.join(os.path.expanduser('~'), '.kube', 'config')
contexts, active_context = config.list_kube_config_contexts(config_file)
contexts, active_context = config.list_kube_config_contexts()
if not contexts:
print("Cannot find any context in kube-config file.")
return
Expand All @@ -43,7 +40,7 @@ def main():

# Configs can be set in Configuration class directly or using helper
# utility
config.load_kube_config(config_file, context_name)
config.load_kube_config(context=context_name)

print("Active host is %s" % configuration.host)

Expand Down
12 changes: 10 additions & 2 deletions kubernetes/config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from .config_exception import ConfigException

KUBE_CONFIG_DEFAULT_LOCATION = '~/.kube/config'
_temp_files = {}


Expand Down Expand Up @@ -269,12 +270,16 @@ def _get_kube_config_loader_for_yaml_file(filename, **kwargs):
**kwargs)


def list_kube_config_contexts(config_file):
def list_kube_config_contexts(config_file=None):

if config_file is None:
config_file = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)

loader = _get_kube_config_loader_for_yaml_file(config_file)
return loader.list_contexts(), loader.current_context


def load_kube_config(config_file, context=None):
def load_kube_config(config_file=None, context=None):
"""Loads authentication and cluster information from kube-config file
and stores them in kubernetes.client.configuration.

Expand All @@ -283,5 +288,8 @@ def load_kube_config(config_file, context=None):
from config file will be used.
"""

if config_file is None:
config_file = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)

_get_kube_config_loader_for_yaml_file(
config_file, active_context=context).load_and_set()
2 changes: 1 addition & 1 deletion kubernetes/config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from .config_exception import ConfigException
from .kube_config import (ConfigNode, FileOrData, KubeConfigLoader,
_create_temp_file_with_content, _cleanup_temp_files)
_cleanup_temp_files, _create_temp_file_with_content)

NON_EXISTING_FILE = "zz_non_existing_file_472398324"

Expand Down
3 changes: 1 addition & 2 deletions scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
# 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
# See the License for the specific language governing permissions and
# limitations under the License.

# Intentional empty init file to make this folder a python package