-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
59 lines (45 loc) · 1.57 KB
/
config.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
55
56
57
58
59
package config
import (
"fmt"
"time"
"github.com/bpineau/kube-named-ports/pkg/clientset"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// KnpConfig is the configuration struct, passed to controllers's Init()
type KnpConfig struct {
// When DryRun is true, we display but don't really send notifications
DryRun bool
// Logger should be used to send all logs
Logger *logrus.Logger
// ClientSet represents a connection to a Kubernetes cluster
ClientSet kubernetes.Interface
// HealthPort is the facultative healthcheck port
HealthPort int
// ResyncIntv define the duration between full resync. Set to 0 to disable resyncs.
ResyncIntv time.Duration
// Cluster is the name of the cluster we'll operate on. Mandatory.
Cluster string
// Zone is the cluster's zone. Can be guessed if not provided.
Zone string
// Project is the cluster's project. Can be guessed from host's metadata if not provided.
Project string
}
// Init initialize the configuration's ClientSet
func (c *KnpConfig) Init(apiserver string, kubeconfig string) error {
var err error
if c.ClientSet == nil {
c.ClientSet, err = clientset.NewClientSet(apiserver, kubeconfig)
if err != nil {
return fmt.Errorf("Failed init Kubernetes clientset: %+v", err)
}
}
// better fail early, if we can't talk to the cluster's api
_, err = c.ClientSet.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return fmt.Errorf("Failed to query Kubernetes api-server: %+v", err)
}
c.Logger.Info("Kubernetes clientset initialized")
return nil
}