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

Propogate user defined labels on the RGI to resources #229

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions pkg/controller/instance/controller_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import (
"context"
"fmt"
"strings"

"github.com/go-logr/logr"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -221,6 +222,15 @@
) error {
igr.log.V(1).Info("Creating new resource", "resourceID", resourceID)

// Check if label propagation is enabled
annotations, found := igr.runtime.GetInstance().Object["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})
if found {
if val, ok := annotations["kro.run/propagate-labels"]; ok && val == "true" {
igr.log.V(1).Info("Found propage labels annotation")
igr.propagateLabelsToResource(resource)
}
}

// Apply labels and create resource
igr.instanceSubResourcesLabeler.ApplyLabels(resource)
if _, err := rc.Create(ctx, resource, metav1.CreateOptions{}); err != nil {
Expand All @@ -233,6 +243,33 @@
return igr.delayedRequeue(fmt.Errorf("awaiting resource creation completion"))
}

func (igr *instanceGraphReconciler) propagateLabelsToResource(resource *unstructured.Unstructured) {
// Get labels from the RGI
rgiLabels, found := igr.runtime.GetInstance().Object["metadata"].(map[string]interface{})["labels"].(map[string]interface{})

if !found {
igr.log.V(1).Info("No user labels found on the RGI for propagation")
return
}

userLabels := filterUserLabels(rgiLabels)
unstructured.SetNestedStringMap(resource.Object, userLabels, "metadata", "labels")

Check failure on line 256 in pkg/controller/instance/controller_reconcile.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `unstructured.SetNestedStringMap` is not checked (errcheck)
igr.log.V(1).Info("User labels propagated to resource")
}

// filterUserLabels extracts non-"kro.run/" labels
func filterUserLabels(labels map[string]interface{}) map[string]string {
userLabels := make(map[string]string)
for key, value := range labels {
if !strings.HasPrefix(key, "kro.run/") {
if strValue, ok := value.(string); ok {
userLabels[key] = strValue
}
}
}
return userLabels
}

// updateResource handles updates to an existing resource, comparing the desired
// and observed states and applying the necessary changes.
func (igr *instanceGraphReconciler) updateResource(
Expand Down
Loading