Skip to content

Commit 09b6255

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

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"
@@ -230,8 +229,10 @@ func validateSpec(raw *cdi.Spec) error {
230229
// This function always returns the same name for the same vendor/class
231230
// combination. Therefore it cannot be used as such to generate multiple
232231
// Spec file names for a single vendor and class.
232+
//
233+
// Deprecated: Use producer.GenerateSpecName instead
233234
func GenerateSpecName(vendor, class string) string {
234-
return vendor + "-" + class
235+
return producer.GenerateSpecName(vendor, class)
235236
}
236237

237238
// GenerateTransientSpecName generates a vendor+class scoped transient
@@ -250,35 +251,30 @@ func GenerateSpecName(vendor, class string) string {
250251
// The file name is generated without a ".json" or ".yaml" extension.
251252
// The caller can append the desired extension to choose a particular
252253
// encoding. Otherwise WriteSpec() will use its default encoding.
254+
//
255+
// Deprecated: Use producer.GenerateTransientSpecName instead
253256
func GenerateTransientSpecName(vendor, class, transientID string) string {
254-
transientID = strings.ReplaceAll(transientID, "/", "_")
255-
return GenerateSpecName(vendor, class) + "_" + transientID
257+
return producer.GenerateTransientSpecName(vendor, class, transientID)
256258
}
257259

258260
// GenerateNameForSpec generates a name for the given Spec using
259261
// GenerateSpecName with the vendor and class taken from the Spec.
260262
// On success it returns the generated name and a nil error. If
261263
// the Spec does not contain a valid vendor or class, it returns
262264
// an empty name and a non-nil error.
265+
//
266+
// Deprecated: Use producer.GenerateNameForSpec instead
263267
func GenerateNameForSpec(raw *cdi.Spec) (string, error) {
264-
vendor, class := parser.ParseQualifier(raw.Kind)
265-
if vendor == "" {
266-
return "", fmt.Errorf("invalid vendor/class %q in Spec", raw.Kind)
267-
}
268-
269-
return GenerateSpecName(vendor, class), nil
268+
return producer.GenerateNameForSpec(raw)
270269
}
271270

272271
// GenerateNameForTransientSpec generates a name for the given transient
273272
// Spec using GenerateTransientSpecName with the vendor and class taken
274273
// from the Spec. On success it returns the generated name and a nil error.
275274
// If the Spec does not contain a valid vendor or class, it returns an
276275
// an empty name and a non-nil error.
276+
//
277+
// Deprecated: Use producer.GenerateNameForTransientSpec instead
277278
func GenerateNameForTransientSpec(raw *cdi.Spec, transientID string) (string, error) {
278-
vendor, class := parser.ParseQualifier(raw.Kind)
279-
if vendor == "" {
280-
return "", fmt.Errorf("invalid vendor/class %q in Spec", raw.Kind)
281-
}
282-
283-
return GenerateTransientSpecName(vendor, class, transientID), nil
279+
return producer.GenerateNameForTransientSpec(raw, transientID)
284280
}

0 commit comments

Comments
 (0)