Skip to content

Commit

Permalink
Enable GW controller to trnsfer label from ManagedCluster to the GW +…
Browse files Browse the repository at this point in the history
… integration test

bump golangci-lint to 1.55.2
patch in e2e
  • Loading branch information
maksymvavilov authored and mikenairn committed Jan 17, 2024
1 parent a4f3e81 commit 02f61f5
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
go-version: v1.21.x
- uses: golangci/golangci-lint-action@v2
with:
version: v1.53.3
version: v1.55.2
only-new-issues: true
args: --timeout=5m
skip-go-installation: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
annotations:
alm-examples: '[]'
capabilities: Basic Install
createdAt: "2023-12-08T11:41:09Z"
createdAt: "2023-12-21T13:08:31Z"
operators.operatorframework.io/builder: operator-sdk-v1.28.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
name: multicluster-gateway-controller.v0.0.0
Expand Down Expand Up @@ -349,14 +349,6 @@ spec:
- get
- list
- watch
- apiGroups:
- cluster.open-cluster-management.io
resources:
- managedclusters
verbs:
- get
- list
- watch
- apiGroups:
- gateway.networking.k8s.io
resources:
Expand Down
8 changes: 0 additions & 8 deletions config/policy-controller/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ rules:
- get
- list
- watch
- apiGroups:
- cluster.open-cluster-management.io
resources:
- managedclusters
verbs:
- get
- list
- watch
- apiGroups:
- gateway.networking.k8s.io
resources:
Expand Down
1 change: 0 additions & 1 deletion pkg/controllers/dnspolicy/dnspolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ type DNSPolicyReconciler struct {
//+kubebuilder:rbac:groups=kuadrant.io,resources=dnspolicies,verbs=get;list;watch;update;patch;delete
//+kubebuilder:rbac:groups=kuadrant.io,resources=dnspolicies/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=kuadrant.io,resources=dnspolicies/finalizers,verbs=update
//+kubebuilder:rbac:groups=cluster.open-cluster-management.io,resources=managedclusters,verbs=get;list;watch
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get;list;watch;update;patch
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/finalizers,verbs=update
Expand Down
32 changes: 22 additions & 10 deletions pkg/controllers/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"reflect"
"strings"
"time"

clusterv1 "open-cluster-management.io/api/cluster/v1"
Expand Down Expand Up @@ -56,10 +57,11 @@ import (
)

const (
GatewayClusterLabelSelectorAnnotation = "kuadrant.io/gateway-cluster-label-selector"
GatewayClustersAnnotation = "kuadrant.io/gateway-clusters"
GatewayFinalizer = "kuadrant.io/gateway"
ManagedLabel = "kuadrant.io/managed"
LabelPrefix = "kuadrant.io/"
GatewayClusterLabelSelectorAnnotation = LabelPrefix + "gateway-cluster-label-selector"
GatewayClustersAnnotation = LabelPrefix + "gateway-clusters"
GatewayFinalizer = LabelPrefix + "gateway"
ManagedLabel = LabelPrefix + "managed"
)

type GatewayPlacer interface {
Expand Down Expand Up @@ -272,14 +274,24 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, reconcileErr
}

// 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 {
//ToDo Implement me !!

//Iterate clusters and for each find the ManagedCluster resource, get its labels and convert all "kuadrant.io/" labels to a cluster specific label and add it to the gateway
// (cluster kind-mgc-workload-1 label) kuadrant.io/lb-attribute-custom-weight=AWS = (gateway label) kuadrant.io/kind-mgc-workload-1_lb-attribute-custom-weight=AWS
// (cluster kind-mgc-workload-2 label) kuadrant.io/lb-attribute-geo-code=ES = (gateway label) kuadrant.io/kind-mgc-workload-2_lb-attribute-geo-code=ES
// etc ...
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
}
}
}
return nil
}

