Skip to content

Commit

Permalink
fix: Fix cross-namespace creator resource events
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Aug 5, 2021
1 parent 9646085 commit db83739
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
14 changes: 7 additions & 7 deletions pkg/controller/integration/build_kit.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ func (action *buildKitAction) Handle(ctx context.Context, integration *v1.Integr
// Add some information for post-processing, this may need to be refactored
// to a proper data structure
platformKit.Labels = map[string]string{
"camel.apache.org/kit.type": v1.IntegrationKitTypePlatform,
"camel.apache.org/created.by.kind": v1.IntegrationKind,
"camel.apache.org/created.by.name": integration.Name,
"camel.apache.org/created.by.namespace": integration.Namespace,
"camel.apache.org/created.by.version": integration.ResourceVersion,
"camel.apache.org/runtime.version": integration.Status.RuntimeVersion,
"camel.apache.org/runtime.provider": string(integration.Status.RuntimeProvider),
"camel.apache.org/kit.type": v1.IntegrationKitTypePlatform,
"camel.apache.org/runtime.version": integration.Status.RuntimeVersion,
"camel.apache.org/runtime.provider": string(integration.Status.RuntimeProvider),
kubernetes.CamelCreatorLabelKind: v1.IntegrationKind,
kubernetes.CamelCreatorLabelName: integration.Name,
kubernetes.CamelCreatorLabelNamespace: integration.Namespace,
kubernetes.CamelCreatorLabelVersion: integration.ResourceVersion,
}

// Set the kit to have the same characteristics as the integrations
Expand Down
28 changes: 15 additions & 13 deletions pkg/event/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"

ctrl "sigs.k8s.io/controller-runtime/pkg/client"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/log"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -260,17 +262,21 @@ func NotifyBuildError(ctx context.Context, c client.Client, recorder record.Even
}

// nolint:lll
func notifyIfPhaseUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, new runtime.Object, oldPhase, newPhase string, resourceType, name, reason, info string) {
func notifyIfPhaseUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, new ctrl.Object, oldPhase, newPhase string, resourceType, name, reason, info string) {
// Update information about phase changes
if oldPhase != newPhase {
phase := newPhase
if phase == "" {
phase = "[none]"
}
recorder.Eventf(new, corev1.EventTypeNormal, reason, "%s %s in phase %q%s", resourceType, name, phase, info)
recorder.Eventf(new, corev1.EventTypeNormal, reason, "%s %q in phase %q%s", resourceType, name, phase, info)

if creatorRef, creator := getCreatorObject(ctx, c, new); creatorRef != nil && creator != nil {
recorder.Eventf(creator, corev1.EventTypeNormal, ReasonRelatedObjectChanged, "%s %s subresource %s (%s) changed phase to %q%s", creatorRef.Kind, creatorRef.Name, name, resourceType, phase, info)
if namespace := new.GetNamespace(); namespace == creatorRef.Namespace {
recorder.Eventf(creator, corev1.EventTypeNormal, ReasonRelatedObjectChanged, "%s %q, created by %s %q, changed phase to %q%s", resourceType, name, creatorRef.Kind, creatorRef.Name, phase, info)
} else {
recorder.Eventf(creator, corev1.EventTypeNormal, ReasonRelatedObjectChanged, "%s \"%s/%s\", created by %s %q, changed phase to %q%s", resourceType, namespace, name, creatorRef.Kind, creatorRef.Name, phase, info)
}
}
}
}
Expand Down Expand Up @@ -305,12 +311,8 @@ func getCreatorObject(ctx context.Context, c client.Client, obj runtime.Object)
if ref := kubernetes.GetCamelCreator(obj); ref != nil {
if ref.Kind == "Integration" {
it := v1.NewIntegration(ref.Namespace, ref.Name)
key := runtimeclient.ObjectKey{
Namespace: ref.Namespace,
Name: ref.Name,
}
if err := c.Get(ctx, key, &it); err != nil {
log.Infof("Cannot get information about the Integration creating resource %v: %v", ref, err)
if err := c.Get(ctx, ctrl.ObjectKeyFromObject(&it), &it); err != nil {
log.Infof("Cannot get information about the creator Integration %v: %v", ref, err)
return nil, nil
}
return ref, &it
Expand Down
11 changes: 6 additions & 5 deletions pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path"
"sort"

"github.com/apache/camel-k/pkg/util/kubernetes"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -175,11 +176,11 @@ func (t *containerTrait) configureDependencies(e *Environment) error {
// Add some information for post-processing, this may need to be refactored
// to a proper data structure
kit.Labels = map[string]string{
"camel.apache.org/kit.type": v1.IntegrationKitTypeExternal,
"camel.apache.org/created.by.kind": v1.IntegrationKind,
"camel.apache.org/created.by.name": e.Integration.Name,
"camel.apache.org/created.by.namespace": e.Integration.Namespace,
"camel.apache.org/created.by.version": e.Integration.ResourceVersion,
"camel.apache.org/kit.type": v1.IntegrationKitTypeExternal,
kubernetes.CamelCreatorLabelKind: v1.IntegrationKind,
kubernetes.CamelCreatorLabelName: e.Integration.Name,
kubernetes.CamelCreatorLabelNamespace: e.Integration.Namespace,
kubernetes.CamelCreatorLabelVersion: e.Integration.ResourceVersion,
}

t.L.Infof("image %s", kit.Spec.Image)
Expand Down
22 changes: 14 additions & 8 deletions pkg/util/kubernetes/camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ package kubernetes
import (
"strings"

v1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
)

const (
CamelCreatorLabelPrefix = "camel.apache.org/created.by"

CamelCreatorLabelKind = CamelCreatorLabelPrefix + ".kind"
CamelCreatorLabelName = CamelCreatorLabelPrefix + ".name"
CamelCreatorLabelKind = CamelCreatorLabelPrefix + ".kind"
CamelCreatorLabelName = CamelCreatorLabelPrefix + ".name"
CamelCreatorLabelNamespace = CamelCreatorLabelPrefix + ".namespace"
CamelCreatorLabelVersion = CamelCreatorLabelPrefix + ".version"
)

// FilterCamelCreatorLabels is used to inherit the creator information among resources
Expand All @@ -57,16 +59,20 @@ func MergeCamelCreatorLabels(source map[string]string, target map[string]string)
}

// GetCamelCreator returns the Camel creator object referenced by this runtime object, if present
func GetCamelCreator(obj runtime.Object) *v1.ObjectReference {
func GetCamelCreator(obj runtime.Object) *corev1.ObjectReference {
if m, ok := obj.(metav1.Object); ok {
kind := m.GetLabels()[CamelCreatorLabelKind]
name := m.GetLabels()[CamelCreatorLabelName]
namespace, ok := m.GetLabels()[CamelCreatorLabelNamespace]
if !ok {
namespace = m.GetNamespace()
}
if kind != "" && name != "" {
return &v1.ObjectReference{
return &corev1.ObjectReference{
Kind: kind,
Namespace: m.GetNamespace(),
Namespace: namespace,
Name: name,
APIVersion: camelv1.SchemeGroupVersion.String(),
APIVersion: v1.SchemeGroupVersion.String(),
}
}
}
Expand Down

0 comments on commit db83739

Please sign in to comment.