-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmanager.go
113 lines (96 loc) · 3.25 KB
/
manager.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
107
108
109
110
111
112
113
// Copyright Aeraki Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package manager
import (
"context"
"fmt"
"github.com/aeraki-mesh/lazyxds/cmd/lazyxds/app/config"
"github.com/aeraki-mesh/lazyxds/pkg/accesslog"
"github.com/aeraki-mesh/lazyxds/pkg/bootstrap"
"github.com/aeraki-mesh/lazyxds/pkg/controller"
"github.com/aeraki-mesh/lazyxds/pkg/utils"
istioclient "istio.io/client-go/pkg/clientset/versioned"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)
// LazyXdsManager defines the high-level interface of multiCluster management.
type LazyXdsManager interface {
AddCluster(name string, client *kubernetes.Clientset) error
DeleteCluster(name string) error
Run() error
//Run(stopCh <-chan struct{}) error
}
// Manager contains the main lazy xds controller
type Manager struct {
conf *config.Config
stop <-chan struct{} // todo move to args of run function
masterClient *kubernetes.Clientset
istioClient *istioclient.Clientset
controller *controller.AggregationController
accessLogServer *accesslog.Server
}
var singleton *Manager
// NewManager ...
func NewManager(conf *config.Config, stop <-chan struct{}) (*Manager, error) {
if singleton != nil {
klog.Error("LazyXds Manager already exist")
return singleton, nil
}
kubeClient, err := utils.NewKubeClient(conf.KubeConfig)
if err != nil {
return nil, err
}
istioClient, err := utils.NewIstioClient(conf.KubeConfig)
if err != nil {
return nil, err
}
singleton = &Manager{
conf: conf,
stop: stop,
masterClient: kubeClient,
istioClient: istioClient,
controller: controller.NewController(istioClient, kubeClient, stop),
}
singleton.accessLogServer = accesslog.NewAccessLogServer(singleton.controller)
return singleton, nil
}
// Run ...
func (m *Manager) Run() error {
klog.Info("Starting access log server...")
if err := m.accessLogServer.Serve(); err != nil {
return fmt.Errorf("start access log server failed: %w", err)
}
m.controller.Run()
// todo we need support multiple cluster
if err := m.AddCluster("Kubernetes", m.masterClient); err != nil {
return err
}
m.controller.KubeClient = m.masterClient
return nil
}
// AddCluster add new cluster to the mesh
func (m *Manager) AddCluster(name string, client *kubernetes.Clientset) error {
if m.conf.AutoCreateEgress {
klog.Info("Starting create lazyxds egress", "cluster", name)
if err := bootstrap.InitEgress(context.TODO(), name, client, m.istioClient, m.conf.IstiodAddress, m.conf.ProxyImage); err != nil {
return fmt.Errorf("init egress of cluster %s failed: %w", name, err)
}
}
return m.controller.AddCluster(name, client)
}
// DeleteCluster remove cluster from the mesh
// todo need implement
func (m *Manager) DeleteCluster(name string) error {
return nil
}