scopes: moves scopes to enterprise (#100746)

Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
Carl Bergquist
2025-02-21 08:51:34 +01:00
committed by GitHub
parent 0290da6aaa
commit 79c0e5e3ec
29 changed files with 289 additions and 2667 deletions
@@ -1,84 +0,0 @@
package scopes
import (
"context"
"encoding/json"
"os"
"testing"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/tests/apis"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func TestIntegrationScopeNodesExample(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode.")
}
ctx := context.Background()
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
AppModeProduction: false, // required for experimental APIs
EnableFeatureToggles: []string{
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs, // Required to start the example service
},
})
scopeClient := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
Namespace: "default", // actually org1
GVR: schema.GroupVersionResource{
Group: "scope.grafana.app", Version: "v0alpha1", Resource: "scopes",
},
})
scopeNodesClient := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
Namespace: "default", // actually org1
GVR: schema.GroupVersionResource{
Group: "scope.grafana.app", Version: "v0alpha1", Resource: "scopenodes",
},
})
createOptions := metav1.CreateOptions{FieldValidation: "Strict"}
t.Run("Create scopes", func(t *testing.T) {
ul := jsonListToUnstructuredList(t, "testdata/scopeNodesExample/scopes.json")
for _, item := range ul.Items {
_, err := scopeClient.Resource.Create(ctx, &item, createOptions)
require.NoError(t, err)
}
found, err := scopeClient.Resource.List(ctx, metav1.ListOptions{})
require.NoError(t, err)
require.Len(t, found.Items, 5)
})
t.Run("Create scopeNodes", func(t *testing.T) {
ul := jsonListToUnstructuredList(t, "testdata/scopeNodesExample/scopeNodes.json")
for _, item := range ul.Items {
_, err := scopeNodesClient.Resource.Create(ctx, &item, createOptions)
require.NoError(t, err)
}
found, err := scopeNodesClient.Resource.List(ctx, metav1.ListOptions{})
require.NoError(t, err)
require.Len(t, found.Items, 12)
})
}
func jsonListToUnstructuredList(t *testing.T, fname string) (ul unstructured.UnstructuredList) {
// nolint:gosec
f, err := os.ReadFile(fname)
require.NoError(t, err)
err = json.Unmarshal(f, &ul)
require.NoError(t, err)
return
}
-221
View File
@@ -1,221 +0,0 @@
package scopes
import (
"context"
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/tests/apis"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func TestIntegrationScopes(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
helper := apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
AppModeProduction: true,
EnableFeatureToggles: []string{
featuremgmt.FlagScopeApi, // Required to register the API
},
})
t.Run("Check discovery client", func(t *testing.T) {
disco := helper.NewDiscoveryClient()
resources, err := disco.ServerResourcesForGroupVersion("scope.grafana.app/v0alpha1")
require.NoError(t, err)
v1Disco, err := json.MarshalIndent(resources, "", " ")
require.NoError(t, err)
//fmt.Printf("%s", string(v1Disco))
require.JSONEq(t, `{
"kind": "APIResourceList",
"apiVersion": "v1",
"groupVersion": "scope.grafana.app/v0alpha1",
"resources": [
{
"name": "scope_dashboard_bindings",
"singularName": "FindScopeDashboardsResult",
"namespaced": true,
"kind": "FindScopeDashboardBindingsResults",
"verbs": [
"get"
]
},
{
"name": "scope_node_children",
"singularName": "FindScopeNodeChildrenResults",
"namespaced": true,
"kind": "FindScopeNodeChildrenResults",
"verbs": [
"get"
]
},
{
"name": "scopedashboardbindings",
"singularName": "scopedashboardbinding",
"namespaced": true,
"kind": "ScopeDashboardBinding",
"verbs": [
"create",
"delete",
"deletecollection",
"get",
"list",
"patch",
"update",
"watch"
]
},
{
"name": "scopedashboardbindings/status",
"singularName": "",
"namespaced": true,
"kind": "ScopeDashboardBinding",
"verbs": [
"get",
"patch",
"update"
]
},
{
"name": "scopenodes",
"singularName": "scopenode",
"namespaced": true,
"kind": "ScopeNode",
"verbs": [
"create",
"delete",
"deletecollection",
"get",
"list",
"patch",
"update",
"watch"
]
},
{
"name": "scopes",
"singularName": "scope",
"namespaced": true,
"kind": "Scope",
"verbs": [
"create",
"delete",
"deletecollection",
"get",
"list",
"patch",
"update",
"watch"
]
}
]
}`, string(v1Disco))
})
t.Run("Check create and list", func(t *testing.T) {
// Scope create+get
scopeClient := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
Namespace: "default", // actually org1
GVR: schema.GroupVersionResource{
Group: "scope.grafana.app", Version: "v0alpha1", Resource: "scopes",
},
})
createOptions := metav1.CreateOptions{FieldValidation: "Strict"}
s0, err := scopeClient.Resource.Create(ctx,
helper.LoadYAMLOrJSONFile("testdata/example-scope.yaml"),
createOptions,
)
require.NoError(t, err)
require.Equal(t, "example", s0.GetName())
s1, err := scopeClient.Resource.Get(ctx, "example", metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t,
mustNestedString(s0.Object, "spec", "title"),
mustNestedString(s1.Object, "spec", "title"),
)
_, err = scopeClient.Resource.Create(ctx,
helper.LoadYAMLOrJSONFile("testdata/example-scope2.yaml"),
createOptions,
)
require.NoError(t, err)
// Name length test
scope3 := helper.LoadYAMLOrJSONFile("testdata/example-scope3.yaml")
// Name too long (>253)
scope3.SetName(strings.Repeat("0", 254))
_, err = scopeClient.Resource.Create(ctx,
scope3,
createOptions,
)
require.Error(t, err)
// Maximum allowed length for name (253)
scope3.SetName(strings.Repeat("0", 253))
_, err = scopeClient.Resource.Create(ctx,
scope3,
createOptions,
)
require.NoError(t, err)
// Field Selector test
found, err := scopeClient.Resource.List(ctx, metav1.ListOptions{
FieldSelector: "spec.title=foo-scope",
})
require.NoError(t, err)
require.Len(t, found.Items, 1)
require.Equal(t,
"example2",
mustNestedString(found.Items[0].Object, "metadata", "name"),
)
// Create bindings
scopeDashboardBindingClient := helper.GetResourceClient(apis.ResourceClientArgs{
User: helper.Org1.Admin,
Namespace: "default", // actually org1
GVR: schema.GroupVersionResource{
Group: "scope.grafana.app", Version: "v0alpha1", Resource: "scopedashboardbindings",
},
})
_, err = scopeDashboardBindingClient.Resource.Create(ctx,
helper.LoadYAMLOrJSONFile("testdata/example-scope-dashboard-binding-abc.yaml"),
createOptions,
)
require.NoError(t, err)
_, err = scopeDashboardBindingClient.Resource.Create(ctx,
helper.LoadYAMLOrJSONFile("testdata/example-scope-dashboard-binding-xyz.yaml"),
createOptions,
)
require.NoError(t, err)
found, err = scopeDashboardBindingClient.Resource.List(ctx, metav1.ListOptions{})
require.NoError(t, err)
require.Len(t, found.Items, 2)
})
}
func mustNestedString(obj map[string]interface{}, fields ...string) string {
v, _, _ := unstructured.NestedString(obj, fields...)
return v
}
@@ -1,10 +0,0 @@
apiVersion: scope.grafana.app/v0alpha1
kind: ScopeDashboardBinding
metadata:
name: example_abc
spec:
scope: example
dashboard: abc
status:
dashboardTitle: "Example Dashboard ABC"
groups: ["group1", "group2"]
@@ -1,10 +0,0 @@
apiVersion: scope.grafana.app/v0alpha1
kind: ScopeDashboardBinding
metadata:
name: example_xyz
spec:
scope: example
dashboard: xyz
status:
dashboardTitle: "Example Dashboard XYZ"
groups: ["group2", "group3"]
-14
View File
@@ -1,14 +0,0 @@
apiVersion: scope.grafana.app/v0alpha1
kind: Scope
metadata:
name: example
spec:
title: bar-scope
description: Longer description for a scope
filters:
- key: aaa
operator: equals
value: bbb
- key: ccc
operator: not-equals
value: ddd
-14
View File
@@ -1,14 +0,0 @@
apiVersion: scope.grafana.app/v0alpha1
kind: Scope
metadata:
name: example2
spec:
title: foo-scope
description: Longer description for a scope
filters:
- key: aaa
operator: equals
value: zzz
- key: ccc
operator: not-equals
value: yyy
-14
View File
@@ -1,14 +0,0 @@
apiVersion: scope.grafana.app/v0alpha1
kind: Scope
metadata:
name: example-long
spec:
title: baz-scope
description: Longer description for a scope
filters:
- key: aaa
operator: equals
value: eee
- key: ccc
operator: not-equals
value: fff
@@ -1,179 +0,0 @@
{
"kind": "List",
"items": [
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "applications"
},
"spec": {
"description": "Application Scopes",
"title": "Applications",
"nodeType": "container"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "applications-clusters"
},
"spec": {
"description": "Application/Clusters Scopes",
"title": "Clusters",
"nodeType": "container",
"parentName": "applications",
"linkId": "indexHelperCluster",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "clusters"
},
"spec": {
"description": "Cluster Scopes",
"title": "Clusters",
"nodeType": "container",
"linkId": "indexHelperCluster",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "clusters-application"
},
"spec": {
"description": "Clusters/Application Scopes",
"title": "Applications",
"nodeType": "container",
"parentName": "clusters"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "applications-slothPictureFactory"
},
"spec": {
"description": "slothPictureFactory",
"title": "slothPictureFactory",
"nodeType": "leaf",
"parentName": "applications",
"linkId": "slothPictureFactory",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "applications-slothVoteTracker"
},
"spec": {
"description": "slothVoteTracker",
"title": "slothVoteTracker",
"nodeType": "leaf",
"parentName": "applications",
"linkId": "slothVoteTracker",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "applications-clusters-slothClusterNorth"
},
"spec": {
"description": "slothClusterNorth",
"title": "slothClusterNorth",
"nodeType": "leaf",
"parentName": "applications-clusters",
"linkId": "slothClusterNorth",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "applications-clusters-slothClusterSouth"
},
"spec": {
"description": "slothClusterSouth",
"title": "slothClusterSouth",
"nodeType": "leaf",
"parentName": "applications-clusters",
"linkId": "slothClusterSouth",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "clusters-slothClusterNorth"
},
"spec": {
"description": "slothClusterNorth",
"title": "slothClusterNorth",
"nodeType": "leaf",
"parentName": "clusters",
"linkId": "slothClusterNorth",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "clusters-slothClusterSouth"
},
"spec": {
"description": "slothClusterSouth",
"title": "slothClusterSouth",
"nodeType": "leaf",
"parentName": "clusters",
"linkId": "slothClusterSouth",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "clusters-applications-slothPictureFactory"
},
"spec": {
"description": "slothPictureFactory",
"title": "slothPictureFactory",
"nodeType": "leaf",
"parentName": "clusters-applications",
"linkId": "slothPictureFactory",
"linkType": "scope"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "ScopeNode",
"metadata": {
"name": "clusters-applications-slothVoteTracker"
},
"spec": {
"description": "slothVoteTracker",
"title": "slothVoteTracker",
"nodeType": "leaf",
"parentName": "clusters-applications",
"linkId": "slothVoteTracker",
"linkType": "scope"
}
}
]
}
@@ -1,95 +0,0 @@
{
"kind": "List",
"items": [
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "Scope",
"metadata": {
"name": "slothClusterNorth"
},
"spec": {
"description": "slothClusterNorth",
"filters": [
{
"key": "cluster",
"operator": "equals",
"value": "slothClusterNorth"
}
],
"title": "slothClusterNorth"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "Scope",
"metadata": {
"name": "slothClusterSouth"
},
"spec": {
"description": "slothClusterSouth",
"filters": [
{
"key": "cluster",
"operator": "equals",
"value": "slothClusterSouth"
}
],
"title": "slothClusterSouth"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "Scope",
"metadata": {
"name": "slothPictureFactory"
},
"spec": {
"description": "slothPictureFactory",
"filters": [
{
"key": "app",
"operator": "equals",
"value": "slothPictureFactory"
}
],
"title": "slothPictureFactory"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "Scope",
"metadata": {
"name": "slothVoteTracker"
},
"spec": {
"description": "slothVoteTracker",
"filters": [
{
"key": "app",
"operator": "equals",
"value": "slothVoteTracker"
}
],
"title": "slothVoteTracker"
}
},
{
"apiVersion": "scope.grafana.app/v0alpha1",
"kind": "Scope",
"metadata": {
"name": "indexHelperCluster"
},
"spec": {
"description": "redundant label filter but makes queries faster",
"filters": [
{
"key": "indexHelper",
"operator": "equals",
"value": "cluster"
}
],
"title": "Cluster Index Helper"
}
}
]
}