Skip to content

Commit

Permalink
Merge pull request #227 from fluxcd/ssa-diff-exclude
Browse files Browse the repository at this point in the history
ssa: Add exclusion list to diff options
  • Loading branch information
stefanprodan authored Jan 20, 2022
2 parents 6083da5 + a968113 commit 65e0709
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
27 changes: 26 additions & 1 deletion ssa/manager_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,37 @@ const (
diffMask = "******"
)

// DiffOptions contains options for server-side dry-run apply requests.
type DiffOptions struct {
// Exclusions determines which in-cluster objects are skipped from dry-run apply
// based on the specified key-value pairs.
// A nil Exclusions map means all objects are applied
// regardless of their metadata labels and annotations.
Exclusions map[string]string `json:"exclusions"`
}

// DefaultDiffOptions returns the default dry-run apply options.
func DefaultDiffOptions() DiffOptions {
return DiffOptions{
Exclusions: nil,
}
}

// Diff performs a server-side apply dry-un and returns the live and merged objects if drift is detected.
// If the diff contains Kubernetes Secrets, the data values are masked.
func (m *ResourceManager) Diff(ctx context.Context, object *unstructured.Unstructured) (*ChangeSetEntry, *unstructured.Unstructured, *unstructured.Unstructured, error) {
func (m *ResourceManager) Diff(ctx context.Context, object *unstructured.Unstructured, opts DiffOptions) (
*ChangeSetEntry,
*unstructured.Unstructured,
*unstructured.Unstructured,
error,
) {
existingObject := object.DeepCopy()
_ = m.client.Get(ctx, client.ObjectKeyFromObject(object), existingObject)

if existingObject != nil && AnyInMetadata(existingObject, opts.Exclusions) {
return m.changeSetEntry(existingObject, UnchangedAction), nil, nil, nil
}

dryRunObject := object.DeepCopy()
if err := m.dryRunApply(ctx, dryRunObject); err != nil {
return nil, nil, nil, m.validationError(dryRunObject, err)
Expand Down
20 changes: 10 additions & 10 deletions ssa/manager_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestDiff(t *testing.T) {
}

t.Run("generates empty diff for unchanged object", func(t *testing.T) {
changeSetEntry, _, _, err := manager.Diff(ctx, configMap)
changeSetEntry, _, _, err := manager.Diff(ctx, configMap, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand All @@ -71,7 +71,7 @@ func TestDiff(t *testing.T) {
t.Fatal(err)
}

changeSetEntry, _, mergedObj, err := manager.Diff(ctx, configMap)
changeSetEntry, _, mergedObj, err := manager.Diff(ctx, configMap, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestDiff(t *testing.T) {
t.Fatal(err)
}

changeSetEntry, _, mergedObj, err := manager.Diff(ctx, secret)
changeSetEntry, _, mergedObj, err := manager.Diff(ctx, secret, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestDiff_Removals(t *testing.T) {
}

t.Run("generates empty diff for unchanged object", func(t *testing.T) {
changeSetEntry, _, _, err := manager.Diff(ctx, configMap)
changeSetEntry, _, _, err := manager.Diff(ctx, configMap, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand All @@ -164,7 +164,7 @@ func TestDiff_Removals(t *testing.T) {
t.Fatal(err)
}

changeSetEntry, _, mergedObj, err := manager.Diff(ctx, configMap)
changeSetEntry, _, mergedObj, err := manager.Diff(ctx, configMap, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand All @@ -187,7 +187,7 @@ func TestDiff_Removals(t *testing.T) {
t.Run("generates diff for removed map entry", func(t *testing.T) {
unstructured.RemoveNestedField(configMap.Object, "data", "token")

changeSetEntry, _, _, err := manager.Diff(ctx, configMap)
changeSetEntry, _, _, err := manager.Diff(ctx, configMap, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestDiffHPA(t *testing.T) {
}

t.Run("generates empty diff for unchanged object", func(t *testing.T) {
changeSetEntry, _, _, err := manager.Diff(ctx, hpa)
changeSetEntry, _, _, err := manager.Diff(ctx, hpa, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand All @@ -247,7 +247,7 @@ func TestDiffHPA(t *testing.T) {
t.Fatal(err)
}

changeSetEntry, _, _, err := manager.Diff(ctx, hpa)
changeSetEntry, _, _, err := manager.Diff(ctx, hpa, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand All @@ -262,7 +262,7 @@ func TestDiffHPA(t *testing.T) {
})

t.Run("generates empty diff for unchanged metric", func(t *testing.T) {
changeSetEntry, _, _, err := manager.Diff(ctx, hpa)
changeSetEntry, _, _, err := manager.Diff(ctx, hpa, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand All @@ -278,7 +278,7 @@ func TestDiffHPA(t *testing.T) {
t.Fatal(err)
}

changeSetEntry, _, _, err := manager.Diff(ctx, hpa)
changeSetEntry, _, _, err := manager.Diff(ctx, hpa, DefaultDiffOptions())
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 65e0709

Please sign in to comment.