Skip to content

Commit

Permalink
Fixes containers#24032 : To log create and remove network event in po…
Browse files Browse the repository at this point in the history
…dman events

Signed-off-by: Sainath Sativar <Sativar.sainath@gmail.com>
  • Loading branch information
Sativarsainath-26 committed Oct 31, 2024
1 parent 2f6fca6 commit df88e97
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
12 changes: 12 additions & 0 deletions libpod/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ func (c *Container) newExecDiedEvent(sessionID string, exitCode int) {
}
}

// newNetworkEvent creates a new event based on a network create/remove
func (r *Runtime) NewNetworkEvent(status events.Status, netName, netID, netDriver string) {
e := events.NewEvent(status)
e.Network = netName
e.ID = netID
e.Driver = netDriver
e.Type = events.Network
if err := r.eventer.Write(e); err != nil {
logrus.Errorf("Unable to write network event: %q", err)
}
}

// newNetworkEvent creates a new event based on a network connect/disconnect
func (c *Container) newNetworkEvent(status events.Status, netName string) {
e := events.NewEvent(status)
Expand Down
2 changes: 2 additions & 0 deletions libpod/events/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type Event struct {
Name string `json:",omitempty"`
// Network is the network name in a network event
Network string `json:"network,omitempty"`
//Driver is the network driver in a network event
Driver string `json:",omitempty"`
// Status describes the event that occurred
Status Status
// Time the event occurred
Expand Down
6 changes: 5 additions & 1 deletion libpod/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ func (e *Event) ToHumanReadable(truncate bool) string {
}
humanFormat += ")"
case Network:
humanFormat = fmt.Sprintf("%s %s %s %s (container=%s, name=%s)", e.Time, e.Type, e.Status, id, id, e.Network)
if e.Status == Create || e.Status == Remove {
humanFormat = fmt.Sprintf("%s %s %s %s (name=%s, type=%s)", e.Time, e.Type, e.Status, e.ID, e.Network, e.Driver)
} else {
humanFormat = fmt.Sprintf("%s %s %s %s (container=%s, name=%s)", e.Time, e.Type, e.Status, id, id, e.Network)
}
case Image:
humanFormat = fmt.Sprintf("%s %s %s %s %s", e.Time, e.Type, e.Status, id, e.Name)
if e.Error != "" {
Expand Down
8 changes: 7 additions & 1 deletion pkg/domain/infra/abi/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/containers/common/libnetwork/types"
netutil "github.com/containers/common/libnetwork/util"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/libpod/events"
"github.com/containers/podman/v5/pkg/domain/entities"
)

Expand Down Expand Up @@ -127,7 +128,6 @@ func (ic *ContainerEngine) NetworkReload(ctx context.Context, names []string, op

func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, options entities.NetworkRmOptions) ([]*entities.NetworkRmReport, error) {
reports := make([]*entities.NetworkRmReport, 0, len(namesOrIds))

for _, name := range namesOrIds {
report := entities.NetworkRmReport{Name: name}
containers, err := ic.Libpod.GetAllContainers()
Expand Down Expand Up @@ -164,9 +164,14 @@ func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, o
}
}
}
net, err := ic.Libpod.Network().NetworkInspect(name)
if err != nil {
return reports, err
}
if err := ic.Libpod.Network().NetworkRemove(name); err != nil {
report.Err = err
}
ic.Libpod.NewNetworkEvent(events.Remove, net.Name, net.ID, net.Driver)
reports = append(reports, &report)
}
return reports, nil
Expand All @@ -180,6 +185,7 @@ func (ic *ContainerEngine) NetworkCreate(ctx context.Context, network types.Netw
if err != nil {
return nil, err
}
ic.Libpod.NewNetworkEvent(events.Create, network.Name, network.ID, network.Driver)
return &network, nil
}

Expand Down

0 comments on commit df88e97

Please sign in to comment.