Skip to content

Commit

Permalink
add kubectl -f like feature for deploying from file
Browse files Browse the repository at this point in the history
  • Loading branch information
micw523 committed Oct 19, 2018
1 parent 3fb2be1 commit 8b3391f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
32 changes: 32 additions & 0 deletions examples/create_from_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.

from os import path

import yaml

from kubernetes import utils, config


def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
k8s_client, resp = utils.create_deployment_from_yaml("./nginx-deployment.yaml")
print("Deployment created. status='%s'" % str(resp.status))


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions kubernetes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
import kubernetes.config
import kubernetes.watch
import kubernetes.stream
import kubernetes.utils
15 changes: 15 additions & 0 deletions kubernetes/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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.

from .create_deployment_from_yaml import create_deployment_from_yaml
35 changes: 35 additions & 0 deletions kubernetes/utils/create_deployment_from_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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.

from os import path
import sys

import yaml

from kubernetes import client

def create_deployment_from_yaml(yaml_file):
with open(path.abspath(yaml_file)) as f:
dep = yaml.load(f)
api_type, _, api_version = dep["apiVersion"].partition("/")
api_type = api_type.capitalize()
api_version = api_version.capitalize()
k8s_client = getattr(client, "%s%sApi" % (api_type, api_version))()
resp = k8s_client.create_namespaced_deployment(body=dep, namespace="default")
return k8s_client, resp





3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
extras_require=EXTRAS,
packages=['kubernetes', 'kubernetes.client', 'kubernetes.config',
'kubernetes.watch', 'kubernetes.client.apis',
'kubernetes.stream', 'kubernetes.client.models'],
'kubernetes.stream', 'kubernetes.client.models',
'kubernetes.utils'],
include_package_data=True,
long_description="""\
Python client for kubernetes http://kubernetes.io/
Expand Down

0 comments on commit 8b3391f

Please sign in to comment.