Skip to content

Commit 10c59e7

Browse files
Merge pull request #55168 from nikhita/customresources-subresources
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. apiextensions: add subresources for custom resources Fixes #38113 Fixes #58778 **Related**: - Proposal: kubernetes/community#913 - For custom resources to work with `kubectl scale`: kubernetes/kubernetes#58283 **Add types**: - Add `CustomResourceSubResources` type to CRD. - Fix proto generation for `CustomResourceSubResourceStatus`: kubernetes/kubernetes#55970. - Add feature gate for `CustomResourceSubResources`. - Update CRD strategy: if feature gate is disabled, this feature is dropped (i.e. set to `nil`). - Add validation for `CustomResourceSubResources`: - `SpecReplicasPath` should not be empty and should be a valid json path under `.spec`. If there is no value under the given path in the CustomResource, the `/scale` subresource will return an error on GET. - `StatusReplicasPath` should not be empty and should be a valid json path under `.status`. If there is no value under the given path in the CustomResource, the status replica value in the /scale subresource will default to 0. - If present, `LabelSelectorPath` should be a valid json path. If there is no value under `LabelSelectorPath` in the CustomResource, the status label selector value in the `/scale` subresource will default to the empty string. - `ScaleGroupVersion` should be `autoscaling/v1`. - If `CustomResourceSubResources` is enabled, only `properties` is allowed under the root schema for CRD validation. **Add status and scale subresources**: - Use helper functions from `apimachinery/pkg/apis/meta/v1/unstructured/helpers.go`. - Improve error handling: kubernetes/kubernetes#56563, kubernetes/kubernetes#58215. - Introduce Registry interface for storage. - Update storage: - Introduce `CustomResourceStorage` which acts as storage for the custom resource and its status and scale subresources. Note: storage for status and scale is only enabled when the feature gate is enabled _and_ the respective fields are enabled in the CRD. - Introduce `StatusREST` and its `New()`, `Get()` and `Update()` methods. - Introduce `ScaleREST` and its `New()`, `Get()` and `Update()` methods. - Get and Update use the json paths from the CRD and use it to return an `autoscaling/v1.Scale` object. - Update strategy: - In `PrepareForCreate`, - Clear `.status`. - Set `.metadata.generation` = 1 - In `PrepareForUpdate`, - Do not update `.status`. - If both the old and new objects have `.status` and it is changed, set it back to its old value. - If the old object has a `.status` but the new object doesn't, set it to the old value. - If old object did not have a `.status` but the new object does, delete it. - Increment generation if spec changes i.e. in the following cases: - If both the old and new objects had `.spec` and it changed. - If the old object did not have `.spec` but the new object does. - If the old object had a `.spec` but the new object doesn't. - In `Validate` and `ValidateUpdate`, - ensure that values at `specReplicasPath` and `statusReplicasPath` are >=0 and < maxInt32. - make sure there are no errors in getting the value at all the paths. - Introduce `statusStrategy` with its methods. - In `PrepareForUpdate`: - Do not update `.spec`. - If both the old and new objects have `.spec` and it is changed, set it back to its old value. - If the old object has a `.spec` but the new object doesn't, set it to the old value. - If old object did not have a `.spec` but the new object does, delete it. - Do not update `.metadata`. - In `ValidateStatusUpdate`: - For CRD validation, validate only under `.status`. - Validate value at `statusReplicasPath` as above. If `labelSelectorPath` is a path under `.status`, then validate it as well. - Plug into the custom resource handler: - Store all three storage - customResource, status and scale in `crdInfo`. - Use the storage as per the subresource in the request. - Use the validator as per the subresource (for status, only use the schema for `status`, if present). - Serve the endpoint as per the subresource - see `serveResource`, `serveStatus` and `serveScale`. - Update discovery by adding the `/status` and `/scale` resources, if enabled. **Add tests**: - Add unit tests in `etcd_test.go`. - Add integration tests. - In `subresources_test.go`, use the [polymporphic scale client](https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/client-go/scale) to get and update `Scale`. - Add a test to check everything works fine with yaml in `yaml_test.go`. **Release note**: ```release-note `/status` and `/scale` subresources are added for custom resources. ``` Kubernetes-commit: 6e856480c05424b5cd2cfcbec692a801b856ccb2
2 parents c193b72 + e11eac5 commit 10c59e7

File tree

548 files changed

+80870
-2488
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

548 files changed

+80870
-2488
lines changed

Godeps/Godeps.json

Lines changed: 606 additions & 234 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/apiextensions/types.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ limitations under the License.
1616

1717
package apiextensions
1818

19-
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
2022

2123
// CustomResourceDefinitionSpec describes how a user wants their resource to appear
2224
type CustomResourceDefinitionSpec struct {
@@ -30,6 +32,8 @@ type CustomResourceDefinitionSpec struct {
3032
Scope ResourceScope
3133
// Validation describes the validation methods for CustomResources
3234
Validation *CustomResourceValidation
35+
// Subresources describes the subresources for CustomResources
36+
Subresources *CustomResourceSubresources
3337
}
3438

3539
// CustomResourceDefinitionNames indicates the names to serve this CustomResourceDefinition
@@ -146,3 +150,41 @@ type CustomResourceValidation struct {
146150
// OpenAPIV3Schema is the OpenAPI v3 schema to be validated against.
147151
OpenAPIV3Schema *JSONSchemaProps
148152
}
153+
154+
// CustomResourceSubresources defines the status and scale subresources for CustomResources.
155+
type CustomResourceSubresources struct {
156+
// Status denotes the status subresource for CustomResources
157+
Status *CustomResourceSubresourceStatus
158+
// Scale denotes the scale subresource for CustomResources
159+
Scale *CustomResourceSubresourceScale
160+
}
161+
162+
// CustomResourceSubresourceStatus defines how to serve the status subresource for CustomResources.
163+
// Status is represented by the `.status` JSON path inside of a CustomResource. When set,
164+
// * exposes a /status subresource for the custom resource
165+
// * PUT requests to the /status subresource take a custom resource object, and ignore changes to anything except the status stanza
166+
// * PUT/POST/PATCH requests to the custom resource ignore changes to the status stanza
167+
type CustomResourceSubresourceStatus struct{}
168+
169+
// CustomResourceSubresourceScale defines how to serve the scale subresource for CustomResources.
170+
type CustomResourceSubresourceScale struct {
171+
// SpecReplicasPath defines the JSON path inside of a CustomResource that corresponds to Scale.Spec.Replicas.
172+
// Only JSON paths without the array notation are allowed.
173+
// Must be a JSON Path under .spec.
174+
// If there is no value under the given path in the CustomResource, the /scale subresource will return an error on GET.
175+
SpecReplicasPath string
176+
// StatusReplicasPath defines the JSON path inside of a CustomResource that corresponds to Scale.Status.Replicas.
177+
// Only JSON paths without the array notation are allowed.
178+
// Must be a JSON Path under .status.
179+
// If there is no value under the given path in the CustomResource, the status replica value in the /scale subresource
180+
// will default to 0.
181+
StatusReplicasPath string
182+
// LabelSelectorPath defines the JSON path inside of a CustomResource that corresponds to Scale.Status.Selector.
183+
// Only JSON paths without the array notation are allowed.
184+
// Must be a JSON Path under .status.
185+
// Must be set to work with HPA.
186+
// If there is no value under the given path in the CustomResource, the status label selector value in the /scale
187+
// subresource will default to the empty string.
188+
// +optional
189+
LabelSelectorPath *string
190+
}

0 commit comments

Comments
 (0)