Skip to content

Commit

Permalink
Merge pull request #3 from jaimeph/fix/slice-bounds-out-of-range
Browse files Browse the repository at this point in the history
fix: removed deleteItemByIndex
  • Loading branch information
achetronic authored Nov 28, 2024
2 parents 4024689 + 7429cf2 commit 8b7e8e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
23 changes: 10 additions & 13 deletions internal/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,48 +88,45 @@ func (p *Processor) SyncResources() (err error) {
continue
}

filteredResourceList := make([]unstructured.Unstructured, 0)
// Preprocess the targets list to clean the items not matching the user-desired criteria
for rawResourceIndex, rawResourceObject := range resourceList.Items {
for _, rawResourceObject := range resourceList.Items {

// Matching namespace by regex and resource does NOT meet? Skip
if configResource.Target.Namespace.MatchRegex != "" &&
!compiledRegexNamespace.MatchString(rawResourceObject.GetNamespace()) {

deleteItemByIndex(&resourceList.Items, rawResourceIndex)
continue
}

// Matching name by exact string and resource does NOT meet? Skip
if configResource.Target.Name.MatchExact != "" &&
rawResourceObject.GetName() != configResource.Target.Name.MatchExact {

deleteItemByIndex(&resourceList.Items, rawResourceIndex)
continue
}

// Matching name by regex and resource does NOT meet? Skip
if configResource.Target.Name.MatchRegex != "" &&
!compiledRegex.MatchString(rawResourceObject.GetName()) {

deleteItemByIndex(&resourceList.Items, rawResourceIndex)
continue
}

filteredResourceList = append(filteredResourceList, rawResourceObject)
}

templateInjectedObject := &map[string]interface{}{} // TODO, review potential nil pointer dereference

// Perform global user-defined actions when 'preStep' is set in the config
// This is useful to group resources, pre-filter some of them, etc, before evaluating one by one
if configResource.PreStep != "" {
err = p.processPrestep(configResource.PreStep, templateInjectedObject, resourceList.Items)
err = p.processPrestep(configResource.PreStep, templateInjectedObject, filteredResourceList)
if err != nil {
globals.ExecContext.Logger.Infof("error processing prestep: %s", err)
continue
}
}

// Perform the actions over the resources
for _, resource := range resourceList.Items {
for _, resource := range filteredResourceList {

// Process this object. Delete in case of success
objectDeleted, err := p.processObject(gvr, resource, templateInjectedObject, configResource.Conditions)
Expand All @@ -139,12 +136,12 @@ func (p *Processor) SyncResources() (err error) {
}

if !objectDeleted {
globals.ExecContext.Logger.Infof("resource '%s' in namespace '%s' did NOT meet the conditions",
globals.ExecContext.Logger.Debugf("resource '%s' in namespace '%s' did NOT meet the conditions",
resource.GetName(), resource.GetNamespace())
continue
}

globals.ExecContext.Logger.Infof("resource '%s' in namespace '%s' was deleted successfully", resource.GetName(), resource.GetNamespace())
globals.ExecContext.Logger.Infof("resource '%s'/'%s' in namespace '%s' was deleted successfully", resource.GetKind(), resource.GetName(), resource.GetNamespace())
}
}

Expand Down Expand Up @@ -206,8 +203,8 @@ func (p *Processor) processObject(gvr schema.GroupVersionResource, object unstru
}

if globals.ExecContext.DryRun {
globals.ExecContext.Logger.Infof("dry-run enabled. Skipping deletion of object: '%s'/'%s'",
object.GetNamespace(), object.GetName())
globals.ExecContext.Logger.Infof("dry-run enabled. Skipping deletion of object: '%s'/'%s'/'%s'",
object.GetNamespace(), object.GetKind(), object.GetName())
return false, nil
}

Expand Down
9 changes: 0 additions & 9 deletions internal/processor/utils.go

This file was deleted.

0 comments on commit 8b7e8e4

Please sign in to comment.