Skip to content

Commit 3557d2e

Browse files
committed
pkg/cdi: implement default cache.
The default cache is always available and implicitly created the first time it is referenced. It is created with the default cache options but can then be reconfigured explicitly if necessary. Aso add package level variants of the most commonly used cache functions, Refresh(), InjectDevices, and GetErrors(). These all use the default cache. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 7b540ad commit 3557d2e

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

pkg/cdi/default-cache.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright © 2024 The CDI Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cdi
18+
19+
import (
20+
"sync"
21+
22+
oci "github.com/opencontainers/runtime-spec/specs-go"
23+
)
24+
25+
var (
26+
defaultCache *Cache
27+
getDefaultOnce sync.Once
28+
)
29+
30+
func getOrCreateDefaultCache(options ...Option) (*Cache, bool) {
31+
var created bool
32+
getDefaultOnce.Do(func() {
33+
defaultCache = newCache(options...)
34+
created = true
35+
})
36+
return defaultCache, created
37+
}
38+
39+
// GetDefaultCache returns the default CDI cache instance.
40+
func GetDefaultCache() *Cache {
41+
cache, _ := getOrCreateDefaultCache()
42+
return cache
43+
}
44+
45+
// Configure applies options to the default CDI cache. Updates and refreshes
46+
// the default cache if options are not empty.
47+
func Configure(options ...Option) {
48+
cache, created := getOrCreateDefaultCache(options...)
49+
if len(options) == 0 || created {
50+
return
51+
}
52+
cache.Configure(options...)
53+
}
54+
55+
// Refresh explicitly refreshes the default CDI cache instance.
56+
func Refresh() error {
57+
return GetDefaultCache().Refresh()
58+
}
59+
60+
// InjectDevices injects the given qualified devices to the given OCI Spec.
61+
// using the default CDI cache instance to resolve devices.
62+
func InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, error) {
63+
return GetDefaultCache().InjectDevices(ociSpec, devices...)
64+
}
65+
66+
// GetErrors returns all errors encountered during the last refresh of
67+
// the default CDI cache instance.
68+
func GetErrors() map[string][]error {
69+
return GetDefaultCache().GetErrors()
70+
}

pkg/cdi/doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
// the vast majority of CDI consumers need. The API should be usable both
3030
// by OCI runtime clients and runtime implementations.
3131
//
32+
// # Default CDI Cache
33+
//
34+
// There is a default CDI cache instance which is always implicitly
35+
// available and instantiated the first time it is referenced directly
36+
// or indirectly. The most frequently used cache functions are available
37+
// as identically named package level functions which operate on the
38+
// default cache instance.
39+
//
3240
// # CDI Registry
3341
//
3442
// The primary interface to interact with CDI devices is the Registry. It

0 commit comments

Comments
 (0)