-
Notifications
You must be signed in to change notification settings - Fork 618
Refactor and add tests for injections #13086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidjumani
wants to merge
3
commits into
main
Choose a base branch
from
add-injection-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package translator | ||
|
|
||
| import ( | ||
| "istio.io/istio/pkg/kube/krt" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| gwv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
| ) | ||
|
|
||
| type GatewayTransformationFunction func(GatewayCollectionConfig) func(ctx krt.HandlerContext, obj *gwv1.Gateway) (*gwv1.GatewayStatus, []*GatewayListener) | ||
|
|
||
| type GatewayCollectionConfigOption func(o *GatewayCollectionConfig) | ||
|
|
||
| func WithGatewayTransformationFunc(f GatewayTransformationFunction) GatewayCollectionConfigOption { | ||
| return func(o *GatewayCollectionConfig) { | ||
| if f != nil { | ||
| o.transformationFunc = f | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func processGatewayCollectionOptions(cfg *GatewayCollectionConfig, opts ...GatewayCollectionConfigOption) { | ||
| cfg.listenerIndex = krt.NewIndex(cfg.ListenerSets, "gatewayParent", func(o ListenerSet) []types.NamespacedName { | ||
| return []types.NamespacedName{o.GatewayParent} | ||
| }) | ||
| cfg.transformationFunc = GatewayTransformationFunc | ||
| for _, fn := range opts { | ||
| fn(cfg) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package translator | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "istio.io/istio/pkg/kube/krt" | ||
| corev1 "k8s.io/api/core/v1" | ||
| gwv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
|
|
||
| "github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk/krtutil" | ||
| ) | ||
|
|
||
| func TestDefaultGatewayCollectionOptions(t *testing.T) { | ||
| cfg := getConfig(t) | ||
| processGatewayCollectionOptions(&cfg) | ||
|
|
||
| assert.NotNil(t, cfg.listenerIndex) | ||
| assert.NotNil(t, cfg.transformationFunc) | ||
| } | ||
|
|
||
| func TestWithGatewayTransformationFunc(t *testing.T) { | ||
| called := false | ||
| customTransformationFunc := func(cfg GatewayCollectionConfig) func(ctx krt.HandlerContext, obj *gwv1.Gateway) (*gwv1.GatewayStatus, []*GatewayListener) { | ||
| called = true | ||
| return func(ctx krt.HandlerContext, obj *gwv1.Gateway) (*gwv1.GatewayStatus, []*GatewayListener) { | ||
| return nil, nil | ||
| } | ||
| } | ||
|
|
||
| GatewayCollection(getConfig(t), WithGatewayTransformationFunc(customTransformationFunc)) | ||
| assert.True(t, called) | ||
| } | ||
|
|
||
| func getConfig(t *testing.T) GatewayCollectionConfig { | ||
| opts := krtutil.NewKrtOptions(t.Context().Done(), nil) | ||
| return GatewayCollectionConfig{ | ||
| ControllerName: "random-name", | ||
| Gateways: krt.NewStaticCollection[*gwv1.Gateway](nil, nil, opts.ToOptions("Gateways")...), | ||
| ListenerSets: krt.NewStaticCollection[ListenerSet](nil, nil, opts.ToOptions("ListenerSets")...), | ||
| GatewayClasses: krt.NewStaticCollection[GatewayClass](nil, nil, opts.ToOptions("GatewayClasses")...), | ||
| Namespaces: krt.NewStaticCollection[*corev1.Namespace](nil, nil, opts.ToOptions("Namespaces")...), | ||
| Grants: ReferenceGrants{ | ||
| collection: krt.NewStaticCollection[ReferenceGrant](nil, nil, opts.ToOptions("Grants")...), | ||
| index: nil, | ||
| }, | ||
| Secrets: krt.NewStaticCollection[*corev1.Secret](nil, nil, opts.ToOptions("Secrets")...), | ||
| ConfigMaps: krt.NewStaticCollection[*corev1.ConfigMap](nil, nil, opts.ToOptions("ConfigMaps")...), | ||
| KrtOpts: opts, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package proxy_syncer | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk" | ||
| "github.com/kgateway-dev/kgateway/v2/pkg/reports" | ||
| ) | ||
|
|
||
| func TestWithCustomStatusSync(t *testing.T) { | ||
| customStatusSync := func(ctx context.Context, rm reports.ReportMap) {} | ||
| statusSyncer := NewStatusSyncer(nil, pluginsdk.Plugin{}, "controller-name", "agw-controller-name", nil, nil, nil, nil, nil, | ||
| WithCustomStatusSync(customStatusSync)) | ||
|
|
||
| assert.NotNil(t, statusSyncer.customStatusSync) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package krtcollections | ||
|
|
||
| import ( | ||
| "istio.io/istio/pkg/kube/krt" | ||
| gwv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
| gwxv1a1 "sigs.k8s.io/gateway-api/apisx/v1alpha1" | ||
|
|
||
| "github.com/kgateway-dev/kgateway/v2/pkg/kgateway/wellknown" | ||
| "github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk/ir" | ||
| krtpkg "github.com/kgateway-dev/kgateway/v2/pkg/utils/krtutil" | ||
| ) | ||
|
|
||
| type ( | ||
| GatewaysForDeployerTransformationFunction func(config *GatewayIndexConfig) func(kctx krt.HandlerContext, gw *gwv1.Gateway) *ir.GatewayForDeployer | ||
| GatewaysForEnvoyTransformationFunction func(config *GatewayIndexConfig) func(kctx krt.HandlerContext, gw *gwv1.Gateway) *ir.Gateway | ||
| ) | ||
|
|
||
| type GatewayIndexConfigOption func(o *GatewayIndexConfig) | ||
|
|
||
| func WithGatewayForDeployerTransformationFunc(f GatewaysForDeployerTransformationFunction) GatewayIndexConfigOption { | ||
| return func(o *GatewayIndexConfig) { | ||
| if f != nil { | ||
| o.gatewaysForDeployerTransformationFunc = f | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func WithGatewayForEnvoyTransformationFunc(f GatewaysForEnvoyTransformationFunction) GatewayIndexConfigOption { | ||
| return func(o *GatewayIndexConfig) { | ||
| if f != nil { | ||
| o.gatewaysForEnvoyTransformationFunc = f | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func processGatewayIndexConfig(config *GatewayIndexConfig, opts ...GatewayIndexConfigOption) { | ||
| config.byParentRefIndex = krtpkg.UnnamedIndex(config.ListenerSets, func(in *gwxv1a1.XListenerSet) []TargetRefIndexKey { | ||
| pRef := in.Spec.ParentRef | ||
| ns := strOr(pRef.Namespace, "") | ||
| if ns == "" { | ||
| ns = in.GetNamespace() | ||
| } | ||
| // lookup by the root object | ||
| return []TargetRefIndexKey{{ | ||
| Group: wellknown.GatewayGroup, | ||
| Kind: wellknown.GatewayKind, | ||
| Name: string(pRef.Name), | ||
| Namespace: ns, | ||
| // this index intentionally doesn't include sectionName | ||
| }} | ||
| }) | ||
|
|
||
| config.gatewaysForDeployerTransformationFunc = GatewaysForDeployerTransformationFunc | ||
| config.gatewaysForEnvoyTransformationFunc = GatewaysForEnvoyTransformationFunc | ||
|
|
||
| for _, fn := range opts { | ||
| fn(config) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package krtcollections | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "istio.io/istio/pkg/kube/krt" | ||
| "istio.io/istio/pkg/util/smallset" | ||
| gwv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
| gwxv1a1 "sigs.k8s.io/gateway-api/apisx/v1alpha1" | ||
|
|
||
| "github.com/kgateway-dev/kgateway/v2/api/settings" | ||
| "github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk/ir" | ||
| "github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk/krtutil" | ||
| ) | ||
|
|
||
| func TestDefaultGatewayCollectionOptions(t *testing.T) { | ||
| cfg := getConfig(t) | ||
| processGatewayIndexConfig(&cfg) | ||
|
|
||
| assert.NotNil(t, cfg.byParentRefIndex) | ||
| assert.NotNil(t, cfg.gatewaysForDeployerTransformationFunc) | ||
| assert.NotNil(t, cfg.gatewaysForEnvoyTransformationFunc) | ||
| } | ||
|
|
||
| func TestWithGatewayForDeployerTransformationFunc(t *testing.T) { | ||
| called := false | ||
| customTransformationFunc := func(config *GatewayIndexConfig) func(kctx krt.HandlerContext, gw *gwv1.Gateway) *ir.GatewayForDeployer { | ||
| called = true | ||
| return func(kctx krt.HandlerContext, gw *gwv1.Gateway) *ir.GatewayForDeployer { | ||
| return nil | ||
| } | ||
| } | ||
| cfg := getConfig(t) | ||
|
|
||
| NewGatewayIndex(cfg, WithGatewayForDeployerTransformationFunc(customTransformationFunc)) | ||
| assert.True(t, called) | ||
| } | ||
|
|
||
| func TestWithGatewayForEnvoyTransformationFunc(t *testing.T) { | ||
| called := false | ||
| customTransformationFunc := func(config *GatewayIndexConfig) func(kctx krt.HandlerContext, gw *gwv1.Gateway) *ir.Gateway { | ||
| called = true | ||
| return func(kctx krt.HandlerContext, gw *gwv1.Gateway) *ir.Gateway { | ||
| return nil | ||
| } | ||
| } | ||
| cfg := getConfig(t) | ||
|
|
||
| NewGatewayIndex(cfg, WithGatewayForEnvoyTransformationFunc(customTransformationFunc)) | ||
| assert.True(t, called) | ||
| } | ||
|
|
||
| func getConfig(t *testing.T) GatewayIndexConfig { | ||
| krtOpts := krtutil.NewKrtOptions(t.Context().Done(), nil) | ||
| return GatewayIndexConfig{ | ||
| KrtOpts: krtOpts, | ||
| ControllerNames: smallset.New("controller-name"), | ||
| EnvoyControllerName: "envoy-controller-name", | ||
| PolicyIndex: NewPolicyIndex(krtOpts, nil, settings.Settings{}), | ||
| Gateways: krt.NewStaticCollection[*gwv1.Gateway](nil, nil, krtOpts.ToOptions("Gateways")...), | ||
| ListenerSets: krt.NewStaticCollection[*gwxv1a1.XListenerSet](nil, nil, krtOpts.ToOptions("ListenerSets")...), | ||
| GatewayClasses: krt.NewStaticCollection[*gwv1.GatewayClass](nil, nil, krtOpts.ToOptions("GatewayClasses")...), | ||
| Namespaces: krt.NewStaticCollection[NamespaceMetadata](nil, nil, krtOpts.ToOptions("Namespaces")...), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK go can't test the equivalence of functions (test whether cfg.transformationFunc is the same as GatewayTransformationFunc, the default value), so the second best is to ensure it is not nil