Skip to content

Commit

Permalink
Add option to set the value to helm release's timestamp (#36)
Browse files Browse the repository at this point in the history
add option to set the value to helm release's timestamp
  • Loading branch information
knatsakis authored Jul 28, 2020
1 parent 40ce6a2 commit f63863e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions helm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The following table lists the configurable parameters of the helm-exporter chart
| image.pullPolicy | string | `"Always"` | Image pull policy for the webhook integration jobs |
| image.repository | string | `"sstarcher/helm-exporter"` | Repository to use for the webhook integration jobs |
| imagePullSecrets | list | `[]` | Reference to one or more secrets to be used when pulling images |
| infoMetric | bool | `true` | Specifies whether to generate the info metric. |
| ingress.annotations | object | `{}` | Annotations for the helm-exporter |
| ingress.enabled | bool | `false` | If true, helm-exporter Ingress will be created |
| ingress.hosts[0].host | string | `"chart-example.local"` | Ingress hostname |
Expand All @@ -72,6 +73,7 @@ The following table lists the configurable parameters of the helm-exporter chart
| serviceMonitor.namespace | string | `nil` | The namespace where the Prometheus Operator is deployed |
| serviceMonitor.additionalLabels |object | `{}` | Additional labels to add to the ServiceMonitor |
| serviceMonitor.scrapeTimeout | string | `nil` | Scrape Timeout when the metrics endpoint is scraped |
| timestampMetric | bool | `true` | Specifies whether to generate the timestamps metric. |
| tolerations | list | `[]` | Tolerations for use with node taints [https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/](https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/)|


Expand Down
14 changes: 11 additions & 3 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ spec:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if .Values.namespaces }}
args: ["-namespaces", {{ .Values.namespaces | quote }}]
{{- end }}
args:
{{- if .Values.namespaces }}
- "-namespaces"
- {{ .Values.namespaces | quote }}
{{- end }}
{{- if not .Values.infoMetric }}
- "-info-metric=false"
{{- end }}
{{- if not .Values.timestampMetric }}
- "-timestamp-metric=false"
{{- end }}
ports:
- name: http
containerPort: 9571
Expand Down
2 changes: 2 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Specifies which namespaces to query for helm 3 metrics. Defaults to all
namespaces: ""
infoMetric: true
timestampMetric: true

serviceMonitor:
# Specifies whether a ServiceMonitor should be created
Expand Down
35 changes: 31 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
settings = cli.New()
clients = cmap.New()

stats = promauto.NewGaugeVec(prometheus.GaugeOpts{
statsInfo = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "helm_chart_info",
Help: "Information on helm releases",
}, []string{
Expand All @@ -48,9 +48,25 @@ var (
"latestVersion",
})

statsTimestamp = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "helm_chart_timestamp",
Help: "Timestamps of helm releases",
}, []string{
"chart",
"release",
"version",
"appVersion",
"updated",
"namespace",
"latestVersion",
})

namespaces = flag.String("namespaces", "", "namespaces to monitor. Defaults to all")
configFile = flag.String("config", "", "Configfile to load for helm overwrite registries. Default is empty")

infoMetric = flag.Bool("info-metric", true, "Generate info metric. Defaults to true")
timestampMetric = flag.Bool("timestamp-metric", true, "Generate timestamps metric. Defaults to true")

statusCodeMap = map[string]float64{
"unknown": 0,
"deployed": 1,
Expand All @@ -73,8 +89,13 @@ func initFlags() config.AppConfig {
}

func runStats(config config.Config) {
if *infoMetric == true {
statsInfo.Reset()
}
if *timestampMetric == true {
statsTimestamp.Reset()
}

stats.Reset()
for _, client := range clients.Items() {
list := action.NewList(client.(*action.Configuration))
items, err := list.Run()
Expand All @@ -88,12 +109,18 @@ func runStats(config config.Config) {
releaseName := item.Name
version := item.Chart.Metadata.Version
appVersion := item.Chart.AppVersion()
updated := strconv.FormatInt((item.Info.LastDeployed.Unix() * 1000), 10)
updated := item.Info.LastDeployed.Unix() * 1000
namespace := item.Namespace
status := statusCodeMap[item.Info.Status.String()]
latestVersion := getLatestChartVersionFromHelm(item.Chart.Name(), config.HelmRegistries)
//latestVersion := "3.1.8"
stats.WithLabelValues(chart, releaseName, version, appVersion, updated, namespace, latestVersion).Set(status)

if *infoMetric == true {
statsInfo.WithLabelValues(chart, releaseName, version, appVersion, strconv.FormatInt(updated, 10), namespace, latestVersion).Set(status)
}
if *timestampMetric == true {
statsTimestamp.WithLabelValues(chart, releaseName, version, appVersion, strconv.FormatInt(updated, 10), namespace, latestVersion).Set(float64(updated))
}
}
}
}
Expand Down

0 comments on commit f63863e

Please sign in to comment.