Skip to content

Commit

Permalink
Handle multiple apiversions in genericServices (#320)
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob kumar saha <[email protected]>
  • Loading branch information
ArnobKumarSaha authored Aug 21, 2024
1 parent 260a387 commit ee7115d
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 67 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require (
kmodules.xyz/custom-resources v0.30.0
kmodules.xyz/go-containerregistry v0.0.12
kmodules.xyz/monitoring-agent-api v0.29.0
kmodules.xyz/resource-metadata v0.18.12-0.20240821073333-907a964e4f57
kmodules.xyz/resource-metadata v0.18.12-0.20240821140635-5cf069adfd31
kmodules.xyz/resource-metrics v0.30.2
kmodules.xyz/resource-metrics/utils v0.30.1
kmodules.xyz/sets v0.29.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,8 @@ kmodules.xyz/monitoring-agent-api v0.29.0 h1:gpFl6OZrlMLb/ySMHdREI9EwGtnJ91oZBn9
kmodules.xyz/monitoring-agent-api v0.29.0/go.mod h1:iNbvaMTgVFOI5q2LJtGK91j4Dmjv4ZRiRdasGmWLKQI=
kmodules.xyz/offshoot-api v0.29.4 h1:WQV2BIUIoVKKiqZNmZ4gAy367jEdwBhEl3dFCLZM1qA=
kmodules.xyz/offshoot-api v0.29.4/go.mod h1:e+NQ0s4gW/YTPWBWEfdISZcmk+tlTq8IjvP5SLdqvko=
kmodules.xyz/resource-metadata v0.18.12-0.20240821073333-907a964e4f57 h1:yOiNTRLRI9A2SgMBvgGnmsXfbt0znG9uzQroWIeR5Bc=
kmodules.xyz/resource-metadata v0.18.12-0.20240821073333-907a964e4f57/go.mod h1:VvUjfIzmM08SZ9ssZKhduzSrggKjY93ES2Bk+/m04hs=
kmodules.xyz/resource-metadata v0.18.12-0.20240821140635-5cf069adfd31 h1:jFbVW0DCDUKWKNFtT0XC+JdakQ6CbngtY14UsN2aO1U=
kmodules.xyz/resource-metadata v0.18.12-0.20240821140635-5cf069adfd31/go.mod h1:VvUjfIzmM08SZ9ssZKhduzSrggKjY93ES2Bk+/m04hs=
kmodules.xyz/resource-metrics v0.30.2 h1:EGJapJa7Sh2ePc/WyHziLVh9xtuh4vWPBoSGxVZ8uC8=
kmodules.xyz/resource-metrics v0.30.2/go.mod h1:UYcQQLN+3o8rNPQJwJa2D9bt5ihJCeo5bCDuQ4O3MPY=
kmodules.xyz/resource-metrics/utils v0.30.1 h1:iRnAKNMMdAi7QYjMQrK9W3vNMhSC21RWNx9yS3qlpTA=
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(rscoreapi.GroupName, Scheme, metav1.ParameterCodec, Codecs)

v1alpha1storage := map[string]rest.Storage{}
v1alpha1storage[rscoreapi.ResourceGenericResourceServices] = resourcesservicestorage.NewStorage(ctrlClient, cid, rbacAuthorizer)
v1alpha1storage[rscoreapi.ResourceGenericResourceServices] = resourcesservicestorage.NewStorage(ctrlClient, kc, cid, rbacAuthorizer)
v1alpha1storage[rscoreapi.ResourceGenericResources] = genericresourcestorage.NewStorage(ctrlClient, kc, cid, rbacAuthorizer)
v1alpha1storage[rscoreapi.ResourcePodViews] = podviewstorage.NewStorage(ctrlClient, rbacAuthorizer, builder)
v1alpha1storage[rscoreapi.ResourceProjects] = projecttorage.NewStorage(ctrlClient)
Expand Down
29 changes: 25 additions & 4 deletions pkg/registry/core/resourceservice/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import (
"k8s.io/apiserver/pkg/authorization/authorizer"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/restmapper"
"kmodules.xyz/apiversion"
kmapi "kmodules.xyz/client-go/api/v1"
clustermeta "kmodules.xyz/client-go/cluster"
Expand All @@ -55,6 +58,7 @@ import (

type Storage struct {
kc client.Client
dc discovery.DiscoveryInterface
clusterID string
a authorizer.Authorizer
convertor rest.TableConvertor
Expand All @@ -69,9 +73,10 @@ var (
_ rest.SingularNameProvider = &Storage{}
)

func NewStorage(kc client.Client, clusterID string, a authorizer.Authorizer) *Storage {
func NewStorage(kc client.Client, dc discovery.DiscoveryInterface, clusterID string, a authorizer.Authorizer) *Storage {
return &Storage{
kc: kc,
dc: dc,
clusterID: clusterID,
a: a,
convertor: rest.NewDefaultTableConvertor(schema.GroupResource{
Expand Down Expand Up @@ -173,13 +178,25 @@ func (r *Storage) List(ctx context.Context, options *internalversion.ListOptions
return nil, apierrors.NewInternalError(err)
}

items := make([]rscoreapi.GenericResourceService, 0)
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(r.dc))
gvks := make(map[schema.GroupKind]string)
for _, gvk := range api.RegisteredTypes() {
if !selector.Matches(gvk.GroupKind()) {
continue
}
gk := gvk.GroupKind()
if v, exists := gvks[gk]; !exists || apiversion.MustCompare(v, gvk.Version) < 0 {
gvks[gk] = gvk.Version
}
}

items := make([]rscoreapi.GenericResourceService, 0)
for gk, v := range gvks {
if !selector.Matches(gk) {
continue
}

mapping, err := r.kc.RESTMapper().RESTMapping(gvk.GroupKind(), gvk.Version)
mapping, err := mapper.RESTMapping(gk, v)
if meta.IsNoMatchError(err) {
continue
} else if err != nil {
Expand All @@ -198,7 +215,11 @@ func (r *Storage) List(ctx context.Context, options *internalversion.ListOptions
}

var list unstructured.UnstructuredList
list.SetGroupVersionKind(gvk)
list.SetGroupVersionKind(schema.GroupVersionKind{
Group: gk.Group,
Version: v,
Kind: gk.Kind,
})
if err := r.kc.List(ctx, &list, client.InNamespace(ns)); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
kind: Connection
name: Backup Sessions
query:
byLabel: source
byLabel: offshoot
type: GraphQL
ref:
group: core.kubestash.com
Expand Down Expand Up @@ -45,7 +45,18 @@ spec:
kind: AppBinding
- actions:
create: Never
displayMode: Field
displayMode: List
kind: Connection
name: Repository
query:
byLabel: located_on
type: GraphQL
ref:
group: storage.kubestash.com
kind: Repository
- actions:
create: Never
displayMode: List
kind: Connection
name: BackupStorage
query:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ spec:
kind: RetentionPolicy
type: MatchRef
- labels:
- backup_via
- located_on
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
- '{range .spec.sessions[*].repositories[*]}{.name}{"\n"}{end}'
target:
apiVersion: kubedb.com/v1
kind: Postgres
apiVersion: storage.kubestash.com/v1alpha1
kind: Repository
type: MatchRef
- labels:
- backup_via
- storage
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
- '{range .spec.sessions[*].repositories[*]}{.encryptionSecret.name},{.encryptionSecret.namespace}{"\n"}{end}'
target:
apiVersion: kubedb.com/v1
kind: Redis
apiVersion: v1
kind: Secret
type: MatchRef
- labels:
- backup_via
Expand All @@ -66,6 +66,14 @@ spec:
apiVersion: kubedb.com/v1
kind: MongoDB
type: MatchRef
- labels:
- backup_via
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: MSSQLServer
type: MatchRef
- labels:
- backup_via
references:
Expand All @@ -87,8 +95,16 @@ spec:
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: MSSQLServer
apiVersion: kubedb.com/v1
kind: Postgres
type: MatchRef
- labels:
- backup_via
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1
kind: Redis
type: MatchRef
- labels:
- backup_via
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ metadata:
spec:
connections:
- labels:
- source
- offshoot
level: Controller
target:
apiVersion: core.kubestash.com/v1alpha1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,77 +10,101 @@ metadata:
name: core.kubestash.com-v1alpha1-restoresessions
spec:
connections:
- labels:
- located_on
references:
- '{.spec.dataSource.repository}{"\n"}{end}'
target:
apiVersion: storage.kubestash.com/v1alpha1
kind: Repository
type: MatchRef
- labels:
- storage
references:
- '{.spec.dataSource.snapshot}{"\n"}{end}'
target:
apiVersion: storage.kubestash.com/v1alpha1
kind: Snapshot
type: MatchRef
- labels:
- auth_secret
references:
- '{.spec.dataSource.encryptionSecret.name},{ .spec.dataSource.encryptionSecret.namespace}{"\n"}{end}'
target:
apiVersion: v1
kind: Secret
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: Postgres
apiVersion: kubedb.com/v1
kind: Elasticsearch
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: Redis
apiVersion: kubedb.com/v1
kind: MariaDB
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: Elasticsearch
apiVersion: kubedb.com/v1
kind: MongoDB
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: Kafka
kind: MSSQLServer
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: MariaDB
apiVersion: kubedb.com/v1
kind: MySQL
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: MongoDB
apiVersion: kubedb.com/v1
kind: PerconaXtraDB
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: MySQL
apiVersion: kubedb.com/v1
kind: Postgres
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: PerconaXtraDB
apiVersion: kubedb.com/v1
kind: Redis
type: MatchRef
- labels:
- restore_into
references:
- '{.spec.target.name},{.spec.target.namespace},{.spec.target.kind},{.spec.target.apiGroup}'
target:
apiVersion: kubedb.com/v1alpha2
kind: RedisSentinel
kind: Singlestore
type: MatchRef
resource:
group: core.kubestash.com
Expand Down
Loading

0 comments on commit ee7115d

Please sign in to comment.