Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
feat(log) Only log what is about to do
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosjgp committed Jul 5, 2018
1 parent 01794fb commit bec6b34
Showing 1 changed file with 17 additions and 41 deletions.
58 changes: 17 additions & 41 deletions pkg/landscaper/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ func (e *executor) Apply(desired, current Components) error {

logrus.WithFields(logrus.Fields{"create": len(create), "update": len(update), "delete": len(delete)}).Info("Apply desired state")

if err := logDifferences(current, create, update, delete, e.stageEnabled("update"), e.stageEnabled("delete"), logrus.Infof); err != nil {
return err
}

if e.stageEnabled("delete") {
for _, cmp := range delete {
logrus.Infof("Delete: %s", cmp.Name)
if err := e.DeleteComponent(cmp); err != nil {
logrus.WithFields(logrus.Fields{"error": err, "component": cmp}).Error("DeleteComponent failed")
return err
Expand All @@ -99,6 +96,9 @@ func (e *executor) Apply(desired, current Components) error {

if e.stageEnabled("update") {
for _, cmp := range update {
if err := logDifferences(logrus.Infof, "Update: "+cmp.Name, current[cmp.Name], cmp); err != nil {
return err
}
if err := e.UpdateComponent(cmp); err != nil {
logrus.WithFields(logrus.Fields{"error": err, "component": cmp}).Error("UpdateComponent failed")
return err
Expand All @@ -108,6 +108,10 @@ func (e *executor) Apply(desired, current Components) error {

if e.stageEnabled("create") {
for _, cmp := range create {
if err := logDifferences(logrus.Infof, "Create: "+cmp.Name, nil, cmp); err != nil {
return err
}

if err := e.CreateComponent(cmp); err != nil {
logrus.WithFields(logrus.Fields{"error": err, "component": cmp}).Error("CreateComponent failed")
return err
Expand Down Expand Up @@ -317,46 +321,18 @@ func componentDiffText(current, desired *Component) (string, error) {
})
}

// logDifferences logs the Create, Update and Delete w.r.t. current to logf
func logDifferences(current, creates, updates, deletes Components, updateStageEnabled bool, deleteStageEnabled bool, logf func(format string, args ...interface{})) error {
log := func(action string, current, desired *Component) error {
diff, err := componentDiffText(current, desired)
if err != nil {
return err
}
logf("%s", action)
if diff != "" {
logf("Diff:\n%s", diff)
}
if current != nil && desired != nil && !reflect.DeepEqual(current.SecretValues, desired.SecretValues) {
logrus.Info("Diff: secrets have changed, not shown here")
}
return nil
}

// Log diffs only if delete is applied
if deleteStageEnabled {
for _, d := range deletes {
logf("Delete: %s", d.Name)
}
func logDifferences(logf func(format string, args ...interface{}), action string, current, desired *Component) error {
diff, err := componentDiffText(current, desired)
if err != nil {
return err
}

for _, d := range creates {
if err := log("Create: "+d.Name, nil, d); err != nil {
return err
}
logf("%s", action)
if diff != "" {
logf("Diff:\n%s", diff)
}

// Log diffs only if upadete is applied
if updateStageEnabled {
for _, d := range updates {
c := current[d.Name]
if err := log("Update: "+d.Name, c, d); err != nil {
return err
}
}
if current != nil && desired != nil && !reflect.DeepEqual(current.SecretValues, desired.SecretValues) {
logrus.Info("Diff: secrets have changed, not shown here")
}

return nil
}

Expand Down

0 comments on commit bec6b34

Please sign in to comment.