Skip to content

Commit 697220a

Browse files
CopilotAlexsJones
andauthored
Fix: Pass filters as command-line arguments to k8sgpt serve command (#762)
* Initial plan * Fix: Pass filters as arguments to k8sgpt serve command - Modified GetDeployment to use buildK8sGPTArgs helper function - buildK8sGPTArgs now includes --filter args for each specified filter - Added comprehensive test coverage for filter functionality - This fixes the issue where Deployment resources were not generating Results Co-authored-by: AlexsJones <1235925+AlexsJones@users.noreply.github.com> * Apply go fmt to format code properly Co-authored-by: AlexsJones <1235925+AlexsJones@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: AlexsJones <1235925+AlexsJones@users.noreply.github.com>
1 parent cd89976 commit 697220a

2 files changed

Lines changed: 84 additions & 3 deletions

File tree

pkg/resources/k8sgpt.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ func GetClusterRole(config v1alpha1.K8sGPT, serviceAccountName string) (*v1.Clus
244244
return clusterRole, nil
245245
}
246246

247+
// buildK8sGPTArgs builds the command-line arguments for the k8sgpt serve command
248+
func buildK8sGPTArgs(config v1alpha1.K8sGPT) []string {
249+
args := []string{"serve"}
250+
251+
// Add filters if specified
252+
if len(config.Spec.Filters) > 0 {
253+
for _, filter := range config.Spec.Filters {
254+
args = append(args, "--filter", filter)
255+
}
256+
}
257+
258+
return args
259+
}
260+
247261
// GetDeployment Create deployment with the latest K8sGPT image
248262
func GetDeployment(config v1alpha1.K8sGPT, outOfClusterMode bool, c client.Client,
249263
serviceAccountName string) (*appsv1.Deployment, error) {
@@ -286,9 +300,7 @@ func GetDeployment(config v1alpha1.K8sGPT, outOfClusterMode bool, c client.Clien
286300
Name: "k8sgpt",
287301
ImagePullPolicy: config.Spec.ImagePullPolicy,
288302
Image: image,
289-
Args: []string{
290-
"serve",
291-
},
303+
Args: buildK8sGPTArgs(config),
292304
Env: []corev1.EnvVar{
293305
{
294306
Name: "K8SGPT_MODEL",

pkg/resources/k8sgpt_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,72 @@ func Test_GetDeploymentWithKubeconfigAndIRSA(t *testing.T) {
272272
})
273273
}
274274
}
275+
276+
func Test_GetDeploymentWithFilters(t *testing.T) {
277+
scheme := runtime.NewScheme()
278+
require.NoError(t, appsv1.AddToScheme(scheme))
279+
require.NoError(t, v1.AddToScheme(scheme))
280+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
281+
282+
testCases := []struct {
283+
name string
284+
filters []string
285+
expectedArgs []string
286+
}{
287+
{
288+
name: "No filters specified",
289+
filters: []string{},
290+
expectedArgs: []string{"serve"},
291+
},
292+
{
293+
name: "Single filter",
294+
filters: []string{"Pod"},
295+
expectedArgs: []string{"serve", "--filter", "Pod"},
296+
},
297+
{
298+
name: "Multiple filters including Deployment",
299+
filters: []string{"Pod", "Deployment", "Service"},
300+
expectedArgs: []string{"serve", "--filter", "Pod", "--filter", "Deployment", "--filter", "Service"},
301+
},
302+
{
303+
name: "All common filters",
304+
filters: []string{"Pod", "Deployment", "StatefulSet", "DaemonSet", "Service", "Ingress"},
305+
expectedArgs: []string{"serve", "--filter", "Pod", "--filter", "Deployment", "--filter", "StatefulSet", "--filter", "DaemonSet", "--filter", "Service", "--filter", "Ingress"},
306+
},
307+
}
308+
309+
for _, tc := range testCases {
310+
t.Run(tc.name, func(t *testing.T) {
311+
config := v1alpha1.K8sGPT{
312+
TypeMeta: metav1.TypeMeta{
313+
Kind: "K8sGPT",
314+
APIVersion: "core.k8sgpt.ai/v1alpha1",
315+
},
316+
ObjectMeta: metav1.ObjectMeta{
317+
Name: "test-k8sgpt",
318+
Namespace: "test-namespace",
319+
UID: "test-uid",
320+
},
321+
Spec: v1alpha1.K8sGPTSpec{
322+
Repository: "ghcr.io/k8sgpt-ai/k8sgpt",
323+
Version: "v0.4.1",
324+
ImagePullPolicy: v1.PullAlways,
325+
Filters: tc.filters,
326+
AI: &v1alpha1.AISpec{
327+
Backend: "openai",
328+
Model: "gpt-4o-mini",
329+
MaxTokens: "2048",
330+
Topk: "50",
331+
},
332+
},
333+
}
334+
335+
deployment, err := GetDeployment(config, false, fakeClient, "test-sa")
336+
require.NoError(t, err)
337+
338+
// Verify the args contain the expected values
339+
assert.Equal(t, tc.expectedArgs, deployment.Spec.Template.Spec.Containers[0].Args,
340+
"Expected args to match for filter configuration: %v", tc.filters)
341+
})
342+
}
343+
}

0 commit comments

Comments
 (0)