mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-08 20:50:24 +00:00
Kubectl namespace support
Add unit test for load namespace info Different message on display of namespace versus setting namespace
This commit is contained in:
@@ -31,6 +31,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
|
||||
"gopkg.in/v1/yaml"
|
||||
)
|
||||
@@ -68,6 +69,39 @@ type AuthInfo struct {
|
||||
Insecure *bool
|
||||
}
|
||||
|
||||
type NamespaceInfo struct {
|
||||
Namespace string
|
||||
}
|
||||
|
||||
// LoadNamespaceInfo parses a NamespaceInfo object from a file path. It creates a file at the specified path if it doesn't exist with the default namespace.
|
||||
func LoadNamespaceInfo(path string) (*NamespaceInfo, error) {
|
||||
var ns NamespaceInfo
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
ns.Namespace = api.NamespaceDefault
|
||||
err = SaveNamespaceInfo(path, &ns)
|
||||
return &ns, err
|
||||
}
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(data, &ns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ns, err
|
||||
}
|
||||
|
||||
// SaveNamespaceInfo saves a NamespaceInfo object at the specified file path.
|
||||
func SaveNamespaceInfo(path string, ns *NamespaceInfo) error {
|
||||
if !util.IsDNSLabel(ns.Namespace) {
|
||||
return fmt.Errorf("Namespace %s is not a valid DNS Label", ns.Namespace)
|
||||
}
|
||||
data, err := json.Marshal(ns)
|
||||
err = ioutil.WriteFile(path, data, 0600)
|
||||
return err
|
||||
}
|
||||
|
||||
// LoadAuthInfo parses an AuthInfo object from a file path. It prompts user and creates file if it doesn't exist.
|
||||
func LoadAuthInfo(path string, r io.Reader) (*AuthInfo, error) {
|
||||
var auth AuthInfo
|
||||
|
Reference in New Issue
Block a user