Skip to content
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

fix: restore Gateway API generation (issue #1427). #1431

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

battlebyte
Copy link
Collaborator

@battlebyte battlebyte commented Nov 6, 2024

Fixes #1427
Separate integration tests in kong2kic_integration_test.go and must be explicitly invoked with -tags=integration.
Fix tests to evaluate all yaml objects.

Separate integration tests in kong2kic_integration_test.go and must be explicitly invoked with -tags=integration.
Fix tests to evaluate all yaml objects.
@codecov-commenter
Copy link

codecov-commenter commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 75.57756% with 74 lines in your changes missing coverage. Please review.

Project coverage is 29.44%. Comparing base (3c24be7) to head (5f49ae2).

Files with missing lines Patch % Lines
kong2kic/route.go 72.38% 63 Missing and 11 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1431      +/-   ##
==========================================
+ Coverage   27.59%   29.44%   +1.84%     
==========================================
  Files          61       61              
  Lines        6283     6535     +252     
==========================================
+ Hits         1734     1924     +190     
- Misses       4421     4472      +51     
- Partials      128      139      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Prashansa-K
Copy link
Contributor

Adding @randmonkey for reviews here as I don't have much context about KIC. Tao will be able to help out in that area.

@mheap
Copy link
Member

mheap commented Nov 18, 2024

@randmonkey Please review when you can

@battlebyte
Copy link
Collaborator Author

@randmonkey let me know if any changes needed.

@battlebyte
Copy link
Collaborator Author

Hi @randmonkey , sorry to ping you again. Let please know by when can I expect a review. Just to manage expectations with users.

@mheap mheap self-requested a review December 9, 2024 13:55
mheap
mheap previously approved these changes Dec 9, 2024
Copy link
Member

@mheap mheap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kubernetes changes LGTM. Please take a look at code and PR quality @Prashansa-K

@Prashansa-K
Copy link
Contributor

@battlebyte Could you take a look at the failed CI pipelines please?

for _, service := range content.Services {
for _, route := range service.Routes {
httpRoute, err := createHTTPRoute(service, route)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: why are we continuing here even with the err?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input Kong configuration may have many routes that must be converted to HTTPRoute objects. There may be an error when converting one of the routes. If that is the case, we inform of the error and continue processing the next route object.

httpRoute.APIVersion = GatewayAPIVersionV1Beta1
}
if service.Name != nil && route.Name != nil {
httpRoute.ObjectMeta.Name = calculateSlug(*service.Name + "-" + *route.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that httpRoute.ObjectMeta is always non-nil?
I am unaware about the underlying struct but if it ever comes nil, our code can panic here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ObjectMeta is a struct so it is always initialized.

return nil
}

func createHTTPRoute(service file.FService, route *file.FRoute) (k8sgwapiv1.HTTPRoute, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do a lot of dereferencing for service and route here. Can we do a nil check beforehand to avoid potential panics?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provided comments on the concrete potential nil-checking.

func addHosts(httpRoute *k8sgwapiv1.HTTPRoute, route *file.FRoute) {
if route.Hosts != nil {
for _, host := range route.Hosts {
httpRoute.Spec.Hostnames = append(httpRoute.Spec.Hostnames, k8sgwapiv1.Hostname(*host))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as on line 322

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spec is a struct, so it is initialized.

httpRoute.ObjectMeta.Annotations["konghq.com/tags"] = strings.Join(tags, ",")
}
if route.SNIs != nil {
var snis string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance wise, it would be better to use a string builder.
I will leave the choice to you. If this piece of code isn't accessed much, we can leave it as is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, refactored as a string builder.

})
}

func addBackendRefs(httpRoute *k8sgwapiv1.HTTPRoute, service file.FService, route *file.FRoute) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please add some comments in this function to ensure that future contributors can understand what needs to be done and why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments added. Let me know if it is clear.


func addAnnotations(httpRoute *k8sgwapiv1.HTTPRoute, route *file.FRoute) {
if route.PreserveHost != nil {
httpRoute.ObjectMeta.Annotations["konghq.com/preserve-host"] = strconv.FormatBool(*route.PreserveHost)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we treat the annotation names as constants?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two diff functions using the same sort of annotations. It would be better to make these top level constants in my opinion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored as constants.


func createHTTPRoute(service file.FService, route *file.FRoute) (k8sgwapiv1.HTTPRoute, error) {
var httpRoute k8sgwapiv1.HTTPRoute
httpRoute.Kind = "HTTPRoute"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we treat this string as a constant?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored as a constant.

ExtensionRef: &k8sgwapiv1.LocalObjectReference{
Name: k8sgwapiv1.ObjectName(kongPlugin.ObjectMeta.Name),
Kind: KongPluginKind,
Group: "configuration.konghq.com",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Const string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored as constant.

@Prashansa-K
Copy link
Contributor

@battlebyte Just one comment and some linting issues that need to be addressed.
Everything else looks good.

@battlebyte
Copy link
Collaborator Author

I don't see the linting issue when running the linter locally, so not sure why it is complaining here.
What is the comment that needs to be addressed?

@Prashansa-K
Copy link
Contributor

I don't see the linting issue when running the linter locally, so not sure why it is complaining here.

That could either be due to difference in local linter version vs the one used here. Could you just resolve it based on the error it is showing in the pipeline? I think a comment regarding nolint needs to be removed and it should work fine.

What is the comment that needs to be addressed?
It is on utils.go file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

deck file kong2kic default use Ingress API, not Gateway API
4 participants