Skip to content

Commit 125fb04

Browse files
committed
gemini code assist comments incorporated
Signed-off-by: Swarup Ghosh <[email protected]>
1 parent 3559751 commit 125fb04

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

pkg/toolsets/core/mustgather.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package core
22

33
import (
44
"context"
5-
"crypto/rand"
5+
// "crypto/rand"
66
"fmt"
77
"path"
88
"strings"
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1818
"k8s.io/apimachinery/pkg/runtime"
1919
"k8s.io/apimachinery/pkg/runtime/schema"
20+
"k8s.io/apimachinery/pkg/util/rand"
2021
"k8s.io/utils/ptr"
2122
"sigs.k8s.io/yaml"
2223
)
@@ -133,7 +134,7 @@ func mustGatherPlan(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
133134
sourceDir = path.Clean(args["source_dir"].(string))
134135
}
135136

136-
namespace = fmt.Sprintf("openshift-must-gather-%s", generateRandomString(6))
137+
namespace = fmt.Sprintf("openshift-must-gather-%s", rand.String(6))
137138
if args["namespace"] != nil {
138139
namespace = args["namespace"].(string)
139140
}
@@ -152,7 +153,13 @@ func mustGatherPlan(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
152153
}
153154

154155
if args["images"] != nil {
155-
images = args["images"].([]string)
156+
if imagesArg, ok := args["images"].([]interface{}); ok {
157+
for _, img := range imagesArg {
158+
if imgStr, ok := img.(string); ok {
159+
images = append(images, imgStr)
160+
}
161+
}
162+
}
156163
}
157164

158165
if allImages {
@@ -216,8 +223,13 @@ func mustGatherPlan(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
216223
},
217224
}
218225

219-
gatherContainers := make([]corev1.Container, 1)
220-
gatherContainers[0] = *gatherContainerTemplate.DeepCopy()
226+
var gatherContainers = []corev1.Container{
227+
*gatherContainerTemplate.DeepCopy(),
228+
}
229+
230+
if len(images) > 0 {
231+
gatherContainers = make([]corev1.Container, len(images))
232+
}
221233

222234
for i, image := range images {
223235
gatherContainers[i] = *gatherContainerTemplate.DeepCopy()
@@ -272,28 +284,15 @@ func mustGatherPlan(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
272284
},
273285
}
274286

275-
nsList, err := params.NamespacesList(params, internalk8s.ResourceListOptions{})
276-
if err != nil {
277-
return api.NewToolCallResult("", fmt.Errorf("failed to list namespaces: %v", err)), nil
278-
}
279-
280287
namespaceExists := false
281-
if err := nsList.EachListItem(func(obj runtime.Object) error {
282-
if !namespaceExists {
283-
unstruct, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
284-
if err != nil {
285-
return err
286-
}
287288

288-
u := unstructured.Unstructured{Object: unstruct}
289-
if u.GetName() == namespace {
290-
namespaceExists = true
291-
}
292-
}
293-
294-
return nil
295-
}); err != nil {
296-
return api.NewToolCallResult("", fmt.Errorf("failed to check namespaces list: %v", err)), nil
289+
_, err := params.ResourcesGet(params, &schema.GroupVersionKind{
290+
Group: "",
291+
Version: "v1",
292+
Kind: "Namespaces",
293+
}, "", namespace)
294+
if err != nil {
295+
namespaceExists = true
297296
}
298297

299298
var namespaceObj *corev1.Namespace
@@ -433,21 +432,12 @@ func getComponentImages(params api.ToolHandlerParams) ([]string, error) {
433432
return images, err
434433
}
435434

436-
func generateRandomString(length int) string {
437-
r := strings.ToLower(rand.Text())
438-
if length > len(r) {
439-
r = r + generateRandomString(length-len(r))
440-
}
441-
442-
return r[:length]
443-
}
444-
445435
func parseNodeSelector(selector string) map[string]string {
446436
result := make(map[string]string)
447437
pairs := strings.Split(selector, ",")
448438
for _, pair := range pairs {
449439
kv := strings.SplitN(strings.TrimSpace(pair), "=", 2)
450-
if len(kv) == 2 {
440+
if len(kv) == 2 && strings.TrimSpace(kv[0]) != "" {
451441
result[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
452442
}
453443
}

0 commit comments

Comments
 (0)