/
app_operator.go
173 lines (141 loc) · 4.29 KB
/
app_operator.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package collector
import (
"context"
"fmt"
"github.com/Masterminds/semver/v3"
"github.com/giantswarm/k8sclient/v3/pkg/k8sclient"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/prometheus/client_golang/prometheus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/app-operator/pkg/label"
"github.com/giantswarm/app-operator/pkg/project"
)
var (
appOperatorDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "ready", "total"),
"Gauge with ready app-operator instances per app CR version.",
[]string{
labelVersion,
},
nil,
)
)
// AppOperatorConfig is this collector's configuration struct.
type AppOperatorConfig struct {
K8sClient k8sclient.Interface
Logger micrologger.Logger
}
// AppOperator is the main struct for this collector.
type AppOperator struct {
k8sClient k8sclient.Interface
logger micrologger.Logger
}
// NewAppOperator creates a new AppOperator metrics collector
func NewAppOperator(config AppOperatorConfig) (*AppOperator, error) {
if config.K8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.K8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
a := &AppOperator{
k8sClient: config.K8sClient,
logger: config.Logger,
}
return a, nil
}
// Collect is the main metrics collection function.
func (a *AppOperator) Collect(ch chan<- prometheus.Metric) error {
ctx := context.Background()
err := a.collectAppOperatorStatus(ctx, ch)
if err != nil {
return microerror.Mask(err)
}
return nil
}
// Describe emits the description for the metrics collected here.
func (a *AppOperator) Describe(ch chan<- *prometheus.Desc) error {
ch <- appOperatorDesc
return nil
}
func (a *AppOperator) collectAppOperatorStatus(ctx context.Context, ch chan<- prometheus.Metric) error {
var err error
appVersions, err := a.collectAppVersions(ctx)
if err != nil {
return microerror.Mask(err)
}
operatorVersions, err := a.collectOperatorVersions(ctx)
if err != nil {
return microerror.Mask(err)
}
for version := range appVersions {
var ready int32
if version == project.AppTenantVersion() {
// There should be a single app-operator instance with major version
// 1 for Helm 2 tenant clusters.
ready, err = helm2AppOperatorReady(operatorVersions)
if err != nil {
a.logger.LogCtx(ctx, "level", "error", "message", "failed to check helm 2 app-operator ready", "stack", fmt.Sprintf("%#v", err))
ready = 0
}
} else {
// For all other versions there should be a 1:1 mapping from app CR
// version to app-operator version.
result, ok := operatorVersions[version]
if ok {
ready = result
} else {
a.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("no app-operator found for version %#q", version))
ready = 0
}
}
ch <- prometheus.MustNewConstMetric(
appOperatorDesc,
prometheus.GaugeValue,
float64(ready),
version,
)
}
return nil
}
func (a *AppOperator) collectAppVersions(ctx context.Context) (map[string]bool, error) {
appVersions := map[string]bool{}
l, err := a.k8sClient.G8sClient().ApplicationV1alpha1().Apps("").List(metav1.ListOptions{})
if err != nil {
return nil, microerror.Mask(err)
}
for _, app := range l.Items {
version := app.Labels[label.AppOperatorVersion]
appVersions[version] = true
}
return appVersions, nil
}
func (a *AppOperator) collectOperatorVersions(ctx context.Context) (map[string]int32, error) {
operatorVersions := map[string]int32{}
lo := metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", label.App, project.Name()),
}
d, err := a.k8sClient.K8sClient().AppsV1().Deployments("giantswarm").List(lo)
if err != nil {
return nil, microerror.Mask(err)
}
for _, deploy := range d.Items {
version := deploy.Labels[label.AppKubernetesVersion]
operatorVersions[version] = deploy.Status.ReadyReplicas
}
return operatorVersions, nil
}
func helm2AppOperatorReady(operatorVersions map[string]int32) (int32, error) {
var helm2AppOperators int32
for version, ready := range operatorVersions {
v, err := semver.NewVersion(version)
if err != nil {
return 0, microerror.Mask(err)
}
if v.Major() == 1 {
helm2AppOperators += ready
}
}
return helm2AppOperators, nil
}