Skip to content

Commit

Permalink
Support transfering annotations and labels from an integration onto o…
Browse files Browse the repository at this point in the history
…wned resources

Fixes apache#263
  • Loading branch information
astefanutti authored and ipolyzos committed Jul 31, 2019
1 parent cab3cde commit bbaa833
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
23 changes: 20 additions & 3 deletions docs/traits.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ for the `route` trait to work.

The following is a list of common traits that can be configured by the end users:

[options="header",cols="1m,2,3a"]
[options="header",cols="1m,,3a"]
|=======================
| Trait | Profiles | Description

Expand Down Expand Up @@ -226,8 +226,25 @@ It's disabled by default.

There are also platform traits that **normally should not be configured** by the end user. So change them **only if you know what you're doing**.

[options="header",cols="m,,"]
[options="header",cols="1m,2,3a"]
|=======================
| Trait | Profiles | Description
| owner | Kubernetes, OpenShift | Makes sure that every resource created by the traits belongs to the integration custom resource (so they are deleted when the integration is deleted).
| owner
| All
| Ensures that all created resources belong to the integration being created (so they are deleted when the integration is deleted) and transfers annotations and labels on the integration onto these owned resources.
+
+
It's enabled by default.

[cols="m,"]
!===

! owner.target-annotations
! The annotations to be transfered (A comma-separared list of label keys)

! owner.target-labels
! The labels to be transfered (A comma-separared list of label keys)

!===

|=======================
42 changes: 41 additions & 1 deletion pkg/trait/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ limitations under the License.
package trait

import (
"strings"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ownerTrait ensures that all created resources belong to the integration being created
// and transfers annotations and labels on the integration onto these owned resources being created
type ownerTrait struct {
BaseTrait `property:",squash"`

TargetAnnotations string `property:"target-annotations"`
TargetLabels string `property:"target-labels"`
}

func newOwnerTrait() *ownerTrait {
Expand All @@ -41,10 +47,26 @@ func (t *ownerTrait) Configure(e *Environment) (bool, error) {
return e.IntegrationInPhase(v1alpha1.IntegrationPhaseDeploying), nil
}

func (*ownerTrait) Apply(e *Environment) error {
func (t *ownerTrait) Apply(e *Environment) error {
controller := true
blockOwnerDeletion := true

targetLabels := map[string]string{}
for _, k := range strings.Split(t.TargetLabels, ",") {
if v, ok := e.Integration.Labels[k]; ok {
targetLabels[k] = v
}
}

targetAnnotations := map[string]string{}
for _, k := range strings.Split(t.TargetAnnotations, ",") {
if v, ok := e.Integration.Annotations[k]; ok {
targetAnnotations[k] = v
}
}

e.Resources.VisitMetaObject(func(res metav1.Object) {
// Add owner reference
references := []metav1.OwnerReference{
{
APIVersion: e.Integration.APIVersion,
Expand All @@ -56,6 +78,24 @@ func (*ownerTrait) Apply(e *Environment) error {
},
}
res.SetOwnerReferences(references)

// Transfer annotations
annotations := res.GetAnnotations()
for k, v := range targetAnnotations {
if _, ok := annotations[k]; !ok {
annotations[k] = v
}
}
res.SetAnnotations(annotations)

// Transfer labels
labels := res.GetLabels()
for k, v := range targetLabels {
if _, ok := labels[k]; !ok {
labels[k] = v
}
}
res.SetLabels(labels)
})
return nil
}

0 comments on commit bbaa833

Please sign in to comment.