Skip to content

Commit cc2f1c4

Browse files
committed
Move spec name generation to producer
Signed-off-by: Evan Lezar <[email protected]>
1 parent 13ab711 commit cc2f1c4

File tree

2 files changed

+84
-16
lines changed

2 files changed

+84
-16
lines changed

api/producer/spec-names.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package producer
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
cdi "tags.cncf.io/container-device-interface/specs-go"
8+
)
9+
10+
// GenerateSpecName generates a vendor+class scoped Spec file name. The
11+
// name can be passed to WriteSpec() to write a Spec file to the file
12+
// system.
13+
//
14+
// vendor and class should match the vendor and class of the CDI Spec.
15+
// The file name is generated without a ".json" or ".yaml" extension.
16+
// The caller can append the desired extension to choose a particular
17+
// encoding. Otherwise WriteSpec() will use its default encoding.
18+
//
19+
// This function always returns the same name for the same vendor/class
20+
// combination. Therefore it cannot be used as such to generate multiple
21+
// Spec file names for a single vendor and class.
22+
func GenerateSpecName(vendor, class string) string {
23+
return vendor + "-" + class
24+
}
25+
26+
// GenerateTransientSpecName generates a vendor+class scoped transient
27+
// Spec file name. The name can be passed to WriteSpec() to write a Spec
28+
// file to the file system.
29+
//
30+
// Transient Specs are those whose lifecycle is tied to that of some
31+
// external entity, for instance a container. vendor and class should
32+
// match the vendor and class of the CDI Spec. transientID should be
33+
// unique among all CDI users on the same host that might generate
34+
// transient Spec files using the same vendor/class combination. If
35+
// the external entity to which the lifecycle of the transient Spec
36+
// is tied to has a unique ID of its own, then this is usually a
37+
// good choice for transientID.
38+
//
39+
// The file name is generated without a ".json" or ".yaml" extension.
40+
// The caller can append the desired extension to choose a particular
41+
// encoding. Otherwise WriteSpec() will use its default encoding.
42+
func GenerateTransientSpecName(vendor, class, transientID string) string {
43+
transientID = strings.TrimSpace(strings.ReplaceAll(transientID, "/", "_"))
44+
base := GenerateSpecName(vendor, class)
45+
if transientID == "" {
46+
return base
47+
}
48+
return base + "_" + transientID
49+
}
50+
51+
// GenerateNameForSpec generates a name for the given Spec using
52+
// GenerateSpecName with the vendor and class taken from the Spec.
53+
// On success it returns the generated name and a nil error. If
54+
// the Spec does not contain a valid vendor or class, it returns
55+
// an empty name and a non-nil error.
56+
func GenerateNameForSpec(raw *cdi.Spec) (string, error) {
57+
return GenerateNameForTransientSpec(raw, "")
58+
}
59+
60+
// GenerateNameForTransientSpec generates a name for the given transient
61+
// Spec using GenerateTransientSpecName with the vendor and class taken
62+
// from the Spec. On success it returns the generated name and a nil error.
63+
// If the Spec does not contain a valid vendor or class, it returns an
64+
// an empty name and a non-nil error.
65+
func GenerateNameForTransientSpec(raw *cdi.Spec, transientID string) (string, error) {
66+
vendor, class := ParseKind(raw.Kind)
67+
if vendor == "" {
68+
return "", fmt.Errorf("invalid vendor/class %q in Spec", raw.Kind)
69+
}
70+
71+
return GenerateTransientSpecName(vendor, class, transientID), nil
72+
}

pkg/cdi/spec.go

+12-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"os"
2222
"path/filepath"
23-
"strings"
2423
"sync"
2524

2625
oci "github.com/opencontainers/runtime-spec/specs-go"
@@ -223,8 +222,10 @@ func validateSpec(raw *cdi.Spec) error {
223222
// This function always returns the same name for the same vendor/class
224223
// combination. Therefore it cannot be used as such to generate multiple
225224
// Spec file names for a single vendor and class.
225+
//
226+
// Deprecated: Use producer.GenerateSpecName instead
226227
func GenerateSpecName(vendor, class string) string {
227-
return vendor + "-" + class
228+
return producer.GenerateSpecName(vendor, class)
228229
}
229230

230231
// GenerateTransientSpecName generates a vendor+class scoped transient
@@ -243,35 +244,30 @@ func GenerateSpecName(vendor, class string) string {
243244
// The file name is generated without a ".json" or ".yaml" extension.
244245
// The caller can append the desired extension to choose a particular
245246
// encoding. Otherwise WriteSpec() will use its default encoding.
247+
//
248+
// Deprecated: Use producer.GenerateTransientSpecName instead
246249
func GenerateTransientSpecName(vendor, class, transientID string) string {
247-
transientID = strings.ReplaceAll(transientID, "/", "_")
248-
return GenerateSpecName(vendor, class) + "_" + transientID
250+
return producer.GenerateTransientSpecName(vendor, class, transientID)
249251
}
250252

251253
// GenerateNameForSpec generates a name for the given Spec using
252254
// GenerateSpecName with the vendor and class taken from the Spec.
253255
// On success it returns the generated name and a nil error. If
254256
// the Spec does not contain a valid vendor or class, it returns
255257
// an empty name and a non-nil error.
258+
//
259+
// Deprecated: Use producer.GenerateNameForSpec instead
256260
func GenerateNameForSpec(raw *cdi.Spec) (string, error) {
257-
vendor, class := parser.ParseQualifier(raw.Kind)
258-
if vendor == "" {
259-
return "", fmt.Errorf("invalid vendor/class %q in Spec", raw.Kind)
260-
}
261-
262-
return GenerateSpecName(vendor, class), nil
261+
return producer.GenerateNameForSpec(raw)
263262
}
264263

265264
// GenerateNameForTransientSpec generates a name for the given transient
266265
// Spec using GenerateTransientSpecName with the vendor and class taken
267266
// from the Spec. On success it returns the generated name and a nil error.
268267
// If the Spec does not contain a valid vendor or class, it returns an
269268
// an empty name and a non-nil error.
269+
//
270+
// Deprecated: Use producer.GenerateNameForTransientSpec instead
270271
func GenerateNameForTransientSpec(raw *cdi.Spec, transientID string) (string, error) {
271-
vendor, class := parser.ParseQualifier(raw.Kind)
272-
if vendor == "" {
273-
return "", fmt.Errorf("invalid vendor/class %q in Spec", raw.Kind)
274-
}
275-
276-
return GenerateTransientSpecName(vendor, class, transientID), nil
272+
return producer.GenerateNameForTransientSpec(raw, transientID)
277273
}

0 commit comments

Comments
 (0)