Skip to content

Commit

Permalink
Log reason for reconciliations
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
  • Loading branch information
pierDipi committed Sep 30, 2024
1 parent b252486 commit 232e0fe
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
13 changes: 7 additions & 6 deletions hack/images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ if [[ $on_cluster_builds = true ]]; then
logger.info 'Images build'

else
CONTAINER_BUILD_TAG_SUFFIX=${CONTAINER_BUILD_TAG_SUFFIX:-""}
tmp_dockerfile=$(replace_images openshift-knative-operator/Dockerfile)
podman build -t "$repo/serverless-openshift-knative-operator" -f "${tmp_dockerfile}" .
podman push "$repo/serverless-openshift-knative-operator"
podman build -t "$repo/serverless-openshift-knative-operator${CONTAINER_BUILD_TAG_SUFFIX}" -f "${tmp_dockerfile}" .
podman push "$repo/serverless-openshift-knative-operator${CONTAINER_BUILD_TAG_SUFFIX}"

tmp_dockerfile=$(replace_images knative-operator/Dockerfile)
podman build -t "$repo/serverless-knative-operator" -f "${tmp_dockerfile}" .
podman push "$repo/serverless-knative-operator"
podman build -t "$repo/serverless-knative-operator${CONTAINER_BUILD_TAG_SUFFIX}" -f "${tmp_dockerfile}" .
podman push "$repo/serverless-knative-operator${CONTAINER_BUILD_TAG_SUFFIX}"

tmp_dockerfile=$(replace_images serving/ingress/Dockerfile)
podman build -t "$repo/serverless-ingress" -f "${tmp_dockerfile}" .
podman push "$repo/serverless-ingress"
podman build -t "$repo/serverless-ingress${CONTAINER_BUILD_TAG_SUFFIX}" -f "${tmp_dockerfile}" .
podman push "$repo/serverless-ingress${CONTAINER_BUILD_TAG_SUFFIX}"
fi
10 changes: 9 additions & 1 deletion knative-operator/pkg/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -110,12 +111,19 @@ func SetAnnotations(annotations map[string]string) mf.Transformer {
}

// EnqueueRequestByOwnerAnnotations is a common function to enqueue reconcile requests for resources.
func EnqueueRequestByOwnerAnnotations(ownerNameAnnotationKey, ownerNamespaceAnnotationKey string) handler.EventHandler {
func EnqueueRequestByOwnerAnnotations(logger logr.Logger, ownerNameAnnotationKey, ownerNamespaceAnnotationKey string) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(_ context.Context, obj client.Object) []reconcile.Request {
annotations := obj.GetAnnotations()
ownerNamespace := annotations[ownerNamespaceAnnotationKey]
ownerName := annotations[ownerNameAnnotationKey]
if ownerNamespace != "" && ownerName != "" {
logger.Info("Creating reconciliation request for owner",
"ownerNamespace", ownerNamespace,
"owner", ownerName,
"reason.object.gvk", obj.GetObjectKind().GroupVersionKind().String(),
"reason.object.namespace", obj.GetNamespace(),
"reason.object.name", obj.GetName(),
)
return []reconcile.Request{{
NamespacedName: types.NamespacedName{Namespace: ownerNamespace, Name: ownerName},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,19 @@ func add(mgr manager.Manager, r *ReconcileKnativeKafka) error {
r.rawKafkaSinkManifest,
)

namespace := os.Getenv("REQUIRED_KAFKA_NAMESPACE")
if namespace == "" {
namespace = "knative-eventing"
}

for key, t := range gvkToResource {
if strings.EqualFold(key.Group, "cert-manager.io") {
// We cannot watch cert-manager resources since it's an optional addon.
continue
}
err = c.Watch(source.Kind(mgr.GetCache(), t), common.EnqueueRequestByOwnerAnnotations(common.KafkaOwnerName, common.KafkaOwnerNamespace))
err = c.Watch(source.Kind(mgr.GetCache(), t), filteredGlobalResync(context.Background(), c.GetLogger(), r, func(object client.Object) bool {
return object.GetNamespace() == "" /* cluster scoped resources */ || object.GetNamespace() == namespace
}))
if err != nil {
return err
}
Expand All @@ -158,16 +165,16 @@ func add(mgr manager.Manager, r *ReconcileKnativeKafka) error {
}

for _, t := range gvkToEventingResource {
err = c.Watch(source.Kind(mgr.GetCache(), t), filteredGlobalResync(context.Background(), mgr.GetLogger(), r, func(object client.Object) bool {
return object.GetNamespace() == "knative-eventing" && dependentConfigMaps.Has(object.GetName())
err = c.Watch(source.Kind(mgr.GetCache(), t), filteredGlobalResync(context.Background(), c.GetLogger(), r, func(object client.Object) bool {
return object.GetNamespace() == namespace && dependentConfigMaps.Has(object.GetName())
}))
if err != nil {
return err
}
}

// watch KnativeEventing instances as KnativeKafka instances are dependent on them
err = c.Watch(source.Kind(mgr.GetCache(), &operatorv1beta1.KnativeEventing{}), filteredGlobalResync(context.Background(), mgr.GetLogger(), r, func(_ client.Object) bool {
err = c.Watch(source.Kind(mgr.GetCache(), &operatorv1beta1.KnativeEventing{}), filteredGlobalResync(context.Background(), c.GetLogger(), r, func(_ client.Object) bool {
return true
}))
if err != nil {
Expand All @@ -184,6 +191,7 @@ func filteredGlobalResync(ctx context.Context, logger logr.Logger, r *ReconcileK
}

logger.Info("Global resync",
"object.gvk", object.GetObjectKind().GroupVersionKind().String(),
"object.namespace", object.GetNamespace(),
"object.name", object.GetName(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ import (
"os"
"sync/atomic"

"github.com/openshift-knative/serverless-operator/knative-operator/pkg/common"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/controller/knativeserving/consoleclidownload"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/controller/knativeserving/consoleutil"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/controller/knativeserving/quickstart"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/monitoring"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/monitoring/dashboards"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/monitoring/dashboards/health"
socommon "github.com/openshift-knative/serverless-operator/pkg/common"
"github.com/go-logr/logr"
configv1 "github.com/openshift/api/config/v1"
consolev1 "github.com/openshift/api/console/v1"
routev1 "github.com/openshift/api/route/v1"
Expand All @@ -37,6 +30,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/openshift-knative/serverless-operator/knative-operator/pkg/common"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/controller/knativeserving/consoleclidownload"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/controller/knativeserving/consoleutil"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/controller/knativeserving/quickstart"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/monitoring"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/monitoring/dashboards"
"github.com/openshift-knative/serverless-operator/knative-operator/pkg/monitoring/dashboards/health"
socommon "github.com/openshift-knative/serverless-operator/pkg/common"
)

const (
Expand Down Expand Up @@ -94,6 +96,7 @@ func newReconciler(mgr manager.Manager) reconcile.Reconciler {
client: client,
cache: mgr.GetCache(),
scheme: mgr.GetScheme(),
logger: mgr.GetLogger(),
}
}

Expand Down Expand Up @@ -166,7 +169,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
}
}
for _, t := range gvkToResource {
err = c.Watch(source.Kind(mgr.GetCache(), t), common.EnqueueRequestByOwnerAnnotations(socommon.ServingOwnerName, socommon.ServingOwnerNamespace))
err = c.Watch(source.Kind(mgr.GetCache(), t), common.EnqueueRequestByOwnerAnnotations(c.GetLogger(), socommon.ServingOwnerName, socommon.ServingOwnerNamespace))
if err != nil {
return err
}
Expand All @@ -186,6 +189,7 @@ type ReconcileKnativeServing struct {
scheme *runtime.Scheme
cache cache.Cache
c *controller.Controller
logger logr.Logger
}

// Reconcile reads that state of the cluster for a KnativeServing
Expand All @@ -209,7 +213,7 @@ func (r *ReconcileKnativeServing) Reconcile(_ context.Context, request reconcile

// If previously not set let's add a watch for ConsoleCLIDownload
if consoleutil.IsConsoleInstalled() && !cliDownloadWatchSet.Load() {
if err = (*r.c).Watch(source.Kind(r.cache, &consolev1.ConsoleCLIDownload{}), common.EnqueueRequestByOwnerAnnotations(socommon.ServingOwnerName, socommon.ServingOwnerNamespace)); err != nil {
if err = (*r.c).Watch(source.Kind(r.cache, &consolev1.ConsoleCLIDownload{}), common.EnqueueRequestByOwnerAnnotations(r.logger, socommon.ServingOwnerName, socommon.ServingOwnerNamespace)); err != nil {
return reconcile.Result{}, err
}
cliDownloadWatchSet.Store(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package health
import (
"context"

"github.com/openshift-knative/serverless-operator/knative-operator/pkg/common"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -13,6 +12,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/openshift-knative/serverless-operator/knative-operator/pkg/common"
)

var log = common.Log.WithName("health-controller")
Expand All @@ -36,7 +37,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return err
}

err = c.Watch(source.Kind(mgr.GetCache(), &corev1.ConfigMap{}), common.EnqueueRequestByOwnerAnnotations(common.ServerlessOperatorOwnerName, common.ServerlessOperatorOwnerNamespace), skipCreatePredicate{})
err = c.Watch(source.Kind(mgr.GetCache(), &corev1.ConfigMap{}), common.EnqueueRequestByOwnerAnnotations(mgr.GetLogger(), common.ServerlessOperatorOwnerName, common.ServerlessOperatorOwnerNamespace), skipCreatePredicate{})
if err != nil {
return err
}
Expand Down

0 comments on commit 232e0fe

Please sign in to comment.