forked from micro/micro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubernetes.go
executable file
·137 lines (112 loc) · 3.04 KB
/
kubernetes.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
// 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
//
// https://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.
//
// Original source: github.com/2637309949/micro/v3/router/kubernetes/kubernetes.go
// Package kubernetes is a kubernetes router which uses the service name and network to route
package kubernetes
import (
"fmt"
"os"
"github.com/2637309949/micro/v3/service/router"
)
var prefixDns string = "cluster.local"
func init() {
prefix, ok := os.LookupEnv("K8S_DNS_PREFIX")
if ok {
prefixDns = prefix
}
}
// NewRouter returns an initialized kubernetes router
func NewRouter(opts ...router.Option) router.Router {
options := router.DefaultOptions()
for _, o := range opts {
o(&options)
}
return &kubernetes{options}
}
type kubernetes struct {
options router.Options
}
func (k *kubernetes) Init(opts ...router.Option) error {
for _, o := range opts {
o(&k.options)
}
return nil
}
func (k *kubernetes) Options() router.Options {
return k.options
}
func (k *kubernetes) Table() router.Table {
return new(table)
}
func (k *kubernetes) Lookup(service string, opts ...router.LookupOption) ([]router.Route, error) {
options := router.NewLookup(opts...)
if len(options.Network) == 0 {
options.Network = "micro"
}
address := fmt.Sprintf("%v.%v.svc.%s:8080", service, options.Network, prefixDns)
return []router.Route{
router.Route{
Service: service,
Address: address,
Gateway: options.Gateway,
Network: options.Network,
Router: options.Router,
},
}, nil
}
// Watch will return a noop watcher
func (k *kubernetes) Watch(opts ...router.WatchOption) (router.Watcher, error) {
return &watcher{
events: make(chan *router.Event),
}, nil
}
func (k *kubernetes) Close() error {
return nil
}
func (k *kubernetes) String() string {
return "kubernetes"
}
// watcher is a noop implementation
type watcher struct {
events chan *router.Event
}
// Next is a blocking call that returns watch result
func (w *watcher) Next() (*router.Event, error) {
e := <-w.events
return e, nil
}
// Chan returns event channel
func (w *watcher) Chan() (<-chan *router.Event, error) {
return w.events, nil
}
// Stop stops watcher
func (w *watcher) Stop() {
return
}
type table struct{}
// Create new route in the routing table
func (t *table) Create(router.Route) error {
return nil
}
// Delete existing route from the routing table
func (t *table) Delete(router.Route) error {
return nil
}
// Update route in the routing table
func (t *table) Update(router.Route) error {
return nil
}
// Read is for querying the table
func (t *table) Read(...router.ReadOption) ([]router.Route, error) {
return []router.Route{}, nil
}