2016-02-22 00:13:08 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
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 nginx
|
|
|
|
|
|
|
|
import (
|
2016-03-15 15:31:39 +00:00
|
|
|
"bytes"
|
2016-02-22 00:13:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/fatih/structs"
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
var funcMap = template.FuncMap{
|
|
|
|
"empty": func(input interface{}) bool {
|
|
|
|
check, ok := input.(string)
|
|
|
|
if ok {
|
|
|
|
return len(check) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
"dict": func(values ...interface{}) (map[string]interface{}, error) {
|
|
|
|
if len(values)%2 != 0 {
|
|
|
|
return nil, errors.New("invalid dict call")
|
|
|
|
}
|
|
|
|
dict := make(map[string]interface{}, len(values)/2)
|
|
|
|
for i := 0; i < len(values); i += 2 {
|
|
|
|
key, ok := values[i].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("dict keys must be strings")
|
|
|
|
}
|
|
|
|
dict[key] = values[i+1]
|
|
|
|
}
|
|
|
|
return dict, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ngx *NginxManager) loadTemplate() {
|
|
|
|
tmpl, _ := template.New("nginx.tmpl").Funcs(funcMap).ParseFiles("./nginx.tmpl")
|
|
|
|
ngx.template = tmpl
|
|
|
|
}
|
|
|
|
|
2016-03-16 18:57:36 +00:00
|
|
|
func (ngx *NginxManager) writeCfg(cfg *nginxConfiguration, upstreams []*Upstream, servers []*Server, servicesL4 []Service) (bool, error) {
|
2016-02-22 00:13:08 +00:00
|
|
|
fromMap := structs.Map(cfg)
|
|
|
|
toMap := structs.Map(ngx.defCfg)
|
|
|
|
curNginxCfg := merge(toMap, fromMap)
|
|
|
|
|
|
|
|
conf := make(map[string]interface{})
|
2016-03-15 02:29:13 +00:00
|
|
|
conf["upstreams"] = upstreams
|
|
|
|
conf["servers"] = servers
|
2016-02-22 00:13:08 +00:00
|
|
|
conf["tcpServices"] = servicesL4
|
|
|
|
conf["defBackend"] = ngx.defBackend
|
|
|
|
conf["defResolver"] = ngx.defResolver
|
|
|
|
conf["sslDHParam"] = ngx.sslDHParam
|
|
|
|
conf["cfg"] = curNginxCfg
|
|
|
|
|
|
|
|
if ngx.defError.ServiceName != "" {
|
|
|
|
conf["defErrorSvc"] = ngx.defError
|
|
|
|
} else {
|
|
|
|
conf["defErrorSvc"] = false
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:31:39 +00:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
err := ngx.template.Execute(buffer, conf)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2016-03-19 00:41:31 +00:00
|
|
|
changed, err := ngx.needsReload(buffer)
|
2016-03-15 15:31:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if glog.V(3) {
|
2016-02-22 00:13:08 +00:00
|
|
|
b, err := json.Marshal(conf)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error:", err)
|
|
|
|
}
|
|
|
|
glog.Infof("nginx configuration: %v", string(b))
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:31:39 +00:00
|
|
|
return changed, nil
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|