Skip to content

Commit

Permalink
fix(api): mark read-only fields as readOnly (#12735)
Browse files Browse the repository at this point in the history
## Motivation

Described in #12734. This is only
part of the work, the part needed for the TF provider. We should have
validation not only there but on the API as well.

## Implementation information

Add "readOnly" parameter to generated OAPI schema. For modification /
creation time do it always. For `status` do it only when it's present in
the schema.

## Supporting documentation

<!-- Is there a MADR? An Issue? A related PR? -->

xrel #12734

<!--
> Changelog: skip
-->
<!--
Uncomment the above section to explicitly set a [`> Changelog:` entry
here](https://github.com/kumahq/kuma/blob/master/CONTRIBUTING.md#submitting-a-patch)?
-->

---------

Signed-off-by: slonka <[email protected]>
  • Loading branch information
slonka authored Feb 3, 2025
1 parent 577dcea commit 920abdf
Show file tree
Hide file tree
Showing 23 changed files with 167 additions and 1 deletion.
77 changes: 77 additions & 0 deletions docs/generated/openapi.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ properties:
type: string
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ properties:
- match
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
Expand Down Expand Up @@ -291,3 +293,4 @@ properties:
type: string
type: object
type: object
readOnly: true
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ properties:
- selector
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
Expand Down Expand Up @@ -176,3 +178,4 @@ properties:
type: object
type: array
type: object
readOnly: true
3 changes: 3 additions & 0 deletions pkg/core/resources/apis/meshservice/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ properties:
type: string
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
Expand Down Expand Up @@ -196,3 +198,4 @@ properties:
type: object
type: array
type: object
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshaccesslog/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshhealthcheck/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshhttproute/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshmetric/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,16 @@ properties:
type: object
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshpassthrough/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ properties:
type: object
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshproxypatch/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,16 @@ properties:
- default
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshratelimit/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshretry/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshtcproute/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshtimeout/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,16 @@ properties:
type: array
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshtls/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,16 @@ properties:
type: object
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
4 changes: 4 additions & 0 deletions pkg/plugins/policies/meshtrace/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,16 @@ properties:
type: object
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,16 @@ properties:
type: object
type: object
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
example: '0001-01-01T00:00:00Z'
status:
readOnly: true
2 changes: 2 additions & 0 deletions tools/openapi/templates/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ properties:
type: object
spec: {}
creationTime:
readOnly: true
type: string
description: 'Time at which the resource was created'
format: date-time
example: '0001-01-01T00:00:00Z'
modificationTime:
readOnly: true
type: string
description: 'Time at which the resource was updated'
format: date-time
Expand Down
12 changes: 11 additions & 1 deletion tools/policy-gen/generator/cmd/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ func newOpenAPI(rootArgs *args) *cobra.Command {

yqExec := exec.CommandContext(cmd.Context(), // nolint: gosec
localArgs.yqBin, "e", "-i",
fmt.Sprintf(`.properties *= (load(%q)| ((.spec.versions[0] | .schema.openAPIV3Schema.properties | del(.apiVersion) | del(.metadata) | del(.kind)) * {"type": {"enum": [.spec.names.kind]}}))`, crdPath),
fmt.Sprintf(`.properties *= (
load(%q)
| (
.spec.versions[0]
| .schema.openAPIV3Schema.properties
| del(.apiVersion)
| del(.metadata)
| del(.kind)
) * {"type": {"enum": [.spec.names.kind]}}
)
| (.properties.status? ) |= . + {"readOnly": true}`, crdPath),
schemaOutPath,
)
yqExec.Stderr = cmd.ErrOrStderr()
Expand Down

0 comments on commit 920abdf

Please sign in to comment.