Skip to content

Commit

Permalink
Move ClusterEventMapper back into gateway package. No longer relevant to
Browse files Browse the repository at this point in the history
policies, maps cluster events to gateways.

Use "clusters.kuadrant.io" for all cluster labels on the target gateway
and remove all existing cluster labels during reconciliation in order to
remove labels removed from the cluster resource.
  • Loading branch information
mikenairn committed Jan 22, 2024
1 parent 1ff95d2 commit 75f5e95
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package events
package gateway

import (
"context"
Expand Down Expand Up @@ -50,13 +50,13 @@ func (m *ClusterEventMapper) mapToGatewayRequest(ctx context.Context, obj client
allGwList := &gatewayapiv1.GatewayList{}
err := m.Client.List(ctx, allGwList)
if err != nil {
logger.Info("mapToPolicyRequest:", "error", "failed to get gateways")
logger.Info("mapToGatewayRequest:", "error", "failed to get gateways")
return []reconcile.Request{}
}

requests := make([]reconcile.Request, 0)
for _, gw := range allGwList.Items {
val := metadata.GetAnnotation(&gw, "kuadrant.io/gateway-clusters")
val := metadata.GetAnnotation(&gw, GatewayClustersAnnotation)
if val == "" {
continue
}
Expand Down
22 changes: 14 additions & 8 deletions pkg/controllers/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ import (
"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/gracePeriod"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/metadata"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/slice"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/events"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/policysync"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/utils"
)

const (
LabelPrefix = "kuadrant.io/"
ClustersLabelPrefix = "clusters." + LabelPrefix
GatewayClusterLabelSelectorAnnotation = LabelPrefix + "gateway-cluster-label-selector"
GatewayClustersAnnotation = LabelPrefix + "gateway-clusters"
GatewayFinalizer = LabelPrefix + "gateway"
Expand Down Expand Up @@ -276,20 +276,26 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// reconcileClusterLabels fetches labels from ManagedCluster related to clusters array and adds them to the provided Gateway
func (r *GatewayReconciler) reconcileClusterLabels(ctx context.Context, gateway *gatewayapiv1.Gateway, clusters []string) error {
//Remove all existing clusters.kuadrant.io labels
for key := range gateway.Labels {
if strings.HasPrefix(key, ClustersLabelPrefix) {
delete(gateway.Labels, key)
}
}

//Add clusters.kuadrant.io labels for current clusters
for _, cluster := range clusters {
managedCluster := &clusterv1.ManagedCluster{}
if err := r.Client.Get(ctx, client.ObjectKey{Name: cluster}, managedCluster); client.IgnoreNotFound(err) != nil {
return err
}

for key, value := range managedCluster.Labels {
if strings.Contains(key, LabelPrefix) {
_, attribute, found := strings.Cut(key, "/")
if !found {
continue
}
gateway.Labels[LabelPrefix+cluster+"_"+attribute] = value
attribute, found := strings.CutPrefix(key, LabelPrefix)
if !found {
continue
}
gateway.Labels[ClustersLabelPrefix+cluster+"_"+attribute] = value
}
}
return nil
Expand Down Expand Up @@ -506,7 +512,7 @@ func buildAcceptedCondition(generation int64, acceptedStatus metav1.ConditionSta
// SetupWithManager sets up the controller with the Manager.
func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager, ctx context.Context) error {
log := crlog.FromContext(ctx)
clusterEventMapper := events.NewClusterEventMapper(log, mgr.GetClient())
clusterEventMapper := NewClusterEventMapper(log, mgr.GetClient())
//TODO need to trigger gateway reconcile when gatewayclass params changes
return ctrl.NewControllerManagedBy(mgr).
For(&gatewayapiv1.Gateway{}).
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/gateway_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (g *GatewayWrapper) GetClusterGatewayLabels(clusterName string) map[string]

labels := map[string]string{}
for k, v := range g.GetLabels() {
attr, found := strings.CutPrefix(k, "kuadrant.io/"+clusterName+"_")
attr, found := strings.CutPrefix(k, "clusters.kuadrant.io/"+clusterName+"_")
if found {
labels["kuadrant.io/"+attr] = v
} else {
Expand Down

0 comments on commit 75f5e95

Please sign in to comment.