kubernetes/pkg
Kubernetes Submit Queue 6e856480c0
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: https://github.com/kubernetes/community/pull/913
- For custom resources to work with `kubectl scale`: https://github.com/kubernetes/kubernetes/pull/58283

**Add types**:

- Add `CustomResourceSubResources` type to CRD.
    - Fix proto generation for `CustomResourceSubResourceStatus`: https://github.com/kubernetes/kubernetes/pull/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: https://github.com/kubernetes/kubernetes/pull/56563, https://github.com/kubernetes/kubernetes/pull/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.
```
2018-02-22 13:37:35 -08:00
..
api Merge pull request #59428 from mikedanese/id-defaults 2018-02-21 16:55:39 -08:00
apis Merge pull request #54191 from MrHohn/kube-proxy-metrics-flag-fix 2018-02-21 18:46:41 -08:00
auth Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
capabilities Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
client Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
cloudprovider Merge pull request #60186 from feiskyer/vmss-check 2018-02-22 08:30:02 -08:00
controller add support for /token subresource in serviceaccount registry 2018-02-21 13:16:51 -08:00
credentialprovider Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
features Merge pull request #55168 from nikhita/customresources-subresources 2018-02-22 13:37:35 -08:00
fieldpath Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
generated Move the kubeletconfig v1alpha1 API to beta, rename to kubelet.config.k8s.io 2018-02-14 17:30:22 -08:00
kubeapiserver Merge pull request #58684 from hzxuzhonghu/default-enabled-admission 2018-02-22 05:24:44 -08:00
kubectl Merge pull request #60040 from PhilipGough/keys-from-cm 2018-02-22 06:08:55 -08:00
kubelet Merge pull request #59906 from abhi/log_stats 2018-02-21 19:40:42 -08:00
kubemark Secure Kubelet's componentconfig defaults while maintaining CLI compatibility 2018-02-13 18:10:15 -08:00
master Merge pull request #58111 from mikedanese/id-registry 2018-02-21 22:10:31 -08:00
printers collapse printing paths 2018-02-21 08:15:22 -05:00
probe Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
proxy Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
quota Merge pull request #57302 from lichuqiang/resourceQuota4extendedResource 2018-02-20 14:10:46 -08:00
registry add support for /token subresource in serviceaccount registry 2018-02-21 13:16:51 -08:00
routes Remove /ui/ redirect 2018-02-12 10:54:33 -05:00
scheduler Make the Unschedulable Queue interface private 2018-02-21 13:53:40 -08:00
security Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
securitycontext Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
serviceaccount add support for /token subresource in serviceaccount registry 2018-02-21 13:16:51 -08:00
ssh Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
util Merge pull request #52648 from dixudx/refactor_NsenterWriter 2018-02-22 03:16:50 -08:00
version Kubelet flags take precedence 2018-01-29 10:07:37 -08:00
volume Merge pull request #59928 from humblec/more-correction 2018-02-21 07:02:38 -08:00
watch/json remove outdate package 2018-01-15 23:17:19 +08:00
.import-restrictions Add import-boss directives 2017-10-13 07:06:22 -04:00
BUILD Merge pull request #58295 from zouyee/outdate 2018-02-05 22:28:41 -08:00
OWNERS