github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/router/kubernetes/kubernetes.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/tickoalcantara12/micro/v3/router/kubernetes/kubernetes.go 14 15 // Package kubernetes is a kubernetes router which uses the service name and network to route 16 package kubernetes 17 18 import ( 19 "fmt" 20 "github.com/tickoalcantara12/micro/v3/service/router" 21 "os" 22 ) 23 24 var prefixDns string = "cluster.local" 25 26 func init() { 27 prefix, ok := os.LookupEnv("K8S_DNS_PREFIX") 28 if ok { 29 prefixDns = prefix 30 } 31 } 32 33 // NewRouter returns an initialized kubernetes router 34 func NewRouter(opts ...router.Option) router.Router { 35 options := router.DefaultOptions() 36 for _, o := range opts { 37 o(&options) 38 } 39 return &kubernetes{options} 40 } 41 42 type kubernetes struct { 43 options router.Options 44 } 45 46 func (k *kubernetes) Init(opts ...router.Option) error { 47 for _, o := range opts { 48 o(&k.options) 49 } 50 return nil 51 } 52 53 func (k *kubernetes) Options() router.Options { 54 return k.options 55 } 56 57 func (k *kubernetes) Table() router.Table { 58 return new(table) 59 } 60 61 func (k *kubernetes) Lookup(service string, opts ...router.LookupOption) ([]router.Route, error) { 62 options := router.NewLookup(opts...) 63 if len(options.Network) == 0 { 64 options.Network = "micro" 65 } 66 67 address := fmt.Sprintf("%v.%v.svc.%s:8080", service, options.Network, prefixDns) 68 69 return []router.Route{ 70 router.Route{ 71 Service: service, 72 Address: address, 73 Gateway: options.Gateway, 74 Network: options.Network, 75 Router: options.Router, 76 }, 77 }, nil 78 } 79 80 // Watch will return a noop watcher 81 func (k *kubernetes) Watch(opts ...router.WatchOption) (router.Watcher, error) { 82 return &watcher{ 83 events: make(chan *router.Event), 84 }, nil 85 } 86 87 func (k *kubernetes) Close() error { 88 return nil 89 } 90 91 func (k *kubernetes) String() string { 92 return "kubernetes" 93 } 94 95 // watcher is a noop implementation 96 type watcher struct { 97 events chan *router.Event 98 } 99 100 // Next is a blocking call that returns watch result 101 func (w *watcher) Next() (*router.Event, error) { 102 e := <-w.events 103 return e, nil 104 } 105 106 // Chan returns event channel 107 func (w *watcher) Chan() (<-chan *router.Event, error) { 108 return w.events, nil 109 } 110 111 // Stop stops watcher 112 func (w *watcher) Stop() { 113 return 114 } 115 116 type table struct{} 117 118 // Create new route in the routing table 119 func (t *table) Create(router.Route) error { 120 return nil 121 } 122 123 // Delete existing route from the routing table 124 func (t *table) Delete(router.Route) error { 125 return nil 126 } 127 128 // Update route in the routing table 129 func (t *table) Update(router.Route) error { 130 return nil 131 } 132 133 // Read is for querying the table 134 func (t *table) Read(...router.ReadOption) ([]router.Route, error) { 135 return []router.Route{}, nil 136 }