github.com/oam-dev/cluster-gateway@v1.9.0/pkg/event/clustergatewayconfiguration_handler.go (about)

     1  package event
     2  
     3  import (
     4  	"context"
     5  
     6  	"k8s.io/apimachinery/pkg/types"
     7  	"k8s.io/client-go/util/workqueue"
     8  	addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
     9  	ctrl "sigs.k8s.io/controller-runtime"
    10  	"sigs.k8s.io/controller-runtime/pkg/client"
    11  	"sigs.k8s.io/controller-runtime/pkg/event"
    12  	"sigs.k8s.io/controller-runtime/pkg/handler"
    13  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    14  
    15  	proxyv1alpha1 "github.com/oam-dev/cluster-gateway/pkg/apis/proxy/v1alpha1"
    16  	"github.com/oam-dev/cluster-gateway/pkg/common"
    17  )
    18  
    19  var _ handler.EventHandler = &ClusterGatewayConfigurationHandler{}
    20  
    21  type ClusterGatewayConfigurationHandler struct {
    22  	client.Client
    23  }
    24  
    25  func (c *ClusterGatewayConfigurationHandler) Create(ctx context.Context, event event.CreateEvent, q workqueue.RateLimitingInterface) {
    26  	cfg := event.Object.(*proxyv1alpha1.ClusterGatewayConfiguration)
    27  	c.process(ctx, cfg, q)
    28  }
    29  
    30  func (c *ClusterGatewayConfigurationHandler) Update(ctx context.Context, event event.UpdateEvent, q workqueue.RateLimitingInterface) {
    31  	cfg := event.ObjectNew.(*proxyv1alpha1.ClusterGatewayConfiguration)
    32  	c.process(ctx, cfg, q)
    33  }
    34  
    35  func (c *ClusterGatewayConfigurationHandler) Delete(ctx context.Context, event event.DeleteEvent, q workqueue.RateLimitingInterface) {
    36  	cfg := event.Object.(*proxyv1alpha1.ClusterGatewayConfiguration)
    37  	c.process(ctx, cfg, q)
    38  }
    39  
    40  func (c *ClusterGatewayConfigurationHandler) Generic(ctx context.Context, event event.GenericEvent, q workqueue.RateLimitingInterface) {
    41  	cfg := event.Object.(*proxyv1alpha1.ClusterGatewayConfiguration)
    42  	c.process(ctx, cfg, q)
    43  }
    44  
    45  func (c *ClusterGatewayConfigurationHandler) process(ctx context.Context, config *proxyv1alpha1.ClusterGatewayConfiguration, q workqueue.RateLimitingInterface) {
    46  	list := addonv1alpha1.ClusterManagementAddOnList{}
    47  
    48  	if err := c.Client.List(ctx, &list); err != nil {
    49  		ctrl.Log.WithName("ClusterGatewayConfiguration").Error(err, "failed list addons")
    50  		return
    51  	}
    52  
    53  	for _, addon := range list.Items {
    54  		if addon.Spec.AddOnConfiguration.CRDName != common.ClusterGatewayConfigurationCRDName {
    55  			continue
    56  		}
    57  		if addon.Spec.AddOnConfiguration.CRName == config.Name {
    58  			q.Add(reconcile.Request{
    59  				NamespacedName: types.NamespacedName{
    60  					Name: addon.Name,
    61  				},
    62  			})
    63  		}
    64  	}
    65  
    66  }