Skip to content

Commit

Permalink
Merge pull request #456 from fluxcd/fix-ignore
Browse files Browse the repository at this point in the history
ssa: Ignore objects even if they exist in cluster
  • Loading branch information
stefanprodan authored Jan 26, 2023
2 parents 524af9e + 3fcf57c commit 0cd2cf5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 8 additions & 6 deletions ssa/manager_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (m *ResourceManager) Apply(ctx context.Context, object *unstructured.Unstru
existingObject := object.DeepCopy()
_ = m.client.Get(ctx, client.ObjectKeyFromObject(object), existingObject)

if m.shouldSkipApply(existingObject, opts) {
if m.shouldSkipApply(object, existingObject, opts) {
return m.changeSetEntry(object, UnchangedAction), nil
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func (m *ResourceManager) ApplyAll(ctx context.Context, objects []*unstructured.
existingObject := object.DeepCopy()
_ = m.client.Get(ctx, client.ObjectKeyFromObject(object), existingObject)

if m.shouldSkipApply(existingObject, opts) {
if m.shouldSkipApply(object, existingObject, opts) {
changeSet.Add(*m.changeSetEntry(existingObject, UnchangedAction))
continue
}
Expand Down Expand Up @@ -286,11 +286,11 @@ func (m *ResourceManager) cleanupMetadata(ctx context.Context,
// An object is recreated if the apply error was due to immutable field changes and if the object
// contains a label or annotation which matches the ApplyOptions.ForceSelector.
func (m *ResourceManager) shouldForceApply(desiredObject *unstructured.Unstructured,
object *unstructured.Unstructured, opts ApplyOptions, err error) bool {
existingObject *unstructured.Unstructured, opts ApplyOptions, err error) bool {
if IsImmutableError(err) {
if opts.Force ||
AnyInMetadata(desiredObject, opts.ForceSelector) ||
(object != nil && AnyInMetadata(object, opts.ForceSelector)) {
(existingObject != nil && AnyInMetadata(existingObject, opts.ForceSelector)) {
return true
}
}
Expand All @@ -300,6 +300,8 @@ func (m *ResourceManager) shouldForceApply(desiredObject *unstructured.Unstructu

// shouldSkipApply determines based on the object metadata and ApplyOptions if the object should be skipped.
// An object is not applied if it contains a label or annotation which matches the ApplyOptions.ExclusionSelector.
func (m *ResourceManager) shouldSkipApply(object *unstructured.Unstructured, opts ApplyOptions) bool {
return object != nil && AnyInMetadata(object, opts.ExclusionSelector)
func (m *ResourceManager) shouldSkipApply(desiredObject *unstructured.Unstructured,
existingObject *unstructured.Unstructured, opts ApplyOptions) bool {
return AnyInMetadata(desiredObject, opts.ExclusionSelector) ||
(existingObject != nil && AnyInMetadata(existingObject, opts.ExclusionSelector))
}
18 changes: 18 additions & 0 deletions ssa/manager_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,24 @@ func TestApply_Exclusions(t *testing.T) {
}
}
})

t.Run("skips apply when desired state is annotated", func(t *testing.T) {
configMapClone := configMap.DeepCopy()
meta := map[string]string{
"fluxcd.io/ignore": "true",
}
configMapClone.SetAnnotations(meta)

// apply changes without exclusions
changeSet, err := manager.Apply(ctx, configMapClone, DefaultApplyOptions())
if err != nil {
t.Fatal(err)
}

if changeSet.Action != string(UnchangedAction) {
t.Errorf("Diff found for %s", changeSet.String())
}
})
}

func TestApply_Cleanup(t *testing.T) {
Expand Down

0 comments on commit 0cd2cf5

Please sign in to comment.