Skip to content

Commit

Permalink
feat: skip the resources in import process
Browse files Browse the repository at this point in the history
  • Loading branch information
asingh51 committed Feb 3, 2025
1 parent 0975eac commit d3dc331
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
30 changes: 14 additions & 16 deletions cmd/argocd/commands/admin/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func NewImportCommand() *cobra.Command {
ignoreTracking bool
overrideOnConflict bool
promptsEnabled bool
skipResourcesWithLabels []string
skipResourcesWithLabel string
applicationNamespaces []string
applicationsetNamespaces []string
)
Expand Down Expand Up @@ -189,7 +189,7 @@ func NewImportCommand() *cobra.Command {
applicationsetNamespaces = additionalNamespaces.applicationsetNamespaces
}

// pruneObjects tracks live objects and it's current resource version. any remaining
// pruneObjects tracks live objects, and it's current resource version. any remaining
// items in this map indicates the resource should be pruned since it no longer appears
// in the backup

Expand Down Expand Up @@ -246,7 +246,7 @@ func NewImportCommand() *cobra.Command {

errors.CheckError(err)
for _, bakObj := range backupObjects {
if isSkipLabelMatches(bakObj, skipResourcesWithLabels) {
if isSkipLabelMatches(bakObj, skipResourcesWithLabel) {
fmt.Printf("Skipping %s/%s %s in namespace %s\n", bakObj.GroupVersionKind().Group, bakObj.GroupVersionKind().Kind, bakObj.GetName(), bakObj.GetNamespace())
continue
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func NewImportCommand() *cobra.Command {
command.Flags().BoolVar(&overrideOnConflict, "override-on-conflict", false, "Override the resource on conflict when updating resources")
command.Flags().BoolVar(&verbose, "verbose", false, "Verbose output (versus only changed output)")
command.Flags().BoolVar(&stopOperation, "stop-operation", false, "Stop any existing operations")
command.Flags().StringSliceVarP(&skipResourcesWithLabels, "skip-resources-with-labels", "", []string{}, "Skip importing resources based on the labels e.g. '--skip-resources-with-labels my-label/example.io=true --skip-resources-with-labels my-other-label/example.io=true'")
command.Flags().StringVarP(&skipResourcesWithLabel, "skip-resources-with-label", "", "", "Skip importing resources based on the label e.g. '--skip-resources-with-label my-label/example.io=true'")
command.Flags().StringSliceVarP(&applicationNamespaces, "application-namespaces", "", []string{}, fmt.Sprintf("Comma separated list of namespace globs to which import of applications is allowed. If not provided value from '%s' in %s will be used,if it's not defined only applications without an explicit namespace will be imported to the Argo CD namespace", applicationNamespacesCmdParamsKey, common.ArgoCDCmdParamsConfigMapName))
command.Flags().StringSliceVarP(&applicationsetNamespaces, "applicationset-namespaces", "", []string{}, fmt.Sprintf("Comma separated list of namespace globs which import of applicationsets is allowed. If not provided value from '%s' in %s will be used,if it's not defined only applicationsets without an explicit namespace will be imported to the Argo CD namespace", applicationsetNamespacesCmdParamsKey, common.ArgoCDCmdParamsConfigMapName))
command.PersistentFlags().BoolVar(&promptsEnabled, "prompts-enabled", localconfig.GetPromptsEnabled(true), "Force optional interactive prompts to be enabled or disabled, overriding local configuration. If not specified, the local configuration value will be used, which is false by default.")
Expand Down Expand Up @@ -502,20 +502,18 @@ func updateTracking(bak, live *unstructured.Unstructured) {
}
}

// skip resource if any of the specified label exists.
func isSkipLabelMatches(bak *unstructured.Unstructured, skipResourcesWithLabels []string) bool {
if len(skipResourcesWithLabels) == 0 {
// isSkipLabelMatches return if the resource should be skipped based on the labels
func isSkipLabelMatches(bak *unstructured.Unstructured, skipResourcesWithLabel string) bool {
if skipResourcesWithLabel == "" {
return false
}
for _, kv := range skipResourcesWithLabels {
parts := strings.SplitN(kv, "=", 2)
if len(parts) != 2 {
continue
}
key, value := parts[0], parts[1]
if val, ok := bak.GetLabels()[key]; ok && val == value {
return true
}
parts := strings.SplitN(skipResourcesWithLabel, "=", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return false
}
key, value := parts[0], parts[1]
if val, ok := bak.GetLabels()[key]; ok && val == value {
return true
}
return false
}
31 changes: 7 additions & 24 deletions cmd/argocd/commands/admin/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ func Test_updateTracking(t *testing.T) {
})
}
}

Check failure on line 87 in cmd/argocd/commands/admin/backup_test.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not properly formatted (gofumpt)

func TestIsLabelMatches(t *testing.T) {
func TestIsSkipLabelMatches(t *testing.T) {
tests := []struct {
name string
obj *unstructured.Unstructured
skipLabels []string
skipLabels string
expected bool
}{
{
Expand All @@ -104,7 +103,7 @@ func TestIsLabelMatches(t *testing.T) {
},
},
},
skipLabels: []string{"test-label=value"},
skipLabels: "test-label=value",
expected: true,
},
{
Expand All @@ -118,7 +117,7 @@ func TestIsLabelMatches(t *testing.T) {
},
},
},
skipLabels: []string{"test-label=value"},
skipLabels: "test-label=value",
expected: false,
},
{
Expand All @@ -132,7 +131,7 @@ func TestIsLabelMatches(t *testing.T) {
},
},
},
skipLabels: []string{},
skipLabels: "",
expected: false,
},
{
Expand All @@ -147,30 +146,14 @@ func TestIsLabelMatches(t *testing.T) {
},
},
},
skipLabels: []string{"test-label"},
skipLabels: "test-label",
expected: false,
},
{
name: "Multiple labels, one matches",
obj: &unstructured.Unstructured{
Object: map[string]any{
"metadata": map[string]any{
"labels": map[string]any{
"test-label": "value",
"another-label": "value2",
},
},
},
},
skipLabels: []string{"another-label=value2"},
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isSkipLabelMatches(tt.obj, tt.skipLabels)
assert.Equal(t, tt.expected, result)
})
}
}
}

Check failure on line 159 in cmd/argocd/commands/admin/backup_test.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not properly formatted (gofumpt)
3 changes: 2 additions & 1 deletion docs/user-guide/commands/argocd_admin_import.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d3dc331

Please sign in to comment.