Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Update dockerfile
Browse files Browse the repository at this point in the history
Added new metrics
  • Loading branch information
David-VTUK committed Sep 7, 2022
1 parent 6c0e332 commit 234395b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM ubuntu:20.10
FROM ubuntu:22.10
RUN apt-get update && apt-get install -y ca-certificates
RUN mkdir /app
COPY ./bin/prometheus-rancher-exporter /app/
WORKDIR /app
Expand Down
38 changes: 38 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type metrics struct {
// Cluster level metrics
clusterConditionConnected prometheus.GaugeVec
clusterConditionNotConnected prometheus.GaugeVec

// Downstream cluster metrics

downstreamClusterK8sMajorVersion prometheus.GaugeVec
downstreamClusterK8sMinorVersion prometheus.GaugeVec
downstreamClusterK8sPatchVersion prometheus.GaugeVec
}

func new() metrics {
Expand Down Expand Up @@ -96,6 +102,21 @@ func new() metrics {
Help: "identify if a downstream cluster is not connected to Rancher",
}, []string{"Name"},
),
downstreamClusterK8sMajorVersion: *prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "downstream_cluster_major_version",
Help: "major version for k8s version, where version is semantic and formatted major.minor.patch",
}, []string{"Name"},
),
downstreamClusterK8sMinorVersion: *prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "downstream_cluster_minor_version",
Help: "minor version for k8s version, where version is semantic and formatted major.minor.patch",
}, []string{"Name"},
),
downstreamClusterK8sPatchVersion: *prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "downstream_cluster_patch_version",
Help: "patch version for k8s version, where version is semantic and formatted major.minor.patch",
}, []string{"Name"},
),
}

prometheus.MustRegister(m.rancherMajorVersion)
Expand All @@ -114,6 +135,9 @@ func new() metrics {
prometheus.MustRegister(m.managedNodeCount)
prometheus.MustRegister(m.clusterConditionConnected)
prometheus.MustRegister(m.clusterConditionNotConnected)
prometheus.MustRegister(m.downstreamClusterK8sMajorVersion)
prometheus.MustRegister(m.downstreamClusterK8sMinorVersion)
prometheus.MustRegister(m.downstreamClusterK8sPatchVersion)

m.rancherMajorVersion.Set(0)
m.rancherMinorVersion.Set(0)
Expand Down Expand Up @@ -188,6 +212,18 @@ func Collect(client rancher.Client) {
log.Errorf("error retrieving number of managed nodes: %v", err)
}

downstreamClusterVersions, err := client.GetDownstreamClusterVersions()

if err != nil {
log.Errorf("error retrieving downstream k8s cluster versions: %v", err)
}

for _, value := range downstreamClusterVersions {
m.downstreamClusterK8sMajorVersion.WithLabelValues(value.Name).Set(value.Major)
m.downstreamClusterK8sMinorVersion.WithLabelValues(value.Name).Set(value.Minor)
m.downstreamClusterK8sPatchVersion.WithLabelValues(value.Name).Set(value.Patch)
}

m.rancherMajorVersion.Set(float64(vers["major"]))
m.rancherMinorVersion.Set(float64(vers["minor"]))
m.rancherPatchVersion.Set(float64(vers["patch"]))
Expand All @@ -205,8 +241,10 @@ func Collect(client rancher.Client) {
for key, value := range state {
if value == true {
m.clusterConditionConnected.WithLabelValues(key).Set(1)
m.clusterConditionNotConnected.WithLabelValues(key).Set(0)
} else {
m.clusterConditionNotConnected.WithLabelValues(key).Set(1)
m.clusterConditionConnected.WithLabelValues(key).Set(0)
}
}
}
Expand Down
23 changes: 11 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package main

import (
"flag"
"fmt"
"github.com/david-vtuk/prometheus-rancher-exporter/collector"
"github.com/david-vtuk/prometheus-rancher-exporter/query/rancher"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/rest"
"net/http"
"os/user"
)

func main() {
Expand All @@ -19,19 +16,21 @@ func main() {
log.Info("Building Rancher Client")

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

// Use this for out of cluster config
/*
currentUser, err := user.Current()
if err != nil {
log.Fatal(err.Error())
}
currentUser, err := user.Current()
if err != nil {
log.Fatal(err.Error())
}
kubeconfig := flag.String("kubeconfig", fmt.Sprintf("/home/%s/.kube/config", currentUser.Username), "absolute path to the kubeconfig file")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
kubeconfig := flag.String("kubeconfig", fmt.Sprintf("/home/%s/.kube/config", currentUser.Username), "absolute path to the kubeconfig file")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
*/
if err != nil {
log.Fatal("Unable to construct Rancher client Config")
}
Expand Down
2 changes: 1 addition & 1 deletion manifests/exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
containers:
- imagePullPolicy: Always
name: rancher-exporter
image: virtualthoughts/prometheus-rancher-exporter:0.72
image: virtualthoughts/prometheus-rancher-exporter:0.75
ports:
- name: metrics
protocol: TCP
Expand Down
73 changes: 51 additions & 22 deletions query/rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rancher

import (
"context"
"fmt"
"github.com/david-vtuk/prometheus-rancher-exporter/semver"
"github.com/tidwall/gjson"
"io"
Expand All @@ -26,6 +25,13 @@ type Client struct {
Config *rest.Config
}

type clusterVersion struct {
Name string
Major float64
Minor float64
Patch float64
}

func (r Client) GetRancherVersion() (map[string]int64, error) {

res, err := r.Client.Resource(settingGVR).Get(context.Background(), "server-version", v1.GetOptions{})
Expand Down Expand Up @@ -75,8 +81,6 @@ 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")

fmt.Println(resp.Body)

defer resp.Body.Close()

if err != nil {
Expand Down Expand Up @@ -170,32 +174,57 @@ func (r Client) GetClusterConnectedState() (map[string]bool, error) {
return clusterStatus, nil
}

// Version returned from CRD is in the format of "vN.N.N", trim the leading "v"
func TrimVersionChar(version string) string {
for i := range version {
if i > 0 {
return version[i:]
}
}
return version[:0]
}

/*
func (r Client) GetK8sDistributions() (map[string]int, error) {
func (r Client) GetDownstreamClusterVersions() ([]clusterVersion, error) {

distributions := make(map[string]int)
var clusters []clusterVersion

res, err := r.Client.Resource(clustersGVR).List(context.Background(), v1.ListOptions{})
if err != nil {
return nil, err
}

for _, v := range res.Items {
labels := v.GetLabels()
distribution := labels["provider.cattle.io"]
distributions[distribution] += 1
// Iterate through each cluster management object
for _, cluster := range res.Items {

// Grab Cluster name
clusterName, _, err := unstructured.NestedString(cluster.Object, "spec", "displayName")

if err != nil {
return nil, err
}

clusterK8sVersion, _, err := unstructured.NestedString(cluster.Object, "status", "version", "gitVersion")

if err != nil {
return nil, err
}

result, err := semver.Parse(TrimVersionChar(clusterK8sVersion))

if err != nil {
return nil, err
}

clusterInfo := clusterVersion{
Name: clusterName,
Major: float64(result["major"]),
Minor: float64(result["minor"]),
Patch: float64(result["patch"]),
}

clusters = append(clusters, clusterInfo)
}

return distributions, nil
return clusters, nil

}

// TrimVersionChar Version returned from CRD is in the format of "vN.N.N", trim the leading "v"
func TrimVersionChar(version string) string {
for i := range version {
if i > 0 {
return version[i:]
}
}
return version[:0]
}
*/

0 comments on commit 234395b

Please sign in to comment.