Plugins API: Merge meta and installs (#112962)

This commit is contained in:
Todd Treece
2025-10-29 13:32:31 -04:00
committed by GitHub
parent 6a3e95913e
commit de88abafdd
33 changed files with 1350 additions and 2007 deletions
+28 -157
View File
@@ -16,88 +16,54 @@ import (
"github.com/grafana/grafana/pkg/util/testutil"
)
var gvrPluginInstalls = schema.GroupVersionResource{
var gvrPlugins = schema.GroupVersionResource{
Group: "plugins.grafana.app",
Version: "v0alpha1",
Resource: "plugininstalls",
Resource: "plugins",
}
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func TestIntegrationPluginInstalls(t *testing.T) {
func TestIntegrationPlugins(t *testing.T) {
testutil.SkipIntegrationTestInShortMode(t)
t.Run("create plugin install", func(t *testing.T) {
t.Run("create plugin", func(t *testing.T) {
helper := setupHelper(t)
ctx := context.Background()
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
GVR: gvrPluginInstalls,
GVR: gvrPlugins,
})
pluginName := "test-plugin-create"
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"kind": "Plugin",
"metadata": {"name": "%s"},
"spec": {"version": "1.0.0"}
}`, pluginName))
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
created, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
require.NoError(t, err)
require.NotNil(t, created)
require.Equal(t, pluginName, created.GetName())
})
t.Run("create plugin install with status is ignored", func(t *testing.T) {
t.Skip("status is not ignored on create. this might require a change in the SDK. skipping for now")
helper := setupHelper(t)
ctx := context.Background()
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
GVR: gvrPluginInstalls,
})
pluginName := "test-plugin-create-with-status"
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"metadata": {"name": "%s"},
"spec": {"version": "1.0.0"},
"status": {
"operatorStates": {
"test-operator": {
"lastEvaluation": "1",
"state": "success"
}
}
}
}`, pluginName))
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
require.NoError(t, err)
require.NotNil(t, created)
require.Equal(t, pluginName, created.GetName())
// Status should be empty as it's ignored on create
status, found, err := unstructured.NestedMap(created.Object, "status")
require.NoError(t, err)
require.True(t, found) // status field should exist
require.Empty(t, status) // but it should be empty
})
t.Run("get plugin install", func(t *testing.T) {
helper := setupHelper(t)
ctx := context.Background()
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
GVR: gvrPluginInstalls,
GVR: gvrPlugins,
})
pluginName := "test-plugin-get"
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"kind": "Plugin",
"metadata": {"name": "%s"},
"spec": {"version": "1.0.0"}
}`, pluginName))
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
created, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
require.NoError(t, err)
fetched, err := client.Resource.Get(ctx, pluginName, metav1.GetOptions{})
require.NoError(t, err)
@@ -111,16 +77,16 @@ func TestIntegrationPluginInstalls(t *testing.T) {
ctx := context.Background()
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
GVR: gvrPluginInstalls,
GVR: gvrPlugins,
})
pluginName := "test-plugin-update"
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"kind": "Plugin",
"metadata": {"name": "%s"},
"spec": {"version": "1.0.0"}
}`, pluginName))
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
created, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
require.NoError(t, err)
updatedSpec := created.DeepCopy()
updatedSpec.Object["spec"] = map[string]interface{}{
@@ -132,116 +98,21 @@ func TestIntegrationPluginInstalls(t *testing.T) {
require.Equal(t, "2.0.0", updated.Object["spec"].(map[string]interface{})["version"])
})
t.Run("update plugin install with status is ignored", func(t *testing.T) {
helper := setupHelper(t)
ctx := context.Background()
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
GVR: gvrPluginInstalls,
})
pluginName := "test-plugin-update-with-status"
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"metadata": {"name": "%s"},
"spec": {"version": "1.0.0"}
}`, pluginName))
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
require.NoError(t, err)
// Try to update the status via a normal update
withStatus := created.DeepCopy()
withStatus.Object["status"] = map[string]interface{}{
"operatorStates": map[string]interface{}{
"test-operator": map[string]interface{}{
"lastEvaluation": "1",
"state": "success",
},
},
}
updated, err := client.Resource.Update(ctx, withStatus, metav1.UpdateOptions{})
require.NoError(t, err)
require.NotNil(t, updated)
// The status should not have been updated
status, found, err := unstructured.NestedMap(updated.Object, "status")
require.NoError(t, err)
require.True(t, found)
require.Empty(t, status)
// also check with get
fetched, err := client.Resource.Get(ctx, pluginName, metav1.GetOptions{})
require.NoError(t, err)
require.NotNil(t, fetched)
status, found, err = unstructured.NestedMap(fetched.Object, "status")
require.NoError(t, err)
require.True(t, found)
require.Empty(t, status)
})
t.Run("update plugin install status", func(t *testing.T) {
helper := setupHelper(t)
ctx := context.Background()
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
GVR: gvrPluginInstalls,
})
pluginName := "test-plugin-status"
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"metadata": {"name": "%s"},
"spec": {"version": "1.0.0"}
}`, pluginName))
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
require.NoError(t, err)
// Update the status
status := created.DeepCopy()
statusPayload := map[string]interface{}{
"operatorStates": map[string]interface{}{
"test-operator": map[string]interface{}{
"lastEvaluation": "1",
"state": "success",
},
},
}
status.Object["status"] = statusPayload
updated, err := client.Resource.UpdateStatus(ctx, status, metav1.UpdateOptions{})
require.NoError(t, err)
require.NotNil(t, updated)
// Check the status on the returned object
actualStatus, found, err := unstructured.NestedMap(updated.Object, "status")
require.NoError(t, err)
require.True(t, found)
require.Equal(t, statusPayload, actualStatus)
// Get the status to ensure it persisted
fetched, err := client.Resource.Get(ctx, pluginName, metav1.GetOptions{})
require.NoError(t, err)
require.NotNil(t, fetched)
actualStatus, found, err = unstructured.NestedMap(fetched.Object, "status")
require.NoError(t, err)
require.True(t, found)
require.Equal(t, statusPayload, actualStatus)
})
t.Run("list plugin installs", func(t *testing.T) {
helper := setupHelper(t)
ctx := context.Background()
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
GVR: gvrPluginInstalls,
GVR: gvrPlugins,
})
pluginName := "test-plugin-list"
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"kind": "Plugin",
"metadata": {"name": "%s"},
"spec": {"version": "1.0.0"}
}`, pluginName))
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
created, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
require.NoError(t, err)
list, err := client.Resource.List(ctx, metav1.ListOptions{})
require.NoError(t, err)
@@ -254,16 +125,16 @@ func TestIntegrationPluginInstalls(t *testing.T) {
ctx := context.Background()
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
GVR: gvrPluginInstalls,
GVR: gvrPlugins,
})
pluginName := "test-plugin-delete"
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"kind": "Plugin",
"metadata": {"name": "%s"},
"spec": {"version": "1.0.0"}
}`, pluginName))
_, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
_, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
require.NoError(t, err)
err = client.Resource.Delete(ctx, pluginName, metav1.DeleteOptions{})
require.NoError(t, err)
@@ -281,15 +152,15 @@ func TestIntegrationPluginInstalls(t *testing.T) {
t.Run(fmt.Sprintf("with basic role: %s", user.Identity.GetOrgRole()), func(t *testing.T) {
client := helper.GetResourceClient(apis.ResourceClientArgs{
User: user,
GVR: gvrPluginInstalls,
GVR: gvrPlugins,
})
pluginInstall := helper.LoadYAMLOrJSON(`{
plugin := helper.LoadYAMLOrJSON(`{
"apiVersion": "plugins.grafana.app/v0alpha1",
"kind": "PluginInstall",
"kind": "Plugin",
"metadata": {"name": "test-plugin"},
"spec": {"version": "1.0.0"}
}`)
_, err := client.Resource.Create(context.Background(), pluginInstall, metav1.CreateOptions{})
_, err := client.Resource.Create(context.Background(), plugin, metav1.CreateOptions{})
statusError := helper.AsStatusError(err)
require.Equal(t, metav1.StatusReasonForbidden, statusError.Status().Reason)
err = client.Resource.Delete(context.Background(), "test-plugin", metav1.DeleteOptions{})