mirror of
https://github.com/kubernetes/sample-controller.git
synced 2026-04-12 00:00:26 +08:00
sync: update required packages
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"args.go",
|
||||
"gvpackages.go",
|
||||
"gvtype.go",
|
||||
],
|
||||
importpath = "k8s.io/code-generator/cmd/client-gen/args",
|
||||
deps = [
|
||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||
"//vendor/k8s.io/code-generator/cmd/client-gen/types:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["gvpackages_test.go"],
|
||||
importpath = "k8s.io/code-generator/cmd/client-gen/args",
|
||||
library = ":go_default_library",
|
||||
deps = [
|
||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||
"//vendor/k8s.io/code-generator/cmd/client-gen/types:go_default_library",
|
||||
],
|
||||
)
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Copyright 2015 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 args
|
||||
|
||||
import (
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
"k8s.io/code-generator/cmd/client-gen/types"
|
||||
)
|
||||
|
||||
// ClientGenArgs is a wrapper for arguments to client-gen.
|
||||
type CustomArgs struct {
|
||||
// A sorted list of group versions to generate. For each of them the package path is found
|
||||
// in GroupVersionToInputPath.
|
||||
Groups []types.GroupVersions
|
||||
// GroupVersionToInputPath is a map between GroupVersion and the path to the respective
|
||||
// types.go, relative to InputBasePath. We still need GroupVersions in the
|
||||
// struct because we need an order.
|
||||
GroupVersionToInputPath map[types.GroupVersion]string
|
||||
// The base for the path of GroupVersionToInputPath.
|
||||
InputBasePath string
|
||||
|
||||
// Overrides for which types should be included in the client.
|
||||
IncludedTypesOverrides map[types.GroupVersion][]string
|
||||
|
||||
// ClientsetName is the name of the clientset to be generated. It's
|
||||
// populated from command-line arguments.
|
||||
ClientsetName string
|
||||
// ClientsetOutputPath is the path the clientset will be generated at. It's
|
||||
// populated from command-line arguments.
|
||||
ClientsetOutputPath string
|
||||
// ClientsetAPIPath is the default API path for generated clients.
|
||||
ClientsetAPIPath string
|
||||
// ClientsetOnly determines if we should generate the clients for groups and
|
||||
// types along with the clientset. It's populated from command-line
|
||||
// arguments.
|
||||
ClientsetOnly bool
|
||||
// FakeClient determines if client-gen generates the fake clients.
|
||||
FakeClient bool
|
||||
}
|
||||
|
||||
func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
|
||||
pflag.Var(NewGVPackagesValue(&ca.GroupVersionToInputPath, &ca.Groups, nil), "input", "group/versions that client-gen will generate clients for. At most one version per group is allowed. Specified in the format \"group1/version1,group2/version2...\".")
|
||||
pflag.Var(NewGVTypesValue(&ca.IncludedTypesOverrides, []string{}), "included-types-overrides", "list of group/version/type for which client should be generated. By default, client is generated for all types which have genclient in types.go. This overrides that. For each groupVersion in this list, only the types mentioned here will be included. The default check of genclient will be used for other group versions.")
|
||||
pflag.StringVar(&ca.InputBasePath, "input-base", "k8s.io/kubernetes/pkg/apis", "base path to look for the api group.")
|
||||
pflag.StringVarP(&ca.ClientsetName, "clientset-name", "n", "internalclientset", "the name of the generated clientset package.")
|
||||
pflag.StringVarP(&ca.ClientsetAPIPath, "clientset-api-path", "", "", "the value of default API path.")
|
||||
pflag.StringVar(&ca.ClientsetOutputPath, "clientset-path", "k8s.io/kubernetes/pkg/client/clientset_generated/", "the generated clientset will be output to <clientset-path>/<clientset-name>.")
|
||||
pflag.BoolVar(&ca.ClientsetOnly, "clientset-only", false, "when set, client-gen only generates the clientset shell, without generating the individual typed clients")
|
||||
pflag.BoolVar(&ca.FakeClient, "fake-clientset", true, "when set, client-gen will generate the fake clientset that can be used in tests")
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
Copyright 2017 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 args
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"flag"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"k8s.io/code-generator/cmd/client-gen/types"
|
||||
)
|
||||
|
||||
type gvPackagesValue struct {
|
||||
gvToPath *map[types.GroupVersion]string
|
||||
groups *[]types.GroupVersions
|
||||
changed bool
|
||||
}
|
||||
|
||||
func NewGVPackagesValue(gvToPath *map[types.GroupVersion]string, groups *[]types.GroupVersions, def []string) *gvPackagesValue {
|
||||
gvp := new(gvPackagesValue)
|
||||
gvp.gvToPath = gvToPath
|
||||
gvp.groups = groups
|
||||
if def != nil {
|
||||
if err := gvp.set(def); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return gvp
|
||||
}
|
||||
|
||||
var _ flag.Value = &gvPackagesValue{}
|
||||
|
||||
func readAsCSV(val string) ([]string, error) {
|
||||
if val == "" {
|
||||
return []string{}, nil
|
||||
}
|
||||
stringReader := strings.NewReader(val)
|
||||
csvReader := csv.NewReader(stringReader)
|
||||
return csvReader.Read()
|
||||
}
|
||||
|
||||
func writeAsCSV(vals []string) (string, error) {
|
||||
b := &bytes.Buffer{}
|
||||
w := csv.NewWriter(b)
|
||||
err := w.Write(vals)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
w.Flush()
|
||||
return strings.TrimSuffix(b.String(), "\n"), nil
|
||||
}
|
||||
|
||||
func (s *gvPackagesValue) set(vs []string) error {
|
||||
if !s.changed {
|
||||
*s.gvToPath = map[types.GroupVersion]string{}
|
||||
*s.groups = []types.GroupVersions{}
|
||||
}
|
||||
|
||||
var seenGroups = make(map[types.Group]*types.GroupVersions)
|
||||
for _, g := range *s.groups {
|
||||
seenGroups[g.Group] = &g
|
||||
}
|
||||
|
||||
for _, v := range vs {
|
||||
pth, gvString := parsePathGroupVersion(v)
|
||||
gv, err := types.ToGroupVersion(gvString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if group, ok := seenGroups[gv.Group]; ok {
|
||||
seenGroups[gv.Group].Versions = append(group.Versions, gv.Version)
|
||||
} else {
|
||||
seenGroups[gv.Group] = &types.GroupVersions{
|
||||
PackageName: gv.Group.NonEmpty(),
|
||||
Group: gv.Group,
|
||||
Versions: []types.Version{gv.Version},
|
||||
}
|
||||
}
|
||||
|
||||
(*s.gvToPath)[gv] = groupVersionPath(pth, gv.Group.String(), gv.Version.String())
|
||||
}
|
||||
|
||||
var groupNames []string
|
||||
for groupName := range seenGroups {
|
||||
groupNames = append(groupNames, groupName.String())
|
||||
}
|
||||
sort.Strings(groupNames)
|
||||
*s.groups = []types.GroupVersions{}
|
||||
for _, groupName := range groupNames {
|
||||
*s.groups = append(*s.groups, *seenGroups[types.Group(groupName)])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *gvPackagesValue) Set(val string) error {
|
||||
vs, err := readAsCSV(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.set(vs); err != nil {
|
||||
return err
|
||||
}
|
||||
s.changed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *gvPackagesValue) Type() string {
|
||||
return "stringSlice"
|
||||
}
|
||||
|
||||
func (s *gvPackagesValue) String() string {
|
||||
strs := make([]string, 0, len(*s.gvToPath))
|
||||
for gv, pth := range *s.gvToPath {
|
||||
strs = append(strs, path.Join(pth, gv.Group.String(), gv.Version.String()))
|
||||
}
|
||||
str, _ := writeAsCSV(strs)
|
||||
return "[" + str + "]"
|
||||
}
|
||||
|
||||
func parsePathGroupVersion(pgvString string) (gvPath string, gvString string) {
|
||||
subs := strings.Split(pgvString, "/")
|
||||
length := len(subs)
|
||||
switch length {
|
||||
case 0, 1, 2:
|
||||
return "", pgvString
|
||||
default:
|
||||
return strings.Join(subs[:length-2], "/"), strings.Join(subs[length-2:], "/")
|
||||
}
|
||||
}
|
||||
|
||||
func groupVersionPath(gvPath string, group string, version string) (path string) {
|
||||
// special case for the core group
|
||||
if group == "api" {
|
||||
path = filepath.Join("core", version)
|
||||
} else {
|
||||
path = filepath.Join(gvPath, group, version)
|
||||
}
|
||||
return
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
Copyright 2017 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 args
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
"k8s.io/code-generator/cmd/client-gen/types"
|
||||
)
|
||||
|
||||
func TestGVPackageFlag(t *testing.T) {
|
||||
tests := []struct {
|
||||
args []string
|
||||
def []string
|
||||
expected map[types.GroupVersion]string
|
||||
expectedGroups []types.GroupVersions
|
||||
parseError string
|
||||
}{
|
||||
{
|
||||
args: []string{},
|
||||
expected: map[types.GroupVersion]string{},
|
||||
expectedGroups: []types.GroupVersions{},
|
||||
},
|
||||
{
|
||||
args: []string{"foo/bar/v1", "foo/bar/v2", "foo/bar/", "foo/v1"},
|
||||
expected: map[types.GroupVersion]string{
|
||||
{Group: "bar", Version: ""}: "foo/bar",
|
||||
{Group: "bar", Version: "v1"}: "foo/bar/v1",
|
||||
{Group: "bar", Version: "v2"}: "foo/bar/v2",
|
||||
{Group: "foo", Version: "v1"}: "foo/v1",
|
||||
},
|
||||
expectedGroups: []types.GroupVersions{
|
||||
{PackageName: "bar", Group: types.Group("bar"), Versions: []types.Version{types.Version("v1"), types.Version("v2"), types.Version("")}},
|
||||
{PackageName: "foo", Group: types.Group("foo"), Versions: []types.Version{types.Version("v1")}},
|
||||
},
|
||||
},
|
||||
{
|
||||
args: []string{"foo/bar/v1", "foo/bar/v2", "foo/bar/", "foo/v1"},
|
||||
def: []string{"foo/bar/v1alpha1", "foo/v1"},
|
||||
expected: map[types.GroupVersion]string{
|
||||
{Group: "bar", Version: ""}: "foo/bar",
|
||||
{Group: "bar", Version: "v1"}: "foo/bar/v1",
|
||||
{Group: "bar", Version: "v2"}: "foo/bar/v2",
|
||||
{Group: "foo", Version: "v1"}: "foo/v1",
|
||||
},
|
||||
expectedGroups: []types.GroupVersions{
|
||||
{PackageName: "bar", Group: types.Group("bar"), Versions: []types.Version{types.Version("v1"), types.Version("v2"), types.Version("")}},
|
||||
{PackageName: "foo", Group: types.Group("foo"), Versions: []types.Version{types.Version("v1")}},
|
||||
},
|
||||
},
|
||||
{
|
||||
args: []string{"api/v1", "api"},
|
||||
expected: map[types.GroupVersion]string{
|
||||
{Group: "api", Version: "v1"}: "core/v1",
|
||||
{Group: "api", Version: ""}: "core",
|
||||
},
|
||||
expectedGroups: []types.GroupVersions{
|
||||
{PackageName: "core", Group: types.Group("api"), Versions: []types.Version{types.Version("v1"), types.Version("")}},
|
||||
},
|
||||
},
|
||||
}
|
||||
for i, test := range tests {
|
||||
fs := pflag.NewFlagSet("testGVPackage", pflag.ContinueOnError)
|
||||
gvp := map[types.GroupVersion]string{}
|
||||
groups := []types.GroupVersions{}
|
||||
fs.Var(NewGVPackagesValue(&gvp, &groups, test.def), "input", "usage")
|
||||
|
||||
args := []string{}
|
||||
for _, a := range test.args {
|
||||
args = append(args, fmt.Sprintf("--input=%s", a))
|
||||
}
|
||||
|
||||
err := fs.Parse(args)
|
||||
if test.parseError != "" {
|
||||
if err == nil {
|
||||
t.Errorf("%d: expected error %q, got nil", i, test.parseError)
|
||||
} else if !strings.Contains(err.Error(), test.parseError) {
|
||||
t.Errorf("%d: expected error %q, got %q", i, test.parseError, err)
|
||||
}
|
||||
} else if err != nil {
|
||||
t.Errorf("%d: expected nil error, got %v", i, err)
|
||||
}
|
||||
if !reflect.DeepEqual(gvp, test.expected) {
|
||||
t.Errorf("%d: expected %+v, got %+v", i, test.expected, gvp)
|
||||
}
|
||||
if !reflect.DeepEqual(groups, test.expectedGroups) {
|
||||
t.Errorf("%d: expected groups %+v, got groups %+v", i, test.expectedGroups, groups)
|
||||
}
|
||||
}
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
Copyright 2017 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 args
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"k8s.io/code-generator/cmd/client-gen/types"
|
||||
)
|
||||
|
||||
type gvTypeValue struct {
|
||||
gvToTypes *map[types.GroupVersion][]string
|
||||
changed bool
|
||||
}
|
||||
|
||||
func NewGVTypesValue(gvToTypes *map[types.GroupVersion][]string, def []string) *gvTypeValue {
|
||||
gvt := new(gvTypeValue)
|
||||
gvt.gvToTypes = gvToTypes
|
||||
if def != nil {
|
||||
if err := gvt.set(def); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return gvt
|
||||
}
|
||||
|
||||
var _ flag.Value = &gvTypeValue{}
|
||||
|
||||
func (s *gvTypeValue) set(vs []string) error {
|
||||
if !s.changed {
|
||||
*s.gvToTypes = map[types.GroupVersion][]string{}
|
||||
}
|
||||
|
||||
for _, input := range vs {
|
||||
gvString, typeStr, err := parseGroupVersionType(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gv, err := types.ToGroupVersion(gvString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
types, ok := (*s.gvToTypes)[gv]
|
||||
if !ok {
|
||||
types = []string{}
|
||||
}
|
||||
types = append(types, typeStr)
|
||||
(*s.gvToTypes)[gv] = types
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *gvTypeValue) Set(val string) error {
|
||||
vs, err := readAsCSV(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.set(vs); err != nil {
|
||||
return err
|
||||
}
|
||||
s.changed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *gvTypeValue) Type() string {
|
||||
return "stringSlice"
|
||||
}
|
||||
|
||||
func (s *gvTypeValue) String() string {
|
||||
strs := make([]string, 0, len(*s.gvToTypes))
|
||||
for gv, ts := range *s.gvToTypes {
|
||||
for _, t := range ts {
|
||||
strs = append(strs, gv.Group.String()+"/"+gv.Version.String()+"/"+t)
|
||||
}
|
||||
}
|
||||
str, _ := writeAsCSV(strs)
|
||||
return "[" + str + "]"
|
||||
}
|
||||
|
||||
func parseGroupVersionType(gvtString string) (gvString string, typeStr string, err error) {
|
||||
invalidFormatErr := fmt.Errorf("invalid value: %s, should be of the form group/version/type", gvtString)
|
||||
subs := strings.Split(gvtString, "/")
|
||||
length := len(subs)
|
||||
switch length {
|
||||
case 2:
|
||||
// gvtString of the form group/type, e.g. api/Service,extensions/ReplicaSet
|
||||
return subs[0] + "/", subs[1], nil
|
||||
case 3:
|
||||
return strings.Join(subs[:length-1], "/"), subs[length-1], nil
|
||||
default:
|
||||
return "", "", invalidFormatErr
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user