1
0
mirror of https://github.com/k8sgpt-ai/k8sgpt.git synced 2025-05-05 14:47:12 +00:00
k8sgpt/pkg/analyzer/ingress_test.go
Alex Jones e41ffd80d0
feat: add MCP support ()
* feat: first mcp impl

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: update

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: wip

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: switcheed to stdio transport

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: readme

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: fix the linter 🤖

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: fix the linter 🤖

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat(mcp): implement MCP server and handler

- Implement MCP server and handler
- Add MCP server to serve
- Add MCP handler to handle MCP requests
- Add MCP server to serve
- Add MCP handler to handle MCP requests

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: consolidating code duplication

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* feat: added http sse support

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: fixed broken tests

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: updated and fixed linter

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: updated and fixed linter

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: updated the linter issues

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

---------

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
2025-04-29 09:22:44 +01:00

258 lines
7.0 KiB
Go

/*
Copyright 2023 The K8sGPT Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package analyzer
import (
"context"
"testing"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)
func TestIngressAnalyzer(t *testing.T) {
// Create test cases
testCases := []struct {
name string
ingress *networkingv1.Ingress
expectedIssues []string
}{
{
name: "Non-existent backend service",
ingress: &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress",
Namespace: "default",
},
Spec: networkingv1.IngressSpec{
Rules: []networkingv1.IngressRule{
{
Host: "example.com",
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: "non-existent-service",
Port: networkingv1.ServiceBackendPort{
Number: 80,
},
},
},
},
},
},
},
},
},
},
},
expectedIssues: []string{
"Ingress default/test-ingress does not specify an Ingress class.",
"Ingress uses the service default/non-existent-service which does not exist.",
},
},
{
name: "Non-existent TLS secret",
ingress: &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress-tls",
Namespace: "default",
},
Spec: networkingv1.IngressSpec{
TLS: []networkingv1.IngressTLS{
{
Hosts: []string{"example.com"},
SecretName: "non-existent-secret",
},
},
Rules: []networkingv1.IngressRule{
{
Host: "example.com",
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: "test-service",
Port: networkingv1.ServiceBackendPort{
Number: 80,
},
},
},
},
},
},
},
},
},
},
},
expectedIssues: []string{
"Ingress default/test-ingress-tls does not specify an Ingress class.",
"Ingress uses the service default/test-service which does not exist.",
"Ingress uses the secret default/non-existent-secret as a TLS certificate which does not exist.",
},
},
{
name: "Multiple issues",
ingress: &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress-multi",
Namespace: "default",
},
Spec: networkingv1.IngressSpec{
TLS: []networkingv1.IngressTLS{
{
Hosts: []string{"example.com"},
SecretName: "non-existent-secret",
},
},
Rules: []networkingv1.IngressRule{
{
Host: "example.com",
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: "non-existent-service",
Port: networkingv1.ServiceBackendPort{
Number: 80,
},
},
},
},
},
},
},
},
},
},
},
expectedIssues: []string{
"Ingress default/test-ingress-multi does not specify an Ingress class.",
"Ingress uses the service default/non-existent-service which does not exist.",
"Ingress uses the secret default/non-existent-secret as a TLS certificate which does not exist.",
},
},
}
// Run test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create a new context and clientset for each test case
ctx := context.Background()
clientset := fake.NewSimpleClientset()
// Create the ingress in the fake clientset
_, err := clientset.NetworkingV1().Ingresses(tc.ingress.Namespace).Create(ctx, tc.ingress, metav1.CreateOptions{})
assert.NoError(t, err)
// Create the analyzer configuration
config := common.Analyzer{
Client: &kubernetes.Client{
Client: clientset,
},
Context: ctx,
Namespace: tc.ingress.Namespace,
}
// Create the analyzer and run analysis
analyzer := IngressAnalyzer{}
results, err := analyzer.Analyze(config)
assert.NoError(t, err)
// Check that we got the expected number of issues
assert.Len(t, results, 1, "Expected 1 result")
result := results[0]
assert.Len(t, result.Error, len(tc.expectedIssues), "Expected %d issues, got %d", len(tc.expectedIssues), len(result.Error))
// Check that each expected issue is present
for _, expectedIssue := range tc.expectedIssues {
found := false
for _, failure := range result.Error {
if failure.Text == expectedIssue {
found = true
break
}
}
assert.True(t, found, "Expected to find issue: %s", expectedIssue)
}
})
}
}
func TestIngressAnalyzerLabelSelector(t *testing.T) {
clientSet := fake.NewSimpleClientset(
&networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "ingress-with-label",
Namespace: "default",
Labels: map[string]string{
"app": "test",
},
},
Spec: networkingv1.IngressSpec{
// Missing ingress class to trigger a failure
},
},
&networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "ingress-without-label",
Namespace: "default",
},
Spec: networkingv1.IngressSpec{
// Missing ingress class to trigger a failure
},
},
)
// Test with label selector
config := common.Analyzer{
Client: &kubernetes.Client{
Client: clientSet,
},
Context: context.Background(),
Namespace: "default",
LabelSelector: "app=test",
}
analyzer := IngressAnalyzer{}
results, err := analyzer.Analyze(config)
require.NoError(t, err)
require.Equal(t, 1, len(results))
require.Equal(t, "default/ingress-with-label", results[0].Name)
}
// Helper functions
func strPtr(s string) *string {
return &s
}
func pathTypePtr(p networkingv1.PathType) *networkingv1.PathType {
return &p
}