-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Iceber Gu <[email protected]>
- Loading branch information
Showing
3 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
pkg/runtime/resourceconfig/factory/resourceconfig_factory.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package factory | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/runtime/serializer/versioning" | ||
serverresourceconfig "k8s.io/apiserver/pkg/server/resourceconfig" | ||
serverstorage "k8s.io/apiserver/pkg/server/storage" | ||
apisstorage "k8s.io/kubernetes/pkg/apis/storage" | ||
|
||
"github.com/clusterpedia-io/clusterpedia/pkg/runtime/resourceconfig" | ||
"github.com/clusterpedia-io/clusterpedia/pkg/runtime/scheme" | ||
) | ||
|
||
type ResourceConfigFactory struct { | ||
legacyResourceEncodingConfig serverstorage.ResourceEncodingConfig | ||
|
||
cohabitatingResources map[schema.GroupResource][]schema.GroupResource | ||
} | ||
|
||
func NewResourceConfigFactory() *ResourceConfigFactory { | ||
resources := []schema.GroupVersionResource{ | ||
apisstorage.Resource("csistoragecapacities").WithVersion("v1beta1"), | ||
} | ||
|
||
resourceEncodingConfig := serverstorage.NewDefaultResourceEncodingConfig(scheme.LegacyResourceScheme) | ||
resourceEncodingConfig = serverresourceconfig.MergeResourceEncodingConfigs(resourceEncodingConfig, resources) | ||
|
||
factory := &ResourceConfigFactory{ | ||
legacyResourceEncodingConfig: resourceEncodingConfig, | ||
cohabitatingResources: make(map[schema.GroupResource][]schema.GroupResource), | ||
} | ||
|
||
/* | ||
factory.addCohabitatingResources(networking.Resource("networkpolicies"), extensions.Resource("networkpolicies")) | ||
factory.addCohabitatingResources(apps.Resource("deployments"), extensions.Resource("deployments")) | ||
factory.addCohabitatingResources(apps.Resource("daemonsets"), extensions.Resource("daemonsets")) | ||
factory.addCohabitatingResources(apps.Resource("replicasets"), extensions.Resource("replicasets")) | ||
factory.addCohabitatingResources(apicore.Resource("events"), events.Resource("events")) | ||
factory.addCohabitatingResources(apicore.Resource("replicationcontrollers"), extensions.Resource("replicationcontrollers")) // to make scale subresources equivalent | ||
factory.addCohabitatingResources(policy.Resource("podsecuritypolicies"), extensions.Resource("podsecuritypolicies")) | ||
factory.addCohabitatingResources(networking.Resource("ingresses"), extensions.Resource("ingresses")) | ||
*/ | ||
return factory | ||
} | ||
|
||
/* | ||
func (f *ResourceConfigFactory) addCohabitatingResources(groupResources ...schema.GroupResource) { | ||
for _, groupResource := range groupResources { | ||
f.cohabitatingResources[groupResource] = groupResources | ||
} | ||
} | ||
*/ | ||
|
||
func (g *ResourceConfigFactory) getStorageGroupResource(groupResource schema.GroupResource) schema.GroupResource { | ||
if len(g.cohabitatingResources[groupResource]) != 0 { | ||
return g.cohabitatingResources[groupResource][0] | ||
} | ||
return groupResource | ||
} | ||
|
||
func (g *ResourceConfigFactory) NewConfig(gvr schema.GroupVersionResource, namespaced bool) (*resourceconfig.ResourceConfig, error) { | ||
if scheme.LegacyResourceScheme.IsGroupRegistered(gvr.Group) { | ||
return g.NewLegacyResourceConfig(gvr.GroupResource(), namespaced) | ||
} | ||
return g.NewUnstructuredConfig(gvr, namespaced) | ||
} | ||
|
||
func (g *ResourceConfigFactory) NewUnstructuredConfig(gvr schema.GroupVersionResource, namespaced bool) (*resourceconfig.ResourceConfig, error) { | ||
version := gvr.GroupVersion() | ||
codec := versioning.NewCodec( | ||
scheme.UnstructuredCodecs, | ||
scheme.UnstructuredCodecs, | ||
scheme.UnstructuredScheme, | ||
scheme.UnstructuredScheme, | ||
scheme.UnstructuredScheme, | ||
nil, | ||
version, | ||
version, | ||
"unstructuredObjectStorage", | ||
) | ||
return &resourceconfig.ResourceConfig{ | ||
Namespaced: namespaced, | ||
GroupResource: gvr.GroupResource(), | ||
|
||
StorageResource: gvr, | ||
MemoryResource: gvr, | ||
Codec: codec, | ||
}, nil | ||
} | ||
|
||
func (g *ResourceConfigFactory) NewLegacyResourceConfig(gr schema.GroupResource, namespaced bool) (*resourceconfig.ResourceConfig, error) { | ||
chosenStorageResource := g.getStorageGroupResource(gr) | ||
|
||
storageVersion, err := g.legacyResourceEncodingConfig.StorageEncodingFor(chosenStorageResource) | ||
if err != nil { | ||
return nil, err | ||
} | ||
memoryVersion, err := g.legacyResourceEncodingConfig.InMemoryEncodingFor(gr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
codecConfig := serverstorage.StorageCodecConfig{ | ||
StorageMediaType: runtime.ContentTypeJSON, | ||
StorageSerializer: scheme.LegacyResourceCodecs, | ||
MemoryVersion: memoryVersion, | ||
StorageVersion: storageVersion, | ||
} | ||
codec, _, err := serverstorage.NewStorageCodec(codecConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resourceconfig.ResourceConfig{ | ||
Namespaced: namespaced, | ||
GroupResource: gr, | ||
|
||
StorageResource: chosenStorageResource.WithVersion(storageVersion.Version), | ||
MemoryResource: chosenStorageResource.WithVersion(memoryVersion.Version), | ||
Codec: codec, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package resourceconfig | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
type ResourceConfig struct { | ||
Namespaced bool | ||
GroupResource schema.GroupResource | ||
|
||
StorageResource schema.GroupVersionResource | ||
MemoryResource schema.GroupVersionResource | ||
|
||
Codec runtime.Codec | ||
} |