forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
160 lines (145 loc) · 4.87 KB
/
server.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package server
import (
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/appruntimesync/clean"
"github.com/goodrain/rainbond/appruntimesync/pb"
"github.com/goodrain/rainbond/appruntimesync/pod"
"github.com/goodrain/rainbond/appruntimesync/source"
"github.com/goodrain/rainbond/appruntimesync/status"
"github.com/goodrain/rainbond/cmd/worker/option"
"golang.org/x/net/context"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
//AppRuntimeSyncServer AppRuntimeSyncServer
type AppRuntimeSyncServer struct {
StatusManager *status.Manager
c option.Config
stopChan chan struct{}
Ctx context.Context
Cancel context.CancelFunc
ClientSet *kubernetes.Clientset
podCache *pod.CacheManager
clean *clean.Manager
}
//NewAppRuntimeSyncServer create app runtime sync server
func NewAppRuntimeSyncServer(conf option.Config) *AppRuntimeSyncServer {
ctx, cancel := context.WithCancel(context.Background())
kubeconfig := conf.KubeConfig
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
logrus.Error(err)
}
config.QPS = 50
config.Burst = 100
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
logrus.Error(err)
}
logrus.Info("Kube client api create success.")
statusManager := status.NewManager(ctx, clientset)
stopChan := make(chan struct{})
podCache := pod.NewCacheManager(clientset)
Clean, err := clean.NewManager(ctx, clientset)
if err != nil {
logrus.Error(err)
}
arss := &AppRuntimeSyncServer{
c: conf,
Ctx: ctx,
stopChan: stopChan,
Cancel: cancel,
ClientSet: clientset,
podCache: podCache,
clean: Clean,
}
arss.StatusManager = statusManager
return arss
}
//GetAppStatus get specified app status
func (a *AppRuntimeSyncServer) GetAppStatus(ctx context.Context, sr *pb.StatusRequest) (*pb.StatusMessage, error) {
var re pb.StatusMessage
if sr.ServiceIds == "" {
re.Status = a.StatusManager.GetAllStatus()
return &re, nil
}
re.Status = make(map[string]string)
if strings.Contains(sr.ServiceIds, ",") {
ids := strings.Split(sr.ServiceIds, ",")
for _, id := range ids {
re.Status[id] = a.StatusManager.GetStatus(id)
}
return &re, nil
}
re.Status[sr.ServiceIds] = a.StatusManager.GetStatus(sr.ServiceIds)
return &re, nil
}
//SetAppStatus set app status
func (a *AppRuntimeSyncServer) SetAppStatus(ctx context.Context, ps *pb.StatusMessage) (*pb.ErrorMessage, error) {
if ps.Status != nil {
for k, v := range ps.Status {
a.StatusManager.SetStatus(k, v)
}
}
return &pb.ErrorMessage{Message: "success"}, nil
}
//CheckAppStatus check app status
func (a *AppRuntimeSyncServer) CheckAppStatus(ctx context.Context, ps *pb.StatusRequest) (*pb.ErrorMessage, error) {
ids := strings.Split(ps.ServiceIds, ",")
for _, id := range ids {
a.StatusManager.CheckStatus(id)
}
return &pb.ErrorMessage{Message: "success"}, nil
}
//IgnoreDeleteEvent ignore resource delete event
func (a *AppRuntimeSyncServer) IgnoreDeleteEvent(ctx context.Context, pi *pb.Ignore) (*pb.ErrorMessage, error) {
a.StatusManager.IgnoreDelete(pi.Name)
return &pb.ErrorMessage{Message: "success"}, nil
}
//RmIgnoreDeleteEvent rm ignore resource delete event
func (a *AppRuntimeSyncServer) RmIgnoreDeleteEvent(ctx context.Context, pi *pb.Ignore) (*pb.ErrorMessage, error) {
a.StatusManager.RmIgnoreDelete(pi.Name)
return &pb.ErrorMessage{Message: "success"}, nil
}
//Start start
func (a *AppRuntimeSyncServer) Start() error {
if err := a.StatusManager.Start(); err != nil {
return err
}
logrus.Info("k8s source watching started...")
go source.NewSourceAPI(a.ClientSet.Core().RESTClient(),
a.ClientSet.AppsV1beta1().RESTClient(),
15*time.Minute,
a.StatusManager.RCUpdateChan,
a.StatusManager.DeploymentUpdateChan,
a.StatusManager.StatefulSetUpdateChan,
a.stopChan,
)
a.podCache.Start()
a.clean.Start()
logrus.Info("app runtime sync server started...")
return nil
}
//Stop stop
func (a *AppRuntimeSyncServer) Stop() {
a.Cancel()
close(a.stopChan)
a.podCache.Stop()
a.clean.Stop()
}