Skip to content

Commit

Permalink
tests: add KongServiceFacade integration test (#5264)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakub Warczarek <jakub.warczarek@konghq.com>
  • Loading branch information
czeslavo and programmer04 authored Dec 4, 2023
1 parent c12e540 commit 96941c3
Show file tree
Hide file tree
Showing 25 changed files with 824 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ generate.clientsets: client-gen
--logtostderr \
--clientset-name clientset \
--input-base $(REPO_URL)/$(GO_MOD_MAJOR_VERSION)/pkg/apis/ \
--input configuration/v1,configuration/v1beta1,configuration/v1alpha1 \
--input-dirs $(REPO_URL)/pkg/apis/configuration/v1alpha1/,$(REPO_URL)/pkg/apis/configuration/v1beta1/,$(REPO_URL)/pkg/apis/configuration/v1/ \
--input configuration/v1,configuration/v1beta1,configuration/v1alpha1,incubator/v1alpha1 \
--input-dirs $(REPO_URL)/pkg/apis/configuration/v1alpha1/,$(REPO_URL)/pkg/apis/configuration/v1beta1/,$(REPO_URL)/pkg/apis/configuration/v1/,$(REPO_URL)/pkg/apis/incubator/v1alpha1 \
--output-base pkg/ \
--output-package $(REPO_URL)/$(GO_MOD_MAJOR_VERSION)/pkg/ \
--trim-path-prefix pkg/$(REPO_URL)/$(GO_MOD_MAJOR_VERSION)/
Expand Down
2 changes: 1 addition & 1 deletion config/dev/manager_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
containers:
- name: ingress-controller
args:
- --feature-gates=GatewayAlpha=true
- --feature-gates=GatewayAlpha=true,KongServiceFacade=true
- --anonymous-reports=false
env:
- name: CONTROLLER_LOG_LEVEL
Expand Down
4 changes: 4 additions & 0 deletions internal/dataplane/translator/ingressrules.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ func resolveKubernetesServiceForBackend(storer store.Storer, ingressNamespace st

// Merge the annotations from the KongServiceFacade with the annotations from the Service.
// KongServiceFacade overrides the Service annotations if they have the same key.
// We make a copy of the Kubernetes Service to avoid mutating the cache (the k8sService we
// get from the storer is a pointer).
k8sService = k8sService.DeepCopy()

for k, v := range serviceFacadeAnnotations {
if k8sService.Annotations == nil {
k8sService.Annotations = make(map[string]string)
Expand Down
47 changes: 47 additions & 0 deletions internal/dataplane/translator/ingressrules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,3 +820,50 @@ func TestResolveKubernetesServiceForBackend(t *testing.T) {
})
}
}

func TestResolveKubernetesServiceForBackend_DoesNotModifyCache(t *testing.T) {
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service",
Namespace: "test-namespace",
Annotations: map[string]string{
"service": "from-service",
},
},
}
// Preserve a copy to compare against later.
svcCopy := svc.DeepCopy()

kongServiceFacade := &incubatorv1alpha1.KongServiceFacade{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service-facade",
Namespace: "test-namespace",
Annotations: map[string]string{
"facade": "from-facade",
},
},
Spec: incubatorv1alpha1.KongServiceFacadeSpec{
Backend: incubatorv1alpha1.KongServiceFacadeBackend{
Name: "test-service",
Port: 80,
},
},
}
fakeStore := lo.Must(store.NewFakeStore(store.FakeObjects{
Services: []*corev1.Service{svc},
KongServiceFacades: []*incubatorv1alpha1.KongServiceFacade{kongServiceFacade},
}))
backend := builder.NewKongstateServiceBackend("test-service-facade").
WithNamespace("test-namespace").
WithPortNumber(80).
WithType(kongstate.ServiceBackendTypeKongServiceFacade).
Build()

resolvedService, err := resolveKubernetesServiceForBackend(fakeStore, "test-namespace", backend)
require.NoError(t, err)
require.Equal(t, svcCopy, svc, "service stored in cache should not be modified")
require.Equal(t, resolvedService.Annotations, map[string]string{
"service": "from-service",
"facade": "from-facade",
}, "annotations should be merged in the returned service")
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ kind: KongServiceFacade
metadata:
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugin: auth-alpha
konghq.com/plugins: auth-alpha
name: svc-facade-alpha
namespace: default
spec:
Expand Down
11 changes: 11 additions & 0 deletions internal/util/builder/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ func (b *IngressBuilder) WithNamespace(namespace string) *IngressBuilder {
b.ingress.ObjectMeta.Namespace = namespace
return b
}

func (b *IngressBuilder) WithAnnotations(annotations map[string]string) *IngressBuilder {
if b.ingress.Annotations == nil {
b.ingress.Annotations = annotations
return b
}
for k, v := range annotations {
b.ingress.Annotations[k] = v
}
return b
}
4 changes: 4 additions & 0 deletions internal/util/test/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func DeployCRDsForCluster(ctx context.Context, cluster clusters.Cluster) error {
if err := clusters.KustomizeDeployForCluster(ctx, cluster, kongCRDsKustomize); err != nil {
return err
}
fmt.Printf("INFO: deploying Kong incubator CRDs to cluster\n")
if err := clusters.KustomizeDeployForCluster(ctx, cluster, kongIncubatorCRDsKustomize); err != nil {
return err
}

fmt.Printf("INFO: deploying Gateway CRDs to cluster\n")
if err := clusters.KustomizeDeployForCluster(ctx, cluster, consts.GatewayExperimentalCRDsKustomizeURL); err != nil {
Expand Down
15 changes: 11 additions & 4 deletions internal/util/test/kustomize_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ import (
)

var (
kongRBACsKustomize = initKongRBACsKustomizePath()
kongGatewayRBACsKustomize = initKongGatewayRBACsKustomizePath()
kongCRDsRBACsKustomize = initKongCRDsRBACsKustomizePath()
kongCRDsKustomize = initCRDsKustomizePath()
kongRBACsKustomize = initKongRBACsKustomizePath()
kongGatewayRBACsKustomize = initKongGatewayRBACsKustomizePath()
kongCRDsRBACsKustomize = initKongCRDsRBACsKustomizePath()
kongCRDsKustomize = initCRDsKustomizePath()
kongIncubatorCRDsKustomize = initIncubatorCRDsKustomizePath()
)

func initIncubatorCRDsKustomizePath() string {
dir := filepath.Join(lo.Must(getRepoRoot()), "config/crd/incubator")
ensureDirExists(dir)
return dir
}

func initKongRBACsKustomizePath() string {
dir := filepath.Join(lo.Must(getRepoRoot()), "config/rbac/")
ensureDirExists(dir)
Expand Down
13 changes: 13 additions & 0 deletions pkg/clientset/clientset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/clientset/fake/clientset_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/clientset/fake/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/clientset/scheme/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions pkg/clientset/typed/incubator/v1alpha1/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions pkg/clientset/typed/incubator/v1alpha1/fake/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 96941c3

Please sign in to comment.