danielgormly / drone-plugin-kube

Drone CI plugin for creating & updating K8s Deployments, ConfigMaps
21 stars 15 forks source link

Template Subtitution #3

Closed SvenC56 closed 4 years ago

SvenC56 commented 4 years ago

Hello

I tried to make use of the template referencing but it is not working for me. I get this error in the log. It seems that the build.number value is empty.

1 | kubano v0.0.1 https://github.com/danielgormly/drone-plugin-kube
-- | --
2 | 2020/04/03 07:57:41 ⛔️ Error decoding template into valid Kubernetes object:
3 | 2020/04/03 07:57:41 ⛔️ Fatal error:
4 | yaml: line 19: mapping values are not allowed in this context
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: wiki
  labels:
    app: wiki
  name: wiki
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wiki
  template:
    metadata:
      labels:
        app: wiki
    spec:
      containers:
        - image: registry.example.com/ti/wiki:{{ build.number }}
          imagePullPolicy: Always
          name: wiki
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: wiki-ingress
  namespace: wiki
spec:
  rules:
    - host: wiki.example.com
      http:
        paths:
          - backend:
              serviceName: wiki-http
              servicePort: 80
            path: /
  tls:
    - hosts:
        - wiki.example.com
      secretName: wiki-cert
---
apiVersion: v1
kind: Service
metadata:
  name: wiki-http
  namespace: wiki
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: wiki
  type: ClusterIP
danielgormly commented 4 years ago

Hi! You are using an older version of the plugin, I wonder if that's my fault? Are you pulling it from Docker? If so what version are you using?!

danielgormly commented 4 years ago

Hi, if you're pulling from the Docker Hub, it's absolutely my fault. From docker.io: latest Last updated 7 months ago by danielgormly Ironically I don't have CI on this project! I've just pushed 0.2.0 to Docker Hub and tagged it with latest too.

It's good practice to fix the plugin version i.e. danielgormly/drone-plugin-kube:0.2.0

But if you do use just danielgormly/drone-plugin-kube you may have to also use the attribute pull: always for your next push, to ensure it actually downloads the latest plugin. You'll see the version number in the logs either way though.

Let me know how you go and I'll close the issue if it works

danielgormly commented 4 years ago

Ok issue number two:

image: registry.example.com/ti/wiki:{{ build.number }} You can't use this directly in the kubernetes yaml file! You have to pass it through to drone-plugin-kube via .drone.yml... e.g.:

- name: whatever
  image: danielgormly/drone-plugin-kube:0.2.0
  settings:
    build_number: ${build.number}

then access it like {{build_number}} inside the kubernetes yaml as you've done above

Potential issue three you might run into:

This plugin can only read one .yaml and change one resource at a time. I'm not sure if you're only using one kubernetes yaml scaffold? But you'd need to run the plugin one time for each resource i.e.:

- name: service
  image: danielgormly/drone-plugin-kube:0.2.0
...
- name: deployment
  image: danielgormly/drone-plugin-kube:0.2.0
...
- name: service
  image: danielgormly/drone-plugin-kube:0.2.0
SvenC56 commented 4 years ago

Hi many thanks for the fast reply. So I adapted the settings with your help and the issue with the build_number is working now. Now I get this error if I deploy the service:

danielgormly/drone-plugin-kube@0.0.2 https://github.com/danielgormly/drone-plugin-kube
2020/04/03 11:00:40 ⛔️ Fatal error: 
⛔️ This plugin doesn't support that resource type

These are my current settings:

kind: pipeline
type: kubernetes
name: default

steps:
  - name: Build Image
    image: node
    commands:
      - yarn global add vuepress
      - yarn install
      - yarn docs:build

  - name: "Docker: build & push"
    image: plugins/docker:18.09.1
    settings:
      registry: registry.example.com
      repo: registry.example.com/ti/wiki
      dockerfile: Dockerfile
      tag: ["latest", "v1.${DRONE_BUILD_NUMBER}"]
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password

  - name: Deploy K8s - Workload
    image: danielgormly/drone-plugin-kube:0.0.2
    settings:
      build_number: ${DRONE_BUILD_NUMBER}
      namespace: wiki
      template: deployment.yaml
      ca:
        from_secret: k8s_cert
      server:
        from_secret: k8s_server
      token:
        from_secret: k8s_token

  - name: Deploy K8s - Service
    image: danielgormly/drone-plugin-kube:0.0.2
    settings:
      namespace: wiki
      template: service.yaml
      ca:
        from_secret: k8s_cert
      server:
        from_secret: k8s_server
      token:
        from_secret: k8s_token

  - name: Deploy K8s - Ingress
    image: danielgormly/drone-plugin-kube:0.0.2
    settings:
      namespace: wiki
      template: ingress.yaml
      ca:
        from_secret: k8s_cert
      server:
        from_secret: k8s_server
      token:
        from_secret: k8s_token

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: wiki-http
  namespace: wiki
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: wiki
  type: ClusterIP
danielgormly commented 4 years ago

Oh hahah..... Sorry not danielgormly/drone-plugin-kube:0.0.2 but danielgormly/drone-plugin-kube:0.2.0

I'm terrible

SvenC56 commented 4 years ago

Yes I also overlooked it. It's working now with 0.2.0 🙂 . Thank you and have a nice weekend!

malikbenkirane commented 3 years ago

I filed #9 to see if we can get the images we can find on the docker hub with the releases tags and the log messages.

Thank you all!