Skip to content
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
10 changes: 10 additions & 0 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ func Command() *cobra.Command { //nolint:funlen,maintidx
"not-apiversion",
"exclude resources by api version")

// Define --argocd flag.
mf.StringSliceMatcher(matcher.NewArgoCDMatcher,
"argocd",
"include resources by ArgoCD application")

// Define --not-argocd flag.
mf.StringSliceMatcher(matcher.NewArgoCDMatcher,
"not-argocd",
"exclude resources by ArgoCD application")

// Define --cel flag.
mf.StringMatcher(matcher.NewCELMatcher,
"cel",
Expand Down
51 changes: 51 additions & 0 deletions matcher/matcher-argocd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright Josh Komoroske. All rights reserved.
// Use of this source code is governed by the MIT license,
// a copy of which can be found in the LICENSE.txt file.
// SPDX-License-Identifier: MIT

package matcher

import (
"strings"

"github.com/gobwas/glob"

"github.com/joshdk/krf/resources"
)

// NewArgoCDMatcher matches resources.Resource instances based on the ArgoCD
// Application that the resource is being tracked by.
func NewArgoCDMatcher(selector string) (Matcher, error) {
appGlob, err := glob.Compile(selector)
if err != nil {
return nil, err
}

m := argocdMatcher{appGlob: appGlob}

return m, nil
}

type argocdMatcher struct {
appGlob glob.Glob
}

func (m argocdMatcher) Matches(item resources.Resource) bool {
value, found := item.GetAnnotations()["argocd.argoproj.io/tracking-id"]
if !found {
return false
}

// The format of the tracking annotation looks like this:
// <application name>:<resource group>/<resource kind>:<resource namespace>/<resource name>
// We are interested in the application name, so we look for the location
// of the first ':' character and slice the string up to that point.
index := strings.IndexByte(value, ':')
if index == -1 {
return false
}

app := value[:index]

return m.appGlob.Match(app)
}
49 changes: 49 additions & 0 deletions matcher/matcher-argocd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Josh Komoroske. All rights reserved.
// Use of this source code is governed by the MIT license,
// a copy of which can be found in the LICENSE.txt file.
// SPDX-License-Identifier: MIT

package matcher_test

import (
"testing"

"github.com/joshdk/krf/matcher"
)

func TestArgoCDMatcher(t *testing.T) {
t.Parallel()

testMatcher(t, []spec{
{
title: "literal app name",
matcher: must(matcher.NewArgoCDMatcher("app")),
matches: []string{
"Service/my-service",
},
},
{
title: "app name with wildcard suffix",
matcher: must(matcher.NewArgoCDMatcher("app*")),
matches: []string{
"Deployment/nginx-deployment",
"Service/my-service",
},
},
{
title: "wildcard",
matcher: must(matcher.NewArgoCDMatcher("*")),
matches: []string{
"Deployment/nginx-deployment",
"Service/my-service",
},
},
{
title: "wildcard with suffix",
matcher: must(matcher.NewArgoCDMatcher("*-dev")),
matches: []string{
"Deployment/nginx-deployment",
},
},
})
}
2 changes: 2 additions & 0 deletions matcher/testdata/resources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ metadata:
component: api
name: my-service
namespace: custom-app
annotations:
argocd.argoproj.io/tracking-id: app:/Service:custom-app/my-service
spec:
ports:
- port: 80
Expand Down
2 changes: 2 additions & 0 deletions matcher/testdata/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ metadata:
labels:
app: myapp
component: api
annotations:
argocd.argoproj.io/tracking-id: app:/Service:custom-app/my-service
spec:
selector:
app.kubernetes.io/name: myapp
Expand Down
1 change: 1 addition & 0 deletions matcher/testdata/subdir/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ metadata:
component: proxy
annotations:
kubernetes.io/description: proxy
argocd.argoproj.io/tracking-id: app-dev:apps/Deployment:default/nginx-deployment
spec:
replicas: 3
selector:
Expand Down