Пример #1
0
// Convert JSON to YAML.
func JSONToYAML(j []byte) ([]byte, error) {
	// Convert the JSON to an object.
	var jsonObj interface{}
	err := json.Unmarshal(j, &jsonObj)
	if err != nil {
		return nil, err
	}

	// Marshal this object into YAML.
	return yaml.Marshal(jsonObj)
}
Пример #2
0
// WriteToFile serializes the config to yaml and writes it out to a file.  If no present, it creates the file with 0644.  If it is present
// it stomps the contents
func WriteToFile(config Config, filename string) error {
	content, err := yaml.Marshal(config)
	if err != nil {
		return err
	}

	err = ioutil.WriteFile(filename, content, 0644)
	if err != nil {
		return err
	}

	return nil
}
Пример #3
0
func ExampleEmptyConfig() {
	defaultConfig := NewConfig()

	output, err := yaml.Marshal(defaultConfig)
	if err != nil {
		fmt.Printf("Unexpected error: %v", err)
	}

	fmt.Printf("%v", string(output))
	// Output:
	// preferences: {}
	// clusters: {}
	// users: {}
	// contexts: {}
	// current-context: ""
}
Пример #4
0
func ExampleMergingSomeWithConflict() {
	commandLineFile, _ := ioutil.TempFile("", "")
	defer os.Remove(commandLineFile.Name())
	envVarFile, _ := ioutil.TempFile("", "")
	defer os.Remove(envVarFile.Name())

	WriteToFile(testConfigAlfa, commandLineFile.Name())
	WriteToFile(testConfigConflictAlfa, envVarFile.Name())

	loadingRules := ClientConfigLoadingRules{
		CommandLinePath: commandLineFile.Name(),
		EnvVarPath:      envVarFile.Name(),
	}

	mergedConfig, err := loadingRules.Load()

	output, err := yaml.Marshal(mergedConfig)
	if err != nil {
		fmt.Printf("Unexpected error: %v", err)
	}

	fmt.Printf("%v", string(output))
	// Output:
	// preferences: {}
	// clusters:
	//   cow-cluster:
	//     server: http://cow.org:8080
	//   donkey-cluster:
	//     server: http://donkey.org:8080
	//     insecure-skip-tls-verify: true
	// users:
	//   red-user:
	//     token: red-token
	//   yellow-user:
	//     token: yellow-token
	// contexts:
	//   federal-context:
	//     cluster: cow-cluster
	//     user: red-user
	//     namespace: hammer-ns
	// current-context: federal-context
}
Пример #5
0
func ExampleOfOptionsConfig() {
	defaultConfig := NewConfig()
	defaultConfig.Preferences.Colors = true
	defaultConfig.Clusters["alfa"] = Cluster{
		Server:                "https://alfa.org:8080",
		APIVersion:            "v1beta2",
		InsecureSkipTLSVerify: true,
		CertificateAuthority:  "path/to/my/cert-ca-filename",
	}
	defaultConfig.Clusters["bravo"] = Cluster{
		Server:                "https://bravo.org:8080",
		APIVersion:            "v1beta1",
		InsecureSkipTLSVerify: false,
	}
	defaultConfig.AuthInfos["black-mage-via-file"] = AuthInfo{
		AuthPath: "path/to/my/.kubernetes_auth",
	}
	defaultConfig.AuthInfos["white-mage-via-cert"] = AuthInfo{
		ClientCertificate: "path/to/my/client-cert-filename",
		ClientKey:         "path/to/my/client-key-filename",
	}
	defaultConfig.AuthInfos["red-mage-via-token"] = AuthInfo{
		Token: "my-secret-token",
	}
	defaultConfig.Contexts["bravo-as-black-mage"] = Context{
		Cluster:   "bravo",
		AuthInfo:  "black-mage-via-file",
		Namespace: "yankee",
	}
	defaultConfig.Contexts["alfa-as-black-mage"] = Context{
		Cluster:   "alfa",
		AuthInfo:  "black-mage-via-file",
		Namespace: "zulu",
	}
	defaultConfig.Contexts["alfa-as-white-mage"] = Context{
		Cluster:  "alfa",
		AuthInfo: "white-mage-via-cert",
	}
	defaultConfig.CurrentContext = "alfa-as-white-mage"

	output, err := yaml.Marshal(defaultConfig)
	if err != nil {
		fmt.Printf("Unexpected error: %v", err)
	}

	fmt.Printf("%v", string(output))
	// Output:
	// preferences:
	//   colors: true
	// clusters:
	//   alfa:
	//     server: https://alfa.org:8080
	//     api-version: v1beta2
	//     insecure-skip-tls-verify: true
	//     certificate-authority: path/to/my/cert-ca-filename
	//   bravo:
	//     server: https://bravo.org:8080
	//     api-version: v1beta1
	// users:
	//   black-mage-via-file:
	//     auth-path: path/to/my/.kubernetes_auth
	//   red-mage-via-token:
	//     token: my-secret-token
	//   white-mage-via-cert:
	//     client-certificate: path/to/my/client-cert-filename
	//     client-key: path/to/my/client-key-filename
	// contexts:
	//   alfa-as-black-mage:
	//     cluster: alfa
	//     user: black-mage-via-file
	//     namespace: zulu
	//   alfa-as-white-mage:
	//     cluster: alfa
	//     user: white-mage-via-cert
	//   bravo-as-black-mage:
	//     cluster: bravo
	//     user: black-mage-via-file
	//     namespace: yankee
	// current-context: alfa-as-white-mage
}
Пример #6
0
func ExampleMergingEverythingNoConflicts() {
	commandLineFile, _ := ioutil.TempFile("", "")
	defer os.Remove(commandLineFile.Name())
	envVarFile, _ := ioutil.TempFile("", "")
	defer os.Remove(envVarFile.Name())
	currentDirFile, _ := ioutil.TempFile("", "")
	defer os.Remove(currentDirFile.Name())
	homeDirFile, _ := ioutil.TempFile("", "")
	defer os.Remove(homeDirFile.Name())

	WriteToFile(testConfigAlfa, commandLineFile.Name())
	WriteToFile(testConfigBravo, envVarFile.Name())
	WriteToFile(testConfigCharlie, currentDirFile.Name())
	WriteToFile(testConfigDelta, homeDirFile.Name())

	loadingRules := ClientConfigLoadingRules{
		CommandLinePath:      commandLineFile.Name(),
		EnvVarPath:           envVarFile.Name(),
		CurrentDirectoryPath: currentDirFile.Name(),
		HomeDirectoryPath:    homeDirFile.Name(),
	}

	mergedConfig, err := loadingRules.Load()

	output, err := yaml.Marshal(mergedConfig)
	if err != nil {
		fmt.Printf("Unexpected error: %v", err)
	}

	fmt.Printf("%v", string(output))
	// Output:
	// preferences: {}
	// clusters:
	//   chicken-cluster:
	//     server: http://chicken.org:8080
	//   cow-cluster:
	//     server: http://cow.org:8080
	//   horse-cluster:
	//     server: http://horse.org:8080
	//   pig-cluster:
	//     server: http://pig.org:8080
	// users:
	//   black-user:
	//     token: black-token
	//   blue-user:
	//     token: blue-token
	//   green-user:
	//     token: green-token
	//   red-user:
	//     token: red-token
	// contexts:
	//   federal-context:
	//     cluster: cow-cluster
	//     user: red-user
	//     namespace: hammer-ns
	//   gothic-context:
	//     cluster: chicken-cluster
	//     user: blue-user
	//     namespace: plane-ns
	//   queen-anne-context:
	//     cluster: pig-cluster
	//     user: black-user
	//     namespace: saw-ns
	//   shaker-context:
	//     cluster: horse-cluster
	//     user: green-user
	//     namespace: chisel-ns
	// current-context: ""
}