Add resource annotations for storing manager properties (#99683)
What is this feature? This change adds properties and known annotations to store them in for recording resource manager information, such as: The type (kind) of the manager (ex. Terraform / kubectl / etc.) The identity of the manager (ex. grafana/terraform-provider-grafana) Whether the managers allows the resource to be edited by others. Whether a resource is temporarily excluded from the manager's control. These annotations are inspired by Kubernetes field management API (https://kubernetes.io/docs/reference/using-api/server-side-apply/#field-management) and known Kubernetes annotations (https://kubernetes.io/docs/reference/labels-annotations-taints/#app-kubernetes-io-managed-by). It also adds annotations for storing information about the source of a provisioned resource, such as path, checksum & timestamp. Why do we need this feature? To make it possible to mark resources as managed by specific managers, modifying how these resources appear in the UI and are treated in the backend APIs. For example, we'd like to make managed resources read-only, or show specific docs / workflows based on the tool which is used to manage resources and so on. The identity is required for ensuring that managers of the same kind can still be told apart. Who is this feature for? For as-code practitioners and API users. --------- Signed-off-by: Igor Suleymanov <igor.suleymanov@grafana.com> Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package utils_test
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -433,6 +434,135 @@ func TestMetaAccessor(t *testing.T) {
|
||||
require.Equal(t, obj2.Spec, spec)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("ManagerProperties", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setProperties *utils.ManagerProperties
|
||||
wantProperties utils.ManagerProperties
|
||||
wantOK bool
|
||||
}{
|
||||
{
|
||||
name: "get default values",
|
||||
wantProperties: utils.ManagerProperties{
|
||||
Identity: "",
|
||||
Kind: utils.ManagerKindUnknown,
|
||||
AllowsEdits: true,
|
||||
Suspended: false,
|
||||
},
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "set and get valid values",
|
||||
setProperties: &utils.ManagerProperties{
|
||||
Identity: "identity",
|
||||
Kind: utils.ManagerKindTerraform,
|
||||
AllowsEdits: false,
|
||||
Suspended: false,
|
||||
},
|
||||
wantProperties: utils.ManagerProperties{
|
||||
Identity: "identity",
|
||||
Kind: utils.ManagerKindTerraform,
|
||||
AllowsEdits: false,
|
||||
Suspended: false,
|
||||
},
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "set empty identity returns default values",
|
||||
setProperties: &utils.ManagerProperties{
|
||||
Identity: "",
|
||||
Kind: utils.ManagerKindRepo,
|
||||
AllowsEdits: false,
|
||||
Suspended: false,
|
||||
},
|
||||
wantProperties: utils.ManagerProperties{
|
||||
Identity: "",
|
||||
Kind: utils.ManagerKindUnknown,
|
||||
AllowsEdits: true,
|
||||
Suspended: false,
|
||||
},
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "invalid kind falls back to generic kind",
|
||||
setProperties: &utils.ManagerProperties{
|
||||
Identity: "identity",
|
||||
Kind: utils.ManagerKind("invalid"),
|
||||
AllowsEdits: false,
|
||||
Suspended: true,
|
||||
},
|
||||
wantProperties: utils.ManagerProperties{
|
||||
Identity: "identity",
|
||||
Kind: utils.ManagerKindUnknown,
|
||||
AllowsEdits: false,
|
||||
Suspended: true,
|
||||
},
|
||||
wantOK: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
res := &TestResource2{}
|
||||
meta, err := utils.MetaAccessor(res)
|
||||
require.NoError(t, err)
|
||||
|
||||
if tt.setProperties != nil {
|
||||
meta.SetManagerProperties(*tt.setProperties)
|
||||
}
|
||||
|
||||
mp, ok := meta.GetManagerProperties()
|
||||
require.Equal(t, tt.wantOK, ok)
|
||||
require.Equal(t, tt.wantProperties, mp)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("SourceProperties", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setProperties *utils.SourceProperties
|
||||
wantProperties utils.SourceProperties
|
||||
wantOK bool
|
||||
}{
|
||||
{
|
||||
name: "get default values",
|
||||
wantProperties: utils.SourceProperties{},
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "set and get valid values",
|
||||
setProperties: &utils.SourceProperties{
|
||||
Path: "path",
|
||||
Checksum: "hash",
|
||||
Timestamp: time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
},
|
||||
wantProperties: utils.SourceProperties{
|
||||
Path: "path",
|
||||
Checksum: "hash",
|
||||
Timestamp: time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
},
|
||||
wantOK: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
res := &TestResource2{}
|
||||
meta, err := utils.MetaAccessor(res)
|
||||
require.NoError(t, err)
|
||||
|
||||
if tt.setProperties != nil {
|
||||
meta.SetSourceProperties(*tt.setProperties)
|
||||
}
|
||||
|
||||
sp, ok := meta.GetSourceProperties()
|
||||
require.Equal(t, tt.wantProperties, sp)
|
||||
require.Equal(t, tt.wantOK, ok)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func asJSON(v any, pretty bool) string {
|
||||
|
||||
Reference in New Issue
Block a user