Support shortening of revision with digest

The `\b` in the regular expression ensures we only match with a
hexadecimal notation as awhole, while still allowing to match with
e.g. `sha1:...` which would not have been possible by using `\W`
as this includes `_`.

Signed-off-by: Hidde Beydals <hello@hidde.co>
pull/3587/head
Hidde Beydals 2 years ago
parent ae9728685c
commit bb6a7b8f07

@ -18,15 +18,15 @@ package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
"github.com/fluxcd/flux2/internal/utils"
)
var getKsCmd = &cobra.Command{
@ -80,10 +80,8 @@ func (a kustomizationListAdapter) summariseItem(i int, includeNamespace bool, in
item := a.Items[i]
revision := item.Status.LastAppliedRevision
status, msg := statusAndMessage(item.Status.Conditions)
if status == string(metav1.ConditionTrue) {
revision = shortenCommitSha(revision)
msg = shortenCommitSha(msg)
}
revision = utils.TruncateHex(revision)
msg = utils.TruncateHex(msg)
return append(nameColumns(&item, includeNamespace, includeKind),
revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)), status, msg)
}
@ -100,13 +98,3 @@ func (a kustomizationListAdapter) statusSelectorMatches(i int, conditionType, co
item := a.Items[i]
return statusMatches(conditionType, conditionStatus, item.Status.Conditions)
}
func shortenCommitSha(msg string) string {
r := regexp.MustCompile("/([a-f0-9]{40})$")
sha := r.FindString(msg)
if sha != "" {
msg = strings.Replace(msg, sha, string([]rune(sha)[:8]), -1)
}
return msg
}

@ -25,6 +25,8 @@ import (
"k8s.io/apimachinery/pkg/runtime"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/fluxcd/flux2/internal/utils"
)
var getSourceBucketCmd = &cobra.Command{
@ -80,6 +82,8 @@ func (a *bucketListAdapter) summariseItem(i int, includeNamespace bool, includeK
revision = item.GetArtifact().Revision
}
status, msg := statusAndMessage(item.Status.Conditions)
revision = utils.TruncateHex(revision)
msg = utils.TruncateHex(msg)
return append(nameColumns(&item, includeNamespace, includeKind),
revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)), status, msg)
}

@ -25,6 +25,8 @@ import (
"k8s.io/apimachinery/pkg/runtime"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/fluxcd/flux2/internal/utils"
)
var getSourceHelmChartCmd = &cobra.Command{
@ -80,6 +82,9 @@ func (a *helmChartListAdapter) summariseItem(i int, includeNamespace bool, inclu
revision = item.GetArtifact().Revision
}
status, msg := statusAndMessage(item.Status.Conditions)
// NB: do not shorten revision as it contains a SemVer
// Message may still contain reference of e.g. commit chart was build from
msg = utils.TruncateHex(msg)
return append(nameColumns(&item, includeNamespace, includeKind),
revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)), status, msg)
}

@ -22,10 +22,11 @@ import (
"strings"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/fluxcd/flux2/internal/utils"
)
var getSourceGitCmd = &cobra.Command{
@ -81,10 +82,8 @@ func (a *gitRepositoryListAdapter) summariseItem(i int, includeNamespace bool, i
revision = item.GetArtifact().Revision
}
status, msg := statusAndMessage(item.Status.Conditions)
if status == string(metav1.ConditionTrue) {
revision = shortenCommitSha(revision)
msg = shortenCommitSha(msg)
}
revision = utils.TruncateHex(revision)
msg = utils.TruncateHex(msg)
return append(nameColumns(&item, includeNamespace, includeKind),
revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)), status, msg)
}

@ -25,6 +25,8 @@ import (
"k8s.io/apimachinery/pkg/runtime"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/fluxcd/flux2/internal/utils"
)
var getSourceHelmCmd = &cobra.Command{
@ -80,6 +82,8 @@ func (a *helmRepositoryListAdapter) summariseItem(i int, includeNamespace bool,
revision = item.GetArtifact().Revision
}
status, msg := statusAndMessage(item.Status.Conditions)
revision = utils.TruncateHex(revision)
msg = utils.TruncateHex(msg)
return append(nameColumns(&item, includeNamespace, includeKind),
revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)), status, msg)
}

@ -25,6 +25,8 @@ import (
"k8s.io/apimachinery/pkg/runtime"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/fluxcd/flux2/internal/utils"
)
var getSourceOCIRepositoryCmd = &cobra.Command{
@ -80,6 +82,8 @@ func (a *ociRepositoryListAdapter) summariseItem(i int, includeNamespace bool, i
revision = item.GetArtifact().Revision
}
status, msg := statusAndMessage(item.Status.Conditions)
revision = utils.TruncateHex(revision)
msg = utils.TruncateHex(msg)
return append(nameColumns(&item, includeNamespace, includeKind),
revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)), status, msg)
}

