-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.go
200 lines (170 loc) · 5.2 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2019 gosoon.
*
* 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 server
import (
"context"
"net"
"net/http"
"sync"
installerv1 "github.com/gosoon/kubernetes-operator/pkg/apis/installer/v1"
"github.com/gosoon/kubernetes-operator/pkg/types"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/gosoon/glog"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
const (
ServerPort = "10022"
AgentPort = "10023"
ImagesRegistry = "registry.cn-hangzhou.aliyuncs.com/aliyun_kube_system"
)
// Options is installer server options
type Options struct {
ServerPort string
AgentPort string
ImagesRegistry string
}
// gserver xxx
type installer struct {
opt *Options
}
// NewInstaller is a new installer server
func NewInstaller(opt *Options) *installer {
grpcInstaller := &installer{opt: opt}
//start grpc installer's grpc server and http server
go grpcInstaller.Run()
return grpcInstaller
}
// run is start grpc gateway
// TODO: remove http server,only use grcp server
func (inst *installer) Run() {
grpcServerEndpoint := ":" + inst.opt.ServerPort
l, err := net.Listen("tcp", grpcServerEndpoint)
if err != nil {
glog.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
// register grpc server
installerv1.RegisterInstallerServer(grpcServer, inst)
reflection.Register(grpcServer)
go func() {
glog.Info("starting grpc server...")
glog.Fatal(grpcServer.Serve(l))
}()
// start http server
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Register gRPC server endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err = installerv1.RegisterInstallerHandlerFromEndpoint(ctx, mux, grpcServerEndpoint, opts)
if err != nil {
glog.Fatal(err)
}
glog.Info("starting http server...")
// Start HTTP server (and proxy calls to gRPC server endpoint)
glog.Fatal(http.ListenAndServe(":8080", mux))
}
// CopyFile is a definition of InstallerServer Interface,server do not use CopyFile
// so it is no a implementation
func (inst *installer) CopyFile(
file *installerv1.File,
stream installerv1.Installer_CopyFileServer) error {
return nil
}
func (inst *installer) InstallCluster(
ctx context.Context,
cluster *installerv1.KubernetesClusterRequest) (*installerv1.InstallClusterResponse, error) {
// images registry,retain,wait and others agent flags is inject by grpc server,
// because it's don't have to in kubernetes controller
cluster = inst.injectClusterConfig(cluster)
finish := make(chan bool)
go inst.DispatchClusterConfig(ctx, cluster, finish)
select {
case <-ctx.Done():
// callback install failed
case success := <-finish:
if !success {
// callback install failed
}
}
return nil, nil
}
// InstallCluster is send KubernetesCluster config to all installer agent
func (inst *installer) DispatchClusterConfig(
ctx context.Context,
cluster *installerv1.KubernetesClusterRequest,
finish chan<- bool) {
// get all nodeList
var clusterNodeList []installerv1.Node
clusterNodeList = append(clusterNodeList, cluster.Spec.Cluster.NodeList...)
clusterNodeList = append(clusterNodeList, cluster.Spec.Cluster.MasterList...)
// dispatch config to echo node and record result in a result channel
// set dispatch concurrent default is 16
results := make([]chan types.DispatchConfigResult, len(clusterNodeList))
chanLimits := make(chan bool, 16)
var wg sync.WaitGroup
for idx, node := range clusterNodeList {
chanLimits <- true
results[idx] = make(chan types.DispatchConfigResult, 1)
wg.Add(1)
go func(ip string) {
defer wg.Done()
inst.dispatchConfig(ip, cluster, chanLimits, results[idx])
}(node.IP)
}
wg.Wait()
// get all result by node
success := true
for _, result := range results {
res := <-result
// if dispatchConfig failed and record log
if !res.Success {
success = false
glog.Errorf("dispatchConfig res.Host %v failed with:%v", res.Host, res.Message)
}
}
finish <- success
}
func (inst *installer) dispatchConfig(
ip string,
cluster *installerv1.KubernetesClusterRequest,
chanLimits <-chan bool,
result chan<- types.DispatchConfigResult) {
defer func() { <-chanLimits }()
failedResult := func(err error) {
result <- types.DispatchConfigResult{
Host: ip,
Success: false,
Message: err.Error(),
}
}
address := ip + ":" + inst.opt.AgentPort
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
failedResult(err)
return
}
defer conn.Close()
client := installerv1.NewInstallerClient(conn)
//send cluster config to installer agent
_, err = client.InstallCluster(context.Background(), cluster)
if err != nil {
failedResult(err)
return
}
}