Skip to content

Commit

Permalink
chore: Update CHANGELOG.md
Browse files Browse the repository at this point in the history
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] authored and David MICHENEAU committed Oct 29, 2024
1 parent 5876177 commit 75909d4
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/58.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
`feat` - Add calver Rule Semantic.
```
2 changes: 1 addition & 1 deletion api/v1alpha1/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type (
Name string `json:"name"`

// +kubebuilder:validation:Required
// +kubebuilder:validation:Enum=semver-major;semver-minor;semver-patch;regex;always
// +kubebuilder:validation:Enum=calver-major;calver-minor;calver-patch;semver-major;semver-minor;semver-patch;regex;always
Type rules.Name `json:"type"`

// +kubebuilder:validation:Optional
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/kimup.cloudavenue.io_images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ spec:
type: string
type:
enum:
- calver-major
- calver-minor
- calver-patch
- semver-major
- semver-minor
- semver-patch
Expand Down
71 changes: 71 additions & 0 deletions docs/rules/calver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
hide:
- toc
---

# Semantic Versioning (calver)

The `calver` rule allows you to define a rule that will be executed when the image is updated with a new calver version.
It follows the [Semantic Versioning](https://calver.org/) specification.
A lot of options are available to match the version you want to update.

* `calver-major`: Update the image with the latest major version.
* `calver-minor`: Update the image with the latest minor version.
* `calver-patch`: Update the image with the latest patch version.

**`calver-major`** is the most restrictive and will only update the image when the major version is updated [calver documentation](https://calver.org/).
``` { .yaml .no-copy title="calver rule" }
version: 2024.0.0
Match: >=2024.*.* # (1)
```

1. :man_raising_hand: For more information about the calver range, you can check the [calver documentation](https://calver.org/).

**`calver-minor`** is less restrictive and will update the image when the minor version is updated.
``` { .yaml .no-copy title="calver rule" }
version: 2024.0.0
Match: >=2024.1.* <2 # (1)
```

1. :man_raising_hand: For more information about the calver range, you can check the [calver documentation](https://calver.org/).

**`calver-patch`** is the least restrictive and will update the image when the patch version is updated.
``` { .yaml .no-copy title="calver rule" }
version: 2024.0.0
Match: >=2024.0.1 <2024.1.0 # (1)
```

1. :man_raising_hand: For more information about the calver range, you can check the [calver documentation](https://calver.org/#spec-item-6).

## Who to use

Create an `Image` resource with the `calver` rule.

```yaml hl_lines="15 16 17 20 21 22 24 25 26"
apiVersion: kimup.cloudavenue.io/v1alpha1
kind: Image
metadata:
labels:
app.kubernetes.io/name: kube-image-updater
app.kubernetes.io/managed-by: kustomize
name: image-sample-with-auth
spec:
image: registry.127.0.0.1.nip.io/demo
baseTag: v2024.0.4
triggers:
- [...]
rules:
- name: Notify when calver major is detected
type: calver-major
actions:
- type: alert-xxx
[...]
- name: Automatic update calver minor
type: calver-minor
actions:
- type: apply
- name: Automatic update calver patch
type: calver-patch
actions:
- type: apply
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/ory/dockertest/v3 v3.11.0
github.com/prometheus/client_golang v1.20.5
github.com/reugn/go-quartz v0.13.0
github.com/shipengqi/vc v0.2.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/thanhpk/randstr v1.0.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ github.com/reugn/go-quartz v0.13.0 h1:0eMxvj28Qu1npIDdN9Mzg9hwyksGH6XJt4Cz0QB8EU
github.com/reugn/go-quartz v0.13.0/go.mod h1:0ghKksELp8MJ4h84T203aTHRF3Kug5BrxEW3ErBvhzY=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/shipengqi/vc v0.2.0 h1:o12S/csSz9siuTU2EmpzKnaHa7LuYxDA5zdjjSEv6F4=
github.com/shipengqi/vc v0.2.0/go.mod h1:OzfQDNheQAkQm5BZ2ZTiRjKhirVn4yaGUFGsN8C4IPY=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
96 changes: 96 additions & 0 deletions internal/rules/calver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package rules

import (
"github.com/shipengqi/vc"

"github.com/orange-cloudavenue/kube-image-updater/internal/log"
)

type (
// calverMajor - The first number in the version.
calverMajor struct {
rule
}

// calverMinor - The second number in the version.
calverMinor struct {
rule
}

// calverPatch - The third and usually final number in the version. Sometimes referred to as the "micro" segment.
calverPatch struct {
rule
}
)

func init() {
register(CalverMajor, &calverMajor{})
register(CalverMinor, &calverMinor{})
register(CalverPatch, &calverPatch{})
}

func (c *calverMajor) Evaluate() (matchWithRule bool, newTag string, err error) {
actualCV, err := vc.NewCalVerStr(c.actualTag)
if err != nil {
log.WithError(err).WithField("tag", c.actualTag).Error("Error parsing actual tag")
return false, "", err
}

for _, t := range c.tags {
cv, err := vc.NewCalVerStr(t)
if err != nil {
log.WithError(err).WithField("tag", t).Error("Error parsing tag")
continue
}

if cv.Major() > actualCV.Major() {
return true, t, nil
}
}

return false, "", nil
}

func (c *calverMinor) Evaluate() (matchWithRule bool, newTag string, err error) {
actualCV, err := vc.NewCalVerStr(c.actualTag)
if err != nil {
log.WithError(err).WithField("tag", c.actualTag).Error("Error parsing actual tag")
return false, "", err
}

for _, t := range c.tags {
cv, err := vc.NewCalVerStr(t)
if err != nil {
log.WithError(err).WithField("tag", t).Error("Error parsing tag")
continue
}

if cv.Minor() > actualCV.Minor() && cv.Major() == actualCV.Major() {
return true, t, nil
}
}

return false, "", nil
}

func (c *calverPatch) Evaluate() (matchWithRule bool, newTag string, err error) {
actualCV, err := vc.NewCalVerStr(c.actualTag)
if err != nil {
log.WithError(err).WithField("tag", c.actualTag).Error("Error parsing actual tag")
return false, "", err
}

for _, t := range c.tags {
cv, err := vc.NewCalVerStr(t)
if err != nil {
log.WithError(err).WithField("tag", t).Error("Error parsing tag")
continue
}

if cv.Patch() > actualCV.Patch() && cv.Minor() == actualCV.Minor() && cv.Major() == actualCV.Major() {
return true, t, nil
}
}

return false, "", nil
}
Loading

0 comments on commit 75909d4

Please sign in to comment.