Skip to content

Commit

Permalink
feat: add optional logging for expander stages
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoweim committed Jun 26, 2024
1 parent 148ffb5 commit 7243459
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ func (s *FacadeStatus) ClearCondition(condition ConditionType) {

// Validation
func (f *Facade) Validate() bool {
return true
// Validate annotations. This is to check whether the facade CR has turned on log capturing expander stages.
annotationFound := false
for annotationKey := range f.Annotations {
if annotationKey == "composition-expander-stage-logs" {
annotationFound = true
}
}
if !annotationFound {
f.Annotations["composition-expander-stage-logs"] = "false"
}
}

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -221,6 +222,16 @@ func (r *ExpanderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c

// ------------------- APPLIER SECTION -----------------------
stagesApplied := []string{}
expanderLogsEnabled := false
var err error
for annotationKey, annotationValue := range inputcr.GetAnnotations() {
if annotationKey == "composition-expander-stage-logs" {
expanderLogsEnabled, err = strconv.ParseBool(annotationValue)
if err != nil {
logger.Error(err, "unable to parse annotation value for composition-expander-stage-logs. Setting it to false.")
}
}
}

// Re-read the Plan CR to load the expanded manifests
oldGeneration := plancr.GetGeneration()
Expand Down Expand Up @@ -324,6 +335,16 @@ func (r *ExpanderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
newStatus.ClearCondition(compositionv1alpha1.Ready)
message := fmt.Sprintf("Evaluated stages: %s \n Applied stages: %s", strings.Join(stagesEvaluated, ", "), strings.Join(stagesApplied, ", "))
newStatus.AppendCondition(compositionv1alpha1.Ready, metav1.ConditionFalse, message, "PendingStages")

if expanderLogsEnabled {
// TODO(@xiaoweim): Log ressource UID in the future if possible
r.Recorder.Event(&inputcr, "Normal", fmt.Sprintf("Finished expander stage %d: %s", index, expander), fmt.Sprintf("version: %d, resource count", inputcr.GetGeneration(), applier.Count()))
for i, resourceStatus := range newStatus.Stages[applier.stageName].LastApplied {
logger.Info("Resource %d\n", i)
logger.Info("Resource name: %s, resource namespace: %s, resource gvk: %s %s %s, resource status: %s",
resourceStatus.Name, resourceStatus.Namespace, resourceStatus.Group, resourceStatus.Version, resourceStatus.Kind, resourceStatus.Health)
}
}
}

// Inject plan.Ready Condition with list of expanders
Expand Down

0 comments on commit 7243459

Please sign in to comment.