This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_group.go
144 lines (118 loc) · 3.3 KB
/
resource_group.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
package collector
import (
"context"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
"github.com/Azure/go-autorest/autorest/to"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/kubernetes"
"github.com/giantswarm/azure-collector/v2/service/credential"
)
const (
labelID = "id"
labelName = "name"
labelState = "state"
labelLocation = "location"
labelManagedBy = "managed_by"
)
var (
resourceGroupDesc = prometheus.NewDesc(
prometheus.BuildFQName(MetricsNamespace, "resource_group", "info"),
"Resource group information.",
[]string{
labelID,
labelName,
labelState,
labelLocation,
labelManagedBy,
},
nil,
)
gaugeValue float64 = 1
)
type ResourceGroupConfig struct {
K8sClient kubernetes.Interface
Logger micrologger.Logger
GSTenantID string
}
type ResourceGroup struct {
k8sClient kubernetes.Interface
logger micrologger.Logger
gsTenantID string
}
// NewResourceGroup exposes metrics on the existing resource groups for every subscription.
// It exposes metrcis about the subscriptions found in the "credential-*" secrets of the control plane.
func NewResourceGroup(config ResourceGroupConfig) (*ResourceGroup, 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)
}
if config.GSTenantID == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.GSTenantID must not be empty", config)
}
r := &ResourceGroup{
k8sClient: config.K8sClient,
logger: config.Logger,
gsTenantID: config.GSTenantID,
}
return r, nil
}
func (r *ResourceGroup) Collect(ch chan<- prometheus.Metric) error {
ctx := context.Background()
clientSets, err := credential.GetAzureClientSetsFromCredentialSecretsBySubscription(ctx, r.k8sClient, r.gsTenantID)
if err != nil {
return microerror.Mask(err)
}
var g errgroup.Group
for _, item := range clientSets {
clientSet := item
g.Go(func() error {
err := r.collectForClientSet(ctx, ch, clientSet.GroupsClient)
if err != nil {
return microerror.Mask(err)
}
return nil
})
}
if err := g.Wait(); err != nil {
return microerror.Mask(err)
}
return nil
}
func (r *ResourceGroup) collectForClientSet(ctx context.Context, ch chan<- prometheus.Metric, client *resources.GroupsClient) error {
resultsPage, err := client.ListComplete(context.Background(), "", nil)
if err != nil {
return microerror.Mask(err)
}
for resultsPage.NotDone() {
group := resultsPage.Value()
ch <- prometheus.MustNewConstMetric(
resourceGroupDesc,
prometheus.GaugeValue,
gaugeValue,
to.String(group.ID),
to.String(group.Name),
getState(group),
to.String(group.Location),
to.String(group.ManagedBy),
)
if err := resultsPage.NextWithContext(ctx); err != nil {
return microerror.Mask(err)
}
}
return nil
}
func (r *ResourceGroup) Describe(ch chan<- *prometheus.Desc) error {
ch <- resourceGroupDesc
return nil
}
func getState(group resources.Group) string {
if group.Properties != nil {
return to.String(group.Properties.ProvisioningState)
}
return ""
}