github.com/verrazzano/verrazzano@v1.7.0/platform-operator/experimental/controllers/integration/single/manager.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package single
     5  
     6  import (
     7  	"context"
     8  	"github.com/verrazzano/verrazzano-modules/pkg/controller/basecontroller"
     9  	"github.com/verrazzano/verrazzano-modules/pkg/controller/spi/controllerspi"
    10  	"github.com/verrazzano/verrazzano-modules/pkg/controller/spi/handlerspi"
    11  	"github.com/verrazzano/verrazzano/platform-operator/constants"
    12  	"github.com/verrazzano/verrazzano/platform-operator/experimental/event"
    13  	corev1 "k8s.io/api/core/v1"
    14  	ctrlruntime "sigs.k8s.io/controller-runtime"
    15  	"sigs.k8s.io/controller-runtime/pkg/client"
    16  )
    17  
    18  // Specify the SPI interfaces that this controller implements
    19  var _ controllerspi.Reconciler = Reconciler{}
    20  
    21  type Reconciler struct {
    22  	*basecontroller.BaseReconciler
    23  	IntegrationControllerConfig
    24  }
    25  
    26  type IntegrationControllerConfig struct {
    27  	ControllerManager ctrlruntime.Manager
    28  	ModuleHandlerInfo handlerspi.ModuleHandlerInfo
    29  }
    30  
    31  // InitController start the  controller
    32  func InitController(modConfig IntegrationControllerConfig) error {
    33  	controller := Reconciler{}
    34  
    35  	// The config MUST contain at least the BaseReconciler.  Other spi interfaces are optional.
    36  	config := basecontroller.ControllerConfig{
    37  		Reconciler:  &controller,
    38  		EventFilter: &controller,
    39  	}
    40  
    41  	baseReconciler, err := basecontroller.CreateControllerAndAddItToManager(modConfig.ControllerManager, config)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	controller.BaseReconciler = baseReconciler
    46  
    47  	// init other controller fields
    48  	controller.IntegrationControllerConfig = modConfig
    49  	return nil
    50  }
    51  
    52  // GetReconcileObject returns the kind of object being reconciled
    53  func (r Reconciler) GetReconcileObject() client.Object {
    54  	return &corev1.ConfigMap{}
    55  }
    56  
    57  func (r Reconciler) HandlePredicateEvent(cli client.Client, object client.Object) bool {
    58  	cm := corev1.ConfigMap{}
    59  	objectkey := client.ObjectKeyFromObject(object)
    60  	if err := cli.Get(context.TODO(), objectkey, &cm); err != nil {
    61  		return false
    62  	}
    63  	if cm.Labels == nil {
    64  		return false
    65  	}
    66  	evType, ok := cm.Labels[constants.VerrazzanoModuleEventLabel]
    67  	if !ok {
    68  		return false
    69  	}
    70  	return evType == string(event.IntegrateSingleRequestEvent)
    71  }