-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix status when system is insync (#410)
DM must not have status.delta when controllers are insync. This commit ensures no delta for host, system and data network controllers. Test plan: PASS - perform day-2 operations, ensure no delta once controllers are insync. Signed-off-by: Wallysson Silva <wallysson.silva@windriver.com>
- Loading branch information
Showing
7 changed files
with
119 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/google/go-cmp/cmp" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type instance interface { | ||
client.Object | ||
|
||
SetStatusDelta(string) | ||
GetStatusDelta() string | ||
GetInsync() bool | ||
} | ||
|
||
func GetDeltaString(spec interface{}, current interface{}, parameters map[string]interface{}) (string, error) { | ||
|
||
specBytes, err := json.Marshal(spec) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
currentBytes, err := json.Marshal(current) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var specData map[string]interface{} | ||
var currentData map[string]interface{} | ||
|
||
err = json.Unmarshal([]byte(specBytes), &specData) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = json.Unmarshal([]byte(currentBytes), ¤tData) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
diff := cmp.Diff(currentData, specData) | ||
deltaString := collectDiffValues(diff, parameters) | ||
deltaString = strings.TrimSuffix(deltaString, "\n") | ||
return deltaString, nil | ||
} | ||
|
||
func SetInstanceDelta( | ||
inst instance, spec, current interface{}, | ||
parameters map[string]interface{}, | ||
status client.StatusWriter, log logr.Logger, | ||
) { | ||
kind := inst.GetObjectKind().GroupVersionKind().Kind | ||
log.Info(fmt.Sprintf("Updating delta for kind %s", kind)) | ||
|
||
oldDelta := inst.GetStatusDelta() | ||
if inst.GetInsync() { | ||
inst.SetStatusDelta("") | ||
} else if delta, err := GetDeltaString(spec, current, parameters); err == nil { | ||
inst.SetStatusDelta(delta) | ||
} else { | ||
log.Info(fmt.Sprintf("Failed to get Delta string for kind %s: %s\n", kind, err)) | ||
} | ||
|
||
if oldDelta != inst.GetStatusDelta() { | ||
err := status.Update(context.TODO(), inst) | ||
if err != nil { | ||
log.Info(fmt.Sprintf("Failed to update the status for kind %s: %s", kind, err)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters