Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to ignore manifests' fields from kubernetes' drift detection #4249

Merged
merged 4 commits into from
Mar 7, 2023
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
24 changes: 24 additions & 0 deletions pkg/app/piped/driftdetector/kubernetes/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,24 @@ func (d *detector) checkApplication(ctx context.Context, app *model.Application,
liveManifests = filterIgnoringManifests(liveManifests)
d.logger.Debug(fmt.Sprintf("application %s has %d live manifests", app.Id, len(liveManifests)))

ddCfg, err := d.getDriftDetectionConfig(repo.GetPath(), app)
if err != nil {
return err
}

ignoreFields := make([]string, 0)
if ddCfg != nil {
ignoreFields = ddCfg.IgnoreFields
}

result, err := provider.DiffList(
headManifests,
liveManifests,
d.logger,
diff.WithEquateEmpty(),
diff.WithIgnoreAddingMapKeys(),
diff.WithCompareNumberAndNumericString(),
diff.WithIgnoredPaths(ignoreFields),
)
if err != nil {
return err
Expand Down Expand Up @@ -370,3 +381,16 @@ func makeSyncState(r *provider.DiffListResult, commit string) model.ApplicationS
Timestamp: time.Now().Unix(),
}
}

func (d *detector) getDriftDetectionConfig(repoDir string, app *model.Application) (*config.DriftDetection, error) {
cfg, err := d.loadApplicationConfiguration(repoDir, app)
if err != nil {
return nil, fmt.Errorf("failed to load application configuration: %w", err)
}
gds, ok := cfg.GetGenericApplication()
if !ok {
return nil, fmt.Errorf("unsupport application kind %s", cfg.Kind)
}

return gds.DriftDetection, nil
}
6 changes: 6 additions & 0 deletions pkg/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type GenericApplicationSpec struct {
DeploymentNotification *DeploymentNotification `json:"notification"`
// List of the configuration for event watcher.
EventWatcher []EventWatcherConfig `json:"eventWatcher"`
// Configuration for drift detection
DriftDetection *DriftDetection `json:"driftDetection"`
}

type DeploymentPlanner struct {
Expand Down Expand Up @@ -631,6 +633,10 @@ func (c *DeploymentChainTriggerCondition) Validate() error {
return nil
}

type DriftDetection struct {
IgnoreFields []string `json:"ignoreFields"`
}

func LoadApplication(repoPath, configRelPath string, appKind model.ApplicationKind) (*GenericApplicationSpec, error) {
var absPath = filepath.Join(repoPath, configRelPath)

Expand Down
118 changes: 107 additions & 11 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"reflect"
"strconv"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
Expand All @@ -26,6 +27,7 @@ type differ struct {
ignoreAddingMapKeys bool
equateEmpty bool
compareNumberAndNumericString bool
ignoredPaths []string

result *Result
}
Expand Down Expand Up @@ -56,6 +58,13 @@ func WithCompareNumberAndNumericString() Option {
}
}

// WithIgnoredPaths configures ignored fields.
func WithIgnoredPaths(paths []string) Option {
return func(d *differ) {
d.ignoredPaths = paths
}
}

// DiffUnstructureds calculates the diff between two unstructured objects.
func DiffUnstructureds(x, y unstructured.Unstructured, opts ...Option) (*Result, error) {
var (
Expand All @@ -82,7 +91,7 @@ func (d *differ) diff(path []PathStep, vx, vy reflect.Value) error {
return nil
}

d.result.addNode(path, nil, vy.Type(), vx, vy)
d.addNode(path, nil, vy.Type(), vx, vy)
return nil
}

Expand All @@ -91,7 +100,7 @@ func (d *differ) diff(path []PathStep, vx, vy reflect.Value) error {
return nil
}

d.result.addNode(path, vx.Type(), nil, vx, vy)
d.addNode(path, vx.Type(), nil, vx, vy)
return nil
}

Expand All @@ -114,7 +123,7 @@ func (d *differ) diff(path []PathStep, vx, vy reflect.Value) error {
}

if vx.Type() != vy.Type() {
d.result.addNode(path, vx.Type(), vy.Type(), vx, vy)
d.addNode(path, vx.Type(), vy.Type(), vx, vy)
return nil
}

Expand All @@ -141,7 +150,7 @@ func (d *differ) diff(path []PathStep, vx, vy reflect.Value) error {

func (d *differ) diffSlice(path []PathStep, vx, vy reflect.Value) error {
if vx.IsNil() || vy.IsNil() {
d.result.addNode(path, vx.Type(), vy.Type(), vx, vy)
d.addNode(path, vx.Type(), vy.Type(), vx, vy)
return nil
}

Expand All @@ -162,21 +171,21 @@ func (d *differ) diffSlice(path []PathStep, vx, vy reflect.Value) error {
for i := minLen; i < vx.Len(); i++ {
nextPath := newSlicePath(path, i)
nextValueX := vx.Index(i)
d.result.addNode(nextPath, nextValueX.Type(), nextValueX.Type(), nextValueX, reflect.Value{})
d.addNode(nextPath, nextValueX.Type(), nextValueX.Type(), nextValueX, reflect.Value{})
}

for i := minLen; i < vy.Len(); i++ {
nextPath := newSlicePath(path, i)
nextValueY := vy.Index(i)
d.result.addNode(nextPath, nextValueY.Type(), nextValueY.Type(), reflect.Value{}, nextValueY)
d.addNode(nextPath, nextValueY.Type(), nextValueY.Type(), reflect.Value{}, nextValueY)
}

return nil
}

func (d *differ) diffMap(path []PathStep, vx, vy reflect.Value) error {
if vx.IsNil() || vy.IsNil() {
d.result.addNode(path, vx.Type(), vy.Type(), vx, vy)
d.addNode(path, vx.Type(), vy.Type(), vx, vy)
return nil
}

Expand Down Expand Up @@ -214,7 +223,7 @@ func (d *differ) diffInterface(path []PathStep, vx, vy reflect.Value) error {
}

if vx.IsNil() || vy.IsNil() {
d.result.addNode(path, vx.Type(), vy.Type(), vx, vy)
d.addNode(path, vx.Type(), vy.Type(), vx, vy)
return nil
}

Expand All @@ -226,15 +235,15 @@ func (d *differ) diffString(path []PathStep, vx, vy reflect.Value) error {
if vx.String() == vy.String() {
return nil
}
d.result.addNode(path, vx.Type(), vy.Type(), vx, vy)
d.addNode(path, vx.Type(), vy.Type(), vx, vy)
return nil
}

func (d *differ) diffBool(path []PathStep, vx, vy reflect.Value) error {
if vx.Bool() == vy.Bool() {
return nil
}
d.result.addNode(path, vx.Type(), vy.Type(), vx, vy)
d.addNode(path, vx.Type(), vy.Type(), vx, vy)
return nil
}

Expand All @@ -243,7 +252,7 @@ func (d *differ) diffNumber(path []PathStep, vx, vy reflect.Value) error {
return nil
}

d.result.addNode(path, vx.Type(), vy.Type(), vx, vy)
d.addNode(path, vx.Type(), vy.Type(), vx, vy)
return nil
}

Expand Down Expand Up @@ -322,3 +331,90 @@ func newMapPath(path []PathStep, index string) []PathStep {
})
return next
}

func (d *differ) addNode(path []PathStep, tx, ty reflect.Type, vx, vy reflect.Value) {
if len(d.ignoredPaths) > 0 {
pathString := makePathString(path)
if d.isIgnoredPaths(pathString) {
return
}
nvx := d.ignoredValue(vx, pathString)
nvy := d.ignoredValue(vy, pathString)

d.result.addNode(path, tx, ty, nvx, nvy)
return
}

d.result.addNode(path, tx, ty, vx, vy)
}

func (d *differ) ignoredValue(v reflect.Value, prefix string) reflect.Value {
switch v.Kind() {
case reflect.Map:
nv := reflect.MakeMap(v.Type())
keys := v.MapKeys()
for _, k := range keys {
nprefix := prefix + "." + k.String()
if d.isIgnoredPaths(nprefix) {
continue
}

sub := v.MapIndex(k)
filtered := d.ignoredValue(sub, nprefix)
if !filtered.IsValid() {
continue
}
nv.SetMapIndex(k, filtered)
}
return nv

case reflect.Slice, reflect.Array:
nv := reflect.MakeSlice(v.Type(), 0, 0)
for i := 0; i < v.Len(); i++ {
nprefix := prefix + "." + strconv.Itoa(i)
if d.isIgnoredPaths(nprefix) {
continue
}

filtered := d.ignoredValue(v.Index(i), nprefix)
if !filtered.IsValid() {
continue
}
nv = reflect.Append(nv, filtered)
}
return nv

case reflect.Interface:
nprefix := prefix + "." + v.String()
if d.isIgnoredPaths(nprefix) {
return reflect.New(v.Type())
}
return d.ignoredValue(v.Elem(), prefix)

case reflect.String:
return v

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v

case reflect.Float32, reflect.Float64:
return v

default:
nprefix := prefix + "." + v.String()
if d.isIgnoredPaths(nprefix) {
return reflect.New(v.Type())
}
return v
}
}

func (d *differ) isIgnoredPaths(pathString string) bool {
for _, ignoredPath := range d.ignoredPaths {
if strings.HasPrefix(pathString, ignoredPath) {
return true
}
}
return false
}
46 changes: 46 additions & 0 deletions pkg/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,52 @@ func TestDiff(t *testing.T) {
},
diffNum: 0,
},
{
name: "diff by ignoring specified field",
yamlFile: "testdata/ignore_specified_field.yaml",
options: []Option{
WithIgnoredPaths([]string{"spec.replicas", "spec.template.spec.containers.0.args.1", "spec.template.spec.strategy.rollingUpdate.maxSurge", "spec.template.spec.containers.3.livenessProbe.initialDelaySeconds"}),
},
diffNum: 6,
diffString: ` spec:
template:
metadata:
labels:
#spec.template.metadata.labels.app
- app: simple
+ app: simple2

#spec.template.metadata.labels.component
- component: foo

spec:
containers:
-
#spec.template.spec.containers.1.image
- image: gcr.io/pipecd/helloworld:v2.0.0
+ image: gcr.io/pipecd/helloworld:v2.1.0

-
#spec.template.spec.containers.2.image
- image:

#spec.template.spec.containers.3
+ - image: new-image
+ livenessProbe:
+ exec:
+ command:
+ - cat
+ - /tmp/healthy
+ name: foo

#spec.template.spec.strategy
+ strategy:
+ rollingUpdate:
+ maxUnavailable: 25%
+ type: RollingUpdate

`,
},
{
name: "has diff",
yamlFile: "testdata/has_diff.yaml",
Expand Down
Loading