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

[Bug]: Unused variable warning in mutating_webhook.go #901

Closed
3 of 4 tasks
amitamrutiya opened this issue Jan 30, 2024 · 5 comments · Fixed by #916
Closed
3 of 4 tasks

[Bug]: Unused variable warning in mutating_webhook.go #901

amitamrutiya opened this issue Jan 30, 2024 · 5 comments · Fixed by #916

Comments

@amitamrutiya
Copy link
Contributor

Checklist

  • I've searched for similar issues and couldn't find anything matching
  • I've included steps to reproduce the behavior

Affected Components

  • K8sGPT (CLI)
  • K8sGPT Operator

K8sGPT Version

v0.3.26

Kubernetes Version

v1.29.1

Host OS and its Version

Linux

Steps to reproduce

  1. Go to 'mutating_webhook.go'
  2. Scroll down to the code block where failures is appended for first time when service is not found case
  3. Notice the warning "this value of failures is never used"

Expected behaviour

The failures variable should be used after being declared and appended to, or it should be removed if it's not needed.

Actual behaviour

In the file mutating_webhook.go, there's a warning indicating that the variable failures is declared but never used. This implies that in the current code structure, if an error occurs while creating services, we append the failure but continue the loop without storing it. As a result, this failure is never accounted for.

image

Additional Information

This warning could potentially indicate dead code or a bug where the failures variable should be used but isn't. It's also a code quality issue as unused variables can lead to confusion and make the code harder to understand. If this issue is valid then I want to fix this.

@VaibhavMalik4187
Copy link
Contributor

VaibhavMalik4187 commented Jan 30, 2024

I'm assuming that you're talking about https://github.com/k8sgpt-ai/k8sgpt/blob/main/pkg/analyzer/mutating_webhook.go.
In that case,failures is not an unused variable, it is being used at:

if len(failures) > 0 {

@amitamrutiya
Copy link
Contributor Author

Due to the continue statement, the code exits the inner for loop prematurely, causing line 133 to never execute. I also tested it in cases where no service is defined, and in such instances, it does not return any result.

@VaibhavMalik4187
Copy link
Contributor

Due to the continue statement, the code exits the inner for loop prematurely, causing line 133 to never execute. I also tested it in cases where no service is defined, and in such instances, it does not return any result.

Ahh, now I get it. Great analysis!

@amitamrutiya
Copy link
Contributor Author

Also, there is same bug on validating_webhook.go

service, err := a.Client.GetClient().CoreV1().Services(svc.Namespace).Get(context.Background(), svc.Name, v1.GetOptions{})
if err != nil {
// If the service is not found, we can't check the pods
failures = append(failures, common.Failure{
Text: fmt.Sprintf("Service %s not found as mapped to by Validating Webhook %s", svc.Name, webhook.Name),
KubernetesDoc: apiDoc.GetApiDocV2("spec.webhook.clientConfig.service"),
Sensitive: []common.Sensitive{
{
Unmasked: webhookConfig.Namespace,
Masked: util.MaskString(webhookConfig.Namespace),
},
{
Unmasked: svc.Name,
Masked: util.MaskString(svc.Name),
},
},
})
continue
}

@VaibhavMalik4187
Copy link
Contributor

VaibhavMalik4187 commented Feb 3, 2024

Also, there is same bug on validating_webhook.go

service, err := a.Client.GetClient().CoreV1().Services(svc.Namespace).Get(context.Background(), svc.Name, v1.GetOptions{})
if err != nil {
// If the service is not found, we can't check the pods
failures = append(failures, common.Failure{
Text: fmt.Sprintf("Service %s not found as mapped to by Validating Webhook %s", svc.Name, webhook.Name),
KubernetesDoc: apiDoc.GetApiDocV2("spec.webhook.clientConfig.service"),
Sensitive: []common.Sensitive{
{
Unmasked: webhookConfig.Namespace,
Masked: util.MaskString(webhookConfig.Namespace),
},
{
Unmasked: svc.Name,
Masked: util.MaskString(svc.Name),
},
},
})
continue
}

Yepp. I noticed this too in https://github.com/k8sgpt-ai/k8sgpt/actions/runs/7746749007/job/21166974081

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

Successfully merging a pull request may close this issue.

2 participants