sigs.k8s.io/external-dns@v0.14.1/source/connector.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 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 */ 16 17 package source 18 19 import ( 20 "context" 21 "encoding/gob" 22 "net" 23 "time" 24 25 log "github.com/sirupsen/logrus" 26 27 "sigs.k8s.io/external-dns/endpoint" 28 ) 29 30 const ( 31 dialTimeout = 30 * time.Second 32 ) 33 34 // connectorSource is an implementation of Source that provides endpoints by connecting 35 // to a remote tcp server. The encoding/decoding is done using encoder/gob package. 36 type connectorSource struct { 37 remoteServer string 38 } 39 40 // NewConnectorSource creates a new connectorSource with the given config. 41 func NewConnectorSource(remoteServer string) (Source, error) { 42 return &connectorSource{ 43 remoteServer: remoteServer, 44 }, nil 45 } 46 47 // Endpoints returns endpoint objects. 48 func (cs *connectorSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) { 49 endpoints := []*endpoint.Endpoint{} 50 51 conn, err := net.DialTimeout("tcp", cs.remoteServer, dialTimeout) 52 if err != nil { 53 log.Errorf("Connection error: %v", err) 54 return nil, err 55 } 56 defer conn.Close() 57 58 decoder := gob.NewDecoder(conn) 59 if err := decoder.Decode(&endpoints); err != nil { 60 log.Errorf("Decode error: %v", err) 61 return nil, err 62 } 63 64 log.Debugf("Received endpoints: %#v", endpoints) 65 66 return endpoints, nil 67 } 68 69 func (cs *connectorSource) AddEventHandler(ctx context.Context, handler func()) { 70 }