Skip to content

Commit

Permalink
Go-native Helpers code-gen
Browse files Browse the repository at this point in the history
 * Go-native code-gen entrypoint structure bcedd4a
 * Parsing of gen-client flags 1160fc7
 * Structure for all commands of the K8s code-gen db6a5d7
 * Helpers-gen calls deepcopy-gen aaf0d26
 * Remove unneeded Printer interface eb6748c
 * Setup a default boilerplate 67bcbb0
 * Helper-gen calls defaulter-gen 5b524d1
 * Streamline logging b596957
 * Fixing tests 323501a
 * Helpers-gen calls the conversion-gen 2831fdd
 * Replace shell code with golang tooling 0615e85
 * Fixing lint errors 30a4a7e
 * Auto-skip if git isn't available 79a1e6a
 * Add k8s.io/code-generator to examples go.mod b760ba5
 * Printing stderr when exec.Command fails 7280dd2
 * Update boilerplate 5c27b82
 * Calculate sourcedir from regular sources, not test ones 8bfa3c5
 * Migrate off using `git-*` shell outs to search Go files 7b61cf6
 * Auto-skip failing test when executed in bad GOPATH 079f581
 * Fix lint errors 41d722d
 * Use deepcopy gen fn for deepcopy-gen main 5be3f70
 * Ensure to run codegen main with GO modules 1cd3950
  • Loading branch information
