Repository: incubator-trafficcontrol
Updated Branches:
  refs/heads/master d7cf76f42 -> e82b68f5e


Add TM2 getting CDN name from Traffic Ops

Adds Traffic Monitor 2.0 getting the name of the CDN to monitor from
Traffic Ops. The CDN is the CDN that this Traffic Monitor is assigned
to in TO (known by the hostname command). If this TO hostname doesn't
exist in Traffic Ops, an error is logged and the old traffic_ops.cfg
cdnName value is used. If a config cdnName is set which differs from
the TO CDN, the TO CDN is used, and a warning is logged (TO is the
Source of Truth).

This makes deployment easier and safer: Traffic Monitor deployment
configs need only the Traffic Ops URL and login, and the Traffic Ops
CDN will be used, so it isn't possible to accidentally deploy a
Monitor configured to use a different CDN than is in Traffic Ops.


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/7cab722b
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/7cab722b
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/7cab722b

Branch: refs/heads/master
Commit: 7cab722b58c7de222594de7ee3320d5aed5916c0
Parents: d7cf76f
Author: Robert Butts <robert.o.bu...@gmail.com>
Authored: Wed Mar 15 15:29:59 2017 -0600
Committer: Jeff Elsloo <jeffrey_els...@cable.comcast.com>
Committed: Thu Mar 30 13:46:28 2017 -0600

----------------------------------------------------------------------
 .../traffic_monitor/manager/opsconfig.go        | 63 ++++++++++++++------
 1 file changed, 44 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/7cab722b/traffic_monitor_golang/traffic_monitor/manager/opsconfig.go
----------------------------------------------------------------------
diff --git a/traffic_monitor_golang/traffic_monitor/manager/opsconfig.go 
b/traffic_monitor_golang/traffic_monitor/manager/opsconfig.go
index d0e3e68..e2a275d 100644
--- a/traffic_monitor_golang/traffic_monitor/manager/opsconfig.go
+++ b/traffic_monitor_golang/traffic_monitor/manager/opsconfig.go
@@ -86,10 +86,32 @@ func StartOpsConfigManager(
 
        // TODO remove change subscribers, give Threadsafes directly to the 
things that need them. If they only set vars, and don't actually do work on 
change.
        go func() {
+               handleErr := func(err error) {
+                       errorCount.Inc()
+                       log.Errorf("OpsConfigManager: %v\n", err)
+               }
+
                httpServer := srvhttp.Server{}
 
                for newOpsConfig := range opsConfigChannel {
-                       var err error
+                       // TODO config? parameter?
+                       useCache := false
+                       trafficOpsRequestTimeout := time.Second * 
time.Duration(10)
+                       realToSession, err := 
to.LoginWithAgent(newOpsConfig.Url, newOpsConfig.Username, 
newOpsConfig.Password, newOpsConfig.Insecure, staticAppData.UserAgent, 
useCache, trafficOpsRequestTimeout)
+                       if err != nil {
+                               handleErr(fmt.Errorf("instantiating Session 
with traffic_ops: %s\n", err))
+                               continue
+                       }
+
+                       if cdn, err := getMonitorCDN(realToSession, 
staticAppData.Hostname); err != nil {
+                               handleErr(fmt.Errorf("getting CDN name from 
Traffic Ops, using config CDN '%s': %s\n", newOpsConfig.CdnName, err))
+                       } else {
+                               if newOpsConfig.CdnName != "" && 
newOpsConfig.CdnName != cdn {
+                                       log.Warnf("%s Traffic Ops CDN '%s' 
doesn't match config CDN '%s' - using Traffic Ops CDN\n", 
staticAppData.Hostname, cdn, newOpsConfig.CdnName)
+                               }
+                               newOpsConfig.CdnName = cdn
+                       }
+
                        opsConfig.Set(newOpsConfig)
 
                        listenAddress := ":80" // default
@@ -98,11 +120,6 @@ func StartOpsConfigManager(
                                listenAddress = newOpsConfig.HttpListener
                        }
 
-                       handleErr := func(err error) {
-                               errorCount.Inc()
-                               log.Errorf("OpsConfigManager: %v\n", err)
-                       }
-
                        endpoints := datareq.MakeDispatchMap(
                                opsConfig,
                                toSession,
@@ -127,25 +144,16 @@ func StartOpsConfigManager(
                                unpolledCaches,
                                monitorConfig,
                        )
-                       err = httpServer.Run(endpoints, listenAddress, 
cfg.ServeReadTimeout, cfg.ServeWriteTimeout, cfg.StaticFileDir)
-                       if err != nil {
-                               handleErr(fmt.Errorf("MonitorConfigPoller: 
error creating HTTP server: %s\n", err))
-                               continue
-                       }
-
-                       // TODO config? parameter?
-                       useCache := false
-                       trafficOpsRequestTimeout := time.Second * 
time.Duration(10)
 
-                       realToSession, err := 
to.LoginWithAgent(newOpsConfig.Url, newOpsConfig.Username, 
newOpsConfig.Password, newOpsConfig.Insecure, staticAppData.UserAgent, 
useCache, trafficOpsRequestTimeout)
-                       if err != nil {
-                               handleErr(fmt.Errorf("MonitorConfigPoller: 
error instantiating Session with traffic_ops: %s\n", err))
+                       if err := httpServer.Run(endpoints, listenAddress, 
cfg.ServeReadTimeout, cfg.ServeWriteTimeout, cfg.StaticFileDir); err != nil {
+                               handleErr(fmt.Errorf("creating HTTP server: 
%s\n", err))
                                continue
                        }
+
                        toSession.Set(realToSession)
 
                        if err := toData.Fetch(toSession, 
newOpsConfig.CdnName); err != nil {
-                               handleErr(fmt.Errorf("Error getting Traffic Ops 
data: %v\n", err))
+                               handleErr(fmt.Errorf("getting Traffic Ops data: 
%v\n", err))
                                continue
                        }
 
@@ -162,3 +170,20 @@ func StartOpsConfigManager(
 
        return opsConfig
 }
+
+// getMonitorCDN returns the CDN of a given Traffic Monitor.
+// TODO change to get by name, when Traffic Ops supports querying a single 
server.
+func getMonitorCDN(toc *to.Session, monitorHostname string) (string, error) {
+       servers, err := toc.Servers()
+       if err != nil {
+               return "", fmt.Errorf("getting monitor %s CDN: %v", 
monitorHostname, err)
+       }
+
+       for _, server := range servers {
+               if server.HostName != monitorHostname {
+                       continue
+               }
+               return server.CDNName, nil
+       }
+       return "", fmt.Errorf("no monitor named %v found in Traffic Ops", 
monitorHostname)
+}

Reply via email to