Skip to content

Commit

Permalink
Use file tags to generate conversions
Browse files Browse the repository at this point in the history
This drives conversion generation from file tags like:
  // +conversion-gen=k8s.io/my/internal/version
.. rather than hardcoded lists of packages.

The only net change in generated code can be explained as correct.  Previously
it didn't know that conversion was available.
  • Loading branch information
thockin committed Jul 8, 2016
1 parent 80490e0 commit 291b51e
Show file tree
Hide file tree
Showing 22 changed files with 338 additions and 255 deletions.
361 changes: 195 additions & 166 deletions cmd/libs/go2idl/conversion-gen/generators/conversion.go

Large diffs are not rendered by default.

33 changes: 0 additions & 33 deletions cmd/libs/go2idl/conversion-gen/generators/tags.go

This file was deleted.

65 changes: 33 additions & 32 deletions cmd/libs/go2idl/conversion-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,53 @@ limitations under the License.

// conversion-gen is a tool for auto-generating Conversion functions.
//
// Structs in the input directories with the below line in their comments
// will be ignored during generation.
// // +genconversion=false
// Given a list of input directories, it will scan for "peer" packages and
// generate functions that efficiently convert between same-name types in each
// package. For any pair of types that has a
// `Convert_<pkg1>_<type>_To_<pkg2>_<Type()`
// function (and its reciprocal), it will simply call that. use standard value
// assignment whenever possible. The resulting file will be stored in the same
// directory as the processed source package.
//
// Generation is governed by comment tags in the source. Any package may
// request Conversion generation by including a comment in the file-comments of
// one file, of the form:
// // +k8s:conversion-gen=<import-path-of-peer-package>
//
// When generating for a package, individual types or fields of structs may opt
// out of Conversion generation by specifying a comment on the of the form:
// // +k8s:conversion-gen=false
package main

import (
"k8s.io/kubernetes/cmd/libs/go2idl/args"
"k8s.io/kubernetes/cmd/libs/go2idl/conversion-gen/generators"

"github.com/golang/glog"
"github.com/spf13/pflag"
)

func main() {
arguments := args.Default()

// Override defaults. These are Kubernetes specific input locations.
arguments.InputDirs = []string{
"k8s.io/kubernetes/pkg/api/v1",
"k8s.io/kubernetes/pkg/api",
"k8s.io/kubernetes/pkg/apis/authentication.k8s.io",
"k8s.io/kubernetes/pkg/apis/authentication.k8s.io/v1beta1",
"k8s.io/kubernetes/pkg/apis/authorization",
"k8s.io/kubernetes/pkg/apis/authorization/v1beta1",
"k8s.io/kubernetes/pkg/apis/autoscaling",
"k8s.io/kubernetes/pkg/apis/autoscaling/v1",
"k8s.io/kubernetes/pkg/apis/batch",
"k8s.io/kubernetes/pkg/apis/batch/v1",
"k8s.io/kubernetes/pkg/apis/batch/v2alpha1",
"k8s.io/kubernetes/pkg/apis/apps",
"k8s.io/kubernetes/pkg/apis/apps/v1alpha1",
"k8s.io/kubernetes/pkg/apis/certificates",
"k8s.io/kubernetes/pkg/apis/certificates/v1alpha1",
"k8s.io/kubernetes/pkg/apis/componentconfig",
"k8s.io/kubernetes/pkg/apis/componentconfig/v1alpha1",
"k8s.io/kubernetes/pkg/apis/policy",
"k8s.io/kubernetes/pkg/apis/policy/v1alpha1",
"k8s.io/kubernetes/pkg/apis/extensions",
"k8s.io/kubernetes/pkg/apis/extensions/v1beta1",
"k8s.io/kubernetes/pkg/apis/rbac",
"k8s.io/kubernetes/pkg/apis/rbac/v1alpha1",
"k8s.io/kubernetes/federation/apis/federation",
"k8s.io/kubernetes/federation/apis/federation/v1beta1",
"k8s.io/kubernetes/pkg/conversion",
"k8s.io/kubernetes/pkg/runtime",
// Override defaults.
arguments.OutputFileBaseName = "conversion_generated"

// Custom args.
customArgs := &generators.CustomArgs{
ExtraPeerDirs: []string{
"k8s.io/kubernetes/pkg/api",
"k8s.io/kubernetes/pkg/api/v1",
"k8s.io/kubernetes/pkg/api/unversioned",
"k8s.io/kubernetes/pkg/conversion",
"k8s.io/kubernetes/pkg/runtime",
},
}
pflag.CommandLine.StringSliceVar(&customArgs.ExtraPeerDirs, "extra-peer-dirs", customArgs.ExtraPeerDirs,
"Comma-separated list of import paths which are considered, after tag-specified peers, for conversions.")
arguments.CustomArgs = customArgs

// Run it.
if err := arguments.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
Expand Down
7 changes: 4 additions & 3 deletions docs/devel/adding-an-APIGroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ cmd/libs/go2idl/ tool.
2. Make sure your pkg/apis/`<group>`/`<version>` directory has a doc.go file
with the comment `// +k8s:deepcopy-gen=package,register`, to catch the
attention of our generation tools.
3. Make sure your pkg/apis/`<group>`/`<version>` directory has a doc.go file
with the comment `// +genconversion=true`, to catch the attention of our
gen-conversion script.
3. Make sure your `pkg/apis/<group>/<version>` directory has a doc.go file
with the comment `// +k8s:conversion-gen=<internal-pkg>`, to catch the
attention of our generation tools. For most APIs the only target you
need is `k8s.io/kubernetes/pkg/apis/<group>` (your internal API).
4. Run hack/update-all.sh.

2. Generate files for Ugorji codec:
Expand Down
2 changes: 1 addition & 1 deletion federation/apis/federation/v1beta1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/federation/apis/federation

// +genconversion=true
package v1beta1
18 changes: 15 additions & 3 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ ${clientgen} --clientset-name="release_1_4" --input="api/v1,extensions/v1beta1,a
# Clientgen for federation clientset.
${clientgen} --clientset-name=federation_internalclientset --clientset-path=k8s.io/kubernetes/federation/client/clientset_generated --input="../../federation/apis/federation/","api/" --included-types-overrides="api/Service" "$@"
${clientgen} --clientset-name=federation_release_1_4 --clientset-path=k8s.io/kubernetes/federation/client/clientset_generated --input="../../federation/apis/federation/v1beta1","api/v1" --included-types-overrides="api/v1/Service" "$@"
${conversiongen} "$@"
${setgen} "$@"

# You may add additional calls of code generators like set-gen above.
Expand All @@ -73,9 +72,22 @@ DEEP_COPY_DIRS=$(
| xargs dirname \
| sort -u
)
INPUTS=$(
DEEPCOPY_INPUTS=$(
for d in ${DEEP_COPY_DIRS}; do
echo k8s.io/kubernetes/$d
done | paste -sd,
)
${deepcopygen} -i ${INPUTS}
${deepcopygen} -i ${DEEPCOPY_INPUTS}

CONVERSION_DIRS=$(
grep '^// *+k8s:conversion-gen=' ${ALL_K8S_TAG_FILES} \
| cut -f1 -d: \
| xargs dirname \
| sort -u \
)
CONVERSION_INPUTS=$(
for d in ${CONVERSION_DIRS}; do
echo k8s.io/kubernetes/$d
done | paste -sd,
)
${conversiongen} -i ${CONVERSION_INPUTS}
3 changes: 2 additions & 1 deletion hack/verify-flags/known-flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ executor-bindall
executor-logv
executor-path
executor-suicide-timeout
exit-on-lock-contention
experimental-flannel-overlay
experimental-keystone-url
experimental-nvidia-gpus
experimental-prefix
external-hostname
external-ip
exit-on-lock-contention
extra-peer-dirs
failover-timeout
failure-domains
fake-clientset
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/api

// Package v1 is the v1 version of the API.
// +genconversion=true
package v1
4 changes: 2 additions & 2 deletions pkg/api/v1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,7 @@ type ServiceSpec struct {
// API for compatibility until at least 8/20/2016. It will be removed from
// any new API revisions. If both deprecatedPublicIPs *and* externalIPs are
// set, deprecatedPublicIPs is used.
// +genconversion=false
// +k8s:conversion-gen=false
DeprecatedPublicIPs []string `json:"deprecatedPublicIPs,omitempty" protobuf:"bytes,6,rep,name=deprecatedPublicIPs"`

// Supports "ClientIP" and "None". Used to maintain session affinity.
Expand Down Expand Up @@ -3096,7 +3096,7 @@ type Secret struct {
// It is provided as a write-only convenience method.
// All keys and values are merged into the data field on write, overwriting any existing values.
// It is never output when reading from the API.
// +genconversion=false
// +k8s:conversion-gen=false
StringData map[string]string `json:"stringData,omitempty" protobuf:"bytes,4,rep,name=stringData"`

// Used to facilitate programmatic handling of secret data.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/apps/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/apis/apps

// +genconversion=true
package v1alpha1
2 changes: 1 addition & 1 deletion pkg/apis/authentication.k8s.io/v1beta1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/apis/authentication.k8s.io

// +genconversion=true
package v1beta1
2 changes: 1 addition & 1 deletion pkg/apis/authorization/v1beta1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/apis/authorization

// +genconversion=true
package v1beta1
2 changes: 1 addition & 1 deletion pkg/apis/autoscaling/v1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/apis/autoscaling

// +genconversion=true
package v1
2 changes: 1 addition & 1 deletion pkg/apis/batch/v1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/apis/batch

// +genconversion=true
package v1
2 changes: 1 addition & 1 deletion pkg/apis/batch/v2alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/apis/batch

// +genconversion=true
package v2alpha1
2 changes: 1 addition & 1 deletion pkg/apis/certificates/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/apis/certificates

// +genconversion=true
package v1alpha1
2 changes: 1 addition & 1 deletion pkg/apis/componentconfig/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=k8s.io/kubernetes/pkg/apis/componentconfig

// +genconversion=true
package v1alpha1
70 changes: 70 additions & 0 deletions pkg/apis/extensions/v1beta1/conversion_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func init() {
Convert_extensions_DeploymentStatus_To_v1beta1_DeploymentStatus,
Convert_v1beta1_DeploymentStrategy_To_extensions_DeploymentStrategy,
Convert_extensions_DeploymentStrategy_To_v1beta1_DeploymentStrategy,
Convert_v1beta1_ExportOptions_To_api_ExportOptions,
Convert_api_ExportOptions_To_v1beta1_ExportOptions,
Convert_v1beta1_FSGroupStrategyOptions_To_extensions_FSGroupStrategyOptions,
Convert_extensions_FSGroupStrategyOptions_To_v1beta1_FSGroupStrategyOptions,
Convert_v1beta1_HTTPIngressPath_To_extensions_HTTPIngressPath,
Expand Down Expand Up @@ -110,6 +112,8 @@ func init() {
Convert_unversioned_LabelSelector_To_v1beta1_LabelSelector,
Convert_v1beta1_LabelSelectorRequirement_To_unversioned_LabelSelectorRequirement,
Convert_unversioned_LabelSelectorRequirement_To_v1beta1_LabelSelectorRequirement,
Convert_v1beta1_ListOptions_To_api_ListOptions,
Convert_api_ListOptions_To_v1beta1_ListOptions,
Convert_v1beta1_NetworkPolicy_To_extensions_NetworkPolicy,
Convert_extensions_NetworkPolicy_To_v1beta1_NetworkPolicy,
Convert_v1beta1_NetworkPolicyIngressRule_To_extensions_NetworkPolicyIngressRule,
Expand Down Expand Up @@ -644,6 +648,32 @@ func autoConvert_extensions_DeploymentStrategy_To_v1beta1_DeploymentStrategy(in
return nil
}

func autoConvert_v1beta1_ExportOptions_To_api_ExportOptions(in *ExportOptions, out *api.ExportOptions, s conversion.Scope) error {
if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil {
return err
}
out.Export = in.Export
out.Exact = in.Exact
return nil
}

func Convert_v1beta1_ExportOptions_To_api_ExportOptions(in *ExportOptions, out *api.ExportOptions, s conversion.Scope) error {
return autoConvert_v1beta1_ExportOptions_To_api_ExportOptions(in, out, s)
}

func autoConvert_api_ExportOptions_To_v1beta1_ExportOptions(in *api.ExportOptions, out *ExportOptions, s conversion.Scope) error {
if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil {
return err
}
out.Export = in.Export
out.Exact = in.Exact
return nil
}

func Convert_api_ExportOptions_To_v1beta1_ExportOptions(in *api.ExportOptions, out *ExportOptions, s conversion.Scope) error {
return autoConvert_api_ExportOptions_To_v1beta1_ExportOptions(in, out, s)
}

func autoConvert_v1beta1_FSGroupStrategyOptions_To_extensions_FSGroupStrategyOptions(in *FSGroupStrategyOptions, out *extensions.FSGroupStrategyOptions, s conversion.Scope) error {
out.Rule = extensions.FSGroupStrategyType(in.Rule)
if in.Ranges != nil {
Expand Down Expand Up @@ -1440,6 +1470,46 @@ func Convert_unversioned_LabelSelectorRequirement_To_v1beta1_LabelSelectorRequir
return autoConvert_unversioned_LabelSelectorRequirement_To_v1beta1_LabelSelectorRequirement(in, out, s)
}

func autoConvert_v1beta1_ListOptions_To_api_ListOptions(in *ListOptions, out *api.ListOptions, s conversion.Scope) error {
if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil {
return err
}
if err := api.Convert_string_To_labels_Selector(&in.LabelSelector, &out.LabelSelector, s); err != nil {
return err
}
if err := api.Convert_string_To_fields_Selector(&in.FieldSelector, &out.FieldSelector, s); err != nil {
return err
}
out.Watch = in.Watch
out.ResourceVersion = in.ResourceVersion
out.TimeoutSeconds = in.TimeoutSeconds
return nil
}

func Convert_v1beta1_ListOptions_To_api_ListOptions(in *ListOptions, out *api.ListOptions, s conversion.Scope) error {
return autoConvert_v1beta1_ListOptions_To_api_ListOptions(in, out, s)
}

func autoConvert_api_ListOptions_To_v1beta1_ListOptions(in *api.ListOptions, out *ListOptions, s conversion.Scope) error {
if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil {
return err
}
if err := api.Convert_labels_Selector_To_string(&in.LabelSelector, &out.LabelSelector, s); err != nil {
return err
}
if err := api.Convert_fields_Selector_To_string(&in.FieldSelector, &out.FieldSelector, s); err != nil {
return err
}
out.Watch = in.Watch
out.ResourceVersion = in.ResourceVersion
out.TimeoutSeconds = in.TimeoutSeconds
return nil
}

func Convert_api_ListOptions_To_v1beta1_ListOptions(in *api.ListOptions, out *ListOptions, s conversion.Scope) error {
return autoConvert_api_ListOptions_To_v1beta1_ListOptions(in, out, s)
}

func autoConvert_v1beta1_NetworkPolicy_To_extensions_NetworkPolicy(in *NetworkPolicy, out *extensions.NetworkPolicy, s conversion.Scope) error {
SetDefaults_NetworkPolicy(in)
if err := api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta(&in.TypeMeta, &out.TypeMeta, s); err != nil {
Expand Down
Loading

0 comments on commit 291b51e

Please sign in to comment.