Skip to content

Commit

Permalink
ebpf: create map's parent directory if it doesn't exist
Browse files Browse the repository at this point in the history
When dealing with a pinned map in the ebpf.OpenOrCreate method, make
sure the map's parent directory does exist (and try to create it in case
it doesn't).

Signed-off-by: Gilberto Bertin <gilberto@isovalent.com>
  • Loading branch information
jibi committed May 4, 2021
1 parent 3d18bb2 commit 320014e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/ebpf/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package ebpf
import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/bpf"
Expand Down Expand Up @@ -73,6 +75,20 @@ func (m *Map) OpenOrCreate() error {

path := bpf.MapPath(m.spec.Name)

if m.spec.Pinning == ciliumebpf.PinByName {
mapDir := filepath.Dir(path)

if _, err := os.Stat(mapDir); os.IsNotExist(err) {
if err = os.MkdirAll(mapDir, 0755); err != nil {
return &os.PathError{
Op: "Unable create map base directory",
Path: path,
Err: err,
}
}
}
}

newMap, err := ciliumebpf.NewMapWithOptions(m.spec, opts)
if err != nil {
if !errors.Is(err, ciliumebpf.ErrMapIncompatible) {
Expand Down

0 comments on commit 320014e

Please sign in to comment.