Skip to content
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

Kubeconfig context support #36

Merged
merged 3 commits into from
Feb 19, 2021
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## In progress

- Kubeconfig context support [#36](https://github.com/devopsspiral/KubeLibrary/pull/36) by [@m-wcislo](https://github.com/m-wcislo)
- Keyword for getting secrets [#31](https://github.com/devopsspiral/KubeLibrary/pull/31 )by [@Nilsty](https://github.com/Nilsty)

## [0.3.0] - 2021-02-01

### Added
- CI implementation [#14](https://github.com/devopsspiral/KubeLibrary/pull/14) by [@m-wcislo](https://github.com/m-wcislo) @m-wcislo
- CI implementation [#14](https://github.com/devopsspiral/KubeLibrary/pull/14) by [@m-wcislo](https://github.com/m-wcislo)
- keywords to list deployments [#13](https://github.com/devopsspiral/KubeLibrary/pull/13) by [@Nilsty](https://github.com/Nilsty)
- keywords for get/create/delete service accounts [#28](https://github.com/devopsspiral/KubeLibrary/pull/28) by [@kutayy](https://github.com/kutayy)

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export KUBECONFIG=~/.kube/config
pip install robotframework-requests
git clone https://github.com/devopsspiral/KubeLibrary.git
cd KubeLibrary
robot testcases
robot -e prerelease testcases
```

## Example testcase
Expand Down Expand Up @@ -51,7 +51,7 @@ To see all the tests passing execute below commands.
### Cluster Tests
```
# run cluster tests
robot -i cluster testcases/
robot -i cluster -e prerelease testcases/
```

### Grafana Tests
Expand All @@ -65,7 +65,7 @@ export KLIB_POD_PATTERN='grafana.*'
export KLIB_POD_ANNOTATIONS='{"kubelibrary":"testing"}'
export KLIB_POD_NAMESPACE=default

robot -i grafana testcases/
robot -i grafana -e prerelease testcases/
```

### Octopus Tests
Expand All @@ -83,7 +83,7 @@ export KLIB_ENV_VARS='{"SECRET_NAME":"webhook-server-secret"}'
export KLIB_POD_NAMESPACE=default
export KLIB_RESOURCE_REQUESTS_MEMORY=20Mi

robot -i octopus testcases/
robot -i octopus -e prerelease testcases/
```

### Other Tests
Expand All @@ -98,7 +98,7 @@ kubectl create namespace $KLIB_POD_NAMESPACE
kubectl label namespaces kubelib-tests test=test
helm install kubelib-test ./test-objects-chart -n $KLIB_POD_NAMESPACE

robot -i other testcases/
robot -i other -e prerelease testcases/
```
### Multi Cluster Tests
These tests require more than one cluster and utilize [KinD](https://kind.sigs.k8s.io/) as a setup.
Expand All @@ -116,7 +116,7 @@ kind create cluster --kubeconfig ./cluster2-conf --name kind-cluster-2
# Create namespace in Test Cluster 2
kubectl create namespace test-ns-2 --context kind-kind-cluster-2 --kubeconfig ./cluster2-conf

robot -i reload-config testcases/
robot -i reload-config -e prerelease testcases/

# Clean up
kind delete cluster --name kind-cluster-1
Expand Down
4 changes: 2 additions & 2 deletions docs/index.html

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions src/KubeLibrary/KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class KubeLibrary(object):
| ***** Settings *****
| Library KubeLibrary /path/to/kubeconfig

= Context =

By default current context from kubeconfig is used. Setting multiple contexts in
different test suites allows working on multiple clusters.

| ***** Settings *****
| Library KubeLibrary context=k3d-k3d-cluster2

= In cluster execution =

If tests are supposed to be executed from within cluster, KubeLibrary can be configured to use standard
Expand All @@ -33,22 +41,26 @@ class KubeLibrary(object):
| Library KubeLibrary None True

"""
def __init__(self, kube_config=None, incluster=False, cert_validation=True):
def __init__(self, kube_config=None, context=None, incluster=False, cert_validation=True):
"""KubeLibrary can be configured with several optional arguments.
- ``kube_config``:
Path pointing to kubeconfig of target Kubernetes cluster.
- ``context``:
Active context. If None current_context from kubeconfig is used.
- ``incuster``:
Default False. Indicates if used from within k8s cluster. Overrides kubeconfig.
- ``cert_validation``:
Default True. Can be set to False for self-signed certificates.
"""
self.reload_config(kube_config=kube_config, incluster=incluster, cert_validation=cert_validation)
self.reload_config(kube_config=kube_config, context=context, incluster=incluster, cert_validation=cert_validation)

def reload_config(self, kube_config=None, incluster=False, cert_validation=True):
def reload_config(self, kube_config=None, context=None, incluster=False, cert_validation=True):
"""Reload the KubeLibrary to be configured with different optional arguments.
This can be used to connect to a different cluster during the same test.
- ``kube_config``:
Path pointing to kubeconfig of target Kubernetes cluster.
- ``context``:
Active context. If None current_context from kubeconfig is used.
- ``incuster``:
Default False. Indicates if used from within k8s cluster. Overrides kubeconfig.
- ``cert_validation``:
Expand All @@ -62,7 +74,7 @@ def reload_config(self, kube_config=None, incluster=False, cert_validation=True)
raise e
else:
try:
config.load_kube_config(kube_config)
config.load_kube_config(kube_config, context)
except TypeError:
logger.error('Neither KUBECONFIG nor ~/.kube/config available.')
self.v1 = client.CoreV1Api()
Expand Down
31 changes: 31 additions & 0 deletions test/resources/multiple_context
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJWekNCL3FBREFnRUNBZ0VBTUFvR0NDcUdTTTQ5QkFNQ01DTXhJVEFmQmdOVkJBTU1HR3N6Y3kxelpYSjIKWlhJdFkyRkFNVFl4TXpVNU1UUXhNVEFlRncweU1UQXlNVGN4T1RVd01URmFGdzB6TVRBeU1UVXhPVFV3TVRGYQpNQ014SVRBZkJnTlZCQU1NR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0FNRVFDSURVWHUvRCtIdGJXClEzRTNlbEtGNHp6N3dMSjNmYXRYbGZpNmtUL0NCanlhQWlCcGo3NktMR1htZ1ZKQ0VrRVBSU3AvSjJ5NkkzMlUKN0JyOG02UG1aaHlJSWc9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://0.0.0.0:37971
name: k3d-k3d-cluster2
contexts:
- context:
cluster: k3d-k3d-cluster
user: admin@k3d-k3d-cluster
name: k3d-k3d-cluster
- context:
cluster: k3d-k3d-cluster2
user: admin@k3d-k3d-cluster2
name: k3d-k3d-cluster2
current-context: k3d-k3d-cluster
kind: Config
preferences: {}
users:
- name: admin@k3d-k3d-cluster
user:
password: 7723eb490bd1900a0686c9dcc55de40e
username: admin
- name: admin@k3d-k3d-cluster2
user:
password: 34e01dc12a8d5054aceead504dec1112
username: admin
8 changes: 8 additions & 0 deletions test/test_KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import unittest
from KubeLibrary import KubeLibrary
from kubernetes.config.config_exception import ConfigException


class AttributeDict(object):
Expand Down Expand Up @@ -89,6 +90,13 @@ class TestKubeLibrary(unittest.TestCase):
def test_KubeLibrary_inits_from_kubeconfig(self):
KubeLibrary(kube_config='test/resources/k3d')

def test_KubeLibrary_inits_with_context(self):
KubeLibrary(kube_config='test/resources/multiple_context', context='k3d-k3d-cluster2')

def test_KubeLibrary_fails_for_wrong_context(self):
kl = KubeLibrary(kube_config='test/resources/multiple_context')
self.assertRaises(ConfigException, kl.reload_config, kube_config='test/resources/multiple_context', context='k3d-k3d-cluster2-wrong')

def test_KubeLibrary_inits_without_cert_validation(self):
KubeLibrary(kube_config='test/resources/k3d', cert_validation=False)

Expand Down
2 changes: 1 addition & 1 deletion testcases/secrets/secret.robot
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Resource ./secret_kw.robot

*** Test Cases ***
Secrets test case example
[Tags] grafana
[Tags] grafana prerelease
List all secrets in namespace default
Read grafana secrets