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

Adding ignoreSubpaths possibility #432

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion cmd/argocd-lovely-plugin/packageDirectories.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"github.com/crumbhole/argocd-lovely-plugin/pkg/features"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/crumbhole/argocd-lovely-plugin/pkg/features"
)

// PackageDirectories is an array of sub-application paths
Expand All @@ -17,6 +19,23 @@ func (d *PackageDirectories) checkFile(path string, info os.DirEntry, err error)
if err != nil {
return err
}

// Check if path matches any ignored subpaths
ignoreSubpaths, err := features.GetIgnoreSubpaths()
if err != nil {
return err
}
for _, subpath := range ignoreSubpaths {
// Remove any leading "../" from the subpath
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a surprising thing, why are we removing some part of the path that the user has supplied?

Copy link
Author

Choose a reason for hiding this comment

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

I thought users would give a path with a potential leading "../", (which would never match), but later I figured it's unlikely.
I'll update the docs that the given paths should be relative to the application base directory.

for strings.HasPrefix(subpath, "../") {
subpath = subpath[len("../"):]
}

if strings.Contains(path, filepath.Clean(subpath)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like a solid match here. We're only matching directories, so I'd check for IsDir() and matching, and then return SkipDir instead.

If I have

/bar
/foo/bar

And I ignore /bar I'd like it only to ignore the first path, not the second.

Copy link
Author

@valibali valibali Feb 26, 2024

Choose a reason for hiding this comment

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

Gosh, that was lame. You're absolutely right. I'm doing the update.

return nil // Skip processing if the path matches any ignored subpath
}
}

if !info.IsDir() {
yamlRegexp := regexp.MustCompile(features.GetDetectionRegex())
dir := filepath.Dir(path)
Expand Down
1 change: 1 addition & 0 deletions doc/parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Environment variables, are conventionally ALL_CAPS, and for our purposes
| Helmfile Merge | LOVELY_HELMFILE_MERGE | Set to some yaml you'd like [strategic merged](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patchesstrategicmerge/) into any helmfile.yaml used by helmfile. | |
| Helmfile Patch | LOVELY_HELMFILE_PATCH | to some yaml or json you'd like [json6902](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patchesjson6902/) patched into any helmfile.yaml used by Helmfile. | |
| Helmfile Template Parameters | LOVELY_HELMFILE_TEMPLATE_PARAMS | Space separated extra parameters to `Helmfile template` as you might use on the command line. You're on your own here if you pass rubbish parameters. | |
| Ignore Subpaths | LOVELY_IGNORE_SUBPATHS | This can come useful when some sub-application is not to be processed by the plugin. This is a space separated list of paths to ignore. | |
## Plugin Name

You can set `PLUGIN_NAME` in the environment of the sidecar to override the default name of the plugin. This allows you to supply multiple pre-configured plugins (with different environment, but the same variation).
8 changes: 7 additions & 1 deletion pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ const (
HelmfileMerge
HelmfilePatch
HelmfileTemplateParams
IgnoreSubpaths
FirstFeature = Plugins
LastFeature = HelmfileTemplateParams
LastFeature = IgnoreSubpaths
)

// Feature is an individual configurable element of the plugin
Expand Down Expand Up @@ -172,5 +173,10 @@ func Features() map[FeatureID]Feature {
Name: `lovely_helmfile_template_params`,
Description: "Space separated extra parameters to `Helmfile template` as you might use on the command line. You're on your own here if you pass rubbish parameters.",
},
IgnoreSubpaths: {
Title: `Ignore Subpaths`,
Name: `lovely_ignore_subpaths`,
Description: "This can come useful when some sub-application is not to be processed by the plugin. This is a space separated list of paths to ignore.",
},
}
}
10 changes: 9 additions & 1 deletion pkg/features/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package features
// The control of this is via environment variables, as that
// is the way argocd allows you to control plugins
import (
"github.com/crumbhole/argocd-lovely-plugin/pkg/config"
"os"

"github.com/crumbhole/argocd-lovely-plugin/pkg/config"
)

// GetPlugins returns the list of plugins to run during the generate phase after main processing
Expand Down Expand Up @@ -165,3 +166,10 @@ func GetHelmfileTemplateParams() ([]string, error) {
f := Features()[HelmfileTemplateParams]
return config.GetStringListParam(f.EnvName(), f.DefaultVal, ' ')
}

// GetIgnoreSubpaths returns paths of subapplications to ignore
// Set LOVELY_IGNORE_SUBPATHS to paths of subapplications to ignore
func GetIgnoreSubpaths() ([]string, error) {
f := Features()[IgnoreSubpaths]
return config.GetStringListParam(f.EnvName(), f.DefaultVal, ' ')
}
Loading