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

Create dangling ingress check #388

Merged
merged 8 commits into from
Aug 17, 2022

Conversation

charlesoconor
Copy link
Contributor

Creating a check for dangling ingresses. I think it will fit in well with the other rules that already exist in this tool. Have been seeing these pop up from time to time in the projects I've been working on and would love to be able to catch them at lint time.

I punted on dealing with resources that ingress can point to an only check for services, but could add that logic in if need be. My assumption was this would cover almost all of the use cases.

Closes #387

@janisz
Copy link
Collaborator

janisz commented Aug 9, 2022

Could you add e2e test?

@charlesoconor
Copy link
Contributor Author

Yes we can. Adding a e2e test

Copy link
Contributor

@dhaus67 dhaus67 left a comment

Choose a reason for hiding this comment

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

Thanks for the contrib + adding the E2E tests so fast!

I would have one question regarding the definition of dangling ingress resource:
Currently, we define the ingress rule as dangling if the service name does not match.

However, for the ingress to work correctly, the service's port name / number must match as well. This can easily be misconfigured.

I saw this multiple times in the past be the culprit of the ingress not working correctly, so I do see the opportunity here to improve this as well with the new check during linting.

I'd suggest we add this to the dangling check as well. Then, we will check that both the service name match as well as defined port (either via its name / number).

pkg/lintcontext/mocks/ingress.go Outdated Show resolved Hide resolved
Key: templateKey,
Description: "Flag ingress which do not match any service",
SupportedObjectKinds: config.ObjectKindsDesc{
ObjectKinds: []string{objectkinds.Service},
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 this be objectkinds.Ingress?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes it should be

if _, ok := selectors[serviceName]; ok {
delete(selectors, serviceName)
if len(selectors) == 0 {
// Found the all!
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit:

Suggested change
// Found the all!
// Found them all!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

}

func getSelectorsFromIngress(ingress *networkingV1.Ingress) map[string]bool {
selectors := map[string]bool{}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd favor map[string]struct{} and setting selectors[s] = struct{}{} subsequently:

Suggested change
selectors := map[string]bool{}
selectors := map[string]struct{}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@charlesoconor
Copy link
Contributor Author

I think it's a great idea to check the ports as well. Update the PR for that.

Copy link
Contributor

@dhaus67 dhaus67 left a comment

Choose a reason for hiding this comment

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

Only two minor doc comments, otherwise LGTM!

Thanks for making the changes quickly!


**Description**: Indicates when ingress do not have any associated services.

**Remediation**: Confirm that your ingress's backend correctly matches the name on one of your services.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
**Remediation**: Confirm that your ingress's backend correctly matches the name on one of your services.
**Remediation**: Confirm that your ingress's backend correctly matches the name and port on one of your services.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@@ -0,0 +1,7 @@
name: "dangling-ingress"
description: "Indicates when ingress do not have any associated services."
remediation: "Confirm that your ingress's backend correctly matches the name on one of your services."
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
remediation: "Confirm that your ingress's backend correctly matches the name on one of your services."
remediation: "Confirm that your ingress's backend correctly matches the name and port on one of your services."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@charlesoconor
Copy link
Contributor Author

Thanks for the approval! Excited to be able to use this check I've been running into issues around this a lot lately.

@dhaus67 dhaus67 merged commit 62197fb into stackrox:main Aug 17, 2022
@charlesoconor charlesoconor deleted the create-dangling-ingress branch October 18, 2022 17:06
abrad3 pushed a commit to abrad3/kube-linter that referenced this pull request Sep 28, 2023
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.

[FEATURE_REQUEST] Add check for dangling ingress
3 participants