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

Implement numerical ordering policy #104

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

15 changes: 15 additions & 0 deletions config/crd/bases/image.toolkit.fluxcd.io_imagepolicies.yaml
Original file line number Diff line number Diff line change
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
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
}
147 changes: 147 additions & 0 deletions internal/policy/numerical_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
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 (
"math/rand"
"testing"
"time"
)

func TestNewNumerical(t *testing.T) {
cases := []struct {
label string
order string
expectErr bool
}{
{
label: "With valid empty order",
order: "",
},
{
label: "With valid asc order",
order: NumericalOrderAsc,
},
{
label: "With valid desc order",
order: NumericalOrderDesc,
},
{
label: "With invalid order",
order: "invalid",
expectErr: true,
},
}

for _, tt := range cases {
t.Run(tt.label, func(t *testing.T) {
_, err := NewNumerical(tt.order)
if tt.expectErr && err == nil {
t.Fatalf("expecting error, got nil")
}
if !tt.expectErr && err != nil {
t.Fatalf("returned unexpected error: %s", err)
}
})
}
}

func TestNumerical_Latest(t *testing.T) {
cases := []struct {
label string
order string
versions []string
expectedVersion string
expectErr bool
}{
{
label: "With unordered list of integers ascending",
versions: shuffle([]string{"-62", "-88", "73", "72", "15", "16", "15", "29", "-33", "-91"}),
expectedVersion: "73",
},
{
label: "With unordered list of integers descending",
versions: shuffle([]string{"5", "-8", "-78", "25", "70", "-4", "80", "92", "-20", "-24"}),
order: NumericalOrderDesc,
expectedVersion: "-78",
},
{
label: "With unordered list of floats ascending",
versions: shuffle([]string{"47.40896403322944", "-27.8520927455902", "-27.930666514224427", "-31.352485948094568", "-50.41072694704882", "-21.962849842263736", "24.71884721436865", "-39.99177354004344", "53.47333823144817", "3.2008658570411086"}),
expectedVersion: "53.47333823144817",
},
{
label: "With unordered list of floats descending",
versions: shuffle([]string{"-65.27202780220686", "57.82948329142309", "22.40184684363291", "-86.36934305697784", "-90.29082099756083", "-12.041712603564264", "77.70488240399305", "-38.98425003883552", "16.06867070412028", "53.735674335181216"}),
order: NumericalOrderDesc,
expectedVersion: "-90.29082099756083",
},
{
label: "With Unix Timestamps ascending",
versions: shuffle([]string{"1606234201", "1606364286", "1606334092", "1606334284", "1606334201"}),
expectedVersion: "1606364286",
},
{
label: "With Unix Timestamps descending",
versions: shuffle([]string{"1606234201", "1606364286", "1606334092", "1606334284", "1606334201"}),
order: NumericalOrderDesc,
expectedVersion: "1606234201",
},
{
label: "With single value ascending",
versions: []string{"1"},
expectedVersion: "1",
},
{
label: "With single value descending",
versions: []string{"1"},
order: NumericalOrderDesc,
expectedVersion: "1",
},
{
label: "Empty version list",
versions: []string{},
expectErr: true,
},
}

for _, tt := range cases {
t.Run(tt.label, func(t *testing.T) {
policy, err := NewNumerical(tt.order)
if err != nil {
t.Fatalf("returned unexpected error: %s", err)
}
latest, err := policy.Latest(tt.versions)
if tt.expectErr && err == nil {
t.Fatalf("expecting error, got nil")
}
if !tt.expectErr && err != nil {
t.Fatalf("returned unexpected error: %s", err)
}

if latest != tt.expectedVersion {
t.Errorf("incorrect computed version returned, got '%s', expected '%s'", latest, tt.expectedVersion)
}
})
}
}

func shuffle(list []string) []string {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(list), func(i, j int) { list[i], list[j] = list[j], list[i] })
return list
}