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