Skip to content

Commit

Permalink
Merge pull request #6 from David-VTUK/throttle_github_api_requests
Browse files Browse the repository at this point in the history
Throttle GitHub api requests, semver changes, yaml manifest
  • Loading branch information
ebauman authored Aug 31, 2022
2 parents c1b6db7 + 4d659f1 commit 5cbc9b8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
29 changes: 19 additions & 10 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ func new() metrics {

func Collect(client rancher.Client) {
m := new()

// Github API request limits necessitate polling at a different interval
go func() {
ticker := time.NewTicker(1 * time.Minute)

for range ticker.C {
latestVers, err := client.GetLatestRancherVersion()

if err != nil {
log.Errorf("error retrieving latest Rancher version: %v", err)
}

m.rancherLatestMajorVersion.Set(float64(latestVers["major"]))
m.rancherLatestMinorVersion.Set(float64(latestVers["minor"]))
m.rancherLatestPatchVersion.Set(float64(latestVers["patch"]))

}
}()

ticker := time.NewTicker(3 * time.Second)

for range ticker.C {
Expand All @@ -142,12 +161,6 @@ func Collect(client rancher.Client) {
log.Errorf("error retrieving number of managed clusters: %v", err)
}

latestVers, err := client.GetLatestRancherVersion()

if err != nil {
log.Errorf("error retrieving latest Rancher version: %v", err)
}

numberOfNodes, err := client.GetNumberOfManagedNodes()

if err != nil {
Expand All @@ -158,10 +171,6 @@ func Collect(client rancher.Client) {
m.rancherMinorVersion.Set(float64(vers["minor"]))
m.rancherPatchVersion.Set(float64(vers["patch"]))

m.rancherLatestMajorVersion.Set(float64(latestVers["major"]))
m.rancherLatestMinorVersion.Set(float64(latestVers["minor"]))
m.rancherLatestPatchVersion.Set(float64(latestVers["patch"]))

m.managedClusterCount.Set(float64(numberOfClusters))
m.managedNodeCount.Set(float64(numberOfNodes))

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
log.Info("Building Rancher Client")

// Use this for in-cluster config
//config, err := rest.InClusterConfig()david
// config, err := rest.InClusterConfig()

// Use this for out of cluster config

Expand Down
43 changes: 39 additions & 4 deletions manifests/exporter.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
apiVersion: v1
kind: Namespace
metadata:
name: cattle-system-exporter
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
namespace: cattle-system-exporter
name: exporter-role
rules:
- apiGroups: ["management.cattle.io"]
resources: ["settings","clusters","nodes"]
verbs: ["get", "list"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: rancher-exporter
namespace: cattle-system-exporter
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: rancher-exporter
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: exporter-role
subjects:
- kind: ServiceAccount
name: rancher-exporter
namespace: cattle-system-exporter
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: rancher-exporter
namespace: default
namespace: cattle-system-exporter
spec:
selector:
matchLabels:
Expand All @@ -12,10 +46,11 @@ spec:
labels:
app: rancher-exporter
spec:
serviceAccountName: rancher-exporter
containers:
- imagePullPolicy: Always
name: rancher-exporter
image: virtualthoughts/prometheus-rancher-exporter:0.1
image: virtualthoughts/prometheus-rancher-exporter:0.4
ports:
- name: metrics
protocol: TCP
Expand All @@ -26,7 +61,7 @@ apiVersion: v1
kind: Service
metadata:
name: rancher-exporter
namespace: default
namespace: cattle-system-exporter
labels:
app: rancher-exporter
spec:
Expand All @@ -43,7 +78,7 @@ apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: rancher-exporter
namespace: default
namespace: cattle-system-exporter
spec:
selector:
matchLabels:
Expand Down
2 changes: 2 additions & 0 deletions query/rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func (r Client) GetK8sDistributions() (map[string]int, error) {

func (r Client) GetLatestRancherVersion() (map[string]int64, error) {
resp, err := http.Get("https://api.github.com/repos/rancher/rancher/releases/latest")

defer resp.Body.Close()

if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion semver/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
)

const semverregex = "^(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)\\.(?P<patch>0|[1-9]\\d*)(?:-(?P<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"
const semverregex = "^(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)\\.(?P<patch>0|[1-9]\\d*)"

func Parse(semver string) (map[string]int64, error) {
regex := regexp.MustCompile(semverregex)
Expand Down

0 comments on commit 5cbc9b8

Please sign in to comment.