-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.go
114 lines (93 loc) · 2.45 KB
/
release.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"io"
"log"
"net/http"
"sort"
yaml "gopkg.in/yaml.v3"
semver "github.com/Masterminds/semver/v3"
"github.com/prometheus/client_golang/prometheus"
)
type Entry struct {
Name string `yaml:"name"`
Annotations map[string]string `yaml:"annotations"`
Version string `yaml:"version"`
}
type Entries map[string][]Entry
type Catalog struct {
APIVersion string `yaml:"apiVersion"`
Entries Entries `yaml:"entries"`
}
type ComponentRelease struct {
Name string
Release string
Team string
State float64
}
type releaseCollector struct {
releaseDesc *prometheus.Desc
componentReleases []ComponentRelease
}
func NewReleaseCollector() (*releaseCollector, error) {
var rc releaseCollector
rc.releaseDesc = prometheus.NewDesc(
prometheus.BuildFQName("operations", "release", "latest_info"),
"App release information from Giant Swarm Catalog index file.",
[]string{"app", "team", "version"},
nil,
)
rc.componentReleases = []ComponentRelease{}
return &rc, nil
}
func (rc *releaseCollector) UpdateFromCatalog() {
resp, err := http.Get("https://raw.githubusercontent.com/giantswarm/giantswarm-catalog/master/index.yaml")
if err != nil {
log.Fatalf("error: %v", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("error: %v", err)
}
catalog := &Catalog{}
err = yaml.Unmarshal(data, &catalog)
if err != nil {
log.Fatalf("error: %v", err)
}
var crs []ComponentRelease
for _, entries := range catalog.Entries {
sort.Slice(entries, func(i, j int) bool {
versionI, _ := semver.NewVersion(entries[i].Version)
versionJ, _ := semver.NewVersion(entries[j].Version)
return versionI.GreaterThan(versionJ)
})
// Print the entry with the latest version
if len(entries) > 0 {
latest := entries[0]
crs = append(crs, ComponentRelease{
Name: latest.Name,
Release: latest.Version,
Team: latest.Annotations["application.giantswarm.io/team"],
State: 1,
})
fmt.Printf("Latest release for %s: %s\n", latest.Name, latest.Version)
}
}
rc.componentReleases = crs
}
func (rc *releaseCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- rc.releaseDesc
}
func (rc *releaseCollector) Collect(ch chan<- prometheus.Metric) {
for _, cr := range rc.componentReleases {
ch <- prometheus.MustNewConstMetric(
rc.releaseDesc,
prometheus.GaugeValue,
cr.State,
cr.Name,
cr.Team,
cr.Release,
)
}
}