Skip to content

Commit dad66db

Browse files
authored
Support for adding prometheus annotations on update minio tenant (#269)
1 parent adf3f92 commit dad66db

File tree

6 files changed

+139
-12
lines changed

6 files changed

+139
-12
lines changed

models/tenant.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/update_tenant_request.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

restapi/admin_tenants.go

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,27 @@ func getTenant(ctx context.Context, operatorClient OperatorClientI, namespace, t
257257
return minInst, nil
258258
}
259259

260+
func isPrometheusEnabled(annotations map[string]string) bool {
261+
if annotations == nil {
262+
return false
263+
}
264+
// if one of the following prometheus annotations are not present
265+
// we consider the tenant as not integrated with prometheus
266+
if _, ok := annotations[prometheusPath]; !ok {
267+
return false
268+
}
269+
if _, ok := annotations[prometheusPort]; !ok {
270+
return false
271+
}
272+
if _, ok := annotations[prometheusScrape]; !ok {
273+
return false
274+
}
275+
return true
276+
}
277+
260278
func getTenantInfo(tenant *operator.Tenant) *models.Tenant {
261279
var zones []*models.Zone
262-
280+
consoleImage := ""
263281
var totalSize int64
264282
for _, z := range tenant.Spec.Zones {
265283
zones = append(zones, parseTenantZone(&z))
@@ -271,15 +289,27 @@ func getTenantInfo(tenant *operator.Tenant) *models.Tenant {
271289
deletion = tenant.ObjectMeta.DeletionTimestamp.String()
272290
}
273291

292+
if tenant.HasConsoleEnabled() {
293+
consoleImage = tenant.Spec.Console.Image
294+
}
295+
296+
if tenant.Spec.Metadata == nil {
297+
tenant.Spec.Metadata = &metav1.ObjectMeta{
298+
Annotations: map[string]string{},
299+
}
300+
}
301+
274302
return &models.Tenant{
275-
CreationDate: tenant.ObjectMeta.CreationTimestamp.String(),
276-
DeletionDate: deletion,
277-
Name: tenant.Name,
278-
TotalSize: totalSize,
279-
CurrentState: tenant.Status.CurrentState,
280-
Zones: zones,
281-
Namespace: tenant.ObjectMeta.Namespace,
282-
Image: tenant.Spec.Image,
303+
CreationDate: tenant.ObjectMeta.CreationTimestamp.String(),
304+
DeletionDate: deletion,
305+
Name: tenant.Name,
306+
TotalSize: totalSize,
307+
CurrentState: tenant.Status.CurrentState,
308+
Zones: zones,
309+
Namespace: tenant.ObjectMeta.Namespace,
310+
Image: tenant.Spec.Image,
311+
ConsoleImage: consoleImage,
312+
EnablePrometheus: isPrometheusEnabled(tenant.Spec.Metadata.Annotations),
283313
}
284314
}
285315

@@ -695,9 +725,9 @@ func getTenantCreatedResponse(session *models.Principal, params admin_api.Create
695725

696726
// prometheus annotations support
697727
if tenantReq.EnablePrometheus != nil && *tenantReq.EnablePrometheus && minInst.Spec.Metadata != nil && minInst.Spec.Metadata.Annotations != nil {
698-
minInst.Spec.Metadata.Annotations["prometheus.io/path"] = "/minio/prometheus/metrics"
699-
minInst.Spec.Metadata.Annotations["prometheus.io/port"] = fmt.Sprint(operator.MinIOPort)
700-
minInst.Spec.Metadata.Annotations["prometheus.io/scrape"] = "true"
728+
minInst.Spec.Metadata.Annotations[prometheusPath] = "/minio/prometheus/metrics"
729+
minInst.Spec.Metadata.Annotations[prometheusPort] = fmt.Sprint(operator.MinIOPort)
730+
minInst.Spec.Metadata.Annotations[prometheusScrape] = "true"
701731
}
702732

703733
// set console image if provided
@@ -831,6 +861,40 @@ func updateTenantAction(ctx context.Context, operatorClient OperatorClientI, cli
831861
}
832862
}
833863

864+
// Prometheus Annotations
865+
if minInst.Spec.Metadata == nil {
866+
minInst.Spec.Metadata = &metav1.ObjectMeta{
867+
Annotations: map[string]string{},
868+
}
869+
}
870+
currentAnnotations := minInst.Spec.Metadata.Annotations
871+
prometheusAnnotations := map[string]string{
872+
prometheusPath: "/minio/prometheus/metrics",
873+
prometheusPort: fmt.Sprint(operator.MinIOPort),
874+
prometheusScrape: "true",
875+
}
876+
if params.Body.EnablePrometheus && minInst.Spec.Metadata != nil && currentAnnotations != nil {
877+
// add prometheus annotations to the tenant
878+
minInst.Spec.Metadata.Annotations = addAnnotations(currentAnnotations, prometheusAnnotations)
879+
// add prometheus annotations to the each zone
880+
if minInst.Spec.Zones != nil {
881+
for _, zone := range minInst.Spec.Zones {
882+
zoneAnnotations := zone.VolumeClaimTemplate.GetObjectMeta().GetAnnotations()
883+
zone.VolumeClaimTemplate.GetObjectMeta().SetAnnotations(addAnnotations(zoneAnnotations, prometheusAnnotations))
884+
}
885+
}
886+
} else {
887+
// remove prometheus annotations to the tenant
888+
minInst.Spec.Metadata.Annotations = removeAnnotations(currentAnnotations, prometheusAnnotations)
889+
// add prometheus annotations from each zone
890+
if minInst.Spec.Zones != nil {
891+
for _, zone := range minInst.Spec.Zones {
892+
zoneAnnotations := zone.VolumeClaimTemplate.GetObjectMeta().GetAnnotations()
893+
zone.VolumeClaimTemplate.GetObjectMeta().SetAnnotations(removeAnnotations(zoneAnnotations, prometheusAnnotations))
894+
}
895+
}
896+
}
897+
834898
payloadBytes, err := json.Marshal(minInst)
835899
if err != nil {
836900
return err
@@ -842,6 +906,28 @@ func updateTenantAction(ctx context.Context, operatorClient OperatorClientI, cli
842906
return nil
843907
}
844908

909+
// addAnnotations will merge two annotation maps
910+
func addAnnotations(annotationsOne, annotationsTwo map[string]string) map[string]string {
911+
if annotationsOne == nil {
912+
annotationsOne = map[string]string{}
913+
}
914+
for key, value := range annotationsTwo {
915+
annotationsOne[key] = value
916+
}
917+
return annotationsOne
918+
}
919+
920+
// removeAnnotations will remove keys from the first annotations map based on the second one
921+
func removeAnnotations(annotationsOne, annotationsTwo map[string]string) map[string]string {
922+
if annotationsOne == nil {
923+
annotationsOne = map[string]string{}
924+
}
925+
for key := range annotationsTwo {
926+
delete(annotationsOne, key)
927+
}
928+
return annotationsOne
929+
}
930+
845931
func getUpdateTenantResponse(session *models.Principal, params admin_api.UpdateTenantParams) *models.Error {
846932
ctx := context.Background()
847933
opClientClientSet, err := cluster.OperatorClient(session.SessionToken)

restapi/consts.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ const (
5050
ConsoleSecureFeaturePolicy = "CONSOLE_SECURE_FEATURE_POLICY"
5151
ConsoleSecureExpectCTHeader = "CONSOLE_SECURE_EXPECT_CT_HEADER"
5252
)
53+
54+
// prometheus annotations
55+
56+
const (
57+
prometheusPath = "prometheus.io/path"
58+
prometheusPort = "prometheus.io/port"
59+
prometheusScrape = "prometheus.io/scrape"
60+
)

restapi/embedded_spec.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swagger.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,11 +1838,15 @@ definitions:
18381838
$ref: "#/definitions/zone"
18391839
image:
18401840
type: string
1841+
console_image:
1842+
type: string
18411843
namespace:
18421844
type: string
18431845
total_size:
18441846
type: integer
18451847
format: int64
1848+
enable_prometheus:
1849+
type: boolean
18461850

18471851
tenantUsage:
18481852
type: object
@@ -1902,6 +1906,8 @@ definitions:
19021906
$ref: "#/definitions/imageRegistry"
19031907
image_pull_secret:
19041908
type: string
1909+
enable_prometheus:
1910+
type: boolean
19051911

19061912
imageRegistry:
19071913
type: object

0 commit comments

Comments
 (0)