Skip to content

Commit

Permalink
Merge pull request #8 from utilitywarehouse/as-upgrade
Browse files Browse the repository at this point in the history
update go and added test
  • Loading branch information
asiyani committed Dec 2, 2022
2 parents 57a0806 + 6ff3d9e commit b7a0c81
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 60 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@v2
- name: Login to Quay.io Container Registry
uses: docker/login-action@v1
with:
Expand All @@ -32,11 +34,11 @@ jobs:
password: ${{ secrets.SYSTEM_QUAY_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
uses: docker/build-push-action@v3
with:
context: .
push: true
Expand Down
58 changes: 29 additions & 29 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ name: "CodeQL"

on:
push:
branches: [ master ]
branches: [master]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
branches: [master]
schedule:
- cron: '30 22 * * 0'
- cron: "30 22 * * 0"

jobs:
analyze:
Expand All @@ -32,40 +32,40 @@ jobs:
strategy:
fail-fast: false
matrix:
language: [ 'go' ]
language: ["go"]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release
#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.17-alpine AS build
FROM golang:1.19-alpine AS build
WORKDIR /src
COPY go.* ./
RUN go mod download
Expand All @@ -8,7 +8,7 @@ RUN apk --no-cache add git &&\
go test ./... &&\
go build -o /kube-summary-exporter .

FROM alpine:3.13
FROM alpine:latest
COPY --from=build /kube-summary-exporter /kube-summary-exporter

ENTRYPOINT [ "/kube-summary-exporter"]
27 changes: 6 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
# kube-summary-exporter

[![Build Status](https://drone.prod.merit.uw.systems/api/badges/utilitywarehouse/kube-summary-exporter/status.svg)](https://drone.prod.merit.uw.systems/utilitywarehouse/kube-summary-exporter)

Exports prometheus metrics for the Kubernetes Summary API.

## Usage
## run locally

Visiting http://localhost:9779/node/example-node will return metrics for the
node 'example-node'.
To run exporter locally run `go run ./...`

Here's an example scrape config. This assumes that the exporter is available at `kube-summary-exporter:9779`.
This will run server on default port `9779`

```
- job_name: "kubernetes-summary"
kubernetes_sd_configs:
- role: node
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /node/${1}
- target_label: __address__
replacement: kube-summary-exporter:9779
```
Visiting http://localhost:9779/node/example-node will return metrics for the
node 'example-node'. App will look for `example-node` in the `current-context` cluster set in kube config.

[Here's an example scrape config.](manifests/scrap-config.yaml)
## Metrics

| Metric | Description | Labels |
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/utilitywarehouse/kube-summary-exporter

go 1.17
go 1.19

require (
github.com/google/go-cmp v0.5.5
github.com/gorilla/mux v1.8.0
github.com/prometheus/client_golang v1.11.0
k8s.io/client-go v0.22.1
Expand All @@ -24,7 +25,6 @@ require (
github.com/go-logr/logr v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.5 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down
104 changes: 104 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"encoding/json"
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
_ "k8s.io/client-go/plugin/pkg/client/auth"
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
)

func Test_collectSummaryMetrics(t *testing.T) {
expectedOut := `# HELP kube_summary_container_logs_available_bytes Number of bytes that aren't consumed by the container logs
# TYPE kube_summary_container_logs_available_bytes gauge
kube_summary_container_logs_available_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 9.0016837632e+10
# HELP kube_summary_container_logs_capacity_bytes Number of bytes that can be consumed by the container logs
# TYPE kube_summary_container_logs_capacity_bytes gauge
kube_summary_container_logs_capacity_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 1.01535985664e+11
# HELP kube_summary_container_logs_inodes Number of Inodes for logs
# TYPE kube_summary_container_logs_inodes gauge
kube_summary_container_logs_inodes{name="dev-server",namespace="mon",pod="dev-server-0"} 2.5474432e+07
# HELP kube_summary_container_logs_inodes_free Number of available Inodes for logs
# TYPE kube_summary_container_logs_inodes_free gauge
kube_summary_container_logs_inodes_free{name="dev-server",namespace="mon",pod="dev-server-0"} 2.5355212e+07
# HELP kube_summary_container_logs_inodes_used Number of used Inodes for logs
# TYPE kube_summary_container_logs_inodes_used gauge
kube_summary_container_logs_inodes_used{name="dev-server",namespace="mon",pod="dev-server-0"} 1
# HELP kube_summary_container_logs_used_bytes Number of bytes that are consumed by the container logs
# TYPE kube_summary_container_logs_used_bytes gauge
kube_summary_container_logs_used_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 8192
# HELP kube_summary_container_rootfs_available_bytes Number of bytes that aren't consumed by the container
# TYPE kube_summary_container_rootfs_available_bytes gauge
kube_summary_container_rootfs_available_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 9.0016837632e+10
# HELP kube_summary_container_rootfs_capacity_bytes Number of bytes that can be consumed by the container
# TYPE kube_summary_container_rootfs_capacity_bytes gauge
kube_summary_container_rootfs_capacity_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 1.01535985664e+11
# HELP kube_summary_container_rootfs_inodes Number of Inodes
# TYPE kube_summary_container_rootfs_inodes gauge
kube_summary_container_rootfs_inodes{name="dev-server",namespace="mon",pod="dev-server-0"} 2.5474432e+07
# HELP kube_summary_container_rootfs_inodes_free Number of available Inodes
# TYPE kube_summary_container_rootfs_inodes_free gauge
kube_summary_container_rootfs_inodes_free{name="dev-server",namespace="mon",pod="dev-server-0"} 2.5355212e+07
# HELP kube_summary_container_rootfs_inodes_used Number of used Inodes
# TYPE kube_summary_container_rootfs_inodes_used gauge
kube_summary_container_rootfs_inodes_used{name="dev-server",namespace="mon",pod="dev-server-0"} 14
# HELP kube_summary_container_rootfs_used_bytes Number of bytes that are consumed by the container
# TYPE kube_summary_container_rootfs_used_bytes gauge
kube_summary_container_rootfs_used_bytes{name="dev-server",namespace="mon",pod="dev-server-0"} 114688
# HELP kube_summary_pod_ephemeral_storage_available_bytes Number of bytes of Ephemeral storage that aren't consumed by the pod
# TYPE kube_summary_pod_ephemeral_storage_available_bytes gauge
kube_summary_pod_ephemeral_storage_available_bytes{namespace="mon",pod="dev-server-0"} 9.0016837632e+10
# HELP kube_summary_pod_ephemeral_storage_capacity_bytes Number of bytes of Ephemeral storage that can be consumed by the pod
# TYPE kube_summary_pod_ephemeral_storage_capacity_bytes gauge
kube_summary_pod_ephemeral_storage_capacity_bytes{namespace="mon",pod="dev-server-0"} 1.01535985664e+11
# HELP kube_summary_pod_ephemeral_storage_inodes Number of Inodes for pod Ephemeral storage
# TYPE kube_summary_pod_ephemeral_storage_inodes gauge
kube_summary_pod_ephemeral_storage_inodes{namespace="mon",pod="dev-server-0"} 2.5474432e+07
# HELP kube_summary_pod_ephemeral_storage_inodes_free Number of available Inodes for pod Ephemeral storage
# TYPE kube_summary_pod_ephemeral_storage_inodes_free gauge
kube_summary_pod_ephemeral_storage_inodes_free{namespace="mon",pod="dev-server-0"} 2.5355212e+07
# HELP kube_summary_pod_ephemeral_storage_inodes_used Number of used Inodes for pod Ephemeral storage
# TYPE kube_summary_pod_ephemeral_storage_inodes_used gauge
kube_summary_pod_ephemeral_storage_inodes_used{namespace="mon",pod="dev-server-0"} 63
# HELP kube_summary_pod_ephemeral_storage_used_bytes Number of bytes of Ephemeral storage that are consumed by the pod
# TYPE kube_summary_pod_ephemeral_storage_used_bytes gauge
kube_summary_pod_ephemeral_storage_used_bytes{namespace="mon",pod="dev-server-0"} 1.33947392e+08
`

d, err := os.ReadFile("test-summary.json")
if err != nil {
t.Fatal(err)
}

var summary stats.Summary
registry := prometheus.NewRegistry()

err = json.Unmarshal(d, &summary)
if err != nil {
t.Fatal(err)
}

collectSummaryMetrics(&summary, registry)

tmpfile, err := os.CreateTemp("", "test-summary.prom")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

if err := prometheus.WriteToTextfile(tmpfile.Name(), registry); err != nil {
t.Fatal(err)
}

fileBytes, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(string(fileBytes), expectedOut); diff != "" {
t.Errorf("collectSummaryMetrics() metrics mismatch (-want +got):\n%s", diff)
}
}
42 changes: 42 additions & 0 deletions manifests/base/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: kube-summary-exporter
---
apiVersion: v1
kind: Service
metadata:
labels:
name: kube-summary-exporter
name: kube-summary-exporter
spec:
ports:
- name: kube-summary-exporter
protocol: TCP
port: 9779
targetPort: 9779
selector:
app: kube-summary-exporter
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kube-summary-exporter
spec:
replicas: 1
selector:
matchLabels:
app: kube-summary-exporter
template:
metadata:
name: kube-summary-exporter
labels:
app: kube-summary-exporter
spec:
serviceAccountName: kube-summary-exporter
containers:
- name: kube-summary-exporter
image: quay.io/utilitywarehouse/kube-summary-exporter:latest
ports:
- name: tcp
containerPort: 9779
4 changes: 4 additions & 0 deletions manifests/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
21 changes: 21 additions & 0 deletions manifests/cluster/clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kube-summary-exporter
rules:
- apiGroups: [""]
resources: ["nodes/proxy"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kube-summary-exporter
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kube-summary-exporter
subjects:
- kind: ServiceAccount
name: kube-summary-exporter
namespace: sys-prom
4 changes: 4 additions & 0 deletions manifests/cluster/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- clusterrole.yaml
14 changes: 14 additions & 0 deletions manifests/scrap-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Scrape config for the Summary API exporter.
# This assumes that the exporter is available at kube-summary-exporter:9779
- job_name: "kubernetes-summary"
kubernetes_sd_configs:
- role: node
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /node/${1}
- target_label: __address__
replacement: kube-summary-exporter:9779
Loading

0 comments on commit b7a0c81

Please sign in to comment.