Skip to content

Commit 23b4b48

Browse files
Merge pull request #768 from ricardomaraschini/imagestream-metrics
IR-120: Implementing metrics for ImageStreams
2 parents 5287de0 + c62275a commit 23b4b48

File tree

20 files changed

+1037
-0
lines changed

20 files changed

+1037
-0
lines changed

pkg/metrics/metrics.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,28 @@ var (
1919
},
2020
[]string{"result"},
2121
)
22+
imageStreamTags = prometheus.NewGaugeVec(
23+
prometheus.GaugeOpts{
24+
Name: "image_registry_image_stream_tags_total",
25+
Help: "Number of image stream tags. Source is either 'imported' or 'pushed'. 'location' label shows if the tag lives in one of the 'openshift' namespaces or 'other'",
26+
},
27+
[]string{"source", "location"},
28+
)
29+
storageType = prometheus.NewGaugeVec(
30+
prometheus.GaugeOpts{
31+
Name: "image_registry_storage_type",
32+
Help: "Holds the storage in use for the image registry",
33+
},
34+
[]string{"storage"},
35+
)
2236
)
2337

2438
func init() {
2539
registry.MustRegister(
2640
storageReconfigured,
2741
imagePrunerInstallStatus,
2842
azurePrimaryKeyCache,
43+
imageStreamTags,
44+
storageType,
2945
)
3046
}

pkg/metrics/server.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ func ImagePrunerInstallStatus(installed bool, enabled bool) {
6060
imagePrunerInstallStatus.Set(2)
6161
}
6262

63+
// ReportOpenShiftImageStreamTags reports the amount of seen ImageStream tags existing in openshift
64+
// namespaces. Receives the total of 'imported' and 'pushed' image streams tags.
65+
func ReportOpenShiftImageStreamTags(imported float64, pushed float64) {
66+
imageStreamTags.WithLabelValues("imported", "openshift").Set(imported)
67+
imageStreamTags.WithLabelValues("pushed", "openshift").Set(pushed)
68+
}
69+
70+
// ReportOtherImageStreamTags reports the amount of seen ImageStream tags existing outside the
71+
// openshift namespaces. Receives the total of 'imported' and 'pushed' image streams tags.
72+
func ReportOtherImageStreamTags(imported float64, pushed float64) {
73+
imageStreamTags.WithLabelValues("imported", "other").Set(imported)
74+
imageStreamTags.WithLabelValues("pushed", "other").Set(pushed)
75+
}
76+
77+
// ReportStorageType sets the storage in use.
78+
func ReportStorageType(stype string) {
79+
storageType.WithLabelValues(stype).Set(1)
80+
}
81+
6382
// AzureKeyCacheHit registers a hit on Azure key cache.
6483
func AzureKeyCacheHit() {
6584
azurePrimaryKeyCache.With(map[string]string{"result": "hit"}).Inc()

pkg/operator/metrics.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package operator
2+
3+
import (
4+
"context"
5+
"strings"
6+
"time"
7+
8+
"k8s.io/apimachinery/pkg/labels"
9+
"k8s.io/apimachinery/pkg/util/wait"
10+
"k8s.io/client-go/tools/cache"
11+
"k8s.io/klog/v2"
12+
13+
imagev1 "github.com/openshift/api/image/v1"
14+
imageinformers "github.com/openshift/client-go/image/informers/externalversions/image/v1"
15+
imagelisters "github.com/openshift/client-go/image/listers/image/v1"
16+
17+
"github.com/openshift/cluster-image-registry-operator/pkg/metrics"
18+
)
19+
20+
// MetricsController is a controller that runs from time to time and reports some metrics about
21+
// the current status of the system.
22+
type MetricsController struct {
23+
lister imagelisters.ImageStreamLister
24+
caches []cache.InformerSynced
25+
}
26+
27+
// NewMetricsController returns a new MetricsController.
28+
func NewMetricsController(informer imageinformers.ImageStreamInformer) *MetricsController {
29+
return &MetricsController{
30+
lister: informer.Lister(),
31+
caches: []cache.InformerSynced{informer.Informer().HasSynced},
32+
}
33+
}
34+
35+
// report gathers all metrics reported by this operator and calls approprate function in the
36+
// metrics package to report the current values.
37+
func (m *MetricsController) report(_ context.Context) {
38+
imgstreams, err := m.lister.List(labels.Everything())
39+
if err != nil {
40+
klog.Errorf("unable to list image streams: %s", err)
41+
return
42+
}
43+
44+
var importedOpenShift float64
45+
var pushedOpenShift float64
46+
var importedOther float64
47+
var pushedOther float64
48+
for _, is := range imgstreams {
49+
imported, pushed := m.assessImageStream(is.DeepCopy())
50+
if strings.HasPrefix(is.Namespace, "openshift") {
51+
importedOpenShift += imported
52+
pushedOpenShift += pushed
53+
continue
54+
}
55+
56+
importedOther += imported
57+
pushedOther += pushed
58+
}
59+
60+
metrics.ReportOpenShiftImageStreamTags(importedOpenShift, pushedOpenShift)
61+
metrics.ReportOtherImageStreamTags(importedOther, pushedOther)
62+
}
63+
64+
// assessImageStream returns the number of imported and the number of pushed tags for the provided
65+
// image stream reference.
66+
func (m *MetricsController) assessImageStream(is *imagev1.ImageStream) (float64, float64) {
67+
spectags := map[string]bool{}
68+
for _, tag := range is.Spec.Tags {
69+
spectags[tag.Name] = true
70+
}
71+
72+
var imported float64
73+
var pushed float64
74+
for _, tag := range is.Status.Tags {
75+
if _, ok := spectags[tag.Tag]; ok {
76+
imported++
77+
} else {
78+
pushed++
79+
}
80+
}
81+
82+
return imported, pushed
83+
}
84+
85+
// Run starts this controller. Runs the main loop in a separate go routine and bails out when
86+
// the provided context is finished.
87+
func (m *MetricsController) Run(ctx context.Context) {
88+
klog.Infof("Starting MetricsController")
89+
if !cache.WaitForCacheSync(ctx.Done(), m.caches...) {
90+
return
91+
}
92+
93+
go wait.UntilWithContext(ctx, m.report, time.Hour)
94+
klog.Infof("Started MetricsController")
95+
<-ctx.Done()
96+
klog.Infof("Shutting down MetricsController")
97+
}

pkg/operator/starter.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
configv1 "github.com/openshift/api/config/v1"
1111
configclient "github.com/openshift/client-go/config/clientset/versioned"
1212
configinformers "github.com/openshift/client-go/config/informers/externalversions"
13+
imageclient "github.com/openshift/client-go/image/clientset/versioned"
14+
imageinformers "github.com/openshift/client-go/image/informers/externalversions"
1315
imageregistryclient "github.com/openshift/client-go/imageregistry/clientset/versioned"
1416
imageregistryinformers "github.com/openshift/client-go/imageregistry/informers/externalversions"
1517
routeclient "github.com/openshift/client-go/route/clientset/versioned"
@@ -38,6 +40,10 @@ func RunOperator(ctx context.Context, kubeconfig *restclient.Config) error {
3840
if err != nil {
3941
return err
4042
}
43+
imageClient, err := imageclient.NewForConfig(kubeconfig)
44+
if err != nil {
45+
return err
46+
}
4147

4248
kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, defaultResyncDuration, kubeinformers.WithNamespace(defaults.ImageRegistryOperatorNamespace))
4349
kubeInformersForOpenShiftConfig := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, defaultResyncDuration, kubeinformers.WithNamespace(defaults.OpenShiftConfigNamespace))
@@ -46,6 +52,7 @@ func RunOperator(ctx context.Context, kubeconfig *restclient.Config) error {
4652
configInformers := configinformers.NewSharedInformerFactory(configClient, defaultResyncDuration)
4753
imageregistryInformers := imageregistryinformers.NewSharedInformerFactory(imageregistryClient, defaultResyncDuration)
4854
routeInformers := routeinformers.NewSharedInformerFactoryWithOptions(routeClient, defaultResyncDuration, routeinformers.WithNamespace(defaults.ImageRegistryOperatorNamespace))
55+
imageInformers := imageinformers.NewSharedInformerFactory(imageClient, defaultResyncDuration)
4956

5057
configOperatorClient := client.NewConfigOperatorClient(
5158
imageregistryClient.ImageregistryV1().Configs(),
@@ -124,13 +131,16 @@ func RunOperator(ctx context.Context, kubeconfig *restclient.Config) error {
124131
kubeInformersForOpenShiftConfig.Core().V1().ConfigMaps(),
125132
)
126133

134+
metricsController := NewMetricsController(imageInformers.Image().V1().ImageStreams())
135+
127136
kubeInformers.Start(ctx.Done())
128137
kubeInformersForOpenShiftConfig.Start(ctx.Done())
129138
kubeInformersForOpenShiftConfigManaged.Start(ctx.Done())
130139
kubeInformersForKubeSystem.Start(ctx.Done())
131140
configInformers.Start(ctx.Done())
132141
imageregistryInformers.Start(ctx.Done())
133142
routeInformers.Start(ctx.Done())
143+
imageInformers.Start(ctx.Done())
134144

135145
go controller.Run(ctx.Done())
136146
go clusterOperatorStatusController.Run(ctx.Done())
@@ -140,6 +150,7 @@ func RunOperator(ctx context.Context, kubeconfig *restclient.Config) error {
140150
go imagePrunerController.Run(ctx.Done())
141151
go loggingController.Run(ctx, 1)
142152
go azureStackCloudController.Run(ctx)
153+
go metricsController.Run(ctx)
143154

144155
<-ctx.Done()
145156
return nil

pkg/storage/storage.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
regopclient "github.com/openshift/cluster-image-registry-operator/pkg/client"
1414
"github.com/openshift/cluster-image-registry-operator/pkg/defaults"
1515
"github.com/openshift/cluster-image-registry-operator/pkg/envvar"
16+
"github.com/openshift/cluster-image-registry-operator/pkg/metrics"
1617
"github.com/openshift/cluster-image-registry-operator/pkg/storage/azure"
1718
"github.com/openshift/cluster-image-registry-operator/pkg/storage/emptydir"
1819
"github.com/openshift/cluster-image-registry-operator/pkg/storage/gcs"
@@ -113,6 +114,7 @@ func NewDriver(cfg *imageregistryv1.ImageRegistryConfigStorage, kubeconfig *rest
113114
case 0:
114115
return nil, ErrStorageNotConfigured
115116
case 1:
117+
metrics.ReportStorageType(names[0])
116118
return drivers[0], nil
117119
}
118120

vendor/github.com/openshift/client-go/image/clientset/versioned/clientset.go

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

vendor/github.com/openshift/client-go/image/clientset/versioned/doc.go

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

0 commit comments

Comments
 (0)