kubernetes kubebuilder在本地运行/调试webhook

ldioqlga  于 2022-12-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(466)

bounty将在3天后过期。回答此问题可获得+100声望奖励。JME正在寻找来自知名来源的答案:我需要一种方法来调试本地的kubebuilder操作员与webhooks,因为它可能没有任何问题之前,使用webhooks与证书等

我们有一个kubebuilder控制器,它按预期工作,现在我们需要创建一个webhook,
我按照教程https://book.kubebuilder.io/reference/markers/webhook.html,现在我想运行和调试它在本地,但不知道该做什么关于证书,有一个简单的方法来创建它,任何例子将是非常有帮助的。
顺便说一句,我已经安装了cert-manager,并应用了下面的样本yaml,但不知道下一步该怎么做...
我需要最简单的解决方案,我能够运行和调试的webhook s本地作为我已经做的控制器(使用webhook之前),
https://book.kubebuilder.io/cronjob-tutorial/running.html
证书管理器
我在集群中创建了以下内容

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: example-com
  namespace: test
spec:
  # Secret names are always required.
  secretName: example-com-tls

  # secretTemplate is optional. If set, these annotations and labels will be
  # copied to the Secret named example-com-tls. These labels and annotations will
  # be re-reconciled if the Certificate's secretTemplate changes. secretTemplate
  # is also enforced, so relevant label and annotation changes on the Secret by a
  # third party will be overwriten by cert-manager to match the secretTemplate.
  secretTemplate:
    annotations:
      my-secret-annotation-1: "foo"
      my-secret-annotation-2: "bar"
    labels:
      my-secret-label: foo

  duration: 2160h # 90d
  renewBefore: 360h # 15d
  subject:
    organizations:
      - jetstack
  # The use of the common name field has been deprecated since 2000 and is
  # discouraged from being used.
  commonName: example.com
  isCA: false
  privateKey:
    algorithm: RSA
    encoding: PKCS1
    size: 2048
  usages:
    - server auth
    - client auth
  # At least one of a DNS Name, URI, or IP address is required.
  dnsNames:
    - example.com
    - www.example.com
  uris:
    - spiffe://cluster.local/ns/sandbox/sa/example
  ipAddresses:
    - 192.168.0.5
  # Issuer references are always required.
  issuerRef:
    name: ca-issuer
    # We can reference ClusterIssuers by changing the kind here.
    # The default value is Issuer (i.e. a locally namespaced Issuer)
    kind: Issuer
    # This is optional since cert-manager will default to this value however
    # if you are using an external issuer, change this to that issuer group.
    group: cert-manager.io

仍然不确定如何将其与kubebuilder同步以在本地工作
因为当我在调试模式下运行该操作符时,我得到了以下错误:
setup problem running manager {"error": "open /var/folders/vh/_418c55133sgjrwr7n0d7bl40000gn/T/k8s-webhook-server/serving-certs/tls.crt: no such file or directory"}
我需要的是最简单的方法在本地运行webhook

mepcadol

mepcadol1#

让我从头开始向您介绍整个过程。
1.像cronJob教程-kubebuilder create webhook --group batch --version v1 --kind CronJob --defaulting --programmatic-validation中所说的那样创建webhook。这将创建用于实现默认逻辑和验证逻辑的webhook。
1.按照说明实施逻辑-Implementing defaulting/validating webhooks
1.安装cert-manager。我发现最简单的安装方法是通过这个命令-kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml
1.编辑config/default/kustomization.yaml文件,取消注解中包含[WEBHOOK]或[CERTMANAGER]的所有内容。对config/crd/kustomization.yaml文件也执行相同操作。
1.使用-make docker-build IMG=<some-registry>/<project-name>:tag在本地构建映像。现在,您无需将映像docker-push到远程存储库。如果您使用的是种类群集,您可以直接将本地映像加载到指定的种类群集:kind load docker-image <your-image-name>:tag --name <your-kind-cluster-name>
1.现在,您可以通过-make deploy IMG=<some-registry>/<project-name>:tag将其部署到集群。
你也可以使用make run命令在本地运行集群。但是,如果你已经启用了webooks,这就有点棘手了。我建议你用KIND集群来运行集群。在这里,你不需要担心注入证书的问题。cert-manager会为你做这些。你可以查看/config/certmanager文件夹来了解它是如何工作的。

相关问题