Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate fake client #19734

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions cmd/libs/go2idl/client-gen/generators/client-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"k8s.io/kubernetes/cmd/libs/go2idl/args"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators/fake"
"k8s.io/kubernetes/cmd/libs/go2idl/generator"
"k8s.io/kubernetes/cmd/libs/go2idl/namer"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
Expand All @@ -47,6 +48,8 @@ type ClientGenArgs struct {
// 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
}

// NameSystems returns the name system used by the generators in this package.
Expand Down Expand Up @@ -188,14 +191,21 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
}

var packageList []generator.Package

packageList = append(packageList, packageForClientset(customArgs, arguments.OutputPackagePath, boilerplate))

// If --clientset-only=true, we don't regenerate the individual typed clients.
if !customArgs.ClientsetOnly {
orderer := namer.Orderer{namer.NewPrivateNamer(0)}
for group, types := range groupToTypes {
packageList = append(packageList, packageForGroup(group, "unversioned", orderer.OrderTypes(types), arguments.OutputPackagePath, arguments.OutputBase, boilerplate))
if customArgs.ClientsetOnly {
return generator.Packages(packageList)
}

orderer := namer.Orderer{namer.NewPrivateNamer(0)}
for group, types := range groupToTypes {
packageList = append(packageList, packageForGroup(group, "unversioned", orderer.OrderTypes(types), arguments.OutputPackagePath, arguments.OutputBase, boilerplate))
if customArgs.FakeClient {
packageList = append(packageList, fake.PackageForGroup(group, "unversioned", orderer.OrderTypes(types), arguments.OutputPackagePath, arguments.OutputBase, boilerplate))
}
}

packageList = append(packageList, packageForClientset(customArgs, arguments.OutputPackagePath, boilerplate))
return generator.Packages(packageList)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.

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 fake

import (
"path/filepath"
"strings"

"k8s.io/kubernetes/cmd/libs/go2idl/generator"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
)

func PackageForGroup(group string, version string, typeList []*types.Type, packageBasePath string, srcTreePath string, boilerplate []byte) generator.Package {
outputPackagePath := filepath.Join(packageBasePath, group, version, "fake")
// TODO: should make this a function, called by here and in client-generator.go
realClientPath := filepath.Join(packageBasePath, group, version)
return &generator.DefaultPackage{
PackageName: "fake",
PackagePath: outputPackagePath,
HeaderText: boilerplate,
PackageDocumentation: []byte(
`// Package fake has the automatically generated clients.
`),
// GeneratorFunc returns a list of generators. Each generator makes a
// single file.
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
generators = []generator.Generator{
// Always generate a "doc.go" file.
generator.DefaultGen{OptionalName: "doc"},
}
// Since we want a file per type that we generate a client for, we
// have to provide a function for this.
for _, t := range typeList {
generators = append(generators, &genFakeForType{
DefaultGen: generator.DefaultGen{
OptionalName: "fake_" + strings.ToLower(c.Namers["private"].Name(t)),
},
outputPackage: outputPackagePath,
group: group,
typeToMatch: t,
imports: generator.NewImportTracker(),
})
}

generators = append(generators, &genFakeForGroup{
DefaultGen: generator.DefaultGen{
OptionalName: "fake_" + group + "_client",
},
outputPackage: outputPackagePath,
realClientPath: realClientPath,
group: group,
types: typeList,
imports: generator.NewImportTracker(),
})
return generators
},
FilterFunc: func(c *generator.Context, t *types.Type) bool {
return types.ExtractCommentTags("+", t.SecondClosestCommentLines)["genclient"] == "true"
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.

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 fake

import (
"fmt"
"io"
"path/filepath"

"k8s.io/kubernetes/cmd/libs/go2idl/generator"
"k8s.io/kubernetes/cmd/libs/go2idl/namer"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
)

// genFakeForGroup produces a file for a group client, e.g. ExtensionsClient for the extension group.
type genFakeForGroup struct {
generator.DefaultGen
outputPackage string
realClientPath string
group string
// types in this group
types []*types.Type
imports *generator.ImportTracker
}

var _ generator.Generator = &genFakeForGroup{}

// We only want to call GenerateType() once per group.
func (g *genFakeForGroup) Filter(c *generator.Context, t *types.Type) bool {
return t == g.types[0]
}

func (g *genFakeForGroup) Namers(c *generator.Context) namer.NameSystems {
return namer.NameSystems{
"raw": namer.NewRawNamer(g.outputPackage, g.imports),
}
}

func (g *genFakeForGroup) Imports(c *generator.Context) (imports []string) {
return append(g.imports.ImportLines(), fmt.Sprintf("%s \"%s\"", filepath.Base(g.realClientPath), g.realClientPath))
}

func (g *genFakeForGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
sw := generator.NewSnippetWriter(w, c, "$", "$")
const pkgTestingCore = "k8s.io/kubernetes/pkg/client/testing/core"
m := map[string]interface{}{
"group": g.group,
"Group": namer.IC(g.group),
"Fake": c.Universe.Type(types.Name{Package: pkgTestingCore, Name: "Fake"}),
}
sw.Do(groupClientTemplate, m)
for _, t := range g.types {
wrapper := map[string]interface{}{
"type": t,
"Group": namer.IC(g.group),
"realClientPackage": filepath.Base(g.realClientPath),
}
namespaced := !(types.ExtractCommentTags("+", t.SecondClosestCommentLines)["nonNamespaced"] == "true")
if namespaced {
sw.Do(getterImplNamespaced, wrapper)
} else {
sw.Do(getterImplNonNamespaced, wrapper)

}
}
return sw.Error()
}

var groupClientTemplate = `
type Fake$.Group$ struct {
*$.Fake|raw$
}
`

var getterImplNamespaced = `
func (c *Fake$.Group$) $.type|publicPlural$(namespace string) $.realClientPackage$.$.type|public$Interface {
return &Fake$.type|publicPlural${c, namespace}
}
`

var getterImplNonNamespaced = `
func (c *Fake$.Group$) $.type|publicPlural$() $.realClientPackage$.$.type|public$Interface {
return &Fake$.type|publicPlural${c}
}
`