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

feat(filter): handle 'tag only' fields #53

Merged
merged 2 commits into from
May 18, 2024
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
16 changes: 12 additions & 4 deletions krm/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ const (
)

const (
KeyType = "type"
KeyTag = "tag"
KeyType = "type"
KeyTag = "tag"
KeyPart = "part"
KeyContext = "context"
)

type Options struct {
Type string
Tag string
Type string
Tag string
Part string
Context string
}

func ParseOpts(expr string) (Options, error) {
Expand All @@ -37,6 +41,10 @@ func ParseOpts(expr string) (Options, error) {
opts.Type = strings.TrimSpace(v)
case KeyTag:
opts.Tag = strings.TrimSpace(v)
case KeyPart:
opts.Part = strings.TrimSpace(v)
case KeyContext:
opts.Context = strings.TrimSpace(v)
default:
return opts, fmt.Errorf("unknown key: %s", v)
}
Expand Down
9 changes: 8 additions & 1 deletion krm/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func DefaultNodeHandler(_, currentRef, nextRef string, opts Options) (string, er
return currentRef, nil
}

oldRef, err := name.ParseReference(currentRef)
fullRef := currentRef
if opts.Part == "tag" {
fullRef = fmt.Sprintf("%s:%s", opts.Context, currentRef)
}
oldRef, err := name.ParseReference(fullRef)
if err != nil {
return currentRef, err
}
Expand All @@ -40,6 +44,9 @@ func DefaultNodeHandler(_, currentRef, nextRef string, opts Options) (string, er
return currentRef, err
}

if opts.Part == "tag" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how to handle the tag in this situation.

One way could be to have the digest as a seperate part to select from. Then we also need a way to combine parts into a single output field.

Or everything after the image is considered the tag or identifier how the container package calls it.

On top of that, the built in decoder actuall generate <tag>@<digest> identifiers. This is technically not correct. Registiries will ignore the tag but its nice for informational purposes.

return newRef.Identifier(), nil
}
return nextRef, nil
}

Expand Down
22 changes: 22 additions & 0 deletions krm/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,28 @@ func Test_renderer_Render(t *testing.T) {
},
},
},
{
name: "argocd",
giveDir: "argocd",
giveEvents: []string{
"docker.io/foo/baz:1.0.1@sha256:220611111e8c9bbe242e9dc1367c0fa89eef83f26203ee3f7c3764046e02b248",
"docker.io/foo/bar:master-124-012345@sha256:220611111e8c9bbe242e9dc1367c0fa89eef83f26203ee3f7c3764046e02b248",
},
wantSourceFieldValue: map[string][]wantFieldValue{
"application.yaml": {
{
rnodeIndex: 0,
field: "spec.source.helm.valuesObject.image.tag",
value: "1.0.1",
},
{
rnodeIndex: 1,
field: "spec.source.helm.valuesObject.image.tag",
value: "master-124-012345",
},
},
},
},
}
for _, tt := range tests {
tt := tt
Expand Down
38 changes: 38 additions & 0 deletions krm/testdata/argocd/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: testcase1
spec:
project: testcase
destination:
server: "https://kubernetes.default.svc"
namespace: testcase
source:
chart: testcase
repoURL: https://example.com/testcase
targetRevision: 1.0.0
helm:
releaseName: testcase
valuesObject:
image:
tag: "1.0" # kobold: tag: ^1; type: semver; part: tag; context: docker.io/foo/baz
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: testcase2
spec:
project: testcase
destination:
server: "https://kubernetes.default.svc"
namespace: testcase
source:
chart: testcase
repoURL: https://example.com/testcase
targetRevision: 1.0.0
helm:
releaseName: testcase
valuesObject:
image:
tag: "master-123-abcdef0" # kobold: tag: master-(\d+)-.*; type: regex; part: tag; context: docker.io/foo/bar
Loading