Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 16 | package main // import "ciena.com/envoyd" |
| 17 | |
| 18 | import ( |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 19 | "context" |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 20 | "os" |
| 21 | "os/exec" |
| 22 | "fmt" |
| 23 | "log" |
| 24 | "strconv" |
| 25 | "time" |
| 26 | "net" |
| 27 | "io/ioutil" |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 28 | "io" |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 29 | "text/template" |
| 30 | "encoding/json" |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 31 | "sync" |
| 32 | "flag" |
| 33 | "bufio" |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 34 | consulapi "github.com/hashicorp/consul/api" |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 35 | etcdapi "github.com/coreos/etcd/clientv3" |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | // DATA STRUCTURES |
| 39 | |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 40 | //Client provides an interface for getting data out of Consul |
| 41 | type Client interface { |
| 42 | // Get a Service from consulapi |
| 43 | Service(string, string) ([]string, error) |
| 44 | // Register a service with local agent |
| 45 | Register(string, int) error |
| 46 | // Deregister a service with local agent |
| 47 | DeRegister(string) error |
| 48 | } |
| 49 | |
| 50 | type client struct { |
| 51 | consulapi *consulapi.Client |
| 52 | } |
| 53 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 54 | type KvConnectFunc func(string, string) (error) |
| 55 | type KvMonitorFunc func() |
| 56 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 57 | type EnvoyConfigVars struct { |
| 58 | VolthaVip string |
| 59 | VolthaRR []string |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 60 | vcorePort string |
| 61 | HttpPort string |
| 62 | HttpsPort string |
| 63 | GrpcPort string |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | type VolthaClusterEntry struct { |
| 67 | Prefix string |
| 68 | Id string |
| 69 | Host string |
| 70 | } |
| 71 | |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 72 | // This struct is not used yet |
| 73 | // TODO: Update the daemon to use this structure to for a |
| 74 | // more object oriented implementation |
| 75 | type EnvoyControl struct { |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 76 | // Command line parameters |
| 77 | assignmentKey string |
| 78 | envoyConfigTemplate string |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 79 | envoyConfigTemplateBoth string |
| 80 | envoyConfigTemplateNoHttps string |
| 81 | envoyConfigTemplateNoHttp string |
| 82 | envoyHttpPort string |
| 83 | envoyHttpsPort string |
| 84 | httpDisabled bool |
| 85 | httpsDisabled bool |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 86 | envoyConfig string |
| 87 | vcoreSvcName string |
| 88 | consulSvcName string |
| 89 | vcorePort string |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 90 | envoyGrpcPort string |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 91 | consulPort string |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 92 | kvStore string |
| 93 | kvSvcName string |
| 94 | kvPort string |
| 95 | kvConnect map[string]KvConnectFunc |
| 96 | kvMonitor map[string]KvMonitorFunc |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 97 | retries int |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 98 | waitTime int |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 99 | // Runtime variables |
| 100 | consul * consulapi.Client |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 101 | etcd * etcdapi.Client |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 102 | vcoreHostIpName string |
| 103 | vcoreIdName string |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 104 | vc []VolthaClusterEntry |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 105 | ipAddrs map[string][]string |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 106 | restartEpoch int |
| 107 | reLock sync.Mutex // Exclusive access to the restartEpoch |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 110 | |
| 111 | func NewEnvoyControl() (ec * EnvoyControl) { |
| 112 | var envCtrl EnvoyControl = EnvoyControl { // Default values |
| 113 | // Command line parameters |
| 114 | assignmentKey: "service/voltha/data/core/assignment", |
| 115 | envoyConfigTemplate: "/envoy/voltha-grpc-proxy.template.json", |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 116 | envoyConfigTemplateBoth: "/envoy/voltha-grpc-proxy.template.json", |
| 117 | envoyConfigTemplateNoHttps: "/envoy/voltha-grpc-proxy-no-https.template.json", |
| 118 | envoyConfigTemplateNoHttp: "/envoy/voltha-grpc-proxy-no-http.template.json", |
| 119 | envoyHttpsPort: "8443", |
| 120 | envoyHttpPort: "8882", |
| 121 | envoyGrpcPort: "50555", |
| 122 | httpDisabled: false, |
| 123 | httpsDisabled: false, |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 124 | envoyConfig: "/envoy/voltha-grpc-proxy.json", |
| 125 | //envoyLogFile: "/envoy/voltha_access_log.log", |
| 126 | vcoreSvcName: "vcore", |
| 127 | consulSvcName: "consul", |
| 128 | vcorePort: "50556", |
| 129 | consulPort: "8500", |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 130 | kvStore: "consul", |
| 131 | kvSvcName: "consul", |
| 132 | kvPort: "8500", |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 133 | retries: 10, |
| 134 | waitTime: 2, |
| 135 | // Runtime variables |
| 136 | vcoreHostIpName: "host", |
| 137 | vcoreIdName: "id", |
| 138 | ipAddrs: make(map[string][]string), |
| 139 | restartEpoch: 0, |
| 140 | } |
| 141 | ec = &envCtrl |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 142 | ec.kvConnect = make(map[string]KvConnectFunc) |
| 143 | ec.kvConnect["consul"] = ec.consulConnect |
| 144 | ec.kvConnect["etcd"] = ec.etcdConnect |
| 145 | ec.kvMonitor = make(map[string]KvMonitorFunc) |
| 146 | ec.kvMonitor["consul"] = ec.monitorConsulKey |
| 147 | ec.kvMonitor["etcd"] = ec.monitorEtcdKey |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 148 | return |
| 149 | } |
| 150 | |
| 151 | func (ec * EnvoyControl) resolveServiceAddress(serviceName string) (err error) { |
| 152 | for i := 0; i < ec.retries; i++ { |
| 153 | ec.ipAddrs[serviceName], err = net.LookupHost(serviceName) |
| 154 | if err != nil { |
| 155 | log.Printf("%s name resolution failed %d time(s) retrying...", serviceName, i+1) |
| 156 | } else { |
| 157 | //fmt.Printf("%s address = %s\n",serviceName, addrs[0]) |
| 158 | break |
| 159 | } |
| 160 | time.Sleep(time.Duration(ec.waitTime) * time.Second) |
| 161 | } |
| 162 | if err != nil { |
Richard Jankowski | d445438 | 2018-02-08 16:21:43 -0500 | [diff] [blame] | 163 | log.Printf("%s name resolution failed %d times giving up", serviceName, ec.retries) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 164 | } |
| 165 | return |
| 166 | } |
| 167 | |
| 168 | func (ec * EnvoyControl) consulConnect(serviceName string, port string) (err error) { |
| 169 | // Fire up a consul client and get the kv store |
| 170 | cConfig := consulapi.DefaultNonPooledConfig() |
| 171 | cConfig.Address = ec.ipAddrs[serviceName][0] + ":" + port |
| 172 | ec.consul, err = consulapi.NewClient(cConfig) |
| 173 | if err != nil { |
| 174 | log.Fatal("error creating consul client aborting") |
| 175 | return |
| 176 | } |
| 177 | return |
| 178 | } |
| 179 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 180 | func (ec * EnvoyControl) etcdConnect(serviceName string, port string) (err error) { |
| 181 | // Fire up an etcd client to access the kv store |
| 182 | cfg := etcdapi.Config { |
| 183 | Endpoints: []string{serviceName + ":" + port}, |
| 184 | DialTimeout: 5 * time.Second, |
| 185 | } |
| 186 | ec.etcd, err = etcdapi.New(cfg) |
| 187 | if err != nil { |
| 188 | log.Fatal("Failed to create etcd client, aborting...") |
| 189 | return |
| 190 | } |
| 191 | return |
| 192 | } |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 193 | |
| 194 | //NewConsul returns a Client interface for given consulapi address |
| 195 | func NewConsulClient(addr string) (*client, error) { |
| 196 | config := consulapi.DefaultConfig() |
| 197 | config.Address = addr |
| 198 | c, err := consulapi.NewClient(config) |
| 199 | if err != nil { |
| 200 | return nil, err |
| 201 | } |
| 202 | return &client{consulapi: c}, nil |
| 203 | } |
| 204 | |
| 205 | // Register a service with consulapi local agent |
| 206 | func (c *client) Register(name string, port int) error { |
| 207 | reg := &consulapi.AgentServiceRegistration{ |
| 208 | ID: name, |
| 209 | Name: name, |
| 210 | Port: port, |
| 211 | } |
| 212 | return c.consulapi.Agent().ServiceRegister(reg) |
| 213 | } |
| 214 | |
| 215 | // DeRegister a service with consulapi local agent |
| 216 | func (c *client) DeRegister(id string) error { |
| 217 | return c.consulapi.Agent().ServiceDeregister(id) |
| 218 | } |
| 219 | |
| 220 | // Service return a service |
| 221 | func (c *client) Service(service, tag string) ([]*consulapi.ServiceEntry, *consulapi.QueryMeta, error) { |
| 222 | passingOnly := true |
| 223 | addrs, meta, err := c.consulapi.Health().Service(service, tag, passingOnly, nil) |
| 224 | if len(addrs) == 0 && err == nil { |
| 225 | return nil, nil, fmt.Errorf("service ( %s ) was not found", service) |
| 226 | } |
| 227 | if err != nil { |
| 228 | return nil, nil, err |
| 229 | } |
| 230 | return addrs, meta, nil |
| 231 | } |
| 232 | |
| 233 | // Starts envoy with the current restartEpoch |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 234 | func (ec * EnvoyControl) startEnvoy() { |
| 235 | var curEpoch int |
| 236 | var err error |
| 237 | var count int |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 238 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 239 | ec.reLock.Lock() // Make sure we've got exclusive access to the variable |
| 240 | cmd := exec.Command("/usr/local/bin/envoy", "--restart-epoch", strconv.Itoa(ec.restartEpoch), |
| 241 | "--config-path", ec.envoyConfig, "--parent-shutdown-time-s", "10") |
| 242 | |
| 243 | curEpoch = ec.restartEpoch |
| 244 | ec.restartEpoch += 1 |
| 245 | ec.reLock.Unlock() // Done, release the lock. |
| 246 | |
| 247 | stderr, err := cmd.StderrPipe() |
| 248 | if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 249 | log.Fatal("Couldn't attach to stderr running envoy command: %s", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 250 | } |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 251 | stdout, err := cmd.StdoutPipe() |
| 252 | if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 253 | log.Fatal("Couldn't attach to stdout running envoy command: %s", err.Error()) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 254 | } |
| 255 | so := bufio.NewReader(stdout) |
| 256 | se := bufio.NewReader(stderr) |
| 257 | |
| 258 | if err = cmd.Start(); err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 259 | log.Fatal("Error starting envoy: %s", err.Error()) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 260 | } |
| 261 | log.Printf("Envoy(%d) started", curEpoch) |
| 262 | soEof := false |
| 263 | seEof := false |
| 264 | |
| 265 | data := make([]byte, 80) |
| 266 | log.Printf("Log forwarding for envoy(%d) started", curEpoch) |
| 267 | for { |
| 268 | data = make([]byte, 80) |
| 269 | count, err = so.Read(data) |
| 270 | log.Printf("ENVOY_LOG(%d): %s", curEpoch, string(data)) |
| 271 | if err == io.EOF { |
| 272 | if seEof == true { |
| 273 | break |
| 274 | } else { |
| 275 | soEof = true |
| 276 | } |
| 277 | } else if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 278 | log.Fatal("Attempt to read envoy standard out failed: %s", err.Error()) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 279 | } else if count > 0 { |
| 280 | log.Printf("ENVOY_LOG(%d)(%d): %s",curEpoch,count,string(data)) |
| 281 | } |
| 282 | data = make([]byte, 80) |
| 283 | count, err = se.Read(data) |
| 284 | if err == io.EOF { |
| 285 | if soEof == true { |
| 286 | break |
| 287 | } else { |
| 288 | seEof = true |
| 289 | } |
| 290 | } else if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 291 | log.Fatal("Attempt to read envoy standard err failed: %s", err.Error()) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 292 | } else if count > 0 { |
| 293 | log.Printf("ENVOY_LOG(%d)(%d): %s",curEpoch,count,string(data)) |
| 294 | } |
| 295 | } |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 296 | log.Printf("Waiting on envoy %d to exit", curEpoch) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 297 | if err = cmd.Wait(); err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 298 | log.Fatal("Envoy %d exited with an unexpected exit code: %s", curEpoch, err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 299 | } |
| 300 | log.Printf("Envoy %d exited", curEpoch) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 301 | // Check if this was the primary envoy, if so |
| 302 | // something went terribly wrong, panic to force |
| 303 | // forcefully exit. |
| 304 | ec.reLock.Lock() |
| 305 | if ec.restartEpoch == (curEpoch + 1) { |
| 306 | ec.reLock.Unlock() |
| 307 | log.Fatal("Last running envoy exited, aborting!") |
| 308 | panic("This should never happen") |
| 309 | } |
| 310 | ec.reLock.Unlock() |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 311 | |
| 312 | } |
| 313 | |
| 314 | // This function will use the provided templete file to generate |
| 315 | // the targetfile substituting |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 316 | func (ec * EnvoyControl) updateEnvoyConfig(ecv * EnvoyConfigVars) (err error) { |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 317 | var firstRun bool = true |
Sergio Slobodrian | 1962874 | 2017-09-05 19:52:54 -0400 | [diff] [blame] | 318 | var firstRun2 bool = true |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 319 | f := func() (bool) { |
| 320 | var rtrn bool = firstRun |
| 321 | firstRun = false |
| 322 | return rtrn |
| 323 | } |
Sergio Slobodrian | 1962874 | 2017-09-05 19:52:54 -0400 | [diff] [blame] | 324 | g := func() (bool) { |
| 325 | var rtrn bool = firstRun2 |
| 326 | firstRun2 = false |
| 327 | return rtrn |
| 328 | } |
| 329 | var funcs = template.FuncMap{"isFirst": f, "isFirst2": g} |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 330 | // Slurp up the template file. |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 331 | tplt, err := ioutil.ReadFile(ec.envoyConfigTemplate) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 332 | if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 333 | log.Fatal("ERROR reading the template file, aborting: %s", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 334 | } |
| 335 | //fmt.Println(string(tplt)) |
| 336 | configTemplate, err := template.New("config").Funcs(funcs).Parse(string(tplt)); |
| 337 | if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 338 | log.Fatal("Unexpected error loading the Envoy template, aborting: %s", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 339 | } |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 340 | outFile,err := os.Create(ec.envoyConfig) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 341 | if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 342 | log.Fatal("Unexpected error opening the Envoy config file for write, aborting: %s", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 343 | } |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 344 | if err = configTemplate.Execute(outFile, ecv); err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 345 | log.Fatal("Unexpected error executing the Envoy config template, aborting: %s", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 346 | } |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 347 | //cfgFile, err := ioutil.ReadFile(ec.envoyConfig) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 348 | //if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 349 | // log.Fatal("ERROR reading the config file, aborting: %s", err.Error()) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 350 | // panic(err) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 351 | //} |
| 352 | //fmt.Println(string(cfgFile)) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 353 | return |
| 354 | } |
| 355 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 356 | func (ec * EnvoyControl) parseAssignment(jsonString []byte) (vCluster []VolthaClusterEntry, err error) { |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 357 | var f interface{} |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 358 | var vc VolthaClusterEntry |
| 359 | //var isErr bool |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 360 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 361 | log.Printf("Parsing %s", string(jsonString)) |
| 362 | //err = json.Unmarshal(jsonString, &f) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 363 | err = json.Unmarshal(jsonString, &f) |
| 364 | if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 365 | log.Fatal("Unable to parse json record %s : %s", jsonString, err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 366 | } else { |
| 367 | m := f.(map[string]interface{}) |
| 368 | for k, v := range m { |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 369 | isErr := false |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 370 | vc.Prefix = k |
| 371 | //log.Printf("Processing key %s\n", k) |
| 372 | switch vv := v.(type) { |
| 373 | case map[string]interface{}: |
| 374 | for i, u := range vv { |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 375 | //log.Printf("Processing key %sn", i) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 376 | switch uu := u.(type) { |
| 377 | case string: |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 378 | if i == ec.vcoreHostIpName { |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 379 | vc.Host = uu |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 380 | } else if i == ec.vcoreIdName { |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 381 | vc.Id = uu |
| 382 | } else { |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 383 | log.Printf("WARNING: unexpected descriptor,%s", i) |
| 384 | isErr = true |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 385 | } |
| 386 | default: |
| 387 | log.Printf("WARNING: unexpected type, ") |
| 388 | log.Println(i, u) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 389 | isErr = true |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | default: |
| 393 | log.Printf("WARNING: unexpected type, ") |
| 394 | log.Println(k, v) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 395 | isErr = true |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 396 | } |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 397 | if ! isErr { |
| 398 | vCluster = append(vCluster, vc) |
| 399 | } |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | log.Println("Parsing complete") |
| 403 | return |
| 404 | } |
| 405 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 406 | func (ec * EnvoyControl) prepareEnvoyConfig(keyValue []byte, ecv * EnvoyConfigVars) (err error) { |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 407 | var vCluster []VolthaClusterEntry |
| 408 | |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 409 | ecv.HttpPort = ec.envoyHttpPort |
| 410 | ecv.HttpsPort = ec.envoyHttpsPort |
| 411 | ecv.GrpcPort = ec.envoyGrpcPort |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 412 | ecv.VolthaVip = ec.ipAddrs[ec.vcoreSvcName][0] + ":" + ec.vcorePort |
| 413 | |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 414 | // Extract all values from the KV record |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 415 | // In the future, the values should all be compared to what we currently have |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 416 | vCluster, err = ec.parseAssignment(keyValue) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 417 | if err == nil { |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 418 | ec.vc = vCluster // For future use to determine if there's been a real change |
| 419 | //templateValues["VolthaRR"] = []string{} |
| 420 | ecv.VolthaRR = []string{} |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 421 | for i := range vCluster { |
| 422 | //log.Printf("Processing %s\n", vCluster[i].Host) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 423 | //templateValues["VolthaRR"] = append(templateValues["VolthaRR"].([]string), vCluster[i].Host) |
| 424 | ecv.VolthaRR = append(ecv.VolthaRR, vCluster[i].Host + ":" + ec.vcorePort) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 425 | } |
| 426 | } else { |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 427 | log.Fatal("Couldn't parse the KV record %s: %s", string(keyValue), err.Error()) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 428 | } |
| 429 | return |
| 430 | } |
| 431 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 432 | func (ec * EnvoyControl) runEnvoy(keyValue []byte) { |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 433 | var err error |
| 434 | var ecv EnvoyConfigVars |
| 435 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 436 | if err = ec.prepareEnvoyConfig(keyValue, &ecv); err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 437 | log.Fatal("Error preparing envoy config variables, aborting: %s", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // Now that we have the data loaded, update the envoy config and start envoy |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 441 | ec.updateEnvoyConfig(&ecv) |
| 442 | go ec.startEnvoy() |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 443 | } |
| 444 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 445 | func (ec * EnvoyControl) readConsulKey(key string, qo * consulapi.QueryOptions) (value []byte, meta * consulapi.QueryMeta, err error) { |
| 446 | |
| 447 | var kvp *consulapi.KVPair |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 448 | |
| 449 | kv := ec.consul.KV() |
| 450 | // Get the initial values of the assignment key which contains individual |
| 451 | // voltha core IP addresses. This may be empty until voltha populates it |
| 452 | // so it must be checked |
| 453 | kvp, meta, err = kv.Get(ec.assignmentKey, qo) |
| 454 | for i := 0; i < ec.retries; i++ { |
| 455 | if err != nil { |
| 456 | fmt.Println(err) |
| 457 | log.Printf("Unable to read assignment consul key, retry %d", i+1) |
| 458 | time.Sleep(time.Duration(ec.waitTime) * time.Second) |
| 459 | kvp, meta, err = kv.Get(ec.assignmentKey, qo) |
| 460 | } else if kvp != nil && len(string(kvp.Value)) > 10 { |
| 461 | // A valid read, return |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 462 | value = kvp.Value |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 463 | break |
| 464 | } else { |
| 465 | log.Printf("Voltha assignment key invalid, retry %d", i+1) |
| 466 | time.Sleep(time.Duration(ec.waitTime) * time.Second) |
| 467 | kvp, meta, err = kv.Get(ec.assignmentKey, qo) |
| 468 | } |
| 469 | if i == ec.retries { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 470 | log.Fatal("Failed to read the assignment key after %d retries, aborting: %s", ec.retries, err.Error()) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | return |
| 474 | } |
| 475 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 476 | func (ec * EnvoyControl) readEtcdKey(key string) (value []byte, index int64, err error) { |
| 477 | // Get the initial values of the assignment key which contains individual |
| 478 | // voltha core IP addresses. This may be empty until voltha populates it |
| 479 | // so it must be checked |
| 480 | resp, err := ec.etcd.Get(context.Background(), ec.assignmentKey) |
| 481 | for i := 0; i < ec.retries; i++ { |
| 482 | if err != nil { |
| 483 | fmt.Println(err) |
| 484 | log.Printf("Unable to read assignment etcd key, retry %d", i+1) |
| 485 | time.Sleep(time.Duration(ec.waitTime) * time.Second) |
| 486 | resp, err = ec.etcd.Get(context.Background(), ec.assignmentKey) |
| 487 | } else if resp != nil && len(resp.Kvs) > 0 && len(resp.Kvs[0].Value) > 10 { |
| 488 | // A valid read, return |
| 489 | kv := resp.Kvs[0] |
| 490 | value = kv.Value |
| 491 | index = kv.ModRevision |
| 492 | break |
| 493 | } else { |
| 494 | log.Printf("Voltha assignment key from etcd invalid, retry %d", i+1) |
| 495 | time.Sleep(time.Duration(ec.waitTime) * time.Second) |
| 496 | resp, err = ec.etcd.Get(context.Background(), ec.assignmentKey) |
| 497 | } |
| 498 | if i == ec.retries { |
| 499 | log.Fatal("Failed to read assignment key from etcd after %d retries, aborting: %s", ec.retries, err.Error()) |
| 500 | } |
| 501 | } |
| 502 | return |
| 503 | } |
| 504 | |
| 505 | func (ec * EnvoyControl) monitorConsulKey() { |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 506 | var err error |
| 507 | var qo consulapi.QueryOptions |
| 508 | |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 509 | // Get the initial values of the assignment key which contains individual |
| 510 | // voltha core IP addresses. This may be empty until voltha populates it |
| 511 | // so it must be checked |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 512 | log.Printf("Monitoring consul key") |
| 513 | val, meta, err := ec.readConsulKey(ec.assignmentKey, nil) |
| 514 | log.Printf("Starting Envoy, initial index = %d", meta.LastIndex) |
| 515 | ec.runEnvoy(val) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 516 | |
| 517 | for { |
| 518 | qo.WaitIndex = meta.LastIndex |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 519 | qo.RequireConsistent = true |
Sergio Slobodrian | 1962874 | 2017-09-05 19:52:54 -0400 | [diff] [blame] | 520 | //qo.AllowStale = true |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 521 | for { |
| 522 | if qo.WaitIndex != meta.LastIndex { |
| 523 | break |
| 524 | } |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 525 | val, meta, err = ec.readConsulKey(ec.assignmentKey, &qo) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 526 | if err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 527 | log.Fatal("Unable to read assignment consul key: %s\n", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 528 | } else { |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 529 | log.Println(string(val)) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 530 | log.Printf("meta.LastIndex = %d", meta.LastIndex) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | // Fell through, the index has changed thus the key has changed |
| 534 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 535 | log.Printf("Starting Envoy") |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 536 | ec.runEnvoy(val) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 537 | log.Printf("meta.LastIndex = %d", meta.LastIndex) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 541 | func (ec * EnvoyControl) monitorEtcdKey() { |
| 542 | var err error |
| 543 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 544 | // Get the initial values of the assignment key which contains individual |
| 545 | // voltha core IP addresses. This may be empty until voltha populates it |
| 546 | // so it must be checked |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 547 | |
Richard Jankowski | 9fb5b08 | 2018-05-16 17:58:00 -0400 | [diff] [blame] | 548 | log.Printf("Monitoring etcd key %s", ec.assignmentKey) |
| 549 | val, index, err := ec.readEtcdKey(ec.assignmentKey) |
| 550 | if err == nil { |
| 551 | lastIndex := index |
| 552 | log.Printf("Starting Envoy, initial index = %d", lastIndex) |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 553 | ec.runEnvoy(val) |
Richard Jankowski | 9fb5b08 | 2018-05-16 17:58:00 -0400 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | rch := ec.etcd.Watch(context.Background(), ec.assignmentKey) |
| 557 | for resp := range rch { |
| 558 | for _, ev := range resp.Events { |
| 559 | val = ev.Kv.Value |
| 560 | log.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value) |
| 561 | if ev.Type == etcdapi.EventTypePut { |
| 562 | log.Printf("Starting Envoy") |
| 563 | ec.runEnvoy(val) |
| 564 | } |
| 565 | } |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 569 | func (ec * EnvoyControl) ParseCommandArguments() { |
| 570 | flag.StringVar(&(ec.assignmentKey), "assignment-key", ec.assignmentKey, |
| 571 | "The key for the voltha assignment value in consul") |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 572 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 573 | flag.StringVar(&( ec.envoyConfigTemplate),"envoy-cfg-template", ec.envoyConfigTemplate, |
| 574 | "The path to envoy's configuration template") |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 575 | |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 576 | flag.StringVar(&( ec.envoyConfigTemplateBoth),"envoy-cfg-template-both", ec.envoyConfigTemplateBoth, |
| 577 | "The path to envoy's configuration template for both http and https") |
| 578 | |
| 579 | flag.StringVar(&( ec.envoyConfigTemplateNoHttps),"envoy-cfg-template-no-https", ec.envoyConfigTemplateNoHttps, |
| 580 | "The path to envoy's configuration template with no https") |
| 581 | |
| 582 | flag.StringVar(&( ec.envoyConfigTemplateNoHttp),"envoy-cfg-template-no-http", ec.envoyConfigTemplateNoHttp, |
| 583 | "The path to envoy's configuration template with no http") |
| 584 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 585 | flag.StringVar(&(ec.envoyConfig), "envoy-config", ec.envoyConfig, |
| 586 | "The path to envoy's configuration file" ) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 587 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 588 | flag.StringVar(&(ec.vcoreSvcName), "vcore-svc-name", ec.vcoreSvcName, |
| 589 | "The service name of the voltha core service") |
| 590 | |
| 591 | flag.StringVar(&(ec.consulSvcName),"consul-svc-nme", ec.consulSvcName, |
| 592 | "The service name of the consul service") |
| 593 | |
| 594 | flag.StringVar(&(ec.vcorePort), "vcore-port", ec.vcorePort, |
| 595 | "The port where the vcore's GRPC service can be found") |
| 596 | |
| 597 | flag.StringVar(&(ec.consulPort), "consul-port", ec.consulPort, |
| 598 | "The port where the consul service api can be found") |
| 599 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 600 | flag.StringVar(&(ec.kvStore), "kv", ec.kvStore, |
| 601 | "The KV store: consul or etcd") |
| 602 | |
| 603 | flag.StringVar(&(ec.kvSvcName), "kv-svc-name", ec.kvSvcName, |
| 604 | "The name of the KV store service") |
| 605 | |
| 606 | flag.StringVar(&(ec.kvPort), "kv-port", ec.kvPort, |
| 607 | "The port where the KV service api can be found") |
| 608 | |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 609 | flag.StringVar(&(ec.envoyHttpPort), "http-port", ec.envoyHttpPort, |
| 610 | "The port where the http front-end is served ") |
| 611 | |
| 612 | flag.StringVar(&(ec.envoyHttpsPort), "https-port", ec.envoyHttpsPort, |
| 613 | "The port where the https front-end is served ") |
| 614 | |
| 615 | flag.StringVar(&(ec.envoyGrpcPort), "grpc-port", ec.envoyGrpcPort, |
| 616 | "The port where the grpc front-end is served ") |
| 617 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 618 | flag.IntVar(&(ec.retries), "retries", ec.retries, |
| 619 | "The number of times to retry name lookups and connect requests before failing") |
| 620 | |
| 621 | flag.IntVar(&(ec.waitTime), "wait-time", ec.waitTime, |
| 622 | "The number of seconds to wait between retries") |
| 623 | |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 624 | flag.BoolVar(&(ec.httpDisabled), "disable-http", ec.httpDisabled, |
| 625 | "Disables the http front-end") |
| 626 | |
| 627 | flag.BoolVar(&(ec.httpsDisabled), "disable-https", ec.httpsDisabled, |
| 628 | "Disables ths https front-end") |
| 629 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 630 | flag.Parse() |
| 631 | } |
| 632 | |
| 633 | func (ec * EnvoyControl) Initialize() (err error) { |
Richard Jankowski | d445438 | 2018-02-08 16:21:43 -0500 | [diff] [blame] | 634 | // Resolve KV store's virtual ip address |
| 635 | if err = ec.resolveServiceAddress(ec.kvSvcName); err != nil { |
| 636 | log.Fatal("Can't proceed without KV store's vIP address: %s", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // Resolve voltha's virtual ip address |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 640 | if err = ec.resolveServiceAddress(ec.vcoreSvcName); err != nil { |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 641 | log.Fatal("Can't proceed without voltha's vIP address: %s", err.Error()) |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 644 | if err = ec.kvConnect[ec.kvStore](ec.kvSvcName, ec.kvPort); err != nil { |
| 645 | log.Fatal("Failed to create KV client, aborting: %s", err.Error()) |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | if ec.httpDisabled == true && ec.httpsDisabled == true { |
Richard Jankowski | d445438 | 2018-02-08 16:21:43 -0500 | [diff] [blame] | 649 | log.Printf("Cowardly refusing to disable both http and https, leaving them both enabled\n") |
Sergio Slobodrian | 6570c74 | 2017-08-07 23:11:33 -0400 | [diff] [blame] | 650 | } else if ec.httpDisabled == true { |
| 651 | log.Printf("Diasabling http\n") |
| 652 | ec.envoyConfigTemplate = ec.envoyConfigTemplateNoHttp |
| 653 | } else if ec.httpsDisabled == true { |
| 654 | log.Printf("Diasabling https\n") |
| 655 | ec.envoyConfigTemplate = ec.envoyConfigTemplateNoHttps |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 656 | } |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 657 | |
| 658 | return |
| 659 | } |
| 660 | |
| 661 | func main() { |
| 662 | |
| 663 | var err error |
| 664 | var ec * EnvoyControl |
| 665 | |
| 666 | ec = NewEnvoyControl() |
| 667 | ec.ParseCommandArguments() |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 668 | if ec.kvStore != "etcd" { |
| 669 | ec.kvStore = "consul" |
| 670 | } |
| 671 | log.Printf("KV-store %s at %s:%s", ec.kvStore, ec.kvSvcName, ec.kvPort) |
| 672 | |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 673 | if err = ec.Initialize(); err != nil { |
Richard Jankowski | d445438 | 2018-02-08 16:21:43 -0500 | [diff] [blame] | 674 | log.Fatal("Envoy control initialization failed, aborting: %s", err.Error()) |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 675 | } |
| 676 | |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 677 | |
| 678 | // Start envoy and monitor changes to the KV store to reload |
Richard Jankowski | 1527459 | 2017-12-12 15:52:37 -0500 | [diff] [blame] | 679 | // consul's config. This never returns unless something crashes. |
| 680 | ec.kvMonitor[ec.kvStore]() |
Sergio Slobodrian | 8dec8de | 2017-07-20 12:45:07 -0400 | [diff] [blame] | 681 | log.Fatal("Monitor returned, this shouldn't happen") |
Sergio Slobodrian | be82927 | 2017-07-17 14:45:45 -0400 | [diff] [blame] | 682 | } |