Skip to content

Commit

Permalink
Update Traffic Monitor to use TO API v5
Browse files Browse the repository at this point in the history
Also use TO API v4 for the TO legacy API version
  • Loading branch information
zrhoffman committed Aug 30, 2023
1 parent 82901de commit 6f4c3f6
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Changed
- [#7757](https://github.com/apache/trafficcontrol/pull/7757) *Traffic Router* Changed Traffic Router to point to api version 5.0 of Traffic Ops.
- [#7732](https://github.com/apache/trafficcontrol/pull/7732) *Traffic Router* Increased negative TTL value to 900 seconds.
- [#7761](https://github.com/apache/trafficcontrol/pull/7761) *Traffic Monitor* Use API v5 for the TM's Traffic Ops client, and use TO API v4 for TM's Traffic Ops legacy client
- [#7584](https://github.com/apache/trafficcontrol/pull/7584) *Documentation* Upgrade Traffic Control Sphinx documentation Makefile OS intelligent.
- [#7521](https://github.com/apache/trafficcontrol/pull/7521) *Traffic Ops* Returns empty array instead of null when no permissions are given for roles endpoint using POST or PUT request.
- [#7369](https://github.com/apache/trafficcontrol/pull/7369) *Traffic Portal* Adds better labels to routing methods widget on the TP dashboard.
Expand Down
2 changes: 1 addition & 1 deletion traffic_monitor/tests/_integration/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ COPY lib ${GOPATH}/src/github.com/apache/trafficcontrol/lib
COPY vendor ${GOPATH}/src/github.com/apache/trafficcontrol/vendor
COPY traffic_monitor/ ${GOPATH}/src/github.com/apache/trafficcontrol/traffic_monitor/
COPY traffic_ops/toclientlib/ ${GOPATH}/src/github.com/apache/trafficcontrol/traffic_ops/toclientlib/
COPY traffic_ops/v3-client/ ${GOPATH}/src/github.com/apache/trafficcontrol/traffic_ops/v3-client/
COPY traffic_ops/v4-client/ ${GOPATH}/src/github.com/apache/trafficcontrol/traffic_ops/v4-client/
COPY traffic_ops/v5-client/ ${GOPATH}/src/github.com/apache/trafficcontrol/traffic_ops/v5-client/

WORKDIR ${GOPATH}/src/github.com/apache/trafficcontrol/traffic_monitor/tests/_integration/
RUN go mod vendor && \
Expand Down
21 changes: 7 additions & 14 deletions traffic_monitor/tmcheck/dsstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (

"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_monitor/dsdata"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"

"github.com/json-iterator/go"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

// ValidateDSStates validates that all Delivery Services in the CRConfig exist in given Traffic Monitor's DSStats.
Expand All @@ -42,17 +40,12 @@ func ValidateDSStats(tmURI string, toClient *to.Session) error {

// ValidateOfflineStatesWithCDN validates per ValidateOfflineStates, but saves an additional query if the Traffic Monitor's CDN is known.
func ValidateDSStatsWithCDN(tmURI string, tmCDN string, toClient *to.Session) error {
crConfigBytes, _, err := toClient.GetCRConfig(tmCDN)
response, _, err := toClient.GetCRConfig(tmCDN, to.RequestOptions{})
if err != nil {
return fmt.Errorf("getting CRConfig: %v", err)
}

crConfig := tc.CRConfig{}
json := jsoniter.ConfigFastest
if err := json.Unmarshal(crConfigBytes, &crConfig); err != nil {
return fmt.Errorf("unmarshalling CRConfig JSON: %v", err)
}

crConfig := response.Response
return ValidateDSStatsWithCRConfig(tmURI, &crConfig, toClient)
}

Expand Down Expand Up @@ -125,14 +118,14 @@ func ValidateAllMonitorsDSStats(toClient *to.Session, includeOffline bool) (map[

errs := map[tc.TrafficMonitorName]error{}
for _, server := range servers {
crConfig := crConfigs[tc.CDNName(server.CDNName)]
crConfig := crConfigs[tc.CDNName(*server.CDNName)]
if err := crConfig.Err; err != nil {
errs[tc.TrafficMonitorName(server.HostName)] = fmt.Errorf("getting CRConfig: %v", err)
errs[tc.TrafficMonitorName(*server.HostName)] = fmt.Errorf("getting CRConfig: %v", err)
continue
}

uri := fmt.Sprintf("http://%s.%s", server.HostName, server.DomainName)
errs[tc.TrafficMonitorName(server.HostName)] = ValidateDSStatsWithCRConfig(uri, crConfig.CRConfig, toClient)
uri := fmt.Sprintf("http://%s.%s", *server.HostName, *server.DomainName)
errs[tc.TrafficMonitorName(*server.HostName)] = ValidateDSStatsWithCRConfig(uri, crConfig.CRConfig, toClient)
}
return errs, nil
}
21 changes: 7 additions & 14 deletions traffic_monitor/tmcheck/offlinestates.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
"time"

"github.com/apache/trafficcontrol/lib/go-tc"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"

"github.com/json-iterator/go"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

// ValidateOfflineStates validates that no OFFLINE or ADMIN_DOWN caches in the given Traffic Ops' CRConfig are marked Available in the given Traffic Monitor's CRStates.
Expand All @@ -40,17 +38,12 @@ func ValidateOfflineStates(tmURI string, toClient *to.Session) error {

// ValidateOfflineStatesWithCDN validates per ValidateOfflineStates, but saves an additional query if the Traffic Monitor's CDN is known.
func ValidateOfflineStatesWithCDN(tmURI string, tmCDN string, toClient *to.Session) error {
crConfigBytes, _, err := toClient.GetCRConfig(tmCDN)
response, _, err := toClient.GetCRConfig(tmCDN, to.RequestOptions{})
if err != nil {
return fmt.Errorf("getting CRConfig: %v", err)
}

crConfig := tc.CRConfig{}
json := jsoniter.ConfigFastest
if err := json.Unmarshal(crConfigBytes, &crConfig); err != nil {
return fmt.Errorf("unmarshalling CRConfig JSON: %v", err)
}

crConfig := response.Response
return ValidateOfflineStatesWithCRConfig(tmURI, &crConfig, toClient)
}

Expand Down Expand Up @@ -122,14 +115,14 @@ func ValidateAllMonitorsOfflineStates(toClient *to.Session, includeOffline bool)

errs := map[tc.TrafficMonitorName]error{}
for _, server := range servers {
crConfig := crConfigs[tc.CDNName(server.CDNName)]
crConfig := crConfigs[tc.CDNName(*server.CDNName)]
if err := crConfig.Err; err != nil {
errs[tc.TrafficMonitorName(server.HostName)] = fmt.Errorf("getting CRConfig: %v", err)
errs[tc.TrafficMonitorName(*server.HostName)] = fmt.Errorf("getting CRConfig: %v", err)
continue
}

uri := fmt.Sprintf("http://%s.%s", server.HostName, server.DomainName)
errs[tc.TrafficMonitorName(server.HostName)] = ValidateOfflineStatesWithCRConfig(uri, crConfig.CRConfig, toClient)
uri := fmt.Sprintf("http://%s.%s", *server.HostName, *server.DomainName)
errs[tc.TrafficMonitorName(*server.HostName)] = ValidateOfflineStatesWithCRConfig(uri, crConfig.CRConfig, toClient)
}
return errs, nil
}
6 changes: 3 additions & 3 deletions traffic_monitor/tmcheck/peerpoller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package tmcheck
import (
"fmt"
"github.com/apache/trafficcontrol/lib/go-tc"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
"time"
)

Expand Down Expand Up @@ -57,8 +57,8 @@ func ValidateAllPeerPollers(toClient *to.Session, includeOffline bool) (map[tc.T
}
errs := map[tc.TrafficMonitorName]error{}
for _, server := range servers {
uri := fmt.Sprintf("http://%s.%s", server.HostName, server.DomainName)
errs[tc.TrafficMonitorName(server.HostName)] = ValidatePeerPoller(uri)
uri := fmt.Sprintf("http://%s.%s", *server.HostName, *server.DomainName)
errs[tc.TrafficMonitorName(*server.HostName)] = ValidatePeerPoller(uri)
}
return errs, nil
}
Expand Down
6 changes: 3 additions & 3 deletions traffic_monitor/tmcheck/queryinterval.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"time"

"github.com/apache/trafficcontrol/lib/go-tc"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

const QueryIntervalMax = time.Duration(10) * time.Second
Expand Down Expand Up @@ -78,8 +78,8 @@ func ValidateAllMonitorsQueryInterval(toClient *to.Session, includeOffline bool)

errs := map[tc.TrafficMonitorName]error{}
for _, server := range servers {
uri := fmt.Sprintf("http://%s.%s", server.HostName, server.DomainName)
errs[tc.TrafficMonitorName(server.HostName)] = ValidateQueryInterval(uri, toClient)
uri := fmt.Sprintf("http://%s.%s", *server.HostName, *server.DomainName)
errs[tc.TrafficMonitorName(*server.HostName)] = ValidateQueryInterval(uri, toClient)
}
return errs, nil
}
26 changes: 11 additions & 15 deletions traffic_monitor/tmcheck/tmcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_monitor/datareq"
"github.com/apache/trafficcontrol/traffic_monitor/dsdata"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"

jsoniter "github.com/json-iterator/go"
)
Expand Down Expand Up @@ -198,11 +198,12 @@ type CRConfigOrError struct {
Err error
}

func GetMonitors(toClient *to.Session, includeOffline bool) ([]tc.Server, error) {
func GetMonitors(toClient *to.Session, includeOffline bool) ([]tc.ServerV4, error) {
trafficMonitorType := "RASCAL"
query := url.Values{}
query.Set("type", trafficMonitorType)
servers, _, err := toClient.GetServers(&query)
response, _, err := toClient.GetServers(to.RequestOptions{QueryParameters: query})
servers := response.Response
if err != nil {
return nil, fmt.Errorf("getting monitors from Traffic Ops: %v", err)
}
Expand Down Expand Up @@ -265,10 +266,10 @@ func AllValidator(
}

// FilterOfflines returns only servers which are REPORTED or ONLINE
func FilterOfflines(servers []tc.Server) []tc.Server {
onlineServers := []tc.Server{}
func FilterOfflines(servers []tc.ServerV4) []tc.ServerV4 {
onlineServers := []tc.ServerV4{}
for _, server := range servers {
status := tc.CacheStatusFromString(server.Status)
status := tc.CacheStatusFromString(*server.Status)
if status != tc.CacheStatusOnline && status != tc.CacheStatusReported {
continue
}
Expand All @@ -277,29 +278,24 @@ func FilterOfflines(servers []tc.Server) []tc.Server {
return onlineServers
}

func GetCDNs(servers []tc.Server) map[tc.CDNName]struct{} {
func GetCDNs(servers []tc.ServerV4) map[tc.CDNName]struct{} {
cdns := map[tc.CDNName]struct{}{}
for _, server := range servers {
cdns[tc.CDNName(server.CDNName)] = struct{}{}
cdns[tc.CDNName(*server.CDNName)] = struct{}{}
}
return cdns
}

func GetCRConfigs(cdns map[tc.CDNName]struct{}, toClient *to.Session) map[tc.CDNName]CRConfigOrError {
crConfigs := map[tc.CDNName]CRConfigOrError{}
for cdn, _ := range cdns {
crConfigBytes, _, err := toClient.GetCRConfig(string(cdn))
response, _, err := toClient.GetCRConfig(string(cdn), to.RequestOptions{})
if err != nil {
crConfigs[cdn] = CRConfigOrError{Err: fmt.Errorf("getting CRConfig: %v", err)}
continue
}

crConfig := tc.CRConfig{}
json := jsoniter.ConfigFastest
if err := json.Unmarshal(crConfigBytes, &crConfig); err != nil {
crConfigs[cdn] = CRConfigOrError{Err: fmt.Errorf("unmarshalling CRConfig JSON: %v", err)}
}

crConfig := response.Response
crConfigs[cdn] = CRConfigOrError{CRConfig: &crConfig}
}
return crConfigs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/apache/trafficcontrol/lib/go-nagios"
"github.com/apache/trafficcontrol/traffic_monitor/tmcheck"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

const UserAgent = "tm-offline-validator/0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/apache/trafficcontrol/lib/go-nagios"
"github.com/apache/trafficcontrol/traffic_monitor/tmcheck"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

const UserAgent = "tm-offline-validator/0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"github.com/apache/trafficcontrol/lib/go-nagios"
"github.com/apache/trafficcontrol/traffic_monitor/tmcheck"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

const UserAgent = "tm-peerpoller-validator/0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"github.com/apache/trafficcontrol/lib/go-nagios"
"github.com/apache/trafficcontrol/traffic_monitor/tmcheck"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
)

const UserAgent = "tm-queryinterval-validator/0.1"
Expand Down
4 changes: 2 additions & 2 deletions traffic_monitor/tools/testto/testto.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
}

toDataThs := NewThs()
toDataThs.Set(&FakeTOData{Servers: []tc.ServerV40{}})
toDataThs.Set(&FakeTOData{Servers: []tc.ServerV50{}})

Serve(*port, toDataThs)
fmt.Printf("Serving on %v\n", *port)
Expand All @@ -57,7 +57,7 @@ func main() {
type FakeTOData struct {
Monitoring tc.TrafficMonitorConfig
CRConfig tc.CRConfig
Servers []tc.ServerV40
Servers []tc.ServerV50
}

// TODO make timeouts configurable?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"fmt"
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_monitor/tmcheck"
to "github.com/apache/trafficcontrol/traffic_ops/v3-client"
to "github.com/apache/trafficcontrol/traffic_ops/v4-client"
"io"
"net/http"
"sort"
Expand Down
Loading

0 comments on commit 6f4c3f6

Please sign in to comment.