Skip to content

Commit

Permalink
chore: log when both upstream configs present
Browse files Browse the repository at this point in the history
Log a warning when a Service has both KongIngress and KongUpstreamPolicy
attached.
  • Loading branch information
rainest committed Oct 30, 2023
1 parent c6ecbf9 commit 3d82093
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions internal/dataplane/kongstate/kongstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/failures"
"github.com/kong/kubernetes-ingress-controller/v3/internal/store"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1"
)

// KongState holds the configuration that should be applied to Kong.
Expand Down Expand Up @@ -203,10 +204,14 @@ func (ks *KongState) FillOverrides(
}
}

ks.FillUpstreamOverrides(s, failuresCollector)
ks.FillUpstreamOverrides(s, logger, failuresCollector)
}

func (ks *KongState) FillUpstreamOverrides(s store.Storer, failuresCollector *failures.ResourceFailuresCollector) {
func (ks *KongState) FillUpstreamOverrides(
s store.Storer,
logger logr.Logger,
failuresCollector *failures.ResourceFailuresCollector,
) {
for i := 0; i < len(ks.Upstreams); i++ {
servicesGroup := lo.Values(ks.Upstreams[i].Service.K8sServices)
servicesAsObjects := func(svc *corev1.Service, _ int) client.Object { return svc }
Expand All @@ -225,6 +230,18 @@ func (ks *KongState) FillUpstreamOverrides(s store.Storer, failuresCollector *fa
failuresCollector.PushResourceFailure(err.Error(), lo.Map(servicesGroup, servicesAsObjects)...)
} else {
if kongUpstreamPolicy != nil {
if kongIngress != nil {
for _, svc := range servicesGroup {
logger.Error(nil,
fmt.Sprintf("Service uses both %s and %s annotations, should use only %s annotation. Settings "+
"from %s will take precedence",
annotations.AnnotationPrefix+annotations.ConfigurationKey,
annotations.AnnotationPrefix+kongv1beta1.KongUpstreamPolicyAnnotationKey,
annotations.AnnotationPrefix+kongv1beta1.KongUpstreamPolicyAnnotationKey,
annotations.AnnotationPrefix+kongv1beta1.KongUpstreamPolicyAnnotationKey),
"namespace", svc.Namespace, "name", svc.Name)
}
}
ks.Upstreams[i].overrideByKongUpstreamPolicy(kongUpstreamPolicy)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/dataplane/kongstate/kongstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ func TestKongState_FillUpstreamOverrides(t *testing.T) {
failuresCollector := failures.NewResourceFailuresCollector(logr.Discard())

kongState := KongState{Upstreams: []Upstream{tc.upstream}}
kongState.FillUpstreamOverrides(s, failuresCollector)
kongState.FillUpstreamOverrides(s, logr.Discard(), failuresCollector)
require.Equal(t, tc.expectedUpstream, kongState.Upstreams[0].Upstream)
require.ElementsMatch(t, tc.expectedFailures, failuresCollector.PopResourceFailures())
})
Expand Down

0 comments on commit 3d82093

Please sign in to comment.