MCPcopy Create free account
hub / github.com/flant/shell-operator / handleRunHook

Method handleRunHook

pkg/shell-operator/operator.go:733–799  ·  view source on GitHub ↗
(ctx context.Context, t task.Task, taskHook *hook.Hook, hookMeta task_metadata.HookMetadata, taskLogEntry *log.Logger, hookLogLabels map[string]string, metricLabels map[string]string)

Source from the content-addressed store, hash-verified

731}
732
733func (op *ShellOperator) handleRunHook(ctx context.Context, t task.Task, taskHook *hook.Hook, hookMeta task_metadata.HookMetadata, taskLogEntry *log.Logger, hookLogLabels map[string]string, metricLabels map[string]string) error {
734 ctx, span := otel.Tracer(serviceName).Start(ctx, "handleRunHook")
735 defer span.End()
736
737 for _, info := range taskHook.HookController.SnapshotsInfo() {
738 taskLogEntry.Debug("snapshot info", slog.String(pkg.LogKeyInfo, info))
739 }
740
741 result, err := taskHook.Run(ctx, hookMeta.BindingType, hookMeta.BindingContext, hookLogLabels)
742 if err != nil {
743 if result != nil && len(result.KubernetesPatchBytes) > 0 {
744 operations, patchStatusErr := objectpatch.ParseOperations(result.KubernetesPatchBytes)
745 if patchStatusErr != nil {
746 return fmt.Errorf("%s: couldn't patch status: %s", err, patchStatusErr)
747 }
748
749 patchStatusErr = op.ObjectPatcher.ExecuteOperations(objectpatch.GetPatchStatusOperationsOnHookError(operations))
750 if patchStatusErr != nil {
751 return fmt.Errorf("%s: couldn't patch status: %s", err, patchStatusErr)
752 }
753 }
754 return err
755 }
756
757 if result.Usage != nil {
758 taskLogEntry.Debug("Usage", slog.String(pkg.LogKeyValue, fmt.Sprintf("%+v", result.Usage)))
759 op.MetricStorage.HistogramObserve(metrics.HookRunSysCPUSeconds, result.Usage.Sys.Seconds(), metricLabels, nil)
760 op.MetricStorage.HistogramObserve(metrics.HookRunUserCPUSeconds, result.Usage.User.Seconds(), metricLabels, nil)
761 op.MetricStorage.GaugeSet(metrics.HookRunMaxRSSBytes, float64(result.Usage.MaxRss)*1024, metricLabels)
762 }
763
764 // Try to apply Kubernetes actions.
765 if len(result.KubernetesPatchBytes) > 0 {
766 operations, err := objectpatch.ParseOperations(result.KubernetesPatchBytes)
767 if err != nil {
768 return err
769 }
770 err = op.ObjectPatcher.ExecuteOperations(operations)
771 if err != nil {
772 return err
773 }
774 }
775
776 // Try to update custom metrics
777 err = op.HookMetricStorage.ApplyBatchOperations(result.Metrics, map[string]string{
778 pkg.MetricKeyHook: hookMeta.HookName,
779 })
780 if err != nil {
781 return err
782 }
783
784 // Save validatingResponse in task props for future use.
785 if result.AdmissionResponse != nil {
786 t.SetProp("admissionResponse", result.AdmissionResponse)
787 taskLogEntry.Info("AdmissionResponse from hook",
788 slog.String(pkg.LogKeyValue, result.AdmissionResponse.Dump()))
789 }
790

Callers 1

taskHandleHookRunMethod · 0.95

Calls 10

ExecuteOperationsMethod · 0.80
ApplyBatchOperationsMethod · 0.80
StartMethod · 0.65
SnapshotsInfoMethod · 0.65
SetPropMethod · 0.65
StringMethod · 0.45
RunMethod · 0.45
HistogramObserveMethod · 0.45
GaugeSetMethod · 0.45
DumpMethod · 0.45

Tested by

no test coverage detected