k8s.io/client-go@v0.31.1/tools/clientcmd/api/types_test.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package api 18 19 import ( 20 "fmt" 21 22 "sigs.k8s.io/yaml" 23 ) 24 25 func Example_emptyConfig() { 26 defaultConfig := NewConfig() 27 28 output, err := yaml.Marshal(defaultConfig) 29 if err != nil { 30 fmt.Printf("Unexpected error: %v", err) 31 } 32 33 fmt.Printf("%v", string(output)) 34 // Output: 35 // clusters: {} 36 // contexts: {} 37 // current-context: "" 38 // preferences: {} 39 // users: {} 40 } 41 42 func Example_ofOptionsConfig() { 43 defaultConfig := NewConfig() 44 defaultConfig.Preferences.Colors = true 45 defaultConfig.Clusters["alfa"] = &Cluster{ 46 Server: "https://alfa.org:8080", 47 InsecureSkipTLSVerify: true, 48 CertificateAuthority: "path/to/my/cert-ca-filename", 49 DisableCompression: true, 50 } 51 defaultConfig.Clusters["bravo"] = &Cluster{ 52 Server: "https://bravo.org:8080", 53 InsecureSkipTLSVerify: false, 54 DisableCompression: false, 55 } 56 defaultConfig.AuthInfos["white-mage-via-cert"] = &AuthInfo{ 57 ClientCertificate: "path/to/my/client-cert-filename", 58 ClientKey: "path/to/my/client-key-filename", 59 } 60 defaultConfig.AuthInfos["red-mage-via-token"] = &AuthInfo{ 61 Token: "my-secret-token", 62 } 63 defaultConfig.AuthInfos["black-mage-via-auth-provider"] = &AuthInfo{ 64 AuthProvider: &AuthProviderConfig{ 65 Name: "gcp", 66 Config: map[string]string{ 67 "foo": "bar", 68 "token": "s3cr3t-t0k3n", 69 }, 70 }, 71 } 72 defaultConfig.Contexts["bravo-as-black-mage"] = &Context{ 73 Cluster: "bravo", 74 AuthInfo: "black-mage-via-auth-provider", 75 Namespace: "yankee", 76 } 77 defaultConfig.Contexts["alfa-as-black-mage"] = &Context{ 78 Cluster: "alfa", 79 AuthInfo: "black-mage-via-auth-provider", 80 Namespace: "zulu", 81 } 82 defaultConfig.Contexts["alfa-as-white-mage"] = &Context{ 83 Cluster: "alfa", 84 AuthInfo: "white-mage-via-cert", 85 } 86 defaultConfig.CurrentContext = "alfa-as-white-mage" 87 88 output, err := yaml.Marshal(defaultConfig) 89 if err != nil { 90 fmt.Printf("Unexpected error: %v", err) 91 } 92 93 fmt.Printf("%v", string(output)) 94 // Output: 95 // clusters: 96 // alfa: 97 // certificate-authority: path/to/my/cert-ca-filename 98 // disable-compression: true 99 // insecure-skip-tls-verify: true 100 // server: https://alfa.org:8080 101 // bravo: 102 // server: https://bravo.org:8080 103 // contexts: 104 // alfa-as-black-mage: 105 // cluster: alfa 106 // namespace: zulu 107 // user: black-mage-via-auth-provider 108 // alfa-as-white-mage: 109 // cluster: alfa 110 // user: white-mage-via-cert 111 // bravo-as-black-mage: 112 // cluster: bravo 113 // namespace: yankee 114 // user: black-mage-via-auth-provider 115 // current-context: alfa-as-white-mage 116 // preferences: 117 // colors: true 118 // users: 119 // black-mage-via-auth-provider: 120 // auth-provider: 121 // config: 122 // foo: bar 123 // token: s3cr3t-t0k3n 124 // name: gcp 125 // red-mage-via-token: 126 // token: my-secret-token 127 // white-mage-via-cert: 128 // client-certificate: path/to/my/client-cert-filename 129 // client-key: path/to/my/client-key-filename 130 }