Skip to content

Commit 7a3e61f

Browse files
authored
add release manifest diff to more verbose logs (#162)
for log V(4), log the release manifest diff when performing an install, uninstall, and upgrade.
1 parent b9e775b commit 7a3e61f

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ require (
130130
github.com/prometheus/procfs v0.6.0 // indirect
131131
github.com/rubenv/sql-migrate v0.0.0-20200616145509-8d140a17f351 // indirect
132132
github.com/russross/blackfriday v1.5.2 // indirect
133+
github.com/sergi/go-diff v1.2.0
133134
github.com/shopspring/decimal v1.2.0 // indirect
134135
github.com/spf13/cast v1.3.1 // indirect
135136
github.com/weppos/publicsuffix-go v0.13.0 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,9 @@ github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0
872872
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
873873
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
874874
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
875-
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
876875
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
876+
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
877+
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
877878
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
878879
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
879880
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=

pkg/reconciler/internal/diff/diff.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2022 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package diff
16+
17+
import (
18+
"bytes"
19+
"regexp"
20+
"strings"
21+
22+
"github.com/sergi/go-diff/diffmatchpatch"
23+
)
24+
25+
// Generate generates a diff between a and b, in color.
26+
func Generate(a, b string) string {
27+
dmp := diffmatchpatch.New()
28+
29+
wSrc, wDst, warray := dmp.DiffLinesToRunes(a, b)
30+
diffs := dmp.DiffMainRunes(wSrc, wDst, false)
31+
diffs = dmp.DiffCharsToLines(diffs, warray)
32+
var buff bytes.Buffer
33+
for _, diff := range diffs {
34+
text := diff.Text
35+
36+
switch diff.Type {
37+
case diffmatchpatch.DiffInsert:
38+
_, _ = buff.WriteString("\x1b[32m")
39+
_, _ = buff.WriteString(prefixLines(text, "+"))
40+
_, _ = buff.WriteString("\x1b[0m")
41+
case diffmatchpatch.DiffDelete:
42+
_, _ = buff.WriteString("\x1b[31m")
43+
_, _ = buff.WriteString(prefixLines(text, "-"))
44+
_, _ = buff.WriteString("\x1b[0m")
45+
case diffmatchpatch.DiffEqual:
46+
_, _ = buff.WriteString(prefixLines(text, " "))
47+
}
48+
}
49+
return buff.String()
50+
}
51+
52+
func prefixLines(s, prefix string) string {
53+
var buf bytes.Buffer
54+
lines := strings.Split(s, "\n")
55+
ls := regexp.MustCompile("^")
56+
for _, line := range lines[:len(lines)-1] {
57+
buf.WriteString(ls.ReplaceAllString(line, prefix))
58+
buf.WriteString("\n")
59+
}
60+
return buf.String()
61+
}

pkg/reconciler/reconciler.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
5252
"github.com/operator-framework/helm-operator-plugins/pkg/hook"
5353
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/conditions"
54+
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/diff"
5455
internalhook "github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/hook"
5556
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/updater"
5657
internalvalues "github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/values"
@@ -746,6 +747,12 @@ func (r *Reconciler) doInstall(actionClient helmclient.ActionInterface, u *updat
746747
r.reportOverrideEvents(obj)
747748

748749
log.Info("Release installed", "name", rel.Name, "version", rel.Version)
750+
751+
// If log verbosity is higher, output Helm Release Manifest that was installed
752+
if log.V(4).Enabled() {
753+
fmt.Println(diff.Generate("", rel.Manifest))
754+
}
755+
749756
return rel, nil
750757
}
751758

@@ -763,6 +770,12 @@ func (r *Reconciler) doUpgrade(actionClient helmclient.ActionInterface, u *updat
763770
}
764771
}
765772

773+
// Get the current release so we can compare the new release in the diff if the diff is being logged.
774+
curRel, err := actionClient.Get(obj.GetName())
775+
if err != nil {
776+
return nil, fmt.Errorf("could not get the current Helm Release: %w", err)
777+
}
778+
766779
rel, err := actionClient.Upgrade(obj.GetName(), obj.GetNamespace(), r.chrt, vals, opts...)
767780
if err != nil {
768781
u.UpdateStatus(
@@ -774,6 +787,11 @@ func (r *Reconciler) doUpgrade(actionClient helmclient.ActionInterface, u *updat
774787
r.reportOverrideEvents(obj)
775788

776789
log.Info("Release upgraded", "name", rel.Name, "version", rel.Version)
790+
791+
// If log verbosity is higher, output upgraded Helm Release Manifest
792+
if log.V(4).Enabled() {
793+
fmt.Println(diff.Generate(curRel.Manifest, rel.Manifest))
794+
}
777795
return rel, nil
778796
}
779797

@@ -823,6 +841,11 @@ func (r *Reconciler) doUninstall(actionClient helmclient.ActionInterface, u *upd
823841
return err
824842
} else {
825843
log.Info("Release uninstalled", "name", resp.Release.Name, "version", resp.Release.Version)
844+
845+
// If log verbosity is higher, output Helm Release Manifest that was uninstalled
846+
if log.V(4).Enabled() {
847+
fmt.Println(diff.Generate(resp.Release.Manifest, ""))
848+
}
826849
}
827850
u.Update(updater.RemoveFinalizer(uninstallFinalizer))
828851
u.UpdateStatus(

0 commit comments

Comments
 (0)