K8s: Add cluster scope support in app runner (#106914)

This commit is contained in:
Todd Treece
2025-06-18 13:15:32 -04:00
committed by GitHub
parent 7e5122d580
commit f3b545a903
4 changed files with 48 additions and 25 deletions
+31 -20
View File
@@ -9,36 +9,47 @@ import (
// ResourceInfo helps define a k8s resource
type ResourceInfo struct {
group string
version string
resourceName string
singularName string
shortName string
kind string
newObj func() runtime.Object
newList func() runtime.Object
columns TableColumns
group string
version string
resourceName string
singularName string
shortName string
kind string
newObj func() runtime.Object
newList func() runtime.Object
columns TableColumns
clusterScoped bool
}
func NewResourceInfo(group, version, resourceName, singularName, kind string,
newObj func() runtime.Object, newList func() runtime.Object, columns TableColumns) ResourceInfo {
shortName := "" // an optional alias helpful in kubectl eg ("sa" for serviceaccounts)
return ResourceInfo{group, version, resourceName, singularName, shortName, kind, newObj, newList, columns}
shortName := "" // an optional alias helpful in kubectl eg ("sa" for serviceaccounts)
clusterScoped := false // if true, this resource is cluster scoped, otherwise it is namespace scoped
return ResourceInfo{group, version, resourceName, singularName, shortName, kind, newObj, newList, columns, clusterScoped}
}
func (info *ResourceInfo) WithGroupAndShortName(group string, shortName string) ResourceInfo {
return ResourceInfo{
group: group,
version: info.version,
resourceName: info.resourceName,
singularName: info.singularName,
kind: info.kind,
shortName: shortName,
newObj: info.newObj,
newList: info.newList,
columns: info.columns,
group: group,
version: info.version,
resourceName: info.resourceName,
singularName: info.singularName,
kind: info.kind,
shortName: shortName,
newObj: info.newObj,
newList: info.newList,
columns: info.columns,
clusterScoped: info.clusterScoped,
}
}
func (info *ResourceInfo) WithClusterScope() ResourceInfo {
info.clusterScoped = true
return *info
}
func (info *ResourceInfo) IsClusterScoped() bool {
return info.clusterScoped
}
func (info *ResourceInfo) GetName() string {
return info.resourceName
@@ -12,6 +12,9 @@ func NewRegistryStore(scheme *runtime.Scheme, resourceInfo utils.ResourceInfo, o
gv := resourceInfo.GroupVersion()
gv.Version = runtime.APIVersionInternal
strategy := NewStrategy(scheme, gv)
if resourceInfo.IsClusterScoped() {
strategy = strategy.WithClusterScope()
}
store := &registry.Store{
NewFunc: resourceInfo.NewFunc,
NewListFunc: resourceInfo.NewListFunc,
+9 -4
View File
@@ -22,17 +22,22 @@ type genericStrategy struct {
runtime.ObjectTyper
names.NameGenerator
gv schema.GroupVersion
gv schema.GroupVersion
clusterScoped bool
}
// NewStrategy creates and returns a genericStrategy instance.
func NewStrategy(typer runtime.ObjectTyper, gv schema.GroupVersion) *genericStrategy {
return &genericStrategy{typer, names.SimpleNameGenerator, gv}
return &genericStrategy{typer, names.SimpleNameGenerator, gv, false}
}
// NamespaceScoped returns true because all Generic resources must be within a namespace.
func (g *genericStrategy) NamespaceScoped() bool {
return true
return !g.clusterScoped
}
func (g *genericStrategy) WithClusterScope() *genericStrategy {
g.clusterScoped = true
return g
}
func (g *genericStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
@@ -9,7 +9,7 @@ import (
)
func KindToResourceInfo(kind resource.Kind) utils.ResourceInfo {
return utils.NewResourceInfo(
r := utils.NewResourceInfo(
kind.Group(),
kind.Version(),
kind.GroupVersionResource().Resource,
@@ -19,4 +19,8 @@ func KindToResourceInfo(kind resource.Kind) utils.ResourceInfo {
func() runtime.Object { return kind.ZeroListValue() },
utils.TableColumns{}, // TODO: this only supports the default columns
)
if kind.Scope() == resource.ClusterScope {
r = r.WithClusterScope()
}
return r
}