@ -1,2 +1,2 @@
NAME REVISION SUSPENDED READY MESSAGE
tkfg 6.0.0/627d5c4 False True Applied revision: 6.0.0/627d5c4
NAME REVISION SUSPENDED READY MESSAGE
tkfg 6.0.0/627d5c4b False True Applied revision: 6.0.0/627d5c4b

@ -1,2 +1,2 @@
NAME REVISION SUSPENDED READY MESSAGE
thrfg 6.1.6/dbdb109711ffb3be77504d2670dbe13c24dd63d8d7f1fb489d350e5bfe930dd3 False True stored artifact for digest '6.1.6/dbdb109711ffb3be77504d2670dbe13c24dd63d8d7f1fb489d350e5bfe930dd3'
NAME REVISION SUSPENDED READY MESSAGE
thrfg 6.1.6/dbdb1097 False True stored artifact for digest '6.1.6/dbdb1097'

@ -0,0 +1,39 @@
/*
Copyright 2023 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 utils
import (
"regexp"
"strings"
)
// hexRegexp matches any hexadecimal notation between 40 and 128 characters.
var hexRegexp = regexp.MustCompile(`\b[a-f0-9]{40,128}\b`)
// TruncateHex will replace any hexadecimal notation between 40 and 128
// characters (SHA-1 up to SHA-512) within the given string with a truncated
// version of 8 characters.
func TruncateHex(str string) string {
if str == "" {
return ""
}
hits := hexRegexp.FindAllString(str, -1)
for _, v := range hits {
str = strings.Replace(str, v, string([]rune(v)[:8]), -1)
}
return str
}

@ -0,0 +1,104 @@
/*
Copyright 2023 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 utils
import (
"testing"
. "github.com/onsi/gomega"
)
func TestTruncateHex(t *testing.T) {
tests := []struct {
name string
str string
want string
}{
{
name: "SHA1 hash",
str: "16cfcc0b9066b3234dda29927ac1c19860d9663f",
want: "16cfcc0b",
},
{
name: "SHA256 hash",
str: "c2448c95e262b10f9c1137bf1472f51c04dffca76966ff15eff409d0b300c0b0",
want: "c2448c95",
},
{
name: "BLAKE3 hash",
str: "d7b332559f4e57c01dcbe24e53346b4e47696fd2a07f39639a4017a8c3a1d045",
want: "d7b33255",
},
{
name: "SHA512 hash",
str: "dd81564b7e1e1d5986b166c21963d602f47f8610bf2a6ebbfd2f9c1e5ef05ef134f07e587383cbc049325c43e0e6817b5a282a74c0d569a5e057118484989781",
want: "dd81564b",
},
{
name: "part of digest",
str: "sha256:c2448c95e262b10f9c1137bf1472f51c04dffca76966ff15eff409d0b300c0b0",
want: "sha256:c2448c95",
},
{
name: "part of revision with digest",
str: "tag@sha256:c2448c95e262b10f9c1137bf1472f51c04dffca76966ff15eff409d0b300c0b0",
want: "tag@sha256:c2448c95",
},
{
name: "legacy revision with hash",
str: "HEAD/16cfcc0b9066b3234dda29927ac1c19860d9663f",
want: "HEAD/16cfcc0b",
},
{
name: "hex exceeding max length",
str: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
want: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
},
{
name: "hex under min length",
str: "ffffffffffffff",
want: "ffffffffffffff",
},
{
name: "within string",
str: "this is a lengthy string with a hash 16cfcc0b9066b3234dda29927ac1c19860d9663f in it",
want: "this is a lengthy string with a hash 16cfcc0b in it",
},
{
name: "within string (quoted)",
str: "this is a lengthy string with a hash \"c2448c95e262b10f9c1137bf1472f51c04dffca76966ff15eff409d0b300c0b0\" in it",
want: "this is a lengthy string with a hash \"c2448c95\" in it",
},
{
name: "within string (single quoted)",
str: "this is a lengthy string with a hash 'sha256:c2448c95e262b10f9c1137bf1472f51c04dffca76966ff15eff409d0b300c0b0' in it",
want: "this is a lengthy string with a hash 'sha256:c2448c95' in it",
},
{
name: "arbitrary string",
str: "which should not be modified",
want: "which should not be modified",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
g.Expect(TruncateHex(tt.str)).To(Equal(tt.want))
})
}
}
Loading…
Cancel
Save