github.com/banzaicloud/operator-tools@v0.28.10/pkg/reconciler/handlers.go (about)

     1  // Copyright © 2020 Banzai Cloud
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package reconciler
    16  
    17  import (
    18  	"strings"
    19  
    20  	"k8s.io/apimachinery/pkg/types"
    21  	"sigs.k8s.io/controller-runtime/pkg/client"
    22  	"sigs.k8s.io/controller-runtime/pkg/handler"
    23  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    24  
    25  	ottypes "github.com/banzaicloud/operator-tools/pkg/types"
    26  )
    27  
    28  func EnqueueByOwnerAnnotationMapper() handler.MapFunc {
    29  	return func(a client.Object) []reconcile.Request {
    30  		pieces := strings.SplitN(a.GetAnnotations()[ottypes.BanzaiCloudRelatedTo], string(types.Separator), 2)
    31  		if len(pieces) != 2 {
    32  			return []reconcile.Request{}
    33  		}
    34  
    35  		return []reconcile.Request{
    36  			{NamespacedName: client.ObjectKey{
    37  				Name:      pieces[1],
    38  				Namespace: pieces[0],
    39  			}},
    40  		}
    41  	}
    42  }