-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move spec name generation to producer
Signed-off-by: Evan Lezar <[email protected]>
- Loading branch information
Showing
2 changed files
with
84 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package producer | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
cdi "tags.cncf.io/container-device-interface/specs-go" | ||
) | ||
|
||
// GenerateSpecName generates a vendor+class scoped Spec file name. The | ||
// name can be passed to WriteSpec() to write a Spec file to the file | ||
// system. | ||
// | ||
// vendor and class should match the vendor and class of the CDI Spec. | ||
// The file name is generated without a ".json" or ".yaml" extension. | ||
// The caller can append the desired extension to choose a particular | ||
// encoding. Otherwise WriteSpec() will use its default encoding. | ||
// | ||
// This function always returns the same name for the same vendor/class | ||
// combination. Therefore it cannot be used as such to generate multiple | ||
// Spec file names for a single vendor and class. | ||
func GenerateSpecName(vendor, class string) string { | ||
return vendor + "-" + class | ||
} | ||
|
||
// GenerateTransientSpecName generates a vendor+class scoped transient | ||
// Spec file name. The name can be passed to WriteSpec() to write a Spec | ||
// file to the file system. | ||
// | ||
// Transient Specs are those whose lifecycle is tied to that of some | ||
// external entity, for instance a container. vendor and class should | ||
// match the vendor and class of the CDI Spec. transientID should be | ||
// unique among all CDI users on the same host that might generate | ||
// transient Spec files using the same vendor/class combination. If | ||
// the external entity to which the lifecycle of the transient Spec | ||
// is tied to has a unique ID of its own, then this is usually a | ||
// good choice for transientID. | ||
// | ||
// The file name is generated without a ".json" or ".yaml" extension. | ||
// The caller can append the desired extension to choose a particular | ||
// encoding. Otherwise WriteSpec() will use its default encoding. | ||
func GenerateTransientSpecName(vendor, class, transientID string) string { | ||
transientID = strings.TrimSpace(strings.ReplaceAll(transientID, "/", "_")) | ||
base := GenerateSpecName(vendor, class) | ||
if transientID == "" { | ||
return base | ||
} | ||
return base + "_" + transientID | ||
} | ||
|
||
// GenerateNameForSpec generates a name for the given Spec using | ||
// GenerateSpecName with the vendor and class taken from the Spec. | ||
// On success it returns the generated name and a nil error. If | ||
// the Spec does not contain a valid vendor or class, it returns | ||
// an empty name and a non-nil error. | ||
func GenerateNameForSpec(raw *cdi.Spec) (string, error) { | ||
return GenerateNameForTransientSpec(raw, "") | ||
} | ||
|
||
// GenerateNameForTransientSpec generates a name for the given transient | ||
// Spec using GenerateTransientSpecName with the vendor and class taken | ||
// from the Spec. On success it returns the generated name and a nil error. | ||
// If the Spec does not contain a valid vendor or class, it returns an | ||
// an empty name and a non-nil error. | ||
func GenerateNameForTransientSpec(raw *cdi.Spec, transientID string) (string, error) { | ||
vendor, class := ParseKind(raw.Kind) | ||
if vendor == "" { | ||
return "", fmt.Errorf("invalid vendor/class %q in Spec", raw.Kind) | ||
} | ||
|
||
return GenerateTransientSpecName(vendor, class, transientID), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters