-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add labels to metrics #1433
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/metric/global" | ||
) | ||
|
||
const ( | ||
scope = "sigs.k8s.io/secrets-store-csi-driver" | ||
) | ||
|
||
var ( | ||
providerKey = "provider" | ||
osTypeKey = "os_type" | ||
runtimeOS = runtime.GOOS | ||
namespaceKey = "namespace" | ||
spcKey = "secret_provider_class" | ||
) | ||
|
||
type reporter struct { | ||
syncK8sSecretTotal metric.Int64Counter | ||
syncK8sSecretDuration metric.Float64Histogram | ||
} | ||
|
||
type StatsReporter interface { | ||
ReportSyncSecretCtMetric(ctx context.Context, provider, namespace, spc string) | ||
ReportSyncSecretDuration(ctx context.Context, duration float64) | ||
} | ||
|
||
func newStatsReporter() (StatsReporter, error) { | ||
var err error | ||
|
||
r := &reporter{} | ||
meter := global.Meter(scope) | ||
|
||
if r.syncK8sSecretTotal, err = meter.Int64Counter("sync_k8s_secret", metric.WithDescription("Total number of k8s secrets synced")); err != nil { | ||
return nil, err | ||
} | ||
if r.syncK8sSecretDuration, err = meter.Float64Histogram("sync_k8s_secret_duration_sec", metric.WithDescription("Distribution of how long it took to sync k8s secret")); err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
|
||
func (r reporter) ReportSyncSecretCtMetric(ctx context.Context, provider, namespace, spc string) { | ||
opt := metric.WithAttributes( | ||
attribute.Key(providerKey).String(provider), | ||
attribute.Key(osTypeKey).String(runtimeOS), | ||
attribute.Key(namespaceKey).String(namespace), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use attributes with unbounded value domains (kubernetes/kubernetes#102523) |
||
attribute.Key(spcKey).String(spc), | ||
) | ||
r.syncK8sSecretTotal.Add(ctx, 1, opt) | ||
} | ||
|
||
func (r reporter) ReportSyncSecretDuration(ctx context.Context, duration float64) { | ||
opt := metric.WithAttributes( | ||
attribute.Key(osTypeKey).String(runtimeOS), | ||
Comment on lines
+76
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment as above |
||
) | ||
r.syncK8sSecretDuration.Record(ctx, duration, opt) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -251,13 +251,16 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *secretsstorev1.Secret | |||||
// after the provider mount request is complete | ||||||
var requiresUpdate bool | ||||||
var providerName string | ||||||
podName := spcps.Status.PodName | ||||||
Suraiya-Hameed marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
podNamespace := spcps.Namespace | ||||||
secretProviderClass := spcps.Status.SecretProviderClassName | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
defer func() { | ||||||
if err != nil { | ||||||
r.reporter.reportRotationErrorCtMetric(ctx, providerName, errorReason, requiresUpdate) | ||||||
r.reporter.reportRotationErrorCtMetric(ctx, providerName, podName, podNamespace, secretProviderClass, errorReason, requiresUpdate) | ||||||
return | ||||||
} | ||||||
r.reporter.reportRotationCtMetric(ctx, providerName, requiresUpdate) | ||||||
r.reporter.reportRotationCtMetric(ctx, providerName, podName, podNamespace, secretProviderClass, requiresUpdate) | ||||||
r.reporter.reportRotationDuration(ctx, time.Since(begin).Seconds()) | ||||||
}() | ||||||
|
||||||
|
@@ -266,14 +269,14 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *secretsstorev1.Secret | |||||
err = r.cache.Get( | ||||||
ctx, | ||||||
client.ObjectKey{ | ||||||
Namespace: spcps.Namespace, | ||||||
Name: spcps.Status.PodName, | ||||||
Namespace: podNamespace, | ||||||
Name: podName, | ||||||
}, | ||||||
pod, | ||||||
) | ||||||
if err != nil { | ||||||
errorReason = internalerrors.PodNotFound | ||||||
return fmt.Errorf("failed to get pod %s/%s, err: %w", spcps.Namespace, spcps.Status.PodName, err) | ||||||
return fmt.Errorf("failed to get pod %s/%s, err: %w", podNamespace, podName, err) | ||||||
} | ||||||
// skip rotation if the pod is being terminated | ||||||
// or the pod is in succeeded state (for jobs that complete aren't gc yet) | ||||||
|
@@ -289,14 +292,14 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *secretsstorev1.Secret | |||||
err = r.cache.Get( | ||||||
ctx, | ||||||
client.ObjectKey{ | ||||||
Namespace: spcps.Namespace, | ||||||
Name: spcps.Status.SecretProviderClassName, | ||||||
Namespace: podNamespace, | ||||||
Name: secretProviderClass, | ||||||
}, | ||||||
spc, | ||||||
) | ||||||
if err != nil { | ||||||
errorReason = internalerrors.SecretProviderClassNotFound | ||||||
return fmt.Errorf("failed to get secret provider class %s/%s, err: %w", spcps.Namespace, spcps.Status.SecretProviderClassName, err) | ||||||
return fmt.Errorf("failed to get secret provider class %s/%s, err: %w", podNamespace, secretProviderClass, err) | ||||||
} | ||||||
|
||||||
// determine which pod volume this is associated with | ||||||
|
@@ -359,16 +362,16 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *secretsstorev1.Secret | |||||
// This comprises the secret parameter in the MountRequest to the provider | ||||||
if nodePublishSecretRef != nil { | ||||||
// read secret from the informer cache | ||||||
secret, err := r.secretStore.GetNodePublishSecretRefSecret(nodePublishSecretRef.Name, spcps.Namespace) | ||||||
secret, err := r.secretStore.GetNodePublishSecretRefSecret(nodePublishSecretRef.Name, podNamespace) | ||||||
if err != nil { | ||||||
if apierrors.IsNotFound(err) { | ||||||
klog.ErrorS(err, | ||||||
fmt.Sprintf("nodePublishSecretRef not found. If the secret with name exists in namespace, label the secret by running 'kubectl label secret %s %s=true -n %s", nodePublishSecretRef.Name, controllers.SecretUsedLabel, spcps.Namespace), | ||||||
"name", nodePublishSecretRef.Name, "namespace", spcps.Namespace) | ||||||
fmt.Sprintf("nodePublishSecretRef not found. If the secret with name exists in namespace, label the secret by running 'kubectl label secret %s %s=true -n %s", nodePublishSecretRef.Name, controllers.SecretUsedLabel, podNamespace), | ||||||
"name", nodePublishSecretRef.Name, "namespace", podNamespace) | ||||||
} | ||||||
errorReason = internalerrors.NodePublishSecretRefNotFound | ||||||
r.generateEvent(pod, corev1.EventTypeWarning, mountRotationFailedReason, fmt.Sprintf("failed to get node publish secret %s/%s, err: %+v", spcps.Namespace, nodePublishSecretRef.Name, err)) | ||||||
return fmt.Errorf("failed to get node publish secret %s/%s, err: %w", spcps.Namespace, nodePublishSecretRef.Name, err) | ||||||
r.generateEvent(pod, corev1.EventTypeWarning, mountRotationFailedReason, fmt.Sprintf("failed to get node publish secret %s/%s, err: %+v", podNamespace, nodePublishSecretRef.Name, err)) | ||||||
return fmt.Errorf("failed to get node publish secret %s/%s, err: %w", podNamespace, nodePublishSecretRef.Name, err) | ||||||
} | ||||||
|
||||||
for k, v := range secret.Data { | ||||||
|
@@ -401,7 +404,7 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *secretsstorev1.Secret | |||||
newObjectVersions, errorReason, err := secretsstore.MountContent(ctx, providerClient, string(paramsJSON), string(secretsJSON), spcps.Status.TargetPath, string(permissionJSON), oldObjectVersions) | ||||||
if err != nil { | ||||||
r.generateEvent(pod, corev1.EventTypeWarning, mountRotationFailedReason, fmt.Sprintf("provider mount err: %+v", err)) | ||||||
return fmt.Errorf("failed to rotate objects for pod %s/%s, err: %w", spcps.Namespace, spcps.Status.PodName, err) | ||||||
return fmt.Errorf("failed to rotate objects for pod %s/%s, err: %w", podNamespace, podName, err) | ||||||
} | ||||||
|
||||||
// compare the old object versions and new object versions to check if any of the objects | ||||||
|
@@ -488,7 +491,7 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *secretsstorev1.Secret | |||||
|
||||||
patchFn := func() (bool, error) { | ||||||
// patch secret data with the new contents | ||||||
if err := r.patchSecret(ctx, secretObj.SecretName, spcps.Namespace, datamap); err != nil { | ||||||
if err := r.patchSecret(ctx, secretObj.SecretName, podNamespace, datamap); err != nil { | ||||||
// syncSecret.enabled is set to false by default in the helm chart for installing the driver in v0.0.23+ | ||||||
// that would result in a forbidden error, so generate a warning that can be helpful for debugging | ||||||
if apierrors.IsForbidden(err) { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The os key attribute is not relevant for the sync controller because it's a Kubernetes API object. The only reason it's used in driver metrics is to show the os runtime for the mount operation.