Expand Down
9 changes: 6 additions & 3 deletions test/e2e/gateway_single_spoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,9 @@ var _ = Describe("Gateway single target cluster", func() {
if gw.Spec.Listeners == nil {
gw.Spec.Listeners = []gatewayapiv1.Listener{}
}
patch := client.MergeFrom(gw.DeepCopy())
AddListener("wildcard", testHostnameWildcard, gatewayapiv1.ObjectName(testHostname), gw)
err = tconfig.HubClient().Update(ctx, gw)
err = tconfig.HubClient().Patch(ctx, gw, patch)
Expect(err).ToNot(HaveOccurred())
Eventually(func(g Gomega, ctx SpecContext) {
checkGateway := &gatewayapiv1.Gateway{}
Expand Down Expand Up @@ -431,9 +432,10 @@ var _ = Describe("Gateway single target cluster", func() {
if gw.Spec.Listeners == nil {
gw.Spec.Listeners = []gatewayapiv1.Listener{}
}
patch := client.MergeFrom(gw.DeepCopy())
AddListener("other", testHostnameOther, gatewayapiv1.ObjectName(testHostnameOther), gw)
Eventually(func(g Gomega, ctx SpecContext) {
err = tconfig.HubClient().Update(ctx, gw)
err = tconfig.HubClient().Patch(ctx, gw, patch)
Expect(err).ToNot(HaveOccurred())
checkGateway := &gatewayapiv1.Gateway{}
err = tconfig.HubClient().Get(ctx, client.ObjectKey{Name: testID, Namespace: tconfig.HubNamespace()}, checkGateway)
Expand All @@ -450,6 +452,7 @@ var _ = Describe("Gateway single target cluster", func() {
// remove the listener
err = tconfig.HubClient().Get(ctx, client.ObjectKey{Name: testID, Namespace: tconfig.HubNamespace()}, gw)
Expect(err).ToNot(HaveOccurred())
patch = client.MergeFrom(gw.DeepCopy())

if gw.Spec.Listeners == nil {
gw.Spec.Listeners = []gatewayapiv1.Listener{}
Expand All @@ -460,7 +463,7 @@ var _ = Describe("Gateway single target cluster", func() {
gw.Spec.Listeners = append(gw.Spec.Listeners[:i], gw.Spec.Listeners[i+1:]...)
}
}
err = tconfig.HubClient().Update(ctx, gw)
err = tconfig.HubClient().Patch(ctx, gw, patch)
Expect(err).ToNot(HaveOccurred())
Eventually(func(g Gomega, ctx SpecContext) {
secret := &corev1.Secret{}
Expand Down
189 changes: 189 additions & 0 deletions test/policy_integration/dnspolicy_controller_multi_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"

"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
mgcgateway "github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/gateway"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/utils"
testutil "github.com/Kuadrant/multicluster-gateway-controller/test/util"
Expand Down Expand Up @@ -462,6 +463,194 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {

})

Context("custom geo+weighted", func() {

BeforeEach(func() {
dnsPolicyBuilder.
WithLoadBalancingWeightedFor(120, []*v1alpha1.CustomWeight{
{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"kuadrant.io/lb-attribute-custom-weight": "CAD",
},
},
Weight: 100,
},
{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"kuadrant.io/lb-attribute-custom-weight": "ES",
},
},
Weight: 160,
},
}).
WithLoadBalancingGeoFor("IE")
dnsPolicy = dnsPolicyBuilder.DNSPolicy
Expect(k8sClient.Create(ctx, dnsPolicy)).To(Succeed())

Eventually(func() error {
gateway.Labels = map[string]string{}
gateway.Labels[mgcgateway.LabelPrefix+TestClusterNameOne+"_lb-attribute-custom-weight"] = "CAD"
gateway.Labels[mgcgateway.LabelPrefix+TestClusterNameTwo+"_lb-attribute-custom-weight"] = "ES"
gateway.Labels[mgcgateway.LabelPrefix+TestClusterNameOne+"_lb-attribute-geo-code"] = "CAD"
gateway.Labels[mgcgateway.LabelPrefix+TestClusterNameTwo+"_lb-attribute-geo-code"] = "ES"
return k8sClient.Update(ctx, gateway)
}, TestTimeoutMedium, TestRetryIntervalMedium).ShouldNot(HaveOccurred())
})

It("should create dns records", func() {
Eventually(func(g Gomega, ctx context.Context) {
recordList := &v1alpha1.DNSRecordList{}
err := k8sClient.List(ctx, recordList, &client.ListOptions{Namespace: testNamespace})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(recordList.Items).To(HaveLen(2))
g.Expect(recordList.Items).To(
ContainElements(
MatchFields(IgnoreExtras, Fields{
"ObjectMeta": HaveField("Name", recordName),
"Spec": MatchFields(IgnoreExtras, Fields{
"ManagedZoneRef": HaveField("Name", "mz-example-com"),
"Endpoints": ConsistOf(
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("2w705o.lb-" + lbHash + ".test.example.com"),
"Targets": ConsistOf(TestIPAddressTwo),
"RecordType": Equal("A"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(v1alpha1.TTL(60)),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("es.lb-" + lbHash + ".test.example.com"),
"Targets": ConsistOf("2w705o.lb-" + lbHash + ".test.example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("2w705o.lb-" + lbHash + ".test.example.com"),
"RecordTTL": Equal(v1alpha1.TTL(60)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "weight", Value: "160"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("cad.lb-" + lbHash + ".test.example.com"),
"Targets": ConsistOf("s07c46.lb-" + lbHash + ".test.example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("s07c46.lb-" + lbHash + ".test.example.com"),
"RecordTTL": Equal(v1alpha1.TTL(60)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "weight", Value: "100"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("s07c46.lb-" + lbHash + ".test.example.com"),
"Targets": ConsistOf(TestIPAddressOne),
"RecordType": Equal("A"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(v1alpha1.TTL(60)),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("lb-" + lbHash + ".test.example.com"),
"Targets": ConsistOf("es.lb-" + lbHash + ".test.example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("ES"),
"RecordTTL": Equal(v1alpha1.TTL(300)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "geo-code", Value: "ES"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("lb-" + lbHash + ".test.example.com"),
"Targets": ConsistOf("cad.lb-" + lbHash + ".test.example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("CAD"),
"RecordTTL": Equal(v1alpha1.TTL(300)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "geo-code", Value: "CAD"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("lb-" + lbHash + ".test.example.com"),
"Targets": ConsistOf("cad.lb-" + lbHash + ".test.example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("default"),
"RecordTTL": Equal(v1alpha1.TTL(300)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "geo-code", Value: "*"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal(TestHostOne),
"Targets": ConsistOf("lb-" + lbHash + ".test.example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(v1alpha1.TTL(300)),
})),
),
}),
}),
MatchFields(IgnoreExtras, Fields{
"ObjectMeta": HaveField("Name", wildcardRecordName),
"Spec": MatchFields(IgnoreExtras, Fields{
"ManagedZoneRef": HaveField("Name", "mz-example-com"),
"Endpoints": ConsistOf(
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("2w705o.lb-" + lbHash + ".example.com"),
"Targets": ConsistOf(TestIPAddressTwo),
"RecordType": Equal("A"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(v1alpha1.TTL(60)),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("es.lb-" + lbHash + ".example.com"),
"Targets": ConsistOf("2w705o.lb-" + lbHash + ".example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("2w705o.lb-" + lbHash + ".example.com"),
"RecordTTL": Equal(v1alpha1.TTL(60)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "weight", Value: "160"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("cad.lb-" + lbHash + ".example.com"),
"Targets": ConsistOf("s07c46.lb-" + lbHash + ".example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("s07c46.lb-" + lbHash + ".example.com"),
"RecordTTL": Equal(v1alpha1.TTL(60)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "weight", Value: "100"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("s07c46.lb-" + lbHash + ".example.com"),
"Targets": ConsistOf(TestIPAddressOne),
"RecordType": Equal("A"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(v1alpha1.TTL(60)),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("lb-" + lbHash + ".example.com"),
"Targets": ConsistOf("cad.lb-" + lbHash + ".example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("CAD"),
"RecordTTL": Equal(v1alpha1.TTL(300)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "geo-code", Value: "CAD"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("lb-" + lbHash + ".example.com"),
"Targets": ConsistOf("es.lb-" + lbHash + ".example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("ES"),
"RecordTTL": Equal(v1alpha1.TTL(300)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "geo-code", Value: "ES"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal("lb-" + lbHash + ".example.com"),
"Targets": ConsistOf("cad.lb-" + lbHash + ".example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal("default"),
"RecordTTL": Equal(v1alpha1.TTL(300)),
"ProviderSpecific": Equal(v1alpha1.ProviderSpecific{{Name: "geo-code", Value: "*"}}),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"DNSName": Equal(TestHostWildcard),
"Targets": ConsistOf("lb-" + lbHash + ".example.com"),
"RecordType": Equal("CNAME"),
"SetIdentifier": Equal(""),
"RecordTTL": Equal(v1alpha1.TTL(300)),
})),
),
}),
}),
))
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})

})

})

})

0 comments on commit 02f61f5

Please sign in to comment.