Skip to content

Commit 73debca

Browse files
authored
Merge pull request #1090 from squeed/gc-improvements
Spec, libcni: add disableGC flag
2 parents cebd7df + c283314 commit 73debca

File tree

6 files changed

+55
-18
lines changed

6 files changed

+55
-18
lines changed

SPEC.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ A network configuration consists of a JSON object with the following keys:
111111
- `cniVersions` (string list): List of all CNI versions which this configuration supports. See [version selection](#version-selection) below.
112112
- `name` (string): Network name. This should be unique across all network configurations on a host (or other administrative domain). Must start with an alphanumeric character, optionally followed by any combination of one or more alphanumeric characters, underscore, dot (.) or hyphen (-).
113113
- `disableCheck` (boolean): Either `true` or `false`. If `disableCheck` is `true`, runtimes must not call `CHECK` for this network configuration list. This allows an administrator to prevent `CHECK`ing where a combination of plugins is known to return spurious errors.
114+
- `disableGC` (boolean): Either `true` or `false`. If `disableGC` is `true`, runtimes must not call `GC` for this network configuration list. This allows an administrator to prevent `GC`ing when it is known that garbage collection may have undesired effects (e.g. shared configuration between multiple runtimes).
114115
- `plugins` (list): A list of CNI plugins and their configuration, which is a list of plugin configuration objects.
115116

116117
#### Plugin configuration objects:

libcni/api.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type NetworkConfigList struct {
7676
Name string
7777
CNIVersion string
7878
DisableCheck bool
79+
DisableGC bool
7980
Plugins []*NetworkConfig
8081
Bytes []byte
8182
}
@@ -425,6 +426,9 @@ func (c *CNIConfig) GetCachedAttachments(containerID string) ([]*NetworkAttachme
425426
dirPath := filepath.Join(c.getCacheDir(&RuntimeConf{}), "results")
426427
entries, err := os.ReadDir(dirPath)
427428
if err != nil {
429+
if os.IsNotExist(err) {
430+
return nil, nil
431+
}
428432
return nil, err
429433
}
430434

@@ -759,6 +763,11 @@ func (c *CNIConfig) GetVersionInfo(ctx context.Context, pluginType string) (vers
759763
// - dump the list of cached attachments, and issue deletes as necessary
760764
// - issue a GC to the underlying plugins (if the version is high enough)
761765
func (c *CNIConfig) GCNetworkList(ctx context.Context, list *NetworkConfigList, args *GCArgs) error {
766+
// If DisableGC is set, then don't bother GCing at all.
767+
if list.DisableGC {
768+
return nil
769+
}
770+
762771
// First, get the list of cached attachments
763772
cachedAttachments, err := c.GetCachedAttachments("")
764773
if err != nil {

libcni/api_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,19 @@ var _ = Describe("Invoking plugins", func() {
15111511
_, err := cniConfig.AddNetworkList(ctx, netConfigList, runtimeConfig)
15121512
Expect(err).NotTo(HaveOccurred())
15131513

1514+
By("Issuing a GC with disableGC=true")
1515+
netConfigList.DisableGC = true
1516+
gcargs := &libcni.GCArgs{}
1517+
err = cniConfig.GCNetworkList(ctx, netConfigList, gcargs)
1518+
Expect(err).NotTo(HaveOccurred())
1519+
1520+
commands, err := noop_debug.ReadCommandLog(plugins[0].commandFilePath)
1521+
Expect(err).NotTo(HaveOccurred())
1522+
Expect(commands).To(HaveLen(1)) // ADD
1523+
15141524
By("Issuing a GC with valid networks")
1515-
gcargs := &libcni.GCArgs{
1525+
netConfigList.DisableGC = false
1526+
gcargs = &libcni.GCArgs{
15161527
ValidAttachments: []types.GCAttachment{{
15171528
ContainerID: runtimeConfig.ContainerID,
15181529
IfName: runtimeConfig.IfName,
@@ -1526,7 +1537,7 @@ var _ = Describe("Invoking plugins", func() {
15261537
err = cniConfig.GCNetworkList(ctx, netConfigList, gcargs)
15271538
Expect(err).NotTo(HaveOccurred())
15281539

1529-
commands, err := noop_debug.ReadCommandLog(plugins[0].commandFilePath)
1540+
commands, err = noop_debug.ReadCommandLog(plugins[0].commandFilePath)
15301541
Expect(err).NotTo(HaveOccurred())
15311542
Expect(commands).To(HaveLen(4))
15321543

libcni/conf.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,43 @@ func ConfListFromBytes(bytes []byte) (*NetworkConfigList, error) {
130130
}
131131
}
132132

133-
disableCheck := false
134-
if rawDisableCheck, ok := rawList["disableCheck"]; ok {
135-
disableCheck, ok = rawDisableCheck.(bool)
133+
readBool := func(key string) (bool, error) {
134+
rawVal, ok := rawList[key]
136135
if !ok {
137-
disableCheckStr, ok := rawDisableCheck.(string)
138-
if !ok {
139-
return nil, fmt.Errorf("error parsing configuration list: invalid disableCheck type %T", rawDisableCheck)
140-
}
141-
switch {
142-
case strings.ToLower(disableCheckStr) == "false":
143-
disableCheck = false
144-
case strings.ToLower(disableCheckStr) == "true":
145-
disableCheck = true
146-
default:
147-
return nil, fmt.Errorf("error parsing configuration list: invalid disableCheck value %q", disableCheckStr)
148-
}
136+
return false, nil
137+
}
138+
if b, ok := rawVal.(bool); ok {
139+
return b, nil
140+
}
141+
142+
s, ok := rawVal.(string)
143+
if !ok {
144+
return false, fmt.Errorf("error parsing configuration list: invalid type %T for %s", rawVal, key)
145+
}
146+
s = strings.ToLower(s)
147+
switch s {
148+
case "false":
149+
return false, nil
150+
case "true":
151+
return true, nil
149152
}
153+
return false, fmt.Errorf("error parsing configuration list: invalid value %q for %s", s, key)
154+
}
155+
156+
disableCheck, err := readBool("disableCheck")
157+
if err != nil {
158+
return nil, err
159+
}
160+
161+
disableGC, err := readBool("disableGC")
162+
if err != nil {
163+
return nil, err
150164
}
151165

152166
list := &NetworkConfigList{
153167
Name: name,
154168
DisableCheck: disableCheck,
169+
DisableGC: disableGC,
155170
CNIVersion: cniVersion,
156171
Bytes: bytes,
157172
}

libcni/conf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ var _ = Describe("Loading configuration from disk", func() {
386386
Expect(os.WriteFile(filepath.Join(configDir, "50-whatever.conflist"), configList, 0o600)).To(Succeed())
387387

388388
_, err := libcni.LoadConfList(configDir, "some-list")
389-
Expect(err).To(MatchError(fmt.Sprintf("error parsing configuration list: invalid disableCheck value \"%s\"", badValue)))
389+
Expect(err).To(MatchError(`error parsing configuration list: invalid value "adsfasdfasf" for disableCheck`))
390390
})
391391
})
392392
})

pkg/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ type NetConfList struct {
119119

120120
Name string `json:"name,omitempty"`
121121
DisableCheck bool `json:"disableCheck,omitempty"`
122+
DisableGC bool `json:"disableGC,omitempty"`
122123
Plugins []*NetConf `json:"plugins,omitempty"`
123124
}
124125

0 commit comments

Comments
 (0)