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
67f53d2e
Commit
67f53d2e
authored
Jun 01, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make kubectl work inside a container in k8s
parent
e3b80db0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
1 deletion
+43
-1
client_config.go
pkg/client/clientcmd/client_config.go
+30
-0
merged_client_builder.go
pkg/client/clientcmd/merged_client_builder.go
+5
-0
factory.go
pkg/kubectl/cmd/util/factory.go
+8
-1
No files found.
pkg/client/clientcmd/client_config.go
View file @
67f53d2e
...
@@ -17,6 +17,7 @@ limitations under the License.
...
@@ -17,6 +17,7 @@ limitations under the License.
package
clientcmd
package
clientcmd
import
(
import
(
"fmt"
"io"
"io"
"os"
"os"
...
@@ -284,3 +285,32 @@ func (config DirectClientConfig) getCluster() clientcmdapi.Cluster {
...
@@ -284,3 +285,32 @@ func (config DirectClientConfig) getCluster() clientcmdapi.Cluster {
return
mergedClusterInfo
return
mergedClusterInfo
}
}
// inClusterClientConfig makes a config that will work from within a kubernetes cluster container environment.
type
inClusterClientConfig
struct
{}
func
(
inClusterClientConfig
)
RawConfig
()
(
clientcmdapi
.
Config
,
error
)
{
return
clientcmdapi
.
Config
{},
fmt
.
Errorf
(
"inCluster environment config doesn't support multiple clusters"
)
}
func
(
inClusterClientConfig
)
ClientConfig
()
(
*
client
.
Config
,
error
)
{
return
client
.
InClusterConfig
()
}
func
(
inClusterClientConfig
)
Namespace
()
(
string
,
error
)
{
// TODO: generic way to figure out what namespace you are running in?
// This way assumes you've set the POD_NAMESPACE environment variable
// using the downward API.
if
ns
:=
os
.
Getenv
(
"POD_NAMESPACE"
);
ns
!=
""
{
return
ns
,
nil
}
return
"default"
,
nil
}
// Possible returns true if loading an inside-kubernetes-cluster is possible.
func
(
inClusterClientConfig
)
Possible
()
bool
{
fi
,
err
:=
os
.
Stat
(
"/var/run/secrets/kubernetes.io/serviceaccount/token"
)
return
os
.
Getenv
(
"KUBERNETES_SERVICE_HOST"
)
!=
""
&&
os
.
Getenv
(
"KUBERNETES_SERVICE_PORT"
)
!=
""
&&
err
==
nil
&&
!
fi
.
IsDir
()
}
pkg/client/clientcmd/merged_client_builder.go
View file @
67f53d2e
...
@@ -45,6 +45,11 @@ func NewInteractiveDeferredLoadingClientConfig(loadingRules *ClientConfigLoading
...
@@ -45,6 +45,11 @@ func NewInteractiveDeferredLoadingClientConfig(loadingRules *ClientConfigLoading
}
}
func
(
config
DeferredLoadingClientConfig
)
createClientConfig
()
(
ClientConfig
,
error
)
{
func
(
config
DeferredLoadingClientConfig
)
createClientConfig
()
(
ClientConfig
,
error
)
{
// Are we running in a cluster? If so, use that.
icc
:=
inClusterClientConfig
{}
if
icc
.
Possible
()
{
return
icc
,
nil
}
mergedConfig
,
err
:=
config
.
loadingRules
.
Load
()
mergedConfig
,
err
:=
config
.
loadingRules
.
Load
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
...
pkg/kubectl/cmd/util/factory.go
View file @
67f53d2e
...
@@ -290,7 +290,7 @@ func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
...
@@ -290,7 +290,7 @@ func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
// 1. CommandLineLocation - this parsed from the command line, so it must be late bound. If you specify this,
// 1. CommandLineLocation - this parsed from the command line, so it must be late bound. If you specify this,
// then no other kubeconfig files are merged. This file must exist.
// then no other kubeconfig files are merged. This file must exist.
// 2. If $KUBECONFIG is set, then it is treated as a list of files that should be merged.
// 2. If $KUBECONFIG is set, then it is treated as a list of files that should be merged.
//
3. HomeDirectoryLocation
//
3. HomeDirectoryLocation
// Empty filenames are ignored. Files with non-deserializable content produced errors.
// Empty filenames are ignored. Files with non-deserializable content produced errors.
// The first file to set a particular value or map key wins and the value or map key is never changed.
// The first file to set a particular value or map key wins and the value or map key is never changed.
// This means that the first file to set CurrentContext will have its context preserved. It also means
// This means that the first file to set CurrentContext will have its context preserved. It also means
...
@@ -316,6 +316,13 @@ func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
...
@@ -316,6 +316,13 @@ func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
// 2. If the command line does not specify one, and the auth info has conflicting techniques, fail.
// 2. If the command line does not specify one, and the auth info has conflicting techniques, fail.
// 3. If the command line specifies one and the auth info specifies another, honor the command line technique.
// 3. If the command line specifies one and the auth info specifies another, honor the command line technique.
// 2. Use default values and potentially prompt for auth information
// 2. Use default values and potentially prompt for auth information
//
// However, if it appears that we're running in a kubernetes cluster
// container environment, then run with the auth info kubernetes mounted for
// us. Specifically:
// The env vars KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT are
// set, and the file /var/run/secrets/kubernetes.io/serviceaccount/token
// exists and is not a directory.
func
DefaultClientConfig
(
flags
*
pflag
.
FlagSet
)
clientcmd
.
ClientConfig
{
func
DefaultClientConfig
(
flags
*
pflag
.
FlagSet
)
clientcmd
.
ClientConfig
{
loadingRules
:=
clientcmd
.
NewDefaultClientConfigLoadingRules
()
loadingRules
:=
clientcmd
.
NewDefaultClientConfigLoadingRules
()
flags
.
StringVar
(
&
loadingRules
.
ExplicitPath
,
"kubeconfig"
,
""
,
"Path to the kubeconfig file to use for CLI requests."
)
flags
.
StringVar
(
&
loadingRules
.
ExplicitPath
,
"kubeconfig"
,
""
,
"Path to the kubeconfig file to use for CLI requests."
)
...
...
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