跳转到主要内容

围绕 kubernetes-clients/python 的包装器

项目描述

CircleCI Code style: black Type checker: mypy Packaging: poetry Packaging: poetry codecov

Tulips

一个小型包装器,围绕 https://github.com/kubernetes-client/python,理解 Kubernetes 图表。

为什么

我需要一个简单的东西,它可以读取 Helm 图表并将它们推送到 Kubernetes 集群,并且是可扩展的。所以类似于 helm+kubectl,能够围绕它们编写自己的工具。

支持的 CRDS 即 Kubernetes 资源

  • 部署
  • 服务
  • 入口
  • 秘密
  • 发行者(cert-manager)
  • 持久卷声明

示例使用

import yaml
from tulips.resources import ResourceRegistry
from kubernetes import client as k8s
from kubernetes import config


client = config.new_client_from_config('kube.conf')

spec = yaml.load('ingress.yaml')

ingress_cls = ResourceRegistry.get_cls(spec['kind'])
ingress = ingress_cls(config.client, namespace='default', spec)
ingress.create()  # Create Ingress resource
ingress.delete()  # Delete Ingress resource

添加新资源

为了添加对新的 Kubernetes 资源的支持,需要创建一个继承自 tulips.resources.Resource 类的类。

示例资源

import tulips.resources.Resource

class ClusterIssuer(Resource):
    """A `cert-manager` ClusterIssuer resource."""

    version = "v1alpha1"
    group = "certmanager.k8s.io"
    plural = "clusterissuers"

    def delete(self, body: k8s.V1DeleteOptions):
        return k8s.CustomObjectsApi(
            self.client
        ).delete_namespaced_custom_object(
            body=body,
            namespace=self.namespace,
            version=self.version,
            group=self.group,
            plural=self.plural,
            name=self.name,
        )

    def create(self):
        return k8s.CustomObjectsApi(
            self.client
        ).create_namespaced_custom_object(
            body=self.resource,
            namespace=self.namespace,
            version=self.version,
            group=self.group,
            plural=self.plural,
        )

它将被注册到 ResourceRegistry 中,并且可以通过 ResourceRegistry.get_cls 方法获取。

Tulip

Tulip 是一个模拟 Helm 但没有 tiller 的客户端示例。

$ python tulips push --help                                    06/25/18 -  9:49
Usage: tulips push [OPTIONS] CHART

  You can pass chart variables via foo=bar, for example '$ tulip push
  app.yaml foo=bar'

Options:
  --namespace TEXT   Kubernetes namespace
  --release TEXT     Name of the release
  --kubeconfig PATH  Path to kubernetes config
  --help             Show this message and exit.

示例客户端

假设我想部署一个 Secret 和 Ingress

apiVersion: v1
kind: Secret
metadata:
  name: {{ release }}-secrets
type: Opaque
data:
  password: {{ @pwd }}
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ release }}-web-ingress
  labels:
    app: woocart-{{ release }}
  annotations:
    nginx.ingress.kubernetes.io/limit-connections: "100"
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: {{ domain }}
    http:
      paths:
        - path: /
          backend:
            serviceName: {{ release }}-web
            servicePort: 80

如果运行 `tulip --release test push --kubeconf kube.conf app.yaml domain=test.tld'

检查规范文件,并将所有 {{ variables }} 替换为实际值。此外,特殊的 {{ @pwd }} 将使用 passlib 库生成强密码。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

tulips-1.5.10.tar.gz (10.0 kB 查看哈希值)

上传时间 源代码

构建分发

tulips-1.5.10-py3-none-any.whl (13.3 kB 查看哈希值)

上传时间 Python 3

由以下组织支持