-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
106 lines (82 loc) · 2.67 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package client
import (
"context"
"time"
"github.com/alehechka/kube-external-sync/client/replicate/common"
"github.com/alehechka/kube-external-sync/client/replicate/ingress"
"github.com/alehechka/kube-external-sync/client/replicate/service"
"github.com/alehechka/kube-external-sync/client/replicate/traefik/ingressroute"
"github.com/traefik/traefik/v2/pkg/provider/kubernetes/crd/generated/clientset/versioned"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// SyncConfig contains the configuration options for the SyncSecrets operation.
type SyncConfig struct {
PodNamespace string
LivenessPort int
ResyncPeriod time.Duration
EnableTraefik bool
OutOfCluster bool
KubeConfig string
}
type Controller struct {
SyncConfig *SyncConfig
Context context.Context
ClientConfig *rest.Config
DefaultClient kubernetes.Interface
TraefikClient *versioned.Clientset
ServiceReplicator common.Replicator
IngressReplicator common.Replicator
TraefikIngressRouteReplicator common.Replicator
}
func NewController() *Controller {
return new(Controller)
}
func (c *Controller) Initialize(config *SyncConfig) (*Controller, error) {
controller := new(Controller)
controller.SyncConfig = config
controller.Context = context.Background()
if err := controller.InitializeClients(); err != nil {
return nil, err
}
controller.InitializeReplicators()
return controller, nil
}
func (c *Controller) InitializeClients() (err error) {
if err := c.InitializeClusterConfig(); err != nil {
return err
}
if err := c.InitializeDefaultClient(); err != nil {
return err
}
if c.SyncConfig.EnableTraefik {
if err := c.InitializeTraefikClient(); err != nil {
return err
}
}
return nil
}
func (c *Controller) InitializeDefaultClient() (err error) {
c.DefaultClient, err = kubernetes.NewForConfig(c.ClientConfig)
return
}
func (c *Controller) InitializeTraefikClient() (err error) {
c.TraefikClient, err = versioned.NewForConfig(c.ClientConfig)
return err
}
func (c *Controller) InitializeClusterConfig() (err error) {
if c.SyncConfig.OutOfCluster {
c.ClientConfig, err = clientcmd.BuildConfigFromFlags("", c.SyncConfig.KubeConfig)
return
}
c.ClientConfig, err = rest.InClusterConfig()
return
}
func (c *Controller) InitializeReplicators() {
c.ServiceReplicator = service.NewReplicator(c.Context, c.DefaultClient, c.SyncConfig.ResyncPeriod)
c.IngressReplicator = ingress.NewReplicator(c.Context, c.DefaultClient, c.SyncConfig.ResyncPeriod)
if c.SyncConfig.EnableTraefik {
c.TraefikIngressRouteReplicator = ingressroute.NewReplicator(c.Context, c.DefaultClient, c.TraefikClient, c.SyncConfig.ResyncPeriod)
}
}