Skip to content

Commit 8b3391f

Browse files
committed
add kubectl -f like feature for deploying from file
1 parent 3fb2be1 commit 8b3391f

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

examples/create_from_yaml.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from os import path
16+
17+
import yaml
18+
19+
from kubernetes import utils, config
20+
21+
22+
def main():
23+
# Configs can be set in Configuration class directly or using helper
24+
# utility. If no argument provided, the config will be loaded from
25+
# default location.
26+
config.load_kube_config()
27+
k8s_client, resp = utils.create_deployment_from_yaml("./nginx-deployment.yaml")
28+
print("Deployment created. status='%s'" % str(resp.status))
29+
30+
31+
if __name__ == '__main__':
32+
main()

kubernetes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
import kubernetes.config
2121
import kubernetes.watch
2222
import kubernetes.stream
23+
import kubernetes.utils

kubernetes/utils/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from .create_deployment_from_yaml import create_deployment_from_yaml
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from os import path
16+
import sys
17+
18+
import yaml
19+
20+
from kubernetes import client
21+
22+
def create_deployment_from_yaml(yaml_file):
23+
with open(path.abspath(yaml_file)) as f:
24+
dep = yaml.load(f)
25+
api_type, _, api_version = dep["apiVersion"].partition("/")
26+
api_type = api_type.capitalize()
27+
api_version = api_version.capitalize()
28+
k8s_client = getattr(client, "%s%sApi" % (api_type, api_version))()
29+
resp = k8s_client.create_namespaced_deployment(body=dep, namespace="default")
30+
return k8s_client, resp
31+
32+
33+
34+
35+

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
extras_require=EXTRAS,
5858
packages=['kubernetes', 'kubernetes.client', 'kubernetes.config',
5959
'kubernetes.watch', 'kubernetes.client.apis',
60-
'kubernetes.stream', 'kubernetes.client.models'],
60+
'kubernetes.stream', 'kubernetes.client.models',
61+
'kubernetes.utils'],
6162
include_package_data=True,
6263
long_description="""\
6364
Python client for kubernetes http://kubernetes.io/

0 commit comments

Comments
 (0)