@@ -20,6 +20,7 @@ import (
20
20
"os"
21
21
"os/signal"
22
22
"regexp"
23
+ "sort"
23
24
"strings"
24
25
"sync"
25
26
"time"
@@ -30,6 +31,7 @@ import (
30
31
"github.com/codefresh-io/cli-v2/pkg/store"
31
32
32
33
"k8s.io/client-go/tools/clientcmd"
34
+ clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
33
35
)
34
36
35
37
const (
@@ -161,34 +163,63 @@ func EscapeAppsetFieldName(field string) string {
161
163
return appsetFieldRegexp .ReplaceAllString (field , "_" )
162
164
}
163
165
164
- func CurrentContext () ( string , error ) {
166
+ func kubeConfig () * clientcmdapi. Config {
165
167
configAccess := clientcmd .NewDefaultPathOptions ()
166
168
conf , err := configAccess .GetStartingConfig ()
167
- if err != nil {
168
- return "" , err
169
- }
169
+ Die ( err , "failed reading kubeconfig file" )
170
+ return conf
171
+ }
170
172
171
- return conf .CurrentContext , nil
173
+ type kubeContext struct {
174
+ Name string
175
+ Current bool
172
176
}
173
177
174
- func CurrentServer () (string , error ) {
175
- configAccess := clientcmd .NewDefaultPathOptions ()
176
- conf , err := configAccess .GetStartingConfig ()
177
- if err != nil {
178
- return "" , err
178
+ func KubeContexts () []kubeContext {
179
+ conf := kubeConfig ()
180
+ contexts := make ([]kubeContext , len (conf .Contexts ))
181
+ i := 0
182
+ for key := range conf .Contexts {
183
+ contexts [i ] = kubeContext {
184
+ Name : key ,
185
+ Current : key == conf .CurrentContext ,
186
+ }
187
+ i += 1
179
188
}
180
189
181
- currentContext := conf .Contexts [conf .CurrentContext ]
182
- return conf .Clusters [currentContext .Cluster ].Server , nil
190
+ sort .SliceStable (contexts , func (i , j int ) bool {
191
+ c1 := contexts [i ]
192
+ if c1 .Current {
193
+ return true
194
+ }
195
+
196
+ c2 := contexts [j ]
197
+ if c2 .Current {
198
+ return true
199
+ }
200
+
201
+ return c1 .Name < c2 .Name
202
+ })
203
+
204
+ return contexts
183
205
}
184
206
185
- func KubeContextNameByServer ( server string ) ( string , error ) {
186
- configAccess := clientcmd . NewDefaultPathOptions ()
187
- conf , err := configAccess . GetStartingConfig ()
188
- if err != nil {
189
- return "" , err
207
+ func CheckExistingContext ( contextName string ) bool {
208
+ for _ , context := range KubeContexts () {
209
+ if context . Name == contextName {
210
+ return true
211
+ }
190
212
}
191
213
214
+ return false
215
+ }
216
+
217
+ func KubeCurrentServer () (string , error ) {
218
+ return KubeServerByContextName ("" )
219
+ }
220
+
221
+ func KubeContextNameByServer (server string ) (string , error ) {
222
+ conf := kubeConfig ()
192
223
for contextName , context := range conf .Contexts {
193
224
if cluster , ok := conf .Clusters [context .Cluster ]; ok {
194
225
if cluster .Server == server {
@@ -201,14 +232,22 @@ func KubeContextNameByServer(server string) (string, error) {
201
232
}
202
233
203
234
func KubeServerByContextName (contextName string ) (string , error ) {
204
- configAccess := clientcmd .NewDefaultPathOptions ()
205
- conf , err := configAccess .GetStartingConfig ()
206
- if err != nil {
207
- return "" , err
235
+ conf := kubeConfig ()
236
+ if contextName == "" {
237
+ contextName = conf .CurrentContext
238
+ }
239
+
240
+ context := conf .Contexts [contextName ]
241
+ if context == nil {
242
+ return "" , fmt .Errorf ("kubeconfig file missing context \" %s\" " , contextName )
243
+ }
244
+
245
+ cluster := conf .Clusters [context .Cluster ]
246
+ if cluster == nil {
247
+ return "" , fmt .Errorf ("kubeconfig file missing cluster \" %s\" " , context .Cluster )
208
248
}
209
249
210
- currentContext := conf .Contexts [contextName ]
211
- return conf .Clusters [currentContext .Cluster ].Server , nil
250
+ return cluster .Server , nil
212
251
}
213
252
214
253
func DecorateErrorWithDocsLink (err error , link ... string ) error {
0 commit comments