Scopes: Adds kinds for browsing the scope node tree. (#86975)
Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
@@ -82,6 +82,22 @@ func (b *ScopeAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = scheme.AddFieldLabelConversionFunc(
|
||||
scope.ScopeNodeResourceInfo.GroupVersionKind(),
|
||||
func(label, value string) (string, string, error) {
|
||||
fieldSet := SelectableScopeNodeFields(&scope.ScopeNode{})
|
||||
for key := range fieldSet {
|
||||
if label == key {
|
||||
return label, value, nil
|
||||
}
|
||||
}
|
||||
return "", "", fmt.Errorf("field label not supported for %s: %s", scope.ScopeNodeResourceInfo.GroupVersionKind(), label)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// This is required for --server-side apply
|
||||
err = scope.AddKnownTypes(scope.InternalGroupVersion, scheme)
|
||||
if err != nil {
|
||||
@@ -102,6 +118,7 @@ func (b *ScopeAPIBuilder) GetAPIGroupInfo(
|
||||
|
||||
scopeResourceInfo := scope.ScopeResourceInfo
|
||||
scopeDashboardResourceInfo := scope.ScopeDashboardBindingResourceInfo
|
||||
scopeNodeResourceInfo := scope.ScopeNodeResourceInfo
|
||||
|
||||
storage := map[string]rest.Storage{}
|
||||
|
||||
@@ -117,6 +134,12 @@ func (b *ScopeAPIBuilder) GetAPIGroupInfo(
|
||||
}
|
||||
storage[scopeDashboardResourceInfo.StoragePath()] = scopeDashboardStorage
|
||||
|
||||
scopeNodeStorage, err := newScopeNodeStorage(scheme, optsGetter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
storage[scopeNodeResourceInfo.StoragePath()] = scopeNodeStorage
|
||||
|
||||
apiGroupInfo.VersionedResourcesStorageMap[scope.VERSION] = storage
|
||||
return &apiGroupInfo, nil
|
||||
}
|
||||
|
||||
@@ -100,6 +100,44 @@ func newScopeDashboardBindingStorage(scheme *runtime.Scheme, optsGetter generic.
|
||||
return &storage{Store: store}, nil
|
||||
}
|
||||
|
||||
func newScopeNodeStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (*storage, error) {
|
||||
strategy := grafanaregistry.NewStrategy(scheme)
|
||||
|
||||
resourceInfo := scope.ScopeNodeResourceInfo
|
||||
store := &genericregistry.Store{
|
||||
NewFunc: resourceInfo.NewFunc,
|
||||
NewListFunc: resourceInfo.NewListFunc,
|
||||
PredicateFunc: Matcher,
|
||||
DefaultQualifiedResource: resourceInfo.GroupResource(),
|
||||
SingularQualifiedResource: resourceInfo.SingularGroupResource(),
|
||||
TableConvertor: utils.NewTableConverter(
|
||||
resourceInfo.GroupResource(),
|
||||
[]metav1.TableColumnDefinition{
|
||||
{Name: "Name", Type: "string", Format: "name"},
|
||||
{Name: "Created At", Type: "date"},
|
||||
},
|
||||
func(obj any) ([]interface{}, error) {
|
||||
m, ok := obj.(*scope.ScopeNode)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected scope node")
|
||||
}
|
||||
return []interface{}{
|
||||
m.Name,
|
||||
m.CreationTimestamp.UTC().Format(time.RFC3339),
|
||||
}, nil
|
||||
},
|
||||
),
|
||||
CreateStrategy: strategy,
|
||||
UpdateStrategy: strategy,
|
||||
DeleteStrategy: strategy,
|
||||
}
|
||||
options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs}
|
||||
if err := store.CompleteWithOptions(options); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &storage{Store: store}, nil
|
||||
}
|
||||
|
||||
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
|
||||
if s, ok := obj.(*scope.Scope); ok {
|
||||
return labels.Set(s.Labels), SelectableScopeFields(s), nil
|
||||
@@ -107,6 +145,9 @@ func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
|
||||
if s, ok := obj.(*scope.ScopeDashboardBinding); ok {
|
||||
return labels.Set(s.Labels), SelectableScopeDashboardBindingFields(s), nil
|
||||
}
|
||||
if s, ok := obj.(*scope.ScopeNode); ok {
|
||||
return labels.Set(s.Labels), SelectableScopeNodeFields(s), nil
|
||||
}
|
||||
return nil, nil, fmt.Errorf("not a scope or ScopeDashboardBinding object")
|
||||
}
|
||||
|
||||
@@ -131,3 +172,15 @@ func SelectableScopeDashboardBindingFields(obj *scope.ScopeDashboardBinding) fie
|
||||
"spec.scope": obj.Spec.Scope,
|
||||
})
|
||||
}
|
||||
|
||||
func SelectableScopeNodeFields(obj *scope.ScopeNode) fields.Set {
|
||||
parentName := ""
|
||||
|
||||
if obj.Spec.ParentName != nil {
|
||||
parentName = *obj.Spec.ParentName
|
||||
}
|
||||
|
||||
return generic.MergeFieldsSets(generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false), fields.Set{
|
||||
"spec.parentName": parentName,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user