2015-11-19 21:54:41 +00:00
/ *
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 .
* /
// client-gen makes the individual typed clients using go2idl.
package main
import (
2015-12-28 05:54:08 +00:00
"fmt"
"path/filepath"
2015-11-19 21:54:41 +00:00
"k8s.io/kubernetes/cmd/libs/go2idl/args"
2016-02-16 22:16:45 +00:00
clientgenargs "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/args"
2015-11-19 21:54:41 +00:00
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators"
2015-12-28 05:54:08 +00:00
"k8s.io/kubernetes/pkg/api/unversioned"
2015-11-19 21:54:41 +00:00
"github.com/golang/glog"
2015-12-04 01:01:33 +00:00
flag "github.com/spf13/pflag"
2015-11-19 21:54:41 +00:00
)
2015-12-28 05:54:08 +00:00
var (
test = flag . BoolP ( "test" , "t" , false , "set this flag to generate the client code for the testdata" )
inputVersions = flag . StringSlice ( "input" , [ ] string { "api/" , "extensions/" } , "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...\". Default to \"api/,extensions\"" )
2016-02-05 21:58:03 +00:00
clientsetName = flag . StringP ( "clientset-name" , "n" , "internalclientset" , "the name of the generated clientset package." )
2015-12-28 05:54:08 +00:00
clientsetPath = flag . String ( "clientset-path" , "k8s.io/kubernetes/pkg/client/clientset_generated/" , "the generated clientset will be output to <clientset-path>/<clientset-name>. Default to \"k8s.io/kubernetes/pkg/client/clientset_generated/\"" )
clientsetOnly = flag . Bool ( "clientset-only" , false , "when set, client-gen only generates the clientset shell, without generating the individual typed clients" )
2016-02-08 21:00:09 +00:00
fakeClient = flag . Bool ( "fake-clientset" , true , "when set, client-gen will generate the fake clientset that can be used in tests" )
2015-12-28 05:54:08 +00:00
)
func versionToPath ( group string , version string ) ( path string ) {
const base = "k8s.io/kubernetes/pkg"
2016-02-03 21:21:05 +00:00
// special case for the core group
2015-12-28 05:54:08 +00:00
if group == "api" {
path = filepath . Join ( base , "api" , version )
} else {
path = filepath . Join ( base , "apis" , group , version )
}
return
}
2016-02-08 21:00:09 +00:00
func parseInputVersions ( ) ( paths [ ] string , groupVersions [ ] unversioned . GroupVersion , gvToPath map [ unversioned . GroupVersion ] string , err error ) {
2015-12-28 05:54:08 +00:00
var visitedGroups = make ( map [ string ] struct { } )
2016-02-08 21:00:09 +00:00
gvToPath = make ( map [ unversioned . GroupVersion ] string )
2015-12-28 05:54:08 +00:00
for _ , gvString := range * inputVersions {
gv , err := unversioned . ParseGroupVersion ( gvString )
if err != nil {
2016-02-08 21:00:09 +00:00
return nil , nil , nil , err
2015-12-28 05:54:08 +00:00
}
if _ , found := visitedGroups [ gv . Group ] ; found {
2016-02-08 21:00:09 +00:00
return nil , nil , nil , fmt . Errorf ( "group %q appeared more than once in the input. At most one version is allowed for each group." , gv . Group )
2015-12-28 05:54:08 +00:00
}
visitedGroups [ gv . Group ] = struct { } { }
groupVersions = append ( groupVersions , gv )
2016-02-08 21:00:09 +00:00
path := versionToPath ( gv . Group , gv . Version )
paths = append ( paths , path )
gvToPath [ gv ] = path
2015-12-28 05:54:08 +00:00
}
2016-02-08 21:00:09 +00:00
return paths , groupVersions , gvToPath , nil
2015-12-28 05:54:08 +00:00
}
2015-12-04 01:01:33 +00:00
2015-11-19 21:54:41 +00:00
func main ( ) {
arguments := args . Default ( )
2015-12-04 01:01:33 +00:00
flag . Parse ( )
2016-03-13 23:26:26 +00:00
var cmdArgs string
flag . VisitAll ( func ( f * flag . Flag ) {
2016-03-14 01:31:05 +00:00
if ! f . Changed || f . Name == "verify-only" {
2016-03-13 23:26:26 +00:00
return
}
cmdArgs = cmdArgs + fmt . Sprintf ( "--%s=%s " , f . Name , f . Value )
} )
2015-12-28 05:54:08 +00:00
dependencies := [ ] string {
"k8s.io/kubernetes/pkg/fields" ,
"k8s.io/kubernetes/pkg/labels" ,
"k8s.io/kubernetes/pkg/watch" ,
"k8s.io/kubernetes/pkg/client/unversioned" ,
2016-01-13 22:40:56 +00:00
"k8s.io/kubernetes/pkg/apimachinery/registered" ,
2015-12-28 05:54:08 +00:00
}
2015-12-04 01:01:33 +00:00
if * test {
2015-12-28 05:54:08 +00:00
arguments . InputDirs = append ( dependencies , [ ] string {
2016-04-08 17:39:21 +00:00
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup.k8s.io" ,
2015-12-28 05:54:08 +00:00
} ... )
2016-02-16 22:16:45 +00:00
arguments . CustomArgs = clientgenargs . Args {
2016-04-08 17:39:21 +00:00
GroupVersions : [ ] unversioned . GroupVersion { { Group : "testgroup.k8s.io" , Version : "" } } ,
2016-04-12 15:49:32 +00:00
GroupVersionToInputPath : map [ unversioned . GroupVersion ] string {
2016-04-08 17:39:21 +00:00
unversioned . GroupVersion { Group : "testgroup.k8s.io" , Version : "" } : "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup.k8s.io" ,
2016-02-08 21:00:09 +00:00
} ,
2016-04-12 15:49:32 +00:00
ClientsetName : "test_internalclientset" ,
ClientsetOutputPath : "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/" ,
ClientsetOnly : false ,
FakeClient : false ,
CmdArgs : cmdArgs ,
2015-12-28 05:54:08 +00:00
}
2015-12-04 01:01:33 +00:00
} else {
2016-02-08 21:00:09 +00:00
inputPath , groupVersions , gvToPath , err := parseInputVersions ( )
2015-12-28 05:54:08 +00:00
if err != nil {
glog . Fatalf ( "Error: %v" , err )
2015-12-04 01:01:33 +00:00
}
2016-01-29 19:35:04 +00:00
glog . Infof ( "going to generate clientset from these input paths: %v" , inputPath )
2015-12-28 05:54:08 +00:00
arguments . InputDirs = append ( inputPath , dependencies ... )
2016-02-16 22:16:45 +00:00
arguments . CustomArgs = clientgenargs . Args {
2016-04-12 15:49:32 +00:00
GroupVersions : groupVersions ,
GroupVersionToInputPath : gvToPath ,
ClientsetName : * clientsetName ,
ClientsetOutputPath : * clientsetPath ,
ClientsetOnly : * clientsetOnly ,
FakeClient : * fakeClient ,
CmdArgs : cmdArgs ,
2015-12-28 05:54:08 +00:00
}
2015-11-19 21:54:41 +00:00
}
if err := arguments . Execute (
generators . NameSystems ( ) ,
generators . DefaultNameSystem ( ) ,
generators . Packages ,
) ; err != nil {
glog . Fatalf ( "Error: %v" , err )
}
}