-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix OpenAPIv2 definitions for dynamic resources #484
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
cozystackAPI: | ||
image: ghcr.io/aenix-io/cozystack/cozystack-api:v0.18.0@sha256:d3f817ee20cc502b7c5deffa46a1ad94a6e1a74fa035dbeb65ef742e67fd1fe5 | ||
image: ghcr.io/aenix-io/cozystack/cozystack-api:latest@sha256:96d277a34aff6d9847251da521ca86615af2aae370ec095d046112bc4044ddea |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package server | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net" | ||
|
@@ -37,6 +38,8 @@ import ( | |
utilversionpkg "k8s.io/apiserver/pkg/util/version" | ||
"k8s.io/component-base/featuregate" | ||
baseversion "k8s.io/component-base/version" | ||
"k8s.io/klog/v2" | ||
"k8s.io/kube-openapi/pkg/validation/spec" | ||
netutils "k8s.io/utils/net" | ||
) | ||
|
||
|
@@ -156,6 +159,22 @@ func (o AppsServerOptions) Validate(args []string) error { | |
return utilerrors.NewAggregate(allErrors) | ||
} | ||
|
||
// DeepCopySchema делает глубокую копию структуры spec.Schema | ||
func DeepCopySchema(schema *spec.Schema) (*spec.Schema, error) { | ||
data, err := json.Marshal(schema) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal schema: %w", err) | ||
} | ||
|
||
var newSchema spec.Schema | ||
err = json.Unmarshal(data, &newSchema) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal schema: %w", err) | ||
} | ||
|
||
return &newSchema, nil | ||
} | ||
Comment on lines
+163
to
+176
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid using Using For example, if func DeepCopySchema(schema *spec.Schema) (*spec.Schema, error) {
- data, err := json.Marshal(schema)
- if err != nil {
- return nil, fmt.Errorf("failed to marshal schema: %w", err)
- }
-
- var newSchema spec.Schema
- err = json.Unmarshal(data, &newSchema)
- if err != nil {
- return nil, fmt.Errorf("failed to unmarshal schema: %w", err)
- }
-
- return &newSchema, nil
+ // Assuming spec.Schema has a DeepCopy method
+ if schema == nil {
+ return nil, fmt.Errorf("input schema is nil")
+ }
+ newSchema := schema.DeepCopy()
+ return newSchema, nil
} If
|
||
|
||
// Config returns the configuration for the API server based on AppsServerOptions | ||
func (o *AppsServerOptions) Config() (*apiserver.Config, error) { | ||
// TODO: set the "real" external address | ||
|
@@ -165,6 +184,12 @@ func (o *AppsServerOptions) Config() (*apiserver.Config, error) { | |
return nil, fmt.Errorf("error creating self-signed certificates: %v", err) | ||
} | ||
|
||
// First, register the dynamic types | ||
err := v1alpha1.RegisterDynamicTypes(apiserver.Scheme, o.ResourceConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to register dynamic types: %v", err) | ||
} | ||
|
||
serverConfig := genericapiserver.NewRecommendedConfig(apiserver.Codecs) | ||
|
||
serverConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig( | ||
|
@@ -173,6 +198,68 @@ func (o *AppsServerOptions) Config() (*apiserver.Config, error) { | |
serverConfig.OpenAPIConfig.Info.Title = "Apps" | ||
serverConfig.OpenAPIConfig.Info.Version = "0.1" | ||
|
||
serverConfig.OpenAPIConfig.PostProcessSpec = func(swagger *spec.Swagger) (*spec.Swagger, error) { | ||
defs := swagger.Definitions | ||
|
||
// Check basic Application definition | ||
appDef, exists := defs["com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.Application"] | ||
if !exists { | ||
return swagger, fmt.Errorf("Application definition not found") | ||
} | ||
|
||
// Check basic ApplicationList definition | ||
listDef, exists := defs["com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.ApplicationList"] | ||
if !exists { | ||
return swagger, fmt.Errorf("ApplicationList definition not found") | ||
} | ||
|
||
for _, gvk := range v1alpha1.RegisteredGVKs { | ||
resourceName := fmt.Sprintf("com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.%s", gvk.Kind) | ||
newDef, err := DeepCopySchema(&appDef) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to deepcopy schema for %s: %w", gvk.Kind, err) | ||
} | ||
|
||
// Fix Extensions for resource | ||
if newDef.Extensions == nil { | ||
newDef.Extensions = map[string]interface{}{} | ||
} | ||
newDef.Extensions["x-kubernetes-group-version-kind"] = []map[string]interface{}{ | ||
{ | ||
"group": gvk.Group, | ||
"version": gvk.Version, | ||
"kind": gvk.Kind, | ||
}, | ||
} | ||
defs[resourceName] = *newDef | ||
klog.V(6).Infof("PostProcessSpec: Added OpenAPI definition for %s\n", resourceName) | ||
|
||
// List resource | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary |
||
listResourceName := fmt.Sprintf("com.github.aenix.io.cozystack.pkg.apis.apps.v1alpha1.%sList", gvk.Kind) | ||
newListDef, err := DeepCopySchema(&listDef) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to deepcopy schema for %sList: %w", gvk.Kind, err) | ||
} | ||
|
||
// Fix Extensions for List resource | ||
if newListDef.Extensions == nil { | ||
newListDef.Extensions = map[string]interface{}{} | ||
} | ||
newListDef.Extensions["x-kubernetes-group-version-kind"] = []map[string]interface{}{ | ||
{ | ||
"group": gvk.Group, | ||
"version": gvk.Version, | ||
"kind": fmt.Sprintf("%sList", gvk.Kind), | ||
}, | ||
} | ||
defs[listResourceName] = *newListDef | ||
klog.V(6).Infof("PostProcessSpec: Added OpenAPI definition for %s\n", listResourceName) | ||
} | ||
|
||
swagger.Definitions = defs | ||
return swagger, nil | ||
} | ||
|
||
serverConfig.OpenAPIV3Config = genericapiserver.DefaultOpenAPIV3Config( | ||
kvaps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sampleopenapi.GetOpenAPIDefinitions, openapi.NewDefinitionNamer(apiserver.Scheme), | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add documentation for the exported variable and consider thread-safety
The
RegisteredGVKs
variable is exported but lacks documentation. Since it's used to track dynamically registered types, it should be documented for API consumers. Additionally, as a global mutable slice, consider thread-safety implications if accessed concurrently.Add documentation and consider making it thread-safe:
📝 Committable suggestion