Skip to content
Merged
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
2 changes: 2 additions & 0 deletions pkg/app/piped/cloudprovider/kubernetes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
srcs = [
"cache.go",
"deployment.go",
"diff.go",
"hasher.go",
"helm.go",
"kubectl.go",
Expand Down Expand Up @@ -43,6 +44,7 @@ go_test(
size = "small",
srcs = [
"deployment_test.go",
"diff_test.go",
"hasher_test.go",
"helm_test.go",
"kubernetes_test.go",
Expand Down
155 changes: 155 additions & 0 deletions pkg/app/piped/cloudprovider/kubernetes/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright 2021 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kubernetes

import (
"fmt"
"sort"
"strings"

"github.com/pipe-cd/pipe/pkg/diff"
)

type DiffListResult struct {
Adds []Manifest
Deletes []Manifest
Changes []DiffListChange
}

func (r *DiffListResult) NoChange() bool {
return len(r.Adds)+len(r.Deletes)+len(r.Changes) == 0
}

type DiffListChange struct {
Old Manifest
New Manifest
Diff *diff.Result
}

func Diff(old, new Manifest, opts ...diff.Option) (*diff.Result, error) {
return diff.DiffUnstructureds(*old.u, *new.u, opts...)
}

func DiffList(olds, news []Manifest, opts ...diff.Option) (*DiffListResult, error) {
adds, deletes, newChanges, oldChanges := groupManifests(olds, news)
cr := &DiffListResult{
Adds: adds,
Deletes: deletes,
Changes: make([]DiffListChange, 0, len(newChanges)),
}

for i := 0; i < len(newChanges); i++ {
result, err := Diff(oldChanges[i], newChanges[i], opts...)
if err != nil {
return nil, err
}
if !result.HasDiff() {
continue
}
cr.Changes = append(cr.Changes, DiffListChange{
Old: oldChanges[i],
New: newChanges[i],
Diff: result,
})
}

return cr, nil
}

func (r *DiffListResult) DiffString() string {
var b strings.Builder
index := 0
for _, delete := range r.Deletes {
index++
b.WriteString(fmt.Sprintf("- %d. %s\n\n", index, delete.Key.ReadableString()))
}
for _, add := range r.Adds {
index++
b.WriteString(fmt.Sprintf("+ %d. %s\n\n", index, add.Key.ReadableString()))
}

const maxPrintDiffs = 3
var prints = 0
for _, change := range r.Changes {
key := change.Old.Key
opts := []diff.RenderOption{
diff.WithLeftPadding(1),
}
switch {
case key.IsSecret():
opts = append(opts, diff.WithMaskPath("data"))
case key.IsConfigMap():
opts = append(opts, diff.WithMaskPath("data"))
}
renderer := diff.NewRenderer(opts...)

index++
b.WriteString(fmt.Sprintf("* %d. %s\n\n", index, key.ReadableString()))
b.WriteString(renderer.Render(change.Diff.Nodes()))
b.WriteString("\n")

prints++
if prints >= maxPrintDiffs {
break
}
}

if prints < len(r.Changes) {
b.WriteString(fmt.Sprintf("... (omitted %d other changed manifests\n", len(r.Changes)-prints))
}

return b.String()
}

func groupManifests(olds, news []Manifest) (adds, deletes, newChanges, oldChanges []Manifest) {
// Sort the manifests before comparing.
sort.Slice(news, func(i, j int) bool {
return news[i].Key.IsLessWithIgnoringNamespace(news[j].Key)
})
sort.Slice(olds, func(i, j int) bool {
return olds[i].Key.IsLessWithIgnoringNamespace(olds[j].Key)
})

var n, o int
for {
if n >= len(news) || o >= len(olds) {
break
}
if news[n].Key.IsEqualWithIgnoringNamespace(olds[o].Key) {
newChanges = append(newChanges, news[n])
oldChanges = append(oldChanges, olds[o])
n++
o++
continue
}
// Has in new but not in old so this should be a deleted one.
if news[n].Key.IsLessWithIgnoringNamespace(olds[o].Key) {
deletes = append(deletes, news[n])
n++
continue
}
// Has in old but not in new so this should be an added one.
adds = append(adds, olds[o])
o++
}

if len(news) > n {
deletes = append(deletes, news[n:]...)
}
if len(olds) > o {
adds = append(adds, olds[o:]...)
}
return
}
115 changes: 115 additions & 0 deletions pkg/app/piped/cloudprovider/kubernetes/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2021 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kubernetes

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGroupManifests(t *testing.T) {
testcases := []struct {
name string
news []Manifest
olds []Manifest
expectedAdds []Manifest
expectedDeletes []Manifest
expectedNewChanges []Manifest
expectedOldChanges []Manifest
}{
{
name: "empty list",
},
{
name: "only adds",
olds: []Manifest{
{Key: ResourceKey{Name: "b"}},
{Key: ResourceKey{Name: "a"}},
},
expectedAdds: []Manifest{
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "b"}},
},
},
{
name: "only deletes",
news: []Manifest{
{Key: ResourceKey{Name: "b"}},
{Key: ResourceKey{Name: "a"}},
},
expectedDeletes: []Manifest{
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "b"}},
},
},
{
name: "only inters",
news: []Manifest{
{Key: ResourceKey{Name: "b"}},
{Key: ResourceKey{Name: "a"}},
},
olds: []Manifest{
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "b"}},
},
expectedNewChanges: []Manifest{
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "b"}},
},
expectedOldChanges: []Manifest{
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "b"}},
},
},
{
name: "all kinds",
news: []Manifest{
{Key: ResourceKey{Name: "b"}},
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "c"}},
},
olds: []Manifest{
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "d"}},
{Key: ResourceKey{Name: "b"}},
},
expectedAdds: []Manifest{
{Key: ResourceKey{Name: "d"}},
},
expectedDeletes: []Manifest{
{Key: ResourceKey{Name: "c"}},
},
expectedNewChanges: []Manifest{
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "b"}},
},
expectedOldChanges: []Manifest{
{Key: ResourceKey{Name: "a"}},
{Key: ResourceKey{Name: "b"}},
},
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
adds, deletes, newChanges, oldChanges := groupManifests(tc.olds, tc.news)
assert.Equal(t, tc.expectedAdds, adds)
assert.Equal(t, tc.expectedDeletes, deletes)
assert.Equal(t, tc.expectedNewChanges, newChanges)
assert.Equal(t, tc.expectedOldChanges, oldChanges)
})
}
}
5 changes: 0 additions & 5 deletions pkg/app/piped/cloudprovider/kubernetes/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"

"github.com/pipe-cd/pipe/pkg/diff"
"github.com/pipe-cd/pipe/pkg/model"
)

Expand Down Expand Up @@ -151,10 +150,6 @@ func (m Manifest) ConvertToStructuredObject(o interface{}) error {
return json.Unmarshal(data, o)
}

func Diff(first, second Manifest, opts ...diff.Option) (*diff.Result, error) {
return diff.DiffUnstructureds(*first.u, *second.u, opts...)
}

func ParseFromStructuredObject(s interface{}) (Manifest, error) {
data, err := json.Marshal(s)
if err != nil {
Expand Down
13 changes: 1 addition & 12 deletions pkg/app/piped/driftdetector/kubernetes/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
Expand All @@ -17,14 +17,3 @@ go_library(
"@org_uber_go_zap//:go_default_library",
],
)

go_test(
name = "go_default_test",
size = "small",
srcs = ["detector_test.go"],
embed = [":go_default_library"],
deps = [
"//pkg/app/piped/cloudprovider/kubernetes:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
Loading