Skip to content

Commit

Permalink
Merge pull request #16019 from mfojtik/codegen-expansion-tags
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

Allow to extend generated clients with custom verbs

We need this in order to generate client calls like `.Instantiate()` or `.Secrets()` (for image stream secrets).
In upstream, this will allow to generate `UpdateStatus` which is currently "guessed" by checking for the presence of ".Status" field in the resource. It also allows to generate custom `.Scale(...)` in autoscaler. 

Additionally this allows to send requests to sub-resources but also allows to override the input and result types for existing client calls or for extended client calls. The extended client calls are generated to the main client interface.

The final syntax is:

```
//+genclient:method=Instantiate,verb=create,subresource=instantiate,input=DeploymentRequest,output=DeploymentConfig
```

(the input/result types will allow to provide full import path, see the checklist)

#### TODO

- [x] Finalize the tag syntax
- [x] Allow cross-group type references for input/result type overrides
- [x] Fix the fake generator
- [x] Move this PR upstream and demonstrate this on `.Scale()` and `.UpdateStatus()`
  • Loading branch information
openshift-merge-robot authored Sep 1, 2017
2 parents a213e8c + c087b7d commit e765e9c
Show file tree
Hide file tree
Showing 17 changed files with 921 additions and 65 deletions.
3 changes: 3 additions & 0 deletions pkg/deploy/apis/apps/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ const (
)

// +genclient
// +genclient:method=Instantiate,verb=create,subresource=instantiate,input=DeploymentRequest
// +genclient:method=GetScale,verb=get,subresource=scale,result=k8s.io/kubernetes/pkg/apis/extensions/v1beta1.Scale
// +genclient:method=UpdateScale,verb=update,subresource=scale,input=k8s.io/kubernetes/pkg/apis/extensions/v1beta1.Scale,result=k8s.io/kubernetes/pkg/apis/extensions/v1beta1.Scale

// DeploymentConfig represents a configuration for a single deployment (represented as a
// ReplicationController). It also contains details about changes which resulted in the current
Expand Down
3 changes: 3 additions & 0 deletions pkg/deploy/apis/apps/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
)

// +genclient
// +genclient:method=Instantiate,verb=create,subresource=instantiate,input=DeploymentRequest
// +genclient:method=GetScale,verb=get,subresource=scale,result=k8s.io/kubernetes/pkg/apis/extensions/v1beta1.Scale
// +genclient:method=UpdateScale,verb=update,subresource=scale,input=k8s.io/kubernetes/pkg/apis/extensions/v1beta1.Scale,result=k8s.io/kubernetes/pkg/apis/extensions/v1beta1.Scale

// Deployment Configs define the template for a pod and manages deploying new images or configuration changes.
// A single deployment configuration is usually analogous to a single micro-service. Can support many different
Expand Down
47 changes: 47 additions & 0 deletions pkg/deploy/generated/clientset/typed/apps/v1/deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
rest "k8s.io/client-go/rest"
v1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
)

// DeploymentConfigsGetter has a method to return a DeploymentConfigInterface.
Expand All @@ -26,6 +27,10 @@ type DeploymentConfigInterface interface {
List(opts meta_v1.ListOptions) (*v1.DeploymentConfigList, error)
Watch(opts meta_v1.ListOptions) (watch.Interface, error)
Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1.DeploymentConfig, err error)
Instantiate(deploymentConfigName string, deploymentRequest *v1.DeploymentRequest) (*v1.DeploymentConfig, error)
GetScale(deploymentConfigName string, options meta_v1.GetOptions) (*v1beta1.Scale, error)
UpdateScale(deploymentConfigName string, scale *v1beta1.Scale) (*v1beta1.Scale, error)

DeploymentConfigExpansion
}

Expand Down Expand Up @@ -154,3 +159,45 @@ func (c *deploymentConfigs) Patch(name string, pt types.PatchType, data []byte,
Into(result)
return
}

// Instantiate takes the representation of a deploymentRequest and creates it. Returns the server's representation of the deploymentConfig, and an error, if there is any.
func (c *deploymentConfigs) Instantiate(deploymentConfigName string, deploymentRequest *v1.DeploymentRequest) (result *v1.DeploymentConfig, err error) {
result = &v1.DeploymentConfig{}
err = c.client.Post().
Namespace(c.ns).
Resource("deploymentconfigs").
Name(deploymentConfigName).
SubResource("instantiate").
Body(deploymentRequest).
Do().
Into(result)
return
}

