Skip to content

Commit 99ec862

Browse files
committed
feat: resource creation returns success message as YAML content
1 parent c81ce4f commit 99ec862

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pkg/kubernetes/resources.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ func (k *Kubernetes) resourcesCreateOrUpdate(ctx context.Context, resources []*u
101101
k.deferredDiscoveryRESTMapper.Reset()
102102
}
103103
}
104-
return marshal(resources)
104+
yaml, err := marshal(resources)
105+
if err != nil {
106+
return "", err
107+
}
108+
return "# The following resources (YAML) have been created or updated successfully\n" + yaml, nil
105109
}
106110

107111
func (k *Kubernetes) resourceFor(gvk *schema.GroupVersionKind) (*schema.GroupVersionResource, error) {

pkg/mcp/resources_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"k8s.io/apimachinery/pkg/runtime/schema"
77
"k8s.io/client-go/dynamic"
88
"sigs.k8s.io/yaml"
9+
"strings"
910
"testing"
1011
)
1112

@@ -204,7 +205,31 @@ func TestResourcesCreateOrUpdate(t *testing.T) {
204205
return
205206
}
206207
if resourcesCreateOrUpdateCm1.IsError {
207-
t.Fatalf("call tool failed")
208+
t.Errorf("call tool failed")
209+
return
210+
}
211+
})
212+
var decodedCreateOrUpdateCm1 []unstructured.Unstructured
213+
err = yaml.Unmarshal([]byte(resourcesCreateOrUpdateCm1.Content[0].(map[string]interface{})["text"].(string)), &decodedCreateOrUpdateCm1)
214+
t.Run("resources_create_or_update with valid namespaced yaml resource returns yaml content", func(t *testing.T) {
215+
if err != nil {
216+
t.Errorf("invalid tool result content %v", err)
217+
return
218+
}
219+
if !strings.HasPrefix(resourcesCreateOrUpdateCm1.Content[0].(map[string]interface{})["text"].(string), "# The following resources (YAML) have been created or updated successfully") {
220+
t.Errorf("Excpected success message, got %v", resourcesCreateOrUpdateCm1.Content[0].(map[string]interface{})["text"].(string))
221+
return
222+
}
223+
if len(decodedCreateOrUpdateCm1) != 1 {
224+
t.Errorf("invalid resource count, expected 1, got %v", len(decodedCreateOrUpdateCm1))
225+
return
226+
}
227+
if decodedCreateOrUpdateCm1[0].GetName() != "a-cm-created-or-updated" {
228+
t.Errorf("invalid resource name, expected a-cm-created-or-updated, got %v", decodedCreateOrUpdateCm1[0].GetName())
229+
return
230+
}
231+
if decodedCreateOrUpdateCm1[0].GetUID() == "" {
232+
t.Errorf("invalid uid, got %v", decodedCreateOrUpdateCm1[0].GetUID())
208233
return
209234
}
210235
})

0 commit comments

Comments
 (0)