Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sample] rewrite image registry #882

Closed
2 tasks done
JimBugwadia opened this issue Jan 8, 2024 · 8 comments · Fixed by #890
Closed
2 tasks done

[Sample] rewrite image registry #882

JimBugwadia opened this issue Jan 8, 2024 · 8 comments · Fixed by #890
Assignees
Labels
good first issue Good for newcomers sample Sample policy

Comments

@JimBugwadia
Copy link
Member

Problem Statement

rewrite the image registry, to force all pulls throug a proxy cache e.g. with harbor

Solution Description

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: replace-image-registry
spec:
  background: false
  rules:
    - name: replace-image-registry
      match:
        any:
        - resources:
            kinds:
            - Pod
      mutate:
        foreach:
        - list: "request.object.spec.containers"
          context: 
          - name: imageData
            imageRegistry: 
              reference: "{{ element.image }}"
          preconditions:
            all:
            - key: "{{imageData.image}}"
              operator: Equals
              value: "docker.io/*"
          patchStrategicMerge:
            spec:
              containers:
              - name: "{{ element.name }}"
                image: "harbor2.zoller.com/dockerhub-proxy/{{imageData.repository}}:{{imageData.identifier}}"

Example "Good" Resource

No response

Example "Bad" Resource

No response

Other Comments

No response

Slack discussion

https://kubernetes.slack.com/archives/CLGR9BJU9/p1704740196988469

Troubleshooting

@JimBugwadia JimBugwadia added good first issue Good for newcomers sample Sample policy labels Jan 8, 2024
@JimBugwadia JimBugwadia changed the title [Sample] rewrite image registrt [Sample] rewrite image registry Jan 8, 2024
@siddhikhapare
Copy link
Contributor

/assign

@siddhikhapare
Copy link
Contributor

I would like to work on this

@jseiser
Copy link

jseiser commented Jan 9, 2024

We tested the above, and it didnt work with the bare registry, so we ended up with this.

    spec:
      background: false
      rules:
      - name: prepend-registry-containers
        match:
          any:
          - resources:
              kinds:
              - Pod
        preconditions:
          all:
          - key: "{{request.operation || 'BACKGROUND'}}"
            operator: AnyIn
            value:
            - CREATE
            - UPDATE
        mutate:
          foreach:
          - list: "request.object.spec.containers"
            patchStrategicMerge:
              spec:
                containers:
                - name: "{{ element.name }}"           
                  image: "harbor.tld/{{ images.containers.\"{{element.name}}\".registry}}/{{ images.containers.\"{{element.name}}\".path}}:{{images.containers.\"{{element.name}}\".tag}}"
      - name: prepend-registry-initcontainers
        match:
          any:
          - resources:
              kinds:
              - Pod
        preconditions:
          all:
          - key: "{{request.operation || 'BACKGROUND'}}"
            operator: AnyIn
            value:
            - CREATE
            - UPDATE
          - key: "{{ request.object.spec.initContainers[] || '' | length(@) }}"
            operator: GreaterThanOrEquals
            value: 1
        mutate:
          foreach:
          - list: "request.object.spec.initContainers"
            patchStrategicMerge:
              spec:
                initContainers:
                - name: "{{ element.name }}"           
                  image: "harbor.tld/{{ images.initContainers.\"{{element.name}}\".registry}}/{{ images.initContainers.\"{{element.name}}\".path}}:{{images.initContainers.\"{{element.name}}\".tag}}"

This assumes your harbor proxy caches, have the same exact name as the registry you are replacing e.g. harbor.tld/quay.io and harbor.tld/dockerhub.io Also, we generate this with TF, which is why you see those extra \ escapes

@siddhikhapare
Copy link
Contributor

siddhikhapare commented Jan 10, 2024

@JimBugwadia I have also tested this policy -

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: replace-image-registry
  annotations:
    policies.kyverno.io/title: Replace Image Registry
    kyverno.io/kubernetes-version: "1.24"
spec:
  background: false
  rules:
    - name: replace-image-registry-pod-containers
      match:
        any:
          - resources:
            kinds:
              - Pod
      mutate:
        foreach:
          - list: "request.object.spec.containers"
            context:
              - name: imageData
                imageRegistry:
                  reference: "{{ element.image }}"
            preconditions:
              any:
                - key: "{{imageData.image}}"
                  operator: Equals
                  value: "docker.io/*"
            patchStrategicMerge:
              spec:
                containers:
                  - name: "{{ element.name }}"
                    image: "harbor.example.com/{{imageData.repository}}:{{imageData.identifier}}"
    - name: replace-image-registry-pod-initcontainers
      match:
        any:
        - resources:
            kinds:
            - Pod
      preconditions:
        all:
          - key: "{{ request.object.spec.initContainers[] || `[]` | length(@) }}"
            operator: GreaterThanOrEquals
            value: 1
      mutate:
        foreach:
          - list: "request.object.spec.initContainers"
            context:
              - name: imageData
                imageRegistry:
                  reference: "{{ element.image }}"
            preconditions:
              any:
                - key: "{{imageData.image}}"
                  operator: Equals
                  value: "docker.io/*"
            patchStrategicMerge:
              spec:
                initContainers:
                  - name: "{{ element.name }}"
                    image: "harbor.example.com/{{imageData.repository}}:{{imageData.identifier}}"

I got this output -
It should not fail for this patchedresource but it failed

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod1
spec:
  containers:
    - image: harbor.example.com/nginx:latest
      name: docker-with-registry

output

harbor is running -
domain

@jseiser
Copy link

jseiser commented Jan 10, 2024

Wanted to note, what we used above, doesnt work well either. It appears to work fine, and then you will randomly get pods that have the registry doubled up.

Successfully pulled image "harbor.tld/cr.l5d.io/linkerd/proxy-init:v2.2.3" in 909ms (909ms including waiting)
Successfully pulled image "harbor.tld/cr.l5d.io/linkerd/proxy:stable-2.14.8" in 794ms (794ms including waiting)
Back-off pulling image "harbor.tld/harbor.tld/registry.k8s.io/ingress-nginx/controller:v1.9.5"

I cant figure out why, when i test it locally

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  namespace: policy-test
  labels:
    app: myapp
spec:
  containers:
    - name: docker-io
      image: docker.io/velero/velero:v1.6.2
      imagePullPolicy: Always

    - name: docker-library-no-reg
      image: nginx:1.25.3-perl
      imagePullPolicy: Always

    - name: docker-library-partial-reg
      image: jenkins/jenkins:2.426.2-jdk17
      imagePullPolicy: Always

    - name: quay
      image: quay.io/jetstack/cert-manager-controller:v1.12.7
      imagePullPolicy: Always

    - name: quay2
      image: quay.io/jetstack/cert-manager-controller:v1.12.7
      imagePullPolicy: Always

  initContainers:
    - name: init-myservice
      image: busybox:1.28
      command: ["sh", "-c", "sleep 2; done"]

I have no problems, but ive noticed with my helm charts we have deployed, it will randomly happen like the above.

@JimBugwadia
Copy link
Member Author

See: kyverno/kyverno#9381

@siddhikhapare
Copy link
Contributor

See: kyverno/kyverno#9381

Thanks @JimBugwadia @chipzoller

@chipzoller
Copy link
Member

@jseiser, the reason you were probably seeing the doubled-up name prefix was due to Pod UPDATE ops. To avoid that, it's best to fire on CREATE requests only. The point may be moot if you still haven't found anything that'll work currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers sample Sample policy
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants