fluxcd / flux2

Open and extensible continuous delivery solution for Kubernetes. Powered by GitOps Toolkit.
https://fluxcd.io
Apache License 2.0
6.14k stars 573 forks source link

Reconciler error failed to parse invalid numeric value '' #3962

Open nguyenlinh1999 opened 1 year ago

nguyenlinh1999 commented 1 year ago

Describe the bug

hi everyone. i have using fluxcd for gitops. i using Image Update Automations for auto update image. i have issue when i create Imagepolicy Resource. before it work for me. but today it have exception please help me!

Steps to reproduce

this is my manifest!

apiVersion: image.toolkit.fluxcd.io/v1alpha1
kind: ImageRepository
metadata:
  name: example-repo
  namespace: flux-system
spec:
  interval: 1m0s
  image: gitlab-registry-repo
  secretRef:
    name: gitlab-secret
---
apiVersion: image.toolkit.fluxcd.io/v1alpha1
kind: ImagePolicy
metadata:
  name: example-policy
  namespace: flux-system
spec:
  imageRepositoryRef:
    name: example-repo
  filterTags:
    pattern: '^test-[a-fA-F0-9]+-(?P.*)'
    extract: '$ts'
  policy:
    numerical:
      order: asc

Expected behavior

and new image my build have tag: test-ff28ff32-1671684624

Screenshots and recordings

No response

OS / Distro

Ubuntu-20.04

Flux version

0.40.1

Flux check

► checking prerequisites ✗ flux 0.40.1 <2.0.0-rc.5 (new version is available, please upgrade) ✔ Kubernetes 1.21.14-eks-0a21954 >=1.20.6-0 ► checking controllers ✔ helm-controller: deployment ready ► ghcr.io/fluxcd/helm-controller:v0.11.1 ✔ image-automation-controller: deployment ready ► ghcr.io/fluxcd/image-automation-controller:v0.14.0 ✔ image-reflector-controller: deployment ready ► ghcr.io/fluxcd/image-reflector-controller:v0.11.0 ✔ kustomize-controller: deployment ready ► ghcr.io/fluxcd/kustomize-controller:v0.13.2 ✔ notification-controller: deployment ready ► ghcr.io/fluxcd/notification-controller:v0.15.0 ✔ source-controller: deployment ready ► ghcr.io/fluxcd/source-controller:v0.15.3 ► checking crds ✔ alerts.notification.toolkit.fluxcd.io/v1beta1 ✔ buckets.source.toolkit.fluxcd.io/v1beta1 ✔ gitrepositories.source.toolkit.fluxcd.io/v1beta1 ✔ helmcharts.source.toolkit.fluxcd.io/v1beta1 ✔ helmreleases.helm.toolkit.fluxcd.io/v2beta1 ✔ helmrepositories.source.toolkit.fluxcd.io/v1beta1 ✔ imagepolicies.image.toolkit.fluxcd.io/v1beta1 ✔ imagerepositories.image.toolkit.fluxcd.io/v1beta1 ✔ imageupdateautomations.image.toolkit.fluxcd.io/v1beta1 ✔ kustomizations.kustomize.toolkit.fluxcd.io/v1beta1 ✔ providers.notification.toolkit.fluxcd.io/v1beta1 ✔ receivers.notification.toolkit.fluxcd.io/v1beta1 ✔ all checks passed

Git provider

Gitlab

Container Registry provider

Gitlab container registry

Additional context

No response

Code of Conduct

darkowlzz commented 1 year ago

Hi, the ImagePolicy is configured to perform numerical sorting but the filter tag doesn't seem to be correct. On checking the regex pattern in https://regex101.com/, it says that the pattern contains some error. Maybe due to which the extracted tags being passed to the numerical sort is failing to parse it as real numbers. Based on examples from https://fluxcd.io/flux/guides/sortable-image-tags/#using-in-an-imagepolicy-object and https://fluxcd.io/flux/components/image/imagepolicies/#filter-tags , the pattern should have a named capture group to identify and extract it. Also, the pattern can be more strict to ensure that only numbers are selected by the capture group, else numerical sort will fail to parse the number again. Based on the pattern and extract provided, ^test-[a-fA-F0-9]+-(?P<ts>[0-9]+) may be what you need. If $ts is expected to contain alphabets as well, numerical sort won't work.

nguyenlinh1999 commented 1 year ago

1671684624

hi @darkowlzz. thanks your reply my docker image have tag: test-ff28ff32-1671684624 $ts => 1671684624 ('^test-[a-fA-F0-9]+-(?P.*)') this policy it work before. but today it not work. i don't have update policy or reconfig docker image tag builder

darkowlzz commented 1 year ago

It may be that one of the newly created tags contain non-numeric values at the end of the pattern and is being selected by the capture group for numerical sorting. Using ?P<ts>[0-9]+ would make sure only the tags that have numbers at that position get selected.

nguyenlinh1999 commented 1 year ago

thanks you so much! @darkowlzz i tried ^test-[a-fA-F0-9]+-(?P[0-9]+). and it work for me.

nguyenlinh1999 commented 1 year ago

But i don't know.! before i using pattern: '^test-[a-fA-F0-9]+-(?P.*)'. it work for many project. and now it's working for some projects

0xStarcat commented 3 months ago

I think I found a reproducible example of this that has given me some insight which I hope helps the maintainers in solving this. It seems like the issue is isolated to the behavior of policy.numerical and its handling of capture groups that are not numerical - even if the capture group is not involved in the extraction.

I received the error failed to parse invalid numeric value 'rc' with the following configuration, which was resolved when i changed to policy.semver:

# failed in extracting rc2024.03.13.0-12345 or v2024.03.13.0-12345
  filterTags:
    pattern: (^rc|^v)(?P<year>.*)\.(?P<month>.*)\.(?P<day>.*)\.(?P<patch>.*)-(?P<job_id>.*) 
    extract: '$job_id'
  policy:
    numerical:
      order: asc

versus successful semver:

# succeeds in extracting rc2024.03.13.0-12345 or v2024.03.13.0-12345
  filterTags:
    pattern: (^rc|^v)(?P<year>.*)\.(?P<month>.*)\.(?P<day>.*)\.(?P<patch>.*)-(?P<job_id>.*) 
    extract: 0.0.$job_id
  policy:
    semver:
      range: ">=0.0.0"