-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ibuildthecloud/master
Add kubeconfig loader helper code
- Loading branch information
Showing
9 changed files
with
123 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package gvk | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/rancher/wrangler/pkg/schemes" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
func Get(obj runtime.Object) (schema.GroupVersionKind, error) { | ||
gvk := obj.GetObjectKind().GroupVersionKind() | ||
if gvk.Kind != "" { | ||
return gvk, nil | ||
} | ||
|
||
gvks, _, err := schemes.All.ObjectKinds(obj) | ||
if err != nil { | ||
return schema.GroupVersionKind{}, err | ||
} | ||
|
||
if len(gvks) == 0 { | ||
return schema.GroupVersionKind{}, fmt.Errorf("failed to find gvk for %v", obj.GetObjectKind()) | ||
} | ||
|
||
return gvks[0], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package kubeconfig | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func GetNonInteractiveClientConfig(kubeConfig string) clientcmd.ClientConfig { | ||
return GetClientConfig(kubeConfig, nil) | ||
} | ||
|
||
func GetInteractiveClientConfig(kubeConfig string) clientcmd.ClientConfig { | ||
return GetClientConfig(kubeConfig, os.Stdin) | ||
} | ||
|
||
func GetClientConfig(kubeConfig string, reader io.Reader) clientcmd.ClientConfig { | ||
loadingRules := GetLoadingRules(kubeConfig) | ||
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults} | ||
return clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, reader) | ||
} | ||
|
||
func GetLoadingRules(kubeConfig string) *clientcmd.ClientConfigLoadingRules { | ||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() | ||
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig | ||
if kubeConfig != "" { | ||
loadingRules.ExplicitPath = kubeConfig | ||
} | ||
|
||
homeDir, err := os.UserHomeDir() | ||
if err == nil { | ||
loadingRules.Precedence = append(loadingRules.Precedence, filepath.Join(homeDir, ".kube", "k3s.yaml")) | ||
} | ||
|
||
loadingRules.Precedence = append(loadingRules.Precedence, "/etc/rancher/k3s/k3s.yaml") | ||
return loadingRules | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package schemes | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
var All = runtime.NewScheme() |