cardil committed Mar 6, 2024
1 parent 74adc0b commit 1ade5f6
Show file tree
Hide file tree
Showing 61 changed files with 2,965 additions and 223 deletions.
Expand Up @@ -338,7 +338,7 @@ func GetTargets(context *generator.Context, args *args.Args) []generator.Target
// from being a candidate for unsafe conversion
for k, v := range manualConversions {
if isCopyOnly(v.CommentLines) {
klog.V(4).Infof("Conversion function %s will not block memory copy because it is copy-only", v.Name)
klog.V(4).Infof("GenerateConversion function %s will not block memory copy because it is copy-only", v.Name)
continue
}
// this type should be excluded from all equivalence, because the converter must be called.
Expand Down Expand Up @@ -738,7 +738,7 @@ func (g *genConversion) generateConversion(inType, outType *types.Type, sw *gene
// There is a public manual Conversion method: use it.
} else if skipped := g.skippedFields[inType]; len(skipped) != 0 {
// The inType had some fields we could not generate.
klog.Errorf("Warning: could not find nor generate a final Conversion function for %v -> %v", inType, outType)
klog.Errorf("Warning: could not find nor generate a final GenerateConversion function for %v -> %v", inType, outType)
klog.Errorf(" the following fields need manual conversion:")
for _, f := range skipped {
klog.Errorf(" - %v", f)
Expand Down
@@ -0,0 +1,51 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generators

import (
"github.com/spf13/pflag"
generatorargs "k8s.io/code-generator/cmd/conversion-gen/args"
"k8s.io/gengo/v2"
"k8s.io/gengo/v2/generator"
)

// GenerateConversion runs the conversion-gen with given flagset and process args.
func GenerateConversion(fs *pflag.FlagSet, processArgs []string) error {
args := generatorargs.New()
args.AddFlags(fs)

if err := fs.Parse(processArgs); err != nil {
return err
}

if err := args.Validate(); err != nil {
return err
}

myTargets := func(context *generator.Context) []generator.Target {
return GetTargets(context, args)
}

// Run it.
return gengo.Execute(
NameSystems(),
DefaultNameSystem(),
myTargets,
gengo.StdBuildTag,
fs.Args(),
)
}
@@ -0,0 +1,43 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generators_test

import (
"bytes"
"errors"

"github.com/spf13/pflag"
"k8s.io/code-generator/cmd/conversion-gen/generators"

"testing"
)

func TestConversion(t *testing.T) {
t.Parallel()
var out bytes.Buffer
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
fs.SetOutput(&out)
args := []string{"--help"}

err := generators.GenerateConversion(fs, args)
if !errors.Is(err, pflag.ErrHelp) {
t.Errorf("unexpected error: %v", err)
}
if out.Len() == 0 {
t.Errorf("expected output, got none")
}
}
26 changes: 3 additions & 23 deletions staging/src/k8s.io/code-generator/cmd/conversion-gen/main.go
Expand Up @@ -96,41 +96,21 @@ package main

import (
"flag"
"os"

"github.com/spf13/pflag"
"k8s.io/klog/v2"

generatorargs "k8s.io/code-generator/cmd/conversion-gen/args"
"k8s.io/code-generator/cmd/conversion-gen/generators"
"k8s.io/gengo/v2"
"k8s.io/gengo/v2/generator"
)

func main() {
klog.InitFlags(nil)
args := generatorargs.New()

args.AddFlags(pflag.CommandLine)
flag.Set("logtostderr", "true")
_ = flag.Set("logtostderr", "true")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

if err := args.Validate(); err != nil {
klog.Fatalf("Error: %v", err)
}

myTargets := func(context *generator.Context) []generator.Target {
return generators.GetTargets(context, args)
}

// Run it.
if err := gengo.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
myTargets,
gengo.StdBuildTag,
pflag.Args(),
); err != nil {
if err := generators.GenerateConversion(pflag.CommandLine, os.Args); err != nil {
klog.Fatalf("Error: %v", err)
}
klog.V(2).Info("Completed successfully.")
Expand Down
@@ -0,0 +1,51 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generators

import (
"github.com/spf13/pflag"
generatorargs "k8s.io/code-generator/cmd/deepcopy-gen/args"
"k8s.io/gengo/v2"
"k8s.io/gengo/v2/generator"
)

// GenerateDeepCopy runs the deepcopy-gen with given flagset and process args.
func GenerateDeepCopy(fs *pflag.FlagSet, processArgs []string) error {
args := generatorargs.New()
args.AddFlags(fs)

if err := fs.Parse(processArgs); err != nil {
return err
}

if err := args.Validate(); err != nil {
return err
}

myTargets := func(context *generator.Context) []generator.Target {
return GetTargets(context, args)
}

// Run it.
return gengo.Execute(
NameSystems(),
DefaultNameSystem(),
myTargets,
gengo.StdBuildTag,
fs.Args(),
)
}
@@ -0,0 +1,42 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generators_test

import (
"bytes"
"errors"
"testing"

"github.com/spf13/pflag"
"k8s.io/code-generator/cmd/deepcopy-gen/generators"
)

func TestGenerateDeepCopy(t *testing.T) {
t.Parallel()
var out bytes.Buffer
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
fs.SetOutput(&out)
args := []string{"--help"}

err := generators.GenerateDeepCopy(fs, args)
if !errors.Is(err, pflag.ErrHelp) {
t.Errorf("unexpected error: %v", err)
}
if out.Len() == 0 {
t.Errorf("expected output, got none")
}
}
26 changes: 3 additions & 23 deletions staging/src/k8s.io/code-generator/cmd/deepcopy-gen/main.go
Expand Up @@ -70,40 +70,20 @@ package main

import (
"flag"
"os"

"github.com/spf13/pflag"
"k8s.io/code-generator/cmd/deepcopy-gen/args"
"k8s.io/code-generator/cmd/deepcopy-gen/generators"
"k8s.io/gengo/v2"
"k8s.io/gengo/v2/generator"
"k8s.io/klog/v2"
)

func main() {
klog.InitFlags(nil)
args := args.New()

args.AddFlags(pflag.CommandLine)
flag.Set("logtostderr", "true")
_ = flag.Set("logtostderr", "true")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

if err := args.Validate(); err != nil {
klog.Fatalf("Error: %v", err)
}

myTargets := func(context *generator.Context) []generator.Target {
return generators.GetTargets(context, args)
}

// Run it.
if err := gengo.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
myTargets,
gengo.StdBuildTag,
pflag.Args(),
); err != nil {
if err := generators.GenerateDeepCopy(pflag.CommandLine, os.Args); err != nil {
klog.Fatalf("Error: %v", err)
}
klog.V(2).Info("Completed successfully.")
Expand Down
@@ -0,0 +1,51 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generators

import (
"github.com/spf13/pflag"
generatorargs "k8s.io/code-generator/cmd/defaulter-gen/args"
"k8s.io/gengo/v2"
"k8s.io/gengo/v2/generator"
)

// GenerateDefaults runs the defaulter-gen with given flagset and process args.
func GenerateDefaults(fs *pflag.FlagSet, processArgs []string) error {
args := generatorargs.New()
args.AddFlags(fs)

if err := fs.Parse(processArgs); err != nil {
return err
}

if err := args.Validate(); err != nil {
return err
}

myTargets := func(context *generator.Context) []generator.Target {
return GetTargets(context, args)
}

// Run it.
return gengo.Execute(
NameSystems(),
DefaultNameSystem(),
myTargets,
gengo.StdBuildTag,
fs.Args(),
)
}
@@ -0,0 +1,43 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generators_test

import (
"bytes"
"errors"

"github.com/spf13/pflag"
"k8s.io/code-generator/cmd/defaulter-gen/generators"

"testing"
)

func TestDefaulter(t *testing.T) {
t.Parallel()
var out bytes.Buffer
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
fs.SetOutput(&out)
args := []string{"--help"}

err := generators.GenerateDefaults(fs, args)
if !errors.Is(err, pflag.ErrHelp) {
t.Errorf("unexpected error: %v", err)
}
if out.Len() == 0 {
t.Errorf("expected output, got none")
}
}

0 comments on commit 1ade5f6

Please sign in to comment.