Skip to content

Commit

Permalink
Implement numerical ordering policy
Browse files Browse the repository at this point in the history
Converts the given list of tags to floats and compares them based on the
ordering rule.
If a tag is not convertible, it will err and fail to compute the latest
version.

Fixes #102

Signed-off-by: Aurel Canciu <aurelcanciu@gmail.com>
  • Loading branch information
relu committed Feb 11, 2021
1 parent 201affa commit c05ecda
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
14 changes: 14 additions & 0 deletions api/v1alpha1/imagepolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type ImagePolicyChoice struct {
// Alphabetical set of rules to use for alphabetical ordering of the tags.
// +optional
Alphabetical *AlphabeticalPolicy `json:"alphabetical,omitempty"`
// Numerical set of rules to use for numerical ordering of the tags.
// +optional
Numerical *NumericalPolicy `json:"numerical,omitempty"`
}

// SemVerPolicy specifices a semantic version policy.
Expand All @@ -73,6 +76,17 @@ type AlphabeticalPolicy struct {
Order string `json:"order,omitempty"`
}

// NumericalPolicy specifices a numerical ordering policy.
type NumericalPolicy struct {
// Order specifies the sorting order of the tags. Given the integer values
// from 0 to 9 as tags, ascending order would select 9, and descending order
// would select 0.
// +kubebuilder:default:="asc"
// +kubebuilder:validation:Enum=asc;desc
// +optional
Order string `json:"order,omitempty"`
}

// TagFilter enables filtering tags based on a set of defined rules
type TagFilter struct {
// Pattern specifies a regular expression pattern used to filter for image
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion config/crd/bases/image.toolkit.fluxcd.io_imagepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.3.0
creationTimestamp: null
name: imagepolicies.image.toolkit.fluxcd.io
spec:
Expand Down Expand Up @@ -84,6 +84,21 @@ spec:
- desc
type: string
type: object
numerical:
description: Numerical set of rules to use for numerical ordering
of the tags.
properties:
order:
default: asc
description: Order specifies the sorting order of the tags.
Given the integer values from 0 to 9 as tags, ascending
order would select 9, and descending order would select
0.
enum:
- asc
- desc
type: string
type: object
semver:
description: SemVer gives a semantic version range to check against
the tags available.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.3.0
creationTimestamp: null
name: imagerepositories.image.toolkit.fluxcd.io
spec:
Expand Down
3 changes: 2 additions & 1 deletion internal/policy/alphabetical.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type Alphabetical struct {
Order string
}

// NewAlphabetical constructs a Alphabetical object validating the provided semver constraint
// NewAlphabetical constructs a Alphabetical object validating the provided
// order argument
func NewAlphabetical(order string) (*Alphabetical, error) {
switch order {
case "":
Expand Down
2 changes: 2 additions & 0 deletions internal/policy/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func PolicerFromSpec(choice imagev1.ImagePolicyChoice) (Policer, error) {
p, err = NewSemVer(choice.SemVer.Range)
case choice.Alphabetical != nil:
p, err = NewAlphabetical(strings.ToUpper(choice.Alphabetical.Order))
case choice.Numerical != nil:
p, err = NewNumerical(strings.ToUpper(choice.Numerical.Order))
default:
return nil, fmt.Errorf("given ImagePolicyChoice object is invalid")
}
Expand Down
79 changes: 79 additions & 0 deletions internal/policy/numerical.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package policy

import (
"fmt"
"strconv"
)

const (
// NumericalOrderAsc ascending order
NumericalOrderAsc = "ASC"
// NumericalOrderDesc descending order
NumericalOrderDesc = "DESC"
)

// Numerical representes a Numerical ordering policy
type Numerical struct {
Order string
}

// NewNumerical constructs a Numerical object validating the provided
// order argument
func NewNumerical(order string) (*Numerical, error) {
switch order {
case "":
order = NumericalOrderAsc
case NumericalOrderAsc, NumericalOrderDesc:
break
default:
return nil, fmt.Errorf("invalid order argument provided: '%s', must be one of: %s, %s", order, NumericalOrderAsc, NumericalOrderDesc)
}

return &Numerical{
Order: order,
}, nil
}

// Latest returns latest version from a provided list of strings
func (p *Numerical) Latest(versions []string) (string, error) {
if len(versions) == 0 {
return "", fmt.Errorf("version list argument cannot be empty")
}

var latest string
var pv float64
for i, version := range versions {
cv, err := strconv.ParseFloat(version, 64)
if err != nil {
return "", fmt.Errorf("failed to parse invalid numeric value '%s'", version)
}

switch {
case i == 0:
break // First iteration, nothing to compare
case p.Order == NumericalOrderAsc && cv < pv, p.Order == NumericalOrderDesc && cv > pv:
continue
}

latest = version
pv = cv
}

return latest, nil
}

0 comments on commit c05ecda

Please sign in to comment.