Skip to content

Commit a2d52a4

Browse files
committed
libcni: implement disableGC
This allows administrators to disable GC behavior. Signed-off-by: Casey Callendrello <[email protected]>
1 parent 35d1d0f commit a2d52a4

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

libcni/api.go

Lines changed: 6 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
}
@@ -764,6 +765,11 @@ func (c *CNIConfig) GetVersionInfo(ctx context.Context, pluginType string) (vers
764765
// - dump the list of cached attachments, and issue deletes as necessary
765766
// - issue a GC to the underlying plugins (if the version is high enough)
766767
func (c *CNIConfig) GCNetworkList(ctx context.Context, list *NetworkConfigList, args *GCArgs) error {
768+
// If DisableGC is set, then don't bother GCing at all.
769+
if list.DisableGC {
770+
return nil
771+
}
772+
767773
// First, get the list of cached attachments
768774
cachedAttachments, err := c.GetCachedAttachments("")
769775
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
})

0 commit comments

Comments
 (0)