Bump sigs.k8s.io/controller-tools from 0.6.2 to 0.11.1 (#5432)

This commit is contained in:
hongming
2022-12-28 10:12:00 +08:00
committed by GitHub
parent 1e2a8c1699
commit 06978f123d
52 changed files with 1335 additions and 964 deletions

View File

@@ -17,14 +17,14 @@ limitations under the License.
// Package genall defines entrypoints for generation tools to hook into and
// share the same set of parsing, typechecking, and marker information.
//
// Generators
// # Generators
//
// Each Generator knows how to register its markers into a central Registry,
// and then how to generate output using a Collector and some root packages.
// Each generator can be considered to be the output type of a marker, for easy
// command line parsing.
//
// Output and Input
// # Output and Input
//
// Generators output artifacts via an OutputRule. OutputRules know how to
// write output for different package-associated (code) files, as well as
@@ -40,7 +40,7 @@ limitations under the License.
// InputRule defines custom input loading, but its shared across all
// Generators. There's currently only a filesystem implementation.
//
// Runtime and Context
// # Runtime and Context
//
// Runtime maps together Generators, and constructs "contexts" which provide
// the common collector and roots, plus the output rule for that generator, and
@@ -50,7 +50,7 @@ limitations under the License.
// skipping type-checking errors (since those are commonly caused by the
// partial type-checking of loader.TypeChecker).
//
// Options
// # Options
//
// The FromOptions (and associated helpers) function makes it easy to use generators
// and output rules as markers that can be parsed from the command line, producing

View File

@@ -17,13 +17,14 @@ limitations under the License.
package genall
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"golang.org/x/tools/go/packages"
"sigs.k8s.io/yaml"
rawyaml "gopkg.in/yaml.v2"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
@@ -100,6 +101,8 @@ type Runtime struct {
GenerationContext
// OutputRules defines how to output artifacts for each Generator.
OutputRules OutputRules
// ErrorWriter defines where to write error messages.
ErrorWriter io.Writer
}
// GenerationContext defines the common information needed for each Generator
@@ -118,10 +121,22 @@ type GenerationContext struct {
InputRule
}
// WriteYAMLOptions implements the Options Pattern for WriteYAML.
type WriteYAMLOptions struct {
transform func(obj map[string]interface{}) error
}
// WithTransform applies a transformation to objects just before writing them.
func WithTransform(transform func(obj map[string]interface{}) error) *WriteYAMLOptions {
return &WriteYAMLOptions{
transform: transform,
}
}
// WriteYAML writes the given objects out, serialized as YAML, using the
// context's OutputRule. Objects are written as separate documents, separated
// from each other by `---` (as per the YAML spec).
func (g GenerationContext) WriteYAML(itemPath string, objs ...interface{}) error {
func (g GenerationContext) WriteYAML(itemPath string, objs []interface{}, options ...*WriteYAMLOptions) error {
out, err := g.Open(nil, itemPath)
if err != nil {
return err
@@ -129,11 +144,11 @@ func (g GenerationContext) WriteYAML(itemPath string, objs ...interface{}) error
defer out.Close()
for _, obj := range objs {
yamlContent, err := yaml.Marshal(obj)
yamlContent, err := yamlMarshal(obj, options...)
if err != nil {
return err
}
n, err := out.Write(append([]byte("\n---\n"), yamlContent...))
n, err := out.Write(append([]byte("---\n"), yamlContent...))
if err != nil {
return err
}
@@ -145,6 +160,41 @@ func (g GenerationContext) WriteYAML(itemPath string, objs ...interface{}) error
return nil
}
// yamlMarshal is based on sigs.k8s.io/yaml.Marshal, but allows for transforming the final data before writing.
func yamlMarshal(o interface{}, options ...*WriteYAMLOptions) ([]byte, error) {
j, err := json.Marshal(o)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %v", err)
}
return yamlJSONToYAMLWithFilter(j, options...)
}
// yamlJSONToYAMLWithFilter is based on sigs.k8s.io/yaml.JSONToYAML, but allows for transforming the final data before writing.
func yamlJSONToYAMLWithFilter(j []byte, options ...*WriteYAMLOptions) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj map[string]interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
if err := rawyaml.Unmarshal(j, &jsonObj); err != nil {
return nil, err
}
for _, option := range options {
if option.transform != nil {
if err := option.transform(jsonObj); err != nil {
return nil, err
}
}
}
// Marshal this object into YAML.
return rawyaml.Marshal(jsonObj)
}
// ReadFile reads the given boilerplate artifact using the context's InputRule.
func (g GenerationContext) ReadFile(path string) ([]byte, error) {
file, err := g.OpenForRead(path)
@@ -188,8 +238,12 @@ func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) {
func (r *Runtime) Run() bool {
// TODO(directxman12): we could make this parallel,
// but we'd need to ensure all underlying machinery is threadsafe
if r.ErrorWriter == nil {
r.ErrorWriter = os.Stderr
}
if len(r.Generators) == 0 {
fmt.Fprintln(os.Stderr, "no generators to run")
fmt.Fprintln(r.ErrorWriter, "no generators to run")
return true
}
@@ -205,7 +259,7 @@ func (r *Runtime) Run() bool {
}
if err := (*gen).Generate(&ctx); err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(r.ErrorWriter, err)
hadErrs = true
}
}

View File

@@ -17,13 +17,13 @@ limitations under the License.
// Package pretty contains utilities for formatting terminal help output,
// and a use of those to display marker help.
//
// Terminal Output
// # Terminal Output
//
// The Span interface and Table struct allow you to construct tables with
// colored formatting, without causing ANSI formatting characters to mess up width
// calculations.
//
// Marker Help
// # Marker Help
//
// The MarkersSummary prints a summary table for marker help, while the MarkersDetails
// prints out more detailed information, with explainations of the individual marker fields.

View File

@@ -30,6 +30,8 @@ var (
// +controllertools:marker:generateHelp:category=""
// InputPaths represents paths and go-style path patterns to use as package roots.
//
// Multiple paths can be specified using "{path1, path2, path3}".
type InputPaths []string
// RegisterOptionsMarkers registers "mandatory" options markers for FromOptions into the given registry.

View File

@@ -103,7 +103,7 @@ type OutputToDirectory string
func (o OutputToDirectory) Open(_ *loader.Package, itemPath string) (io.WriteCloser, error) {
// ensure the directory exists
if err := os.MkdirAll(string(o), os.ModePerm); err != nil {
if err := os.MkdirAll(filepath.Dir(filepath.Join(string(o), itemPath)), os.ModePerm); err != nil {
return nil, err
}
path := filepath.Join(string(o), itemPath)

View File

@@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
/*
@@ -28,8 +29,8 @@ func (InputPaths) Help() *markers.DefinitionHelp {
return &markers.DefinitionHelp{
Category: "",
DetailedHelp: markers.DetailedHelp{
Summary: "represents paths and go-style path patterns to use as package roots.",
Details: "",
Summary: "represents paths and go-style path patterns to use as package roots. ",
Details: "Multiple paths can be specified using \"{path1, path2, path3}\".",
},
FieldHelp: map[string]markers.DetailedHelp{},
}