|
| 1 | +// Copyright 2025 NetApp, Inc. All Rights Reserved. |
| 2 | + |
| 3 | +package core |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/netapp/trident/config" |
| 12 | + . "github.com/netapp/trident/logging" |
| 13 | + "github.com/netapp/trident/storage" |
| 14 | + "github.com/netapp/trident/utils/models" |
| 15 | +) |
| 16 | + |
| 17 | +// recordTiming is used to record in Prometheus the total time taken for an operation as follows: |
| 18 | +// |
| 19 | +// defer recordTiming("backend_add")() |
| 20 | +// |
| 21 | +// see also: https://play.golang.org/p/6xRXlhFdqBd |
| 22 | +func recordTiming(operation string, err *error) func() { |
| 23 | + startTime := time.Now() |
| 24 | + return func() { |
| 25 | + endTime := time.Since(startTime) |
| 26 | + endTimeMS := float64(endTime.Milliseconds()) |
| 27 | + success := "true" |
| 28 | + if *err != nil { |
| 29 | + success = "false" |
| 30 | + } |
| 31 | + operationDurationInMsSummary.WithLabelValues(operation, success).Observe(endTimeMS) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func recordTransactionTiming(txn *storage.VolumeTransaction, err *error) { |
| 36 | + if txn == nil || txn.VolumeCreatingConfig == nil { |
| 37 | + // for unit tests, there will be no txn to record |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + operation := "transaction_volume_finish" |
| 42 | + |
| 43 | + startTime := txn.VolumeCreatingConfig.StartTime |
| 44 | + endTime := time.Since(startTime) |
| 45 | + endTimeMS := float64(endTime.Milliseconds()) |
| 46 | + success := "true" |
| 47 | + if *err != nil { |
| 48 | + success = "false" |
| 49 | + } |
| 50 | + operationDurationInMsSummary.WithLabelValues(operation, success).Observe(endTimeMS) |
| 51 | +} |
| 52 | + |
| 53 | +// getProtocol returns the appropriate protocol based on a specified volume mode, access mode and protocol, or |
| 54 | +// an error if the two settings are incompatible. |
| 55 | +// NOTE: We do not support raw block volumes with NFS protocol. |
| 56 | +func getProtocol( |
| 57 | + ctx context.Context, volumeMode config.VolumeMode, accessMode config.AccessMode, protocol config.Protocol, |
| 58 | +) (config.Protocol, error) { |
| 59 | + Logc(ctx).WithFields(LogFields{ |
| 60 | + "volumeMode": volumeMode, |
| 61 | + "accessMode": accessMode, |
| 62 | + "protocol": protocol, |
| 63 | + }).Debug("Orchestrator#getProtocol") |
| 64 | + |
| 65 | + resultProtocol := protocol |
| 66 | + var err error = nil |
| 67 | + |
| 68 | + defer Logc(ctx).WithFields(LogFields{ |
| 69 | + "resultProtocol": resultProtocol, |
| 70 | + "err": err, |
| 71 | + }).Debug("Orchestrator#getProtocol") |
| 72 | + |
| 73 | + type accessVariables struct { |
| 74 | + volumeMode config.VolumeMode |
| 75 | + accessMode config.AccessMode |
| 76 | + protocol config.Protocol |
| 77 | + } |
| 78 | + type protocolResult struct { |
| 79 | + protocol config.Protocol |
| 80 | + err error |
| 81 | + } |
| 82 | + |
| 83 | + err = fmt.Errorf("incompatible volume mode (%s), access mode (%s) and protocol (%s)", volumeMode, accessMode, |
| 84 | + protocol) |
| 85 | + |
| 86 | + protocolTable := map[accessVariables]protocolResult{ |
| 87 | + {config.Filesystem, config.ModeAny, config.ProtocolAny}: {config.ProtocolAny, nil}, |
| 88 | + {config.Filesystem, config.ModeAny, config.File}: {config.File, nil}, |
| 89 | + {config.Filesystem, config.ModeAny, config.Block}: {config.Block, nil}, |
| 90 | + |
| 91 | + {config.Filesystem, config.ReadWriteOnce, config.ProtocolAny}: {config.ProtocolAny, nil}, |
| 92 | + {config.Filesystem, config.ReadWriteOnce, config.File}: {config.File, nil}, |
| 93 | + {config.Filesystem, config.ReadWriteOnce, config.Block}: {config.Block, nil}, |
| 94 | + |
| 95 | + {config.Filesystem, config.ReadWriteOncePod, config.ProtocolAny}: {config.ProtocolAny, nil}, |
| 96 | + {config.Filesystem, config.ReadWriteOncePod, config.File}: {config.File, nil}, |
| 97 | + {config.Filesystem, config.ReadWriteOncePod, config.Block}: {config.Block, nil}, |
| 98 | + |
| 99 | + {config.Filesystem, config.ReadOnlyMany, config.ProtocolAny}: {config.ProtocolAny, nil}, |
| 100 | + {config.Filesystem, config.ReadOnlyMany, config.File}: {config.File, nil}, |
| 101 | + {config.Filesystem, config.ReadOnlyMany, config.Block}: {config.Block, nil}, |
| 102 | + |
| 103 | + {config.Filesystem, config.ReadWriteMany, config.ProtocolAny}: {config.File, nil}, |
| 104 | + {config.Filesystem, config.ReadWriteMany, config.File}: {config.File, nil}, |
| 105 | + {config.Filesystem, config.ReadWriteMany, config.Block}: {config.ProtocolAny, err}, |
| 106 | + |
| 107 | + {config.RawBlock, config.ModeAny, config.ProtocolAny}: {config.Block, nil}, |
| 108 | + {config.RawBlock, config.ModeAny, config.File}: {config.ProtocolAny, err}, |
| 109 | + {config.RawBlock, config.ModeAny, config.Block}: {config.Block, nil}, |
| 110 | + |
| 111 | + {config.RawBlock, config.ReadWriteOnce, config.ProtocolAny}: {config.Block, nil}, |
| 112 | + {config.RawBlock, config.ReadWriteOnce, config.File}: {config.ProtocolAny, err}, |
| 113 | + {config.RawBlock, config.ReadWriteOnce, config.Block}: {config.Block, nil}, |
| 114 | + |
| 115 | + {config.RawBlock, config.ReadWriteOncePod, config.ProtocolAny}: {config.Block, nil}, |
| 116 | + {config.RawBlock, config.ReadWriteOncePod, config.File}: {config.ProtocolAny, err}, |
| 117 | + {config.RawBlock, config.ReadWriteOncePod, config.Block}: {config.Block, nil}, |
| 118 | + |
| 119 | + {config.RawBlock, config.ReadOnlyMany, config.ProtocolAny}: {config.Block, nil}, |
| 120 | + {config.RawBlock, config.ReadOnlyMany, config.File}: {config.ProtocolAny, err}, |
| 121 | + {config.RawBlock, config.ReadOnlyMany, config.Block}: {config.Block, nil}, |
| 122 | + |
| 123 | + {config.RawBlock, config.ReadWriteMany, config.ProtocolAny}: {config.Block, nil}, |
| 124 | + {config.RawBlock, config.ReadWriteMany, config.File}: {config.ProtocolAny, err}, |
| 125 | + {config.RawBlock, config.ReadWriteMany, config.Block}: {config.Block, nil}, |
| 126 | + } |
| 127 | + |
| 128 | + res, isValid := protocolTable[accessVariables{volumeMode, accessMode, protocol}] |
| 129 | + |
| 130 | + if !isValid { |
| 131 | + return config.ProtocolAny, fmt.Errorf("invalid volume mode (%s), access mode (%s) or protocol (%s)", volumeMode, |
| 132 | + accessMode, protocol) |
| 133 | + } |
| 134 | + |
| 135 | + return res.protocol, res.err |
| 136 | +} |
| 137 | + |
| 138 | +func generateVolumePublication(volName string, publishInfo *models.VolumePublishInfo) *models.VolumePublication { |
| 139 | + vp := &models.VolumePublication{ |
| 140 | + Name: models.GenerateVolumePublishName(volName, publishInfo.HostName), |
| 141 | + VolumeName: volName, |
| 142 | + NodeName: publishInfo.HostName, |
| 143 | + ReadOnly: publishInfo.ReadOnly, |
| 144 | + AccessMode: publishInfo.AccessMode, |
| 145 | + } |
| 146 | + |
| 147 | + return vp |
| 148 | +} |
| 149 | + |
| 150 | +// isDockerPluginMode returns true if the ENV variable config.DockerPluginModeEnvVariable is set |
| 151 | +func isDockerPluginMode() bool { |
| 152 | + return os.Getenv(config.DockerPluginModeEnvVariable) != "" |
| 153 | +} |
| 154 | + |
| 155 | +func isCRDContext(ctx context.Context) bool { |
| 156 | + ctxSource := ctx.Value(ContextKeyRequestSource) |
| 157 | + return ctxSource != nil && ctxSource == ContextSourceCRD |
| 158 | +} |
| 159 | + |
| 160 | +func isPeriodicContext(ctx context.Context) bool { |
| 161 | + ctxSource := ctx.Value(ContextKeyRequestSource) |
| 162 | + return ctxSource != nil && ctxSource == ContextSourcePeriodic |
| 163 | +} |
0 commit comments