Skip to content

Commit

Permalink
fix: do not translate licenses if Kong gateway is not enterprise edit…
Browse files Browse the repository at this point in the history
…ion (#5640)

* fix: do not translate licenses if Kong gateway is not enterprise edition

* add changelog

* Update internal/dataplane/translator/translator.go

Co-authored-by: Jintao Zhang <zhangjintao9020@gmail.com>

---------

Co-authored-by: Jintao Zhang <zhangjintao9020@gmail.com>
  • Loading branch information
randmonkey and tao12345666333 committed Feb 20, 2024
1 parent 5686924 commit 6e1dcfe
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ Adding a new version? You'll need three changes:
- [0.0.5](#005)
- [0.0.4 and prior](#004-and-prior)

## Unreleased

### Fixed

- When managed Kong gateways are OSS edition, KIC will not apply licenses to
the Kong gateway instances to avoid invalid configurations.
[#5640](https://github.com/Kong/kubernetes-ingress-controller/pull/5640)

## [3.1.0]

> Release date: 2024-02-07
Expand Down
7 changes: 6 additions & 1 deletion internal/dataplane/translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type FeatureFlags struct {
// ExpressionRoutes indicates whether to translate Kubernetes objects to expression based Kong Routes.
ExpressionRoutes bool

// EnterpriseEdition indicates whether to translate objects that are only available in the Kong enterprise edition.
EnterpriseEdition bool

// FillIDs enables the translator to fill in the IDs fields of Kong entities - Services, Routes, and Consumers - based
// on their names. It ensures that IDs remain stable across restarts of the controller.
FillIDs bool
Expand All @@ -66,10 +69,12 @@ func NewFeatureFlags(
featureGates featuregates.FeatureGates,
routerFlavor dpconf.RouterFlavor,
updateStatusFlag bool,
enterpriseEdition bool,
) FeatureFlags {
return FeatureFlags{
ReportConfiguredKubernetesObjects: updateStatusFlag,
ExpressionRoutes: dpconf.ShouldEnableExpressionRoutes(routerFlavor),
EnterpriseEdition: enterpriseEdition,
FillIDs: featureGates.Enabled(featuregates.FillIDsFeature),
RewriteURIs: featureGates.Enabled(featuregates.RewriteURIsFeature),
KongServiceFacade: featureGates.Enabled(featuregates.KongServiceFacade),
Expand Down Expand Up @@ -210,7 +215,7 @@ func (t *Translator) BuildKongConfig() KongConfigBuildingResult {
// populate CA certificates in Kong
result.CACertificates = t.getCACerts()

if t.licenseGetter != nil {
if t.licenseGetter != nil && t.featureFlags.EnterpriseEdition {
optionalLicense := t.licenseGetter.GetLicense()
if l, ok := optionalLicense.Get(); ok {
result.Licenses = append(result.Licenses, kongstate.License{License: l})
Expand Down
36 changes: 31 additions & 5 deletions internal/dataplane/translator/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/internal/annotations"
dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config"
"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/kongstate"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/featuregates"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/scheme"
"github.com/kong/kubernetes-ingress-controller/v3/internal/store"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
Expand Down Expand Up @@ -4671,9 +4672,10 @@ func TestNewFeatureFlags(t *testing.T) {
testCases := []struct {
name string

featureGates map[string]bool
routerFlavor dpconf.RouterFlavor
updateStatusFlag bool
featureGates map[string]bool
routerFlavor dpconf.RouterFlavor
updateStatusFlag bool
enterpriseEdition bool

expectedFeatureFlags FeatureFlags
}{
Expand All @@ -4694,11 +4696,22 @@ func TestNewFeatureFlags(t *testing.T) {
ExpressionRoutes: true,
},
},
{
name: "ServiceFacade enabled and enterprise edition",
featureGates: map[string]bool{
featuregates.KongServiceFacade: true,
},
enterpriseEdition: true,
expectedFeatureFlags: FeatureFlags{
EnterpriseEdition: true,
KongServiceFacade: true,
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualFlags := NewFeatureFlags(tc.featureGates, tc.routerFlavor, tc.updateStatusFlag)
actualFlags := NewFeatureFlags(tc.featureGates, tc.routerFlavor, tc.updateStatusFlag, tc.enterpriseEdition)

require.Equal(t, tc.expectedFeatureFlags, actualFlags)
})
Expand All @@ -4716,7 +4729,7 @@ func (m *mockLicenseGetter) GetLicense() mo.Option[kong.License] {
func TestTranslator_License(t *testing.T) {
s, _ := store.NewFakeStore(store.FakeObjects{})
p := mustNewTranslator(t, s)

p.featureFlags.EnterpriseEdition = true
t.Run("no license is populated by default", func(t *testing.T) {
result := p.BuildKongConfig()
require.Empty(t, result.KongState.Licenses)
Expand All @@ -4742,6 +4755,19 @@ func TestTranslator_License(t *testing.T) {
require.Equal(t, "license-id", *license.ID)
require.Equal(t, "license-payload", *license.Payload)
})

t.Run("no license is populated when license getter returns a license but enterprise edition is false", func(t *testing.T) {
p.featureFlags.EnterpriseEdition = false
licenseGetterWithLicense := &mockLicenseGetter{
license: mo.Some(kong.License{
ID: lo.ToPtr("license-id"),
Payload: lo.ToPtr("license-payload"),
}),
}
p.InjectLicenseGetter(licenseGetterWithLicense)
result := p.BuildKongConfig()
require.Empty(t, result.KongState.Licenses)
})
}

func TestTranslator_ConfiguredKubernetesObjects(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func Run(
featureGates,
routerFlavor,
c.UpdateStatus,
kongStartUpConfig.Version.IsKongGatewayEnterprise(),
)

referenceIndexers := ctrlref.NewCacheIndexers(setupLog.WithName("reference-indexers"))
Expand Down

0 comments on commit 6e1dcfe

Please sign in to comment.