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

Mutating image registry does not preserve the project name #2028

Closed
drewhemm opened this issue Jun 16, 2021 · 10 comments · Fixed by #2166
Closed

Mutating image registry does not preserve the project name #2028

drewhemm opened this issue Jun 16, 2021 · 10 comments · Fixed by #2166
Assignees
Labels
bug Something isn't working mutation Issues pertaining to the mutate ability.

Comments

@drewhemm
Copy link

Is your feature request related to a problem? Please describe.

Relates to #674

The sample policy Replace Image Registry does not work well when the image is in project-name/image-name format. The regex replaces project-name with the custom repository, so that you end up with mylocalregistry/image-name instead of mylocalregistry/project-name/image-name

Describe the solution you'd like

I would like a policy that preserves the project-name, where present:

input:

Image: bash:5.0

Output:

Image: mylocalregistry/bash:5.0

Second example specifying an external registry
Input:

Image: quay.io/bash:5.0

Output:

Image: mylocalregistry/bash:5.0

Input:

Image: quay.io/foo/bash:5.0

Output:

Image: mylocalregistry/foo/bash:5.0

input:

Image: foo/bash:5.0

Output:

Image: mylocalregistry/foo/bash:5.0

@drewhemm drewhemm added the enhancement New feature or request label Jun 16, 2021
@github-actions github-actions bot added the sample policy New sample policy to add to library label Jun 16, 2021
@realshuting realshuting removed the sample policy New sample policy to add to library label Jun 16, 2021
@realshuting realshuting self-assigned this Jun 16, 2021
@realshuting realshuting added bug Something isn't working and removed enhancement New feature or request labels Jun 16, 2021
@realshuting realshuting added this to the Kyverno Release 1.4.1 milestone Jun 16, 2021
@realshuting
Copy link
Member

Hi @drewhemm - thanks for reporting. We'll investigate this issue.

@drewhemm
Copy link
Author

I think what is needed is something like this, but I am unsure how to chain the functions:

rules:
    - name: replace-image-registry-with-dots
      match:
        resources:
          kinds:
          - Pod
      mutate:
        patchStrategicMerge:
          spec:
            containers:
            - (name): "*"
              image: |-
                                {{ split(@, '/') | [0] | contains('.') | regex_replace_all('([\w\.-]*)\/', '{{@}}', 'myregistry.corp.com/') }}
    - name: replace-image-registry-without-dots
      match:
        resources:
          kinds:
          - Pod
      mutate:
        patchStrategicMerge:
          spec:
            containers:
            - (name): "*"
              image: |-
                                {{ split(@, '/') | [0] | contains('.') || replace('{{@}}', 'myregistry.corp.com/{{@}}' ) }}

@chipzoller chipzoller added the mutation Issues pertaining to the mutate ability. label Jul 15, 2021
@sjentzsch
Copy link

In addition, if you simply have something like image: busybox:1.28, it would also not prepend the custom repository, given the search string of '(.*)\/'

@realshuting
Copy link
Member

@sjentzsch - we are working on the fix to add the default image registry if it's missing, it should be available soon. We'll update you once we have the test image.

@uderik
Copy link

uderik commented Jul 20, 2021

@sjentzsch it's cool we'll wait

@realshuting
Copy link
Member

@uderik - the image is available with tag v1.4.1-63-gb2515fa9, and I modified the policy slightly in order to mutate the registry, we'll update the sample policy:

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: replace-image-registry
  annotations:
    policies.kyverno.io/title: Replace Image Registry
    policies.kyverno.io/category: Sample
    policies.kyverno.io/severity: medium
    policies.kyverno.io/subject: Pod
    policies.kyverno.io/minversion: 1.4.2
    policies.kyverno.io/description: >-
      Rather than blocking Pods which come from outside registries,
      it is also possible to mutate them so the pulls are directed to
      approved registries. This sample policy mutates all images either
      in the form 'image:tag' or 'registry.corp.com/image:tag' to be prefaced
      with `myregistry.corp.com/`.      
spec:
  background: false
  rules:
    - name: replace-image-registry
      match:
        resources:
          kinds:
          - Pod
      mutate:
        patchStrategicMerge:
          spec:
            containers:
            - (name): "*"
              image: |-
                {{ regex_replace_all('^[^/]+', '{{@}}', 'myregistry.corp.com') }}

@chipzoller
Copy link
Member

@realshuting If this updated sample is ready to go, I can include it in a held PR for v1.4.2 batched with other changes.

@realshuting
Copy link
Member

@realshuting If this updated sample is ready to go, I can include it in a held PR for v1.4.2 batched with other changes.

That will be great! Thanks @chipzoller.

@mohamedthings
Copy link

@realshuting same with prepend image registry https://kyverno.io/policies/other/prepend-image-registry/prepend-image-registry/ unable to add project path, any advice!

@chipzoller
Copy link
Member

@mohamedthings, please open a new issue and provide complete reproduction steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mutation Issues pertaining to the mutate ability.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants