mirror of
https://github.com/minio/minio.git
synced 2025-11-09 21:49:46 -05:00
Bring etcd support for bucket DNS federation
- Supports centralized `config.json` - Supports centralized `bucket` service records for client lookups - implement a new proxy forwarder
This commit is contained in:
committed by
kannappanr
parent
7872c192ec
commit
853ea371ce
140
pkg/dns/coredns.go
Normal file
140
pkg/dns/coredns.go
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2018 Minio, Inc.
|
||||
*
|
||||
* 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 dns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/etcd/msg"
|
||||
etcdc "github.com/coreos/etcd/client"
|
||||
)
|
||||
|
||||
// create a new coredns service record for the bucket.
|
||||
func newCoreDNSMsg(bucket string, ip string, port int, ttl uint32) ([]byte, error) {
|
||||
return json.Marshal(&SrvRecord{
|
||||
Host: ip,
|
||||
Port: port,
|
||||
TTL: ttl,
|
||||
CreationDate: time.Now().UTC(),
|
||||
})
|
||||
}
|
||||
|
||||
// Retrieves list of DNS entries for a bucket.
|
||||
func (c *coreDNS) List() ([]SrvRecord, error) {
|
||||
kapi := etcdc.NewKeysAPI(c.etcdClient)
|
||||
key := msg.Path(fmt.Sprintf("%s.", c.domainName), defaultPrefixPath)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
r, err := kapi.Get(ctx, key, nil)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var srvRecords []SrvRecord
|
||||
for _, n := range r.Node.Nodes {
|
||||
var srvRecord SrvRecord
|
||||
if err = json.Unmarshal([]byte(n.Value), &srvRecord); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
srvRecord.Key = strings.TrimPrefix(n.Key, key)
|
||||
srvRecords = append(srvRecords, srvRecord)
|
||||
}
|
||||
sort.Slice(srvRecords, func(i int, j int) bool {
|
||||
return srvRecords[i].Key < srvRecords[j].Key
|
||||
})
|
||||
return srvRecords, nil
|
||||
}
|
||||
|
||||
// Retrieves DNS record for a bucket.
|
||||
func (c *coreDNS) Get(bucket string) (SrvRecord, error) {
|
||||
kapi := etcdc.NewKeysAPI(c.etcdClient)
|
||||
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, c.domainName), defaultPrefixPath)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
r, err := kapi.Get(ctx, key, nil)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return SrvRecord{}, err
|
||||
}
|
||||
var sr SrvRecord
|
||||
if err = json.Unmarshal([]byte(r.Node.Value), &sr); err != nil {
|
||||
return SrvRecord{}, err
|
||||
}
|
||||
sr.Key = strings.TrimPrefix(r.Node.Key, key)
|
||||
return sr, nil
|
||||
}
|
||||
|
||||
// Adds DNS entries into etcd endpoint in CoreDNS etcd messae format.
|
||||
func (c *coreDNS) Put(bucket string) error {
|
||||
bucketMsg, err := newCoreDNSMsg(bucket, c.domainIP, c.domainPort, defaultTTL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kapi := etcdc.NewKeysAPI(c.etcdClient)
|
||||
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, c.domainName), defaultPrefixPath)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
_, err = kapi.Set(ctx, key, string(bucketMsg), nil)
|
||||
cancel()
|
||||
return err
|
||||
}
|
||||
|
||||
// Removes DNS entries added in Put().
|
||||
func (c *coreDNS) Delete(bucket string) error {
|
||||
kapi := etcdc.NewKeysAPI(c.etcdClient)
|
||||
key := msg.Path(fmt.Sprintf("%s.%s.", bucket, c.domainName), defaultPrefixPath)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
||||
_, err := kapi.Delete(ctx, key, nil)
|
||||
cancel()
|
||||
return err
|
||||
}
|
||||
|
||||
// CoreDNS - represents dns config for coredns server.
|
||||
type coreDNS struct {
|
||||
domainName, domainIP string
|
||||
domainPort int
|
||||
etcdClient etcdc.Client
|
||||
}
|
||||
|
||||
// NewCoreDNS - initialize a new coreDNS set/unset values.
|
||||
func NewCoreDNS(domainName, domainIP, domainPort string, etcdClient etcdc.Client) (Config, error) {
|
||||
if domainName == "" || domainIP == "" || etcdClient == nil {
|
||||
return nil, errors.New("invalid argument")
|
||||
}
|
||||
|
||||
if net.ParseIP(domainIP) == nil {
|
||||
return nil, errors.New("invalid argument")
|
||||
}
|
||||
|
||||
port, err := strconv.Atoi(domainPort)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &coreDNS{
|
||||
domainName: domainName,
|
||||
domainIP: domainIP,
|
||||
domainPort: port,
|
||||
etcdClient: etcdClient,
|
||||
}, nil
|
||||
}
|
||||
65
pkg/dns/dns.go
Normal file
65
pkg/dns/dns.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2018 Minio, Inc.
|
||||
*
|
||||
* 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 dns
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTTL = 30
|
||||
defaultPrefixPath = "/skydns"
|
||||
defaultContextTimeout = 5 * time.Minute
|
||||
)
|
||||
|
||||
// SrvRecord - represents a DNS service record
|
||||
type SrvRecord struct {
|
||||
Host string `json:"host,omitempty"`
|
||||
Port int `json:"port,omitempty"`
|
||||
Priority int `json:"priority,omitempty"`
|
||||
Weight int `json:"weight,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Mail bool `json:"mail,omitempty"` // Be an MX record. Priority becomes Preference.
|
||||
TTL uint32 `json:"ttl,omitempty"`
|
||||
|
||||
// Holds info about when the entry was created first.
|
||||
CreationDate time.Time `json:"creationDate"`
|
||||
|
||||
// When a SRV record with a "Host: IP-address" is added, we synthesize
|
||||
// a srv.Target domain name. Normally we convert the full Key where
|
||||
// the record lives to a DNS name and use this as the srv.Target. When
|
||||
// TargetStrip > 0 we strip the left most TargetStrip labels from the
|
||||
// DNS name.
|
||||
TargetStrip int `json:"targetstrip,omitempty"`
|
||||
|
||||
// Group is used to group (or *not* to group) different services
|
||||
// together. Services with an identical Group are returned in
|
||||
// the same answer.
|
||||
Group string `json:"group,omitempty"`
|
||||
|
||||
// Key carries the original key used during Put().
|
||||
Key string `json:"-"`
|
||||
}
|
||||
|
||||
// Config - represents dns put, get interface. This interface can be
|
||||
// used to implement various backends as needed.
|
||||
type Config interface {
|
||||
Put(key string) error
|
||||
List() ([]SrvRecord, error)
|
||||
Get(key string) (SrvRecord, error)
|
||||
Delete(key string) error
|
||||
}
|
||||
169
pkg/handlers/forwarder.go
Normal file
169
pkg/handlers/forwarder.go
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2018 Minio, Inc.
|
||||
*
|
||||
* 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 handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultFlushInterval = time.Duration(100) * time.Millisecond
|
||||
|
||||
// Forwarder forwards all incoming HTTP requests to configured transport.
|
||||
type Forwarder struct {
|
||||
RoundTripper http.RoundTripper
|
||||
PassHost bool
|
||||
|
||||
// internal variables
|
||||
rewriter *headerRewriter
|
||||
}
|
||||
|
||||
// NewForwarder creates an instance of Forwarder based on the provided list of configuration options
|
||||
func NewForwarder(f *Forwarder) *Forwarder {
|
||||
f.rewriter = &headerRewriter{}
|
||||
if f.RoundTripper == nil {
|
||||
f.RoundTripper = http.DefaultTransport
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
// ServeHTTP forwards HTTP traffic using the configured transport
|
||||
func (f *Forwarder) ServeHTTP(w http.ResponseWriter, inReq *http.Request) {
|
||||
outReq := new(http.Request)
|
||||
*outReq = *inReq // includes shallow copies of maps, but we handle this in Director
|
||||
|
||||
revproxy := httputil.ReverseProxy{
|
||||
Director: func(req *http.Request) {
|
||||
f.modifyRequest(req, inReq.URL)
|
||||
},
|
||||
Transport: f.RoundTripper,
|
||||
FlushInterval: defaultFlushInterval,
|
||||
}
|
||||
revproxy.ServeHTTP(w, outReq)
|
||||
}
|
||||
|
||||
func (f *Forwarder) getURLFromRequest(req *http.Request) *url.URL {
|
||||
// If the Request was created by Go via a real HTTP request, RequestURI will
|
||||
// contain the original query string. If the Request was created in code, RequestURI
|
||||
// will be empty, and we will use the URL object instead
|
||||
u := req.URL
|
||||
if req.RequestURI != "" {
|
||||
parsedURL, err := url.ParseRequestURI(req.RequestURI)
|
||||
if err == nil {
|
||||
u = parsedURL
|
||||
}
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// copyURL provides update safe copy by avoiding shallow copying User field
|
||||
func copyURL(i *url.URL) *url.URL {
|
||||
out := *i
|
||||
if i.User != nil {
|
||||
out.User = &(*i.User)
|
||||
}
|
||||
return &out
|
||||
}
|
||||
|
||||
// Modify the request to handle the target URL
|
||||
func (f *Forwarder) modifyRequest(outReq *http.Request, target *url.URL) {
|
||||
outReq.URL = copyURL(outReq.URL)
|
||||
outReq.URL.Scheme = target.Scheme
|
||||
outReq.URL.Host = target.Host
|
||||
|
||||
u := f.getURLFromRequest(outReq)
|
||||
|
||||
outReq.URL.Path = u.Path
|
||||
outReq.URL.RawPath = u.RawPath
|
||||
outReq.URL.RawQuery = u.RawQuery
|
||||
outReq.RequestURI = "" // Outgoing request should not have RequestURI
|
||||
|
||||
// Do not pass client Host header unless requested.
|
||||
if !f.PassHost {
|
||||
outReq.Host = target.Host
|
||||
}
|
||||
|
||||
// TODO: only supports HTTP 1.1 for now.
|
||||
outReq.Proto = "HTTP/1.1"
|
||||
outReq.ProtoMajor = 1
|
||||
outReq.ProtoMinor = 1
|
||||
|
||||
f.rewriter.Rewrite(outReq)
|
||||
|
||||
// Disable closeNotify when method GET for http pipelining
|
||||
if outReq.Method == http.MethodGet {
|
||||
quietReq := outReq.WithContext(context.Background())
|
||||
*outReq = *quietReq
|
||||
}
|
||||
}
|
||||
|
||||
// headerRewriter is responsible for removing hop-by-hop headers and setting forwarding headers
|
||||
type headerRewriter struct{}
|
||||
|
||||
// Clean up IP in case if it is ipv6 address and it has {zone} information in it, like
|
||||
// "[fe80::d806:a55d:eb1b:49cc%vEthernet (vmxnet3 Ethernet Adapter - Virtual Switch)]:64692"
|
||||
func ipv6fix(clientIP string) string {
|
||||
return strings.Split(clientIP, "%")[0]
|
||||
}
|
||||
|
||||
func (rw *headerRewriter) Rewrite(req *http.Request) {
|
||||
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
||||
clientIP = ipv6fix(clientIP)
|
||||
if req.Header.Get(xRealIP) == "" {
|
||||
req.Header.Set(xRealIP, clientIP)
|
||||
}
|
||||
}
|
||||
|
||||
xfProto := req.Header.Get(xForwardedProto)
|
||||
if xfProto == "" {
|
||||
if req.TLS != nil {
|
||||
req.Header.Set(xForwardedProto, "https")
|
||||
} else {
|
||||
req.Header.Set(xForwardedProto, "http")
|
||||
}
|
||||
}
|
||||
|
||||
if xfPort := req.Header.Get(xForwardedPort); xfPort == "" {
|
||||
req.Header.Set(xForwardedPort, forwardedPort(req))
|
||||
}
|
||||
|
||||
if xfHost := req.Header.Get(xForwardedHost); xfHost == "" && req.Host != "" {
|
||||
req.Header.Set(xForwardedHost, req.Host)
|
||||
}
|
||||
}
|
||||
|
||||
func forwardedPort(req *http.Request) string {
|
||||
if req == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if _, port, err := net.SplitHostPort(req.Host); err == nil && port != "" {
|
||||
return port
|
||||
}
|
||||
|
||||
if req.TLS != nil {
|
||||
return "443"
|
||||
}
|
||||
|
||||
return "80"
|
||||
}
|
||||
@@ -26,8 +26,11 @@ import (
|
||||
var (
|
||||
// De-facto standard header keys.
|
||||
xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
|
||||
xForwardedHost = http.CanonicalHeaderKey("X-Forwarded-Host")
|
||||
xForwardedPort = http.CanonicalHeaderKey("X-Forwarded-Port")
|
||||
xForwardedProto = http.CanonicalHeaderKey("X-Forwarded-Proto")
|
||||
xForwardedScheme = http.CanonicalHeaderKey("X-Forwarded-Scheme")
|
||||
xForwardedServer = http.CanonicalHeaderKey("X-Forwarded-Server")
|
||||
xRealIP = http.CanonicalHeaderKey("X-Real-IP")
|
||||
)
|
||||
|
||||
|
||||
@@ -41,21 +41,21 @@ type Config interface {
|
||||
DeepDiff(Config) ([]structs.Field, error)
|
||||
}
|
||||
|
||||
// config - implements quick.Config interface
|
||||
type config struct {
|
||||
// localConfig - implements quick.Config interface
|
||||
type localConfig struct {
|
||||
data interface{}
|
||||
lock *sync.RWMutex
|
||||
}
|
||||
|
||||
// Version returns the current config file format version
|
||||
func (d config) Version() string {
|
||||
func (d localConfig) Version() string {
|
||||
st := structs.New(d.data)
|
||||
f := st.Field("Version")
|
||||
return f.Value().(string)
|
||||
}
|
||||
|
||||
// String converts JSON config to printable string
|
||||
func (d config) String() string {
|
||||
func (d localConfig) String() string {
|
||||
configBytes, _ := json.MarshalIndent(d.data, "", "\t")
|
||||
return string(configBytes)
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func (d config) String() string {
|
||||
// Save writes config data to a file. Data format
|
||||
// is selected based on file extension or JSON if
|
||||
// not provided.
|
||||
func (d config) Save(filename string) error {
|
||||
func (d localConfig) Save(filename string) error {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
|
||||
@@ -89,19 +89,19 @@ func (d config) Save(filename string) error {
|
||||
// Load - loads config from file and merge with currently set values
|
||||
// File content format is guessed from the file name extension, if not
|
||||
// available, consider that we have JSON.
|
||||
func (d config) Load(filename string) error {
|
||||
func (d localConfig) Load(filename string) error {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
return loadFileConfig(filename, d.data)
|
||||
}
|
||||
|
||||
// Data - grab internal data map for reading
|
||||
func (d config) Data() interface{} {
|
||||
func (d localConfig) Data() interface{} {
|
||||
return d.data
|
||||
}
|
||||
|
||||
//Diff - list fields that are in A but not in B
|
||||
func (d config) Diff(c Config) ([]structs.Field, error) {
|
||||
// Diff - list fields that are in A but not in B
|
||||
func (d localConfig) Diff(c Config) ([]structs.Field, error) {
|
||||
var fields []structs.Field
|
||||
|
||||
currFields := structs.Fields(d.Data())
|
||||
@@ -123,7 +123,7 @@ func (d config) Diff(c Config) ([]structs.Field, error) {
|
||||
}
|
||||
|
||||
// DeepDiff - list fields in A that are missing or not equal to fields in B
|
||||
func (d config) DeepDiff(c Config) ([]structs.Field, error) {
|
||||
func (d localConfig) DeepDiff(c Config) ([]structs.Field, error) {
|
||||
var fields []structs.Field
|
||||
|
||||
currFields := structs.Fields(d.Data())
|
||||
@@ -179,13 +179,13 @@ func writeFile(filename string, data []byte) error {
|
||||
return safeFile.Close()
|
||||
}
|
||||
|
||||
// New - instantiate a new config
|
||||
func New(data interface{}) (Config, error) {
|
||||
// NewLocalConfig - instantiate a new config r/w configs from local files.
|
||||
func NewLocalConfig(data interface{}) (Config, error) {
|
||||
if err := checkData(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d := new(config)
|
||||
d := new(localConfig)
|
||||
d.data = data
|
||||
d.lock = new(sync.RWMutex)
|
||||
return d, nil
|
||||
@@ -204,7 +204,7 @@ func GetVersion(filename string) (version string, err error) {
|
||||
|
||||
// Load - loads json config from filename for the a given struct data
|
||||
func Load(filename string, data interface{}) (qc Config, err error) {
|
||||
if qc, err = New(data); err == nil {
|
||||
if qc, err = NewLocalConfig(data); err == nil {
|
||||
err = qc.Load(filename)
|
||||
}
|
||||
return qc, err
|
||||
@@ -213,7 +213,7 @@ func Load(filename string, data interface{}) (qc Config, err error) {
|
||||
// Save - saves given configuration data into given file as JSON.
|
||||
func Save(filename string, data interface{}) (err error) {
|
||||
var qc Config
|
||||
if qc, err = New(data); err == nil {
|
||||
if qc, err = NewLocalConfig(data); err == nil {
|
||||
err = qc.Save(filename)
|
||||
}
|
||||
|
||||
|
||||
175
pkg/quick/quick_etcd.go
Normal file
175
pkg/quick/quick_etcd.go
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Quick - Quick key value store for config files and persistent state files
|
||||
*
|
||||
* Quick (C) 2018 Minio, Inc.
|
||||
*
|
||||
* 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 quick
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
etcdc "github.com/coreos/etcd/client"
|
||||
"github.com/fatih/structs"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// etcdConfig - implements quick.Config interface
|
||||
type etcdConfig struct {
|
||||
lock *sync.Mutex
|
||||
data interface{}
|
||||
clnt etcdc.Client
|
||||
}
|
||||
|
||||
// Version returns the current config file format version
|
||||
func (d etcdConfig) Version() string {
|
||||
st := structs.New(d.data)
|
||||
f := st.Field("Version")
|
||||
return f.Value().(string)
|
||||
}
|
||||
|
||||
// String converts JSON config to printable string
|
||||
func (d etcdConfig) String() string {
|
||||
configBytes, _ := json.MarshalIndent(d.data, "", "\t")
|
||||
return string(configBytes)
|
||||
}
|
||||
|
||||
// Save writes config data to an configured etcd endpoints. Data format
|
||||
// is selected based on file extension or JSON if not provided.
|
||||
func (d etcdConfig) Save(filename string) error {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
|
||||
// Fetch filename's extension
|
||||
ext := filepath.Ext(filename)
|
||||
// Marshal data
|
||||
dataBytes, err := toMarshaller(ext)(d.data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
dataBytes = []byte(strings.Replace(string(dataBytes), "\n", "\r\n", -1))
|
||||
}
|
||||
|
||||
kapi := etcdc.NewKeysAPI(d.clnt)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
_, err = kapi.Create(ctx, filename, string(dataBytes))
|
||||
cancel()
|
||||
return err
|
||||
}
|
||||
|
||||
// Load - loads config from file and merge with currently set values
|
||||
// File content format is guessed from the file name extension, if not
|
||||
// available, consider that we have JSON.
|
||||
func (d etcdConfig) Load(filename string) error {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
|
||||
kapi := etcdc.NewKeysAPI(d.clnt)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
resp, err := kapi.Get(ctx, filename, nil)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var ev *etcdc.Node
|
||||
switch {
|
||||
case resp.Node.Dir:
|
||||
for _, ev = range resp.Node.Nodes {
|
||||
if string(ev.Key) == filename {
|
||||
break
|
||||
}
|
||||
}
|
||||
default:
|
||||
ev = resp.Node
|
||||
}
|
||||
|
||||
fileData := ev.Value
|
||||
if runtime.GOOS == "windows" {
|
||||
fileData = strings.Replace(ev.Value, "\r\n", "\n", -1)
|
||||
}
|
||||
|
||||
// Unmarshal file's content
|
||||
return toUnmarshaller(filepath.Ext(filename))([]byte(fileData), d.data)
|
||||
}
|
||||
|
||||
// Data - grab internal data map for reading
|
||||
func (d etcdConfig) Data() interface{} {
|
||||
return d.data
|
||||
}
|
||||
|
||||
//Diff - list fields that are in A but not in B
|
||||
func (d etcdConfig) Diff(c Config) ([]structs.Field, error) {
|
||||
var fields []structs.Field
|
||||
|
||||
currFields := structs.Fields(d.Data())
|
||||
newFields := structs.Fields(c.Data())
|
||||
|
||||
var found bool
|
||||
for _, currField := range currFields {
|
||||
found = false
|
||||
for _, newField := range newFields {
|
||||
if reflect.DeepEqual(currField.Name(), newField.Name()) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
fields = append(fields, *currField)
|
||||
}
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
// DeepDiff - list fields in A that are missing or not equal to fields in B
|
||||
func (d etcdConfig) DeepDiff(c Config) ([]structs.Field, error) {
|
||||
var fields []structs.Field
|
||||
|
||||
currFields := structs.Fields(d.Data())
|
||||
newFields := structs.Fields(c.Data())
|
||||
|
||||
var found bool
|
||||
for _, currField := range currFields {
|
||||
found = false
|
||||
for _, newField := range newFields {
|
||||
if reflect.DeepEqual(currField.Value(), newField.Value()) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
fields = append(fields, *currField)
|
||||
}
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
// NewEtcdConfig - instantiate a new etcd config
|
||||
func NewEtcdConfig(data interface{}, clnt etcdc.Client) (Config, error) {
|
||||
if err := checkData(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d := new(etcdConfig)
|
||||
d.data = data
|
||||
d.clnt = clnt
|
||||
d.lock = &sync.Mutex{}
|
||||
return d, nil
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func TestReadVersion(t *testing.T) {
|
||||
Version string
|
||||
}
|
||||
saveMe := myStruct{"1"}
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func TestReadVersionErr(t *testing.T) {
|
||||
Version int
|
||||
}
|
||||
saveMe := myStruct{1}
|
||||
_, err := New(&saveMe)
|
||||
_, err := NewLocalConfig(&saveMe)
|
||||
if err == nil {
|
||||
t.Fatal("Unexpected should fail in initialization for bad input")
|
||||
}
|
||||
@@ -95,7 +95,7 @@ func TestSaveFailOnDir(t *testing.T) {
|
||||
Version string
|
||||
}
|
||||
saveMe := myStruct{"1"}
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -171,7 +171,7 @@ func TestLoadFile(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("Unexpected should fail to load empty JSON")
|
||||
}
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -186,7 +186,7 @@ func TestLoadFile(t *testing.T) {
|
||||
}
|
||||
|
||||
saveMe = myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
||||
config, err = New(&saveMe)
|
||||
config, err = NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -240,7 +240,7 @@ directories:
|
||||
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
||||
|
||||
// Save format using
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -262,7 +262,7 @@ directories:
|
||||
|
||||
// Check if the loaded data is the same as the saved one
|
||||
loadMe := myStruct{}
|
||||
config, err = New(&loadMe)
|
||||
config, err = NewLocalConfig(&loadMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -306,7 +306,7 @@ func TestJSONFormat(t *testing.T) {
|
||||
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
||||
|
||||
// Save format using
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -328,7 +328,7 @@ func TestJSONFormat(t *testing.T) {
|
||||
|
||||
// Check if the loaded data is the same as the saved one
|
||||
loadMe := myStruct{}
|
||||
config, err = New(&loadMe)
|
||||
config, err = NewLocalConfig(&loadMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -351,7 +351,7 @@ func TestSaveLoad(t *testing.T) {
|
||||
Directories []string
|
||||
}
|
||||
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -361,7 +361,7 @@ func TestSaveLoad(t *testing.T) {
|
||||
}
|
||||
|
||||
loadMe := myStruct{Version: "1"}
|
||||
newConfig, err := New(&loadMe)
|
||||
newConfig, err := NewLocalConfig(&loadMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -393,7 +393,7 @@ func TestSaveBackup(t *testing.T) {
|
||||
Directories []string
|
||||
}
|
||||
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -403,7 +403,7 @@ func TestSaveBackup(t *testing.T) {
|
||||
}
|
||||
|
||||
loadMe := myStruct{Version: "1"}
|
||||
newConfig, err := New(&loadMe)
|
||||
newConfig, err := NewLocalConfig(&loadMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -424,7 +424,7 @@ func TestSaveBackup(t *testing.T) {
|
||||
t.Fatal("Expected to mismatch but succeeded instead")
|
||||
}
|
||||
|
||||
config, err = New(&mismatch)
|
||||
config, err = NewLocalConfig(&mismatch)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -442,20 +442,20 @@ func TestDiff(t *testing.T) {
|
||||
Directories []string
|
||||
}
|
||||
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
type myNewStruct struct {
|
||||
type myNewLocalConfigStruct struct {
|
||||
Version string
|
||||
// User string
|
||||
Password string
|
||||
Directories []string
|
||||
}
|
||||
|
||||
mismatch := myNewStruct{"1", "nopassword", []string{"Work", "documents", "Music"}}
|
||||
newConfig, err := New(&mismatch)
|
||||
mismatch := myNewLocalConfigStruct{"1", "nopassword", []string{"Work", "documents", "Music"}}
|
||||
newConfig, err := NewLocalConfig(&mismatch)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -482,13 +482,13 @@ func TestDeepDiff(t *testing.T) {
|
||||
Directories []string
|
||||
}
|
||||
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
|
||||
config, err := New(&saveMe)
|
||||
config, err := NewLocalConfig(&saveMe)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mismatch := myStruct{"1", "Guest", "nopassword", []string{"Work", "documents", "Music"}}
|
||||
newConfig, err := New(&mismatch)
|
||||
newConfig, err := NewLocalConfig(&mismatch)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user