Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
203246b7
Commit
203246b7
authored
Dec 12, 2014
by
Clayton Coleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow the api-version on the command to override the client builder
parent
158f3223
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
7 deletions
+70
-7
client_builder.go
pkg/client/clientcmd/client_builder.go
+32
-6
client_builder_test.go
pkg/client/clientcmd/client_builder_test.go
+35
-0
cmd.go
pkg/kubectl/cmd/cmd.go
+3
-1
No files found.
pkg/client/clientcmd/client_builder.go
View file @
203246b7
...
@@ -33,10 +33,15 @@ import (
...
@@ -33,10 +33,15 @@ import (
type
Builder
interface
{
type
Builder
interface
{
// BindFlags must bind and keep track of all the flags required to build a client config object
// BindFlags must bind and keep track of all the flags required to build a client config object
BindFlags
(
flags
*
pflag
.
FlagSet
)
BindFlags
(
flags
*
pflag
.
FlagSet
)
// Config uses the values of the bound flags and builds a complete client config
Config
()
(
*
client
.
Config
,
error
)
// Client calls BuildConfig under the covers and uses that config to return a client
// Client calls BuildConfig under the covers and uses that config to return a client
Client
()
(
*
client
.
Client
,
error
)
Client
()
(
*
client
.
Client
,
error
)
// Config uses the values of the bound flags and builds a complete client config
Config
()
(
*
client
.
Config
,
error
)
// Override invokes Config(), then passes that to the provided function, and returns a new
// builder that will use that config as its default. If Config() returns an error for the default
// values the function will not be invoked, and the error will be available when Client() is called.
Override
(
func
(
*
client
.
Config
))
Builder
}
}
// cmdAuthInfo is used to track whether flags have been set
// cmdAuthInfo is used to track whether flags have been set
...
@@ -58,6 +63,8 @@ type builder struct {
...
@@ -58,6 +63,8 @@ type builder struct {
apiserver
string
apiserver
string
apiVersion
string
apiVersion
string
matchApiVersion
bool
matchApiVersion
bool
config
*
client
.
Config
}
}
// NewBuilder returns a valid Builder that uses the passed authLoader. If authLoader is nil, the NewDefaultAuthLoader is used.
// NewBuilder returns a valid Builder that uses the passed authLoader. If authLoader is nil, the NewDefaultAuthLoader is used.
...
@@ -124,6 +131,26 @@ func (builder *builder) Client() (*client.Client, error) {
...
@@ -124,6 +131,26 @@ func (builder *builder) Client() (*client.Client, error) {
// Config implements Builder
// Config implements Builder
func
(
builder
*
builder
)
Config
()
(
*
client
.
Config
,
error
)
{
func
(
builder
*
builder
)
Config
()
(
*
client
.
Config
,
error
)
{
if
builder
.
config
!=
nil
{
return
builder
.
config
,
nil
}
return
builder
.
newConfig
()
}
// Override implements Builder
func
(
builder
*
builder
)
Override
(
fn
func
(
*
client
.
Config
))
Builder
{
config
,
err
:=
builder
.
newConfig
()
if
err
!=
nil
{
return
builder
}
fn
(
config
)
b
:=
*
builder
b
.
config
=
config
return
&
b
}
// newConfig creates a new config object for this builder
func
(
builder
*
builder
)
newConfig
()
(
*
client
.
Config
,
error
)
{
clientConfig
:=
client
.
Config
{}
clientConfig
:=
client
.
Config
{}
if
len
(
builder
.
apiserver
)
>
0
{
if
len
(
builder
.
apiserver
)
>
0
{
clientConfig
.
Host
=
builder
.
apiserver
clientConfig
.
Host
=
builder
.
apiserver
...
@@ -140,12 +167,11 @@ func (builder *builder) Config() (*client.Config, error) {
...
@@ -140,12 +167,11 @@ func (builder *builder) Config() (*client.Config, error) {
authInfoFileFound
:=
true
authInfoFileFound
:=
true
authInfo
,
err
:=
builder
.
authLoader
.
LoadAuth
(
builder
.
authPath
)
authInfo
,
err
:=
builder
.
authLoader
.
LoadAuth
(
builder
.
authPath
)
if
authInfo
==
nil
&&
err
!=
nil
{
// only consider failing if we don't have any auth info
if
authInfo
==
nil
&&
err
!=
nil
{
// only consider failing if we don't have any auth info
if
os
.
IsNotExist
(
err
)
{
// if it's just a case of a missing file, simply flag the auth as not found and use the command line arguments
if
!
os
.
IsNotExist
(
err
)
{
// if it's just a case of a missing file, simply flag the auth as not found and use the command line arguments
authInfoFileFound
=
false
authInfo
=
&
clientauth
.
Info
{}
}
else
{
return
nil
,
err
return
nil
,
err
}
}
authInfoFileFound
=
false
authInfo
=
&
clientauth
.
Info
{}
}
}
// If provided, the command line options override options from the auth file
// If provided, the command line options override options from the auth file
...
...
pkg/client/clientcmd/client_builder_test.go
View file @
203246b7
...
@@ -29,6 +29,7 @@ import (
...
@@ -29,6 +29,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/pflag"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth"
"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth"
)
)
...
@@ -251,6 +252,40 @@ func TestLoadClientAuthInfoOrPrompt(t *testing.T) {
...
@@ -251,6 +252,40 @@ func TestLoadClientAuthInfoOrPrompt(t *testing.T) {
}
}
}
}
func
TestOverride
(
t
*
testing
.
T
)
{
b
:=
NewBuilder
(
nil
)
cfg
,
err
:=
b
.
Config
()
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
cfg
.
Version
!=
""
{
t
.
Errorf
(
"unexpected default config version"
)
}
newCfg
,
err
:=
b
.
Override
(
func
(
cfg
*
client
.
Config
)
{
if
cfg
.
Version
!=
""
{
t
.
Errorf
(
"unexpected default config version"
)
}
cfg
.
Version
=
"test"
})
.
Config
()
if
newCfg
.
Version
!=
"test"
{
t
.
Errorf
(
"unexpected override config version"
)
}
if
cfg
.
Version
!=
""
{
t
.
Errorf
(
"original object should not change"
)
}
cfg
,
err
=
b
.
Config
()
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
cfg
.
Version
!=
""
{
t
.
Errorf
(
"override should not be persistent"
)
}
}
func
matchStringArg
(
expected
,
got
string
,
t
*
testing
.
T
)
{
func
matchStringArg
(
expected
,
got
string
,
t
*
testing
.
T
)
{
if
expected
!=
got
{
if
expected
!=
got
{
t
.
Errorf
(
"Expected %v, got %v"
,
expected
,
got
)
t
.
Errorf
(
"Expected %v, got %v"
,
expected
,
got
)
...
...
pkg/kubectl/cmd/cmd.go
View file @
203246b7
...
@@ -64,7 +64,9 @@ func NewFactory(clientBuilder clientcmd.Builder) *Factory {
...
@@ -64,7 +64,9 @@ func NewFactory(clientBuilder clientcmd.Builder) *Factory {
}
}
},
},
Client
:
func
(
cmd
*
cobra
.
Command
,
mapping
*
meta
.
RESTMapping
)
(
kubectl
.
RESTClient
,
error
)
{
Client
:
func
(
cmd
*
cobra
.
Command
,
mapping
*
meta
.
RESTMapping
)
(
kubectl
.
RESTClient
,
error
)
{
return
clientBuilder
.
Client
()
return
clientBuilder
.
Override
(
func
(
c
*
client
.
Config
)
{
c
.
Version
=
mapping
.
APIVersion
})
.
Client
()
},
},
Describer
:
func
(
cmd
*
cobra
.
Command
,
mapping
*
meta
.
RESTMapping
)
(
kubectl
.
Describer
,
error
)
{
Describer
:
func
(
cmd
*
cobra
.
Command
,
mapping
*
meta
.
RESTMapping
)
(
kubectl
.
Describer
,
error
)
{
client
,
err
:=
clientBuilder
.
Client
()
client
,
err
:=
clientBuilder
.
Client
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment