Merge pull request #102802 from adisky/metrics-credential-provider-1

Add Metrics for Kubelet credential provider
This commit is contained in:
Kubernetes Prow Robot
2021-09-27 11:02:22 -07:00
committed by GitHub
2 changed files with 85 additions and 8 deletions

View File

@@ -86,6 +86,9 @@ func RegisterCredentialProviderPlugins(pluginConfigFile, pluginBinDir string) er
return fmt.Errorf("failed to validate credential provider config: %v", errs.ToAggregate())
}
// Register metrics for credential providers
registerMetrics()
for _, provider := range credentialProviderConfig.Providers {
pluginBin := filepath.Join(pluginBinDir, provider.Name)
if _, err := os.Stat(pluginBin); err != nil {
@@ -398,14 +401,8 @@ func (e *execPlugin) ExecPlugin(ctx context.Context, image string) (*credentialp
// also, this behaviour is inline with Credential Provider Config spec
cmd.Env = mergeEnvVars(e.environ(), configEnvVars)
err = cmd.Run()
if ctx.Err() != nil {
return nil, fmt.Errorf("error execing credential provider plugin %s for image %s: %w", e.name, image, ctx.Err())
}
if err != nil {
klog.V(2).Infof("Error execing credential provider plugin, stderr: %v", stderr.String())
return nil, fmt.Errorf("error execing credential provider plugin %s for image %s: %w", e.name, image, err)
if err = e.runPlugin(ctx, cmd, image); err != nil {
return nil, err
}
data = stdout.Bytes()
@@ -428,6 +425,22 @@ func (e *execPlugin) ExecPlugin(ctx context.Context, image string) (*credentialp
return response, nil
}
func (e *execPlugin) runPlugin(ctx context.Context, cmd *exec.Cmd, image string) error {
startTime := time.Now()
defer kubeletCredentialProviderPluginDuration.WithLabelValues(e.name).Observe(time.Since(startTime).Seconds())
err := cmd.Run()
if ctx.Err() != nil {
kubeletCredentialProviderPluginErrors.WithLabelValues(e.name).Inc()
return fmt.Errorf("error execing credential provider plugin %s for image %s: %w", e.name, image, ctx.Err())
}
if err != nil {
kubeletCredentialProviderPluginErrors.WithLabelValues(e.name).Inc()
return fmt.Errorf("error execing credential provider plugin %s for image %s: %w", e.name, image, err)
}
return nil
}
// encodeRequest encodes the internal CredentialProviderRequest type into the v1alpha1 version in json
func (e *execPlugin) encodeRequest(request *credentialproviderapi.CredentialProviderRequest) ([]byte, error) {
data, err := runtime.Encode(e.encoder, request)