/
client.go
54 lines (45 loc) · 1.25 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* *******************************************************************************
* * Copyright (c) 2019 Edgeworx, Inc.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package k8s
import (
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // GCP Auth required
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
type Client struct {
*kubernetes.Clientset
}
func New(kubeConfig string) (*Client, error) {
// Get the kubernetes config from the filepath.
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, err
}
return newClient(config)
}
func NewInCluster() (*Client, error) {
// Create the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
return newClient(config)
}
func newClient(config *rest.Config) (*Client, error) {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return &Client{clientset}, nil
}