// GetScale takes name of the deploymentConfig, and returns the corresponding v1beta1.Scale object, and an error if there is any.
func (c *deploymentConfigs) GetScale(deploymentConfigName string, options meta_v1.GetOptions) (result *v1beta1.Scale, err error) {
result = &v1beta1.Scale{}
err = c.client.Get().
Namespace(c.ns).
Resource("deploymentconfigs").
Name(deploymentConfigName).
SubResource("scale").
VersionedParams(&options, scheme.ParameterCodec).
Do().
Into(result)
return
}

// UpdateScale takes the top resource name and the representation of a scale and updates it. Returns the server's representation of the scale, and an error, if there is any.
func (c *deploymentConfigs) UpdateScale(deploymentConfigName string, scale *v1beta1.Scale) (result *v1beta1.Scale, err error) {
result = &v1beta1.Scale{}
err = c.client.Put().
Namespace(c.ns).
Resource("deploymentconfigs").
Name(deploymentConfigName).
SubResource("scale").
Body(scale).
Do().
Into(result)
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
testing "k8s.io/client-go/testing"
v1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
)

// FakeDeploymentConfigs implements DeploymentConfigInterface
Expand Down Expand Up @@ -120,3 +121,36 @@ func (c *FakeDeploymentConfigs) Patch(name string, pt types.PatchType, data []by
}
return obj.(*apps_v1.DeploymentConfig), err
}

// Instantiate takes the representation of a deploymentRequest and creates it. Returns the server's representation of the deploymentConfig, and an error, if there is any.
func (c *FakeDeploymentConfigs) Instantiate(deploymentConfigName string, deploymentRequest *apps_v1.DeploymentRequest) (result *apps_v1.DeploymentConfig, err error) {
obj, err := c.Fake.
Invokes(testing.NewCreateSubresourceAction(deploymentconfigsResource, deploymentConfigName, "instantiate", c.ns, deploymentRequest), &apps_v1.DeploymentRequest{})

if obj == nil {
return nil, err
}
return obj.(*apps_v1.DeploymentConfig), err
}

// GetScale takes name of the deploymentConfig, and returns the corresponding deploymentConfig object, and an error if there is any.
func (c *FakeDeploymentConfigs) GetScale(deploymentConfigName string, options v1.GetOptions) (result *v1beta1.Scale, err error) {
obj, err := c.Fake.
Invokes(testing.NewGetSubresourceAction(deploymentconfigsResource, c.ns, "scale", deploymentConfigName), &v1beta1.Scale{})

if obj == nil {
return nil, err
}
return obj.(*v1beta1.Scale), err
}

// UpdateScale takes the representation of a scale and updates it. Returns the server's representation of the scale, and an error, if there is any.
func (c *FakeDeploymentConfigs) UpdateScale(deploymentConfigName string, scale *v1beta1.Scale) (result *v1beta1.Scale, err error) {
obj, err := c.Fake.
Invokes(testing.NewUpdateSubresourceAction(deploymentconfigsResource, "scale", c.ns, scale), &v1beta1.Scale{})

if obj == nil {
return nil, err
}
return obj.(*v1beta1.Scale), err
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
rest "k8s.io/client-go/rest"
v1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
)

// DeploymentConfigsGetter has a method to return a DeploymentConfigInterface.
Expand All @@ -26,6 +27,10 @@ type DeploymentConfigInterface interface {
List(opts v1.ListOptions) (*apps.DeploymentConfigList, error)
Watch(opts v1.ListOptions) (watch.Interface, error)
Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *apps.DeploymentConfig, err error)
Instantiate(deploymentConfigName string, deploymentRequest *apps.DeploymentRequest) (*apps.DeploymentConfig, error)
GetScale(deploymentConfigName string, options v1.GetOptions) (*v1beta1.Scale, error)
UpdateScale(deploymentConfigName string, scale *v1beta1.Scale) (*v1beta1.Scale, error)

DeploymentConfigExpansion
}

Expand Down Expand Up @@ -154,3 +159,45 @@ func (c *deploymentConfigs) Patch(name string, pt types.PatchType, data []byte,
Into(result)
return
}

// Instantiate takes the representation of a deploymentRequest and creates it. Returns the server's representation of the deploymentConfig, and an error, if there is any.
func (c *deploymentConfigs) Instantiate(deploymentConfigName string, deploymentRequest *apps.DeploymentRequest) (result *apps.DeploymentConfig, err error) {
result = &apps.DeploymentConfig{}
err = c.client.Post().
Namespace(c.ns).
Resource("deploymentconfigs").
Name(deploymentConfigName).
SubResource("instantiate").
Body(deploymentRequest).
Do().
Into(result)
return
}

// GetScale takes name of the deploymentConfig, and returns the corresponding v1beta1.Scale object, and an error if there is any.
func (c *deploymentConfigs) GetScale(deploymentConfigName string, options v1.GetOptions) (result *v1beta1.Scale, err error) {
result = &v1beta1.Scale{}
err = c.client.Get().
Namespace(c.ns).
Resource("deploymentconfigs").
Name(deploymentConfigName).
SubResource("scale").
VersionedParams(&options, scheme.ParameterCodec).
Do().
Into(result)
return
}

// UpdateScale takes the top resource name and the representation of a scale and updates it. Returns the server's representation of the scale, and an error, if there is any.
func (c *deploymentConfigs) UpdateScale(deploymentConfigName string, scale *v1beta1.Scale) (result *v1beta1.Scale, err error) {
result = &v1beta1.Scale{}
err = c.client.Put().
Namespace(c.ns).
Resource("deploymentconfigs").
Name(deploymentConfigName).
SubResource("scale").
Body(scale).
Do().
Into(result)
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
testing "k8s.io/client-go/testing"
v1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
)

// FakeDeploymentConfigs implements DeploymentConfigInterface
Expand Down Expand Up @@ -120,3 +121,36 @@ func (c *FakeDeploymentConfigs) Patch(name string, pt types.PatchType, data []by
}
return obj.(*apps.DeploymentConfig), err
}

// Instantiate takes the representation of a deploymentRequest and creates it. Returns the server's representation of the deploymentConfig, and an error, if there is any.
func (c *FakeDeploymentConfigs) Instantiate(deploymentConfigName string, deploymentRequest *apps.DeploymentRequest) (result *apps.DeploymentConfig, err error) {
obj, err := c.Fake.
Invokes(testing.NewCreateSubresourceAction(deploymentconfigsResource, deploymentConfigName, "instantiate", c.ns, deploymentRequest), &apps.DeploymentRequest{})

if obj == nil {
return nil, err
}
return obj.(*apps.DeploymentConfig), err
}

// GetScale takes name of the deploymentConfig, and returns the corresponding deploymentConfig object, and an error if there is any.
func (c *FakeDeploymentConfigs) GetScale(deploymentConfigName string, options v1.GetOptions) (result *v1beta1.Scale, err error) {
obj, err := c.Fake.
Invokes(testing.NewGetSubresourceAction(deploymentconfigsResource, c.ns, "scale", deploymentConfigName), &v1beta1.Scale{})

if obj == nil {
return nil, err
}
return obj.(*v1beta1.Scale), err
}

// UpdateScale takes the representation of a scale and updates it. Returns the server's representation of the scale, and an error, if there is any.
func (c *FakeDeploymentConfigs) UpdateScale(deploymentConfigName string, scale *v1beta1.Scale) (result *v1beta1.Scale, err error) {
obj, err := c.Fake.
Invokes(testing.NewUpdateSubresourceAction(deploymentconfigsResource, "scale", c.ns, scale), &v1beta1.Scale{})

if obj == nil {
return nil, err
}
return obj.(*v1beta1.Scale), err
}
1 change: 1 addition & 0 deletions pkg/image/apis/image/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type ImageStreamList struct {
}

// +genclient
// +genclient:method=Secrets,verb=list,subresource=secrets,result=k8s.io/kubernetes/pkg/api.Secret

// ImageStream stores a mapping of tags to images, metadata overrides that are applied
// when images are tagged in a stream, and an optional reference to a Docker image
Expand Down
1 change: 1 addition & 0 deletions pkg/image/apis/image/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ type ImageStreamList struct {
}

// +genclient
// +genclient:method=Secrets,verb=list,subresource=secrets,result=k8s.io/kubernetes/pkg/api/v1.Secret

// ImageStream stores a mapping of tags to images, metadata overrides that are applied
// when images are tagged in a stream, and an optional reference to a Docker image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
testing "k8s.io/client-go/testing"
api_v1 "k8s.io/kubernetes/pkg/api/v1"
)

// FakeImageStreams implements ImageStreamInterface
Expand Down Expand Up @@ -120,3 +121,14 @@ func (c *FakeImageStreams) Patch(name string, pt types.PatchType, data []byte, s
}
return obj.(*image_v1.ImageStream), err
}

// Secrets takes label and field selectors, and returns the list of Secrets that match those selectors.
func (c *FakeImageStreams) Secrets(imageStreamName string, opts v1.ListOptions) (result *api_v1.SecretList, err error) {
obj, err := c.Fake.
Invokes(testing.NewListSubresourceAction(imagestreamsResource, imageStreamName, "secrets", imagestreamsKind, c.ns, opts), &api_v1.SecretList{})

if obj == nil {
return nil, err
}
return obj.(*api_v1.SecretList), err
}
17 changes: 17 additions & 0 deletions pkg/image/generated/clientset/typed/image/v1/imagestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
rest "k8s.io/client-go/rest"
api_v1 "k8s.io/kubernetes/pkg/api/v1"
)

// ImageStreamsGetter has a method to return a ImageStreamInterface.
Expand All @@ -26,6 +27,8 @@ type ImageStreamInterface interface {
List(opts meta_v1.ListOptions) (*v1.ImageStreamList, error)
Watch(opts meta_v1.ListOptions) (watch.Interface, error)
Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1.ImageStream, err error)
Secrets(imageStreamName string, opts meta_v1.ListOptions) (*api_v1.SecretList, error)

ImageStreamExpansion
}

Expand Down Expand Up @@ -154,3 +157,17 @@ func (c *imageStreams) Patch(name string, pt types.PatchType, data []byte, subre
Into(result)
return
}

// Secrets takes v1.ImageStream name, label and field selectors, and returns the list of Secrets that match those selectors.
func (c *imageStreams) Secrets(imageStreamName string, opts meta_v1.ListOptions) (result *api_v1.SecretList, err error) {
result = &api_v1.SecretList{}
err = c.client.Get().
Namespace(c.ns).
Resource("imagestreams").
Name(imageStreamName).
SubResource("secrets").
VersionedParams(&opts, scheme.ParameterCodec).
Do().
Into(result)
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
testing "k8s.io/client-go/testing"
api "k8s.io/kubernetes/pkg/api"
)

// FakeImageStreams implements ImageStreamInterface
Expand Down Expand Up @@ -120,3 +121,14 @@ func (c *FakeImageStreams) Patch(name string, pt types.PatchType, data []byte, s
}
return obj.(*image.ImageStream), err
}

// Secrets takes label and field selectors, and returns the list of Secrets that match those selectors.
func (c *FakeImageStreams) Secrets(imageStreamName string, opts v1.ListOptions) (result *api.SecretList, err error) {
obj, err := c.Fake.
Invokes(testing.NewListSubresourceAction(imagestreamsResource, imageStreamName, "secrets", imagestreamsKind, c.ns, opts), &api.SecretList{})

if obj == nil {
return nil, err
}
return obj.(*api.SecretList), err
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
rest "k8s.io/client-go/rest"
api "k8s.io/kubernetes/pkg/api"
)

// ImageStreamsGetter has a method to return a ImageStreamInterface.
Expand All @@ -26,6 +27,8 @@ type ImageStreamInterface interface {
List(opts v1.ListOptions) (*image.ImageStreamList, error)
Watch(opts v1.ListOptions) (watch.Interface, error)
Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *image.ImageStream, err error)
Secrets(imageStreamName string, opts v1.ListOptions) (*api.SecretList, error)

ImageStreamExpansion
}

Expand Down Expand Up @@ -154,3 +157,17 @@ func (c *imageStreams) Patch(name string, pt types.PatchType, data []byte, subre
Into(result)
return
}

// Secrets takes image.ImageStream name, label and field selectors, and returns the list of Secrets that match those selectors.
func (c *imageStreams) Secrets(imageStreamName string, opts v1.ListOptions) (result *api.SecretList, err error) {
result = &api.SecretList{}
err = c.client.Get().
Namespace(c.ns).
Resource("imagestreams").
Name(imageStreamName).
SubResource("secrets").
VersionedParams(&opts, scheme.ParameterCodec).
Do().
Into(result)
return
}
Loading

0 comments on commit e765e9c

Please sign in to comment.