Skip to content

Commit 8416602

Browse files
author
David Collom
committed
Fix up not all metrics were being cleared down
1 parent 2de31eb commit 8416602

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

cmd/app/app.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ func NewCommand(ctx context.Context) *cobra.Command {
4040
RunE: func(_ *cobra.Command, _ []string) error {
4141
opts.complete()
4242

43-
log := logrus.New().WithField("component", "controller")
43+
logLevel, err := logrus.ParseLevel(opts.LogLevel)
44+
if err != nil {
45+
return fmt.Errorf("failed to parse --log-level %q: %s",
46+
opts.LogLevel, err)
47+
}
48+
49+
log := newLogger(logLevel).WithField("component", "controller")
4450

4551
defaultTestAllInfoMsg := fmt.Sprintf(`only containers with the annotation "%s/${my-container}=true" will be parsed`, api.EnableAnnotationKey)
4652
if opts.DefaultTestAll {
@@ -57,13 +63,13 @@ func NewCommand(ctx context.Context) *cobra.Command {
5763
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
5864
Logger: logrusr.New(log.WithField("controller", "manager").Logger),
5965
LeaderElection: false,
60-
// TODO: See if we can get newer/better solution
6166
Metrics: server.Options{
6267
BindAddress: opts.MetricsServingAddress,
6368
SecureServing: false,
6469
},
6570
GracefulShutdownTimeout: &opts.GracefulShutdownTimeout,
6671
Cache: cache.Options{SyncPeriod: &opts.CacheSyncPeriod},
72+
PprofBindAddress: opts.PprofBindAddress,
6773
})
6874
if err != nil {
6975
return err
@@ -99,10 +105,6 @@ func NewCommand(ctx context.Context) *cobra.Command {
99105
metricsServer.RoundTripper,
100106
)
101107

102-
// nodeController := controller.NewNodeReconciler(log, mgr.GetClient())
103-
// if err := nodeController.SetupWithManager(mgr); err != nil {
104-
// }
105-
106108
client, err := client.New(ctx, log, opts.Client)
107109
if err != nil {
108110
return fmt.Errorf("failed to setup image registry clients: %s", err)

cmd/app/options.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type Options struct {
6868
CacheTimeout time.Duration
6969
LogLevel string
7070

71+
PprofBindAddress string
7172
GracefulShutdownTimeout time.Duration
7273
CacheSyncPeriod time.Duration
7374

@@ -108,6 +109,10 @@ func (o *Options) addAppFlags(fs *pflag.FlagSet) {
108109
"metrics-serving-address", "m", "0.0.0.0:8080",
109110
"Address to serve metrics on at the /metrics path.")
110111

112+
fs.StringVarP(&o.PprofBindAddress,
113+
"pprof-serving-address", "", "",
114+
"Address to serve pprof on for profiling.")
115+
111116
fs.BoolVarP(&o.DefaultTestAll,
112117
"test-all-containers", "a", false,
113118
"If enabled, all containers will be tested, unless they have the "+

pkg/client/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Options struct {
4444
}
4545

4646
func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error) {
47+
log = log.WithField("component", "client")
4748
// Setup Transporters for all remaining clients (if one is set)
4849
if opts.Transport != nil {
4950
opts.Quay.Transporter = opts.Transport
@@ -106,7 +107,7 @@ func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error)
106107
quay.New(opts.Quay, log),
107108
),
108109
fallbackClient: fallbackClient,
109-
log: log.WithField("client", "registry"),
110+
log: log,
110111
}
111112

112113
for _, client := range append(c.clients, fallbackClient) {

pkg/controller/pod_controller.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
corev1 "k8s.io/api/core/v1"
88
apierrors "k8s.io/apimachinery/pkg/api/errors"
99

10-
schema "k8s.io/apimachinery/pkg/runtime/schema"
11-
1210
ctrl "sigs.k8s.io/controller-runtime"
1311
"sigs.k8s.io/controller-runtime/pkg/builder"
1412
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -68,12 +66,6 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
6866

6967
// Fetch the Pod instance
7068
pod := &corev1.Pod{}
71-
// Because we're using builder.OnlyMetadata and caching only metadata
72-
pod.SetGroupVersionKind(schema.GroupVersionKind{
73-
Group: "",
74-
Version: "v1",
75-
Kind: "Pod",
76-
})
7769
err := r.Get(ctx, req.NamespacedName, pod)
7870
if apierrors.IsNotFound(err) {
7971
// Pod deleted, remove from metrics

pkg/metrics/metrics.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,43 @@ func (m *Metrics) AddImage(namespace, pod, container, containerType, imageURL st
106106
func (m *Metrics) RemoveImage(namespace, pod, container, containerType string) {
107107
m.mu.Lock()
108108
defer m.mu.Unlock()
109+
total := 0
109110

110-
m.containerImageVersion.DeletePartialMatch(
111+
total += m.containerImageVersion.DeletePartialMatch(
111112
m.buildPartialLabels(namespace, pod),
112113
)
113-
m.containerImageDuration.DeletePartialMatch(
114+
total += m.containerImageDuration.DeletePartialMatch(
114115
m.buildPartialLabels(namespace, pod),
115116
)
116-
m.containerImageChecked.DeletePartialMatch(
117+
118+
total += m.containerImageChecked.DeletePartialMatch(
119+
m.buildPartialLabels(namespace, pod),
120+
)
121+
total += m.containerImageErrors.DeletePartialMatch(
117122
m.buildPartialLabels(namespace, pod),
118123
)
124+
m.log.Infof("Removed %d metrics for image %s/%s/%s", total, namespace, pod, container)
119125
}
120126

121127
func (m *Metrics) RemovePod(namespace, pod string) {
122128
m.mu.Lock()
123129
defer m.mu.Unlock()
124130

125-
m.containerImageVersion.DeletePartialMatch(
131+
total := 0
132+
total += m.containerImageVersion.DeletePartialMatch(
126133
m.buildPartialLabels(namespace, pod),
127134
)
128-
m.containerImageDuration.DeletePartialMatch(
135+
total += m.containerImageDuration.DeletePartialMatch(
129136
m.buildPartialLabels(namespace, pod),
130137
)
131-
m.containerImageChecked.DeletePartialMatch(
138+
total += m.containerImageChecked.DeletePartialMatch(
132139
m.buildPartialLabels(namespace, pod),
133140
)
141+
total += m.containerImageErrors.DeletePartialMatch(
142+
m.buildPartialLabels(namespace, pod),
143+
)
144+
145+
m.log.Infof("Removed %d metrics for pod %s/%s", total, namespace, pod)
134146
}
135147

136148
func (m *Metrics) RegisterImageDuration(namespace, pod, container, image string, startTime time.Time) {

0 commit comments

Comments
 (0)