Merge pull request #14108 from Luap99/machine-event-sock

machine events: only open sockets when needed
This commit is contained in:
OpenShift Merge Robot 2022-05-04 11:12:08 -04:00 committed by GitHub
commit 88e50d9d45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 28 deletions

View File

@ -26,7 +26,6 @@ var (
// Pull in configured json library // Pull in configured json library
json = registry.JSONLibrary() json = registry.JSONLibrary()
sockPaths []string // Paths to unix domain sockets for publishing
openEventSock sync.Once // Singleton support for opening sockets as needed openEventSock sync.Once // Singleton support for opening sockets as needed
sockets []net.Conn // Opened sockets, if any sockets []net.Conn // Opened sockets, if any
@ -35,7 +34,7 @@ var (
Use: "machine", Use: "machine",
Short: "Manage a virtual machine", Short: "Manage a virtual machine",
Long: "Manage a virtual machine. Virtual machines are used to run Podman.", Long: "Manage a virtual machine. Virtual machines are used to run Podman.",
PersistentPreRunE: initMachineEvents, PersistentPreRunE: validate.NoOp,
PersistentPostRunE: closeMachineEvents, PersistentPostRunE: closeMachineEvents,
RunE: validate.SubCommandExists, RunE: validate.SubCommandExists,
} }
@ -79,17 +78,10 @@ func getMachines(toComplete string) ([]string, cobra.ShellCompDirective) {
return suggestions, cobra.ShellCompDirectiveNoFileComp return suggestions, cobra.ShellCompDirectiveNoFileComp
} }
func initMachineEvents(cmd *cobra.Command, _ []string) error { func initMachineEvents() {
logrus.Debugf("Called machine %s.PersistentPreRunE(%s)", cmd.Name(), strings.Join(os.Args, " "))
sockPaths, err := resolveEventSock() sockPaths, err := resolveEventSock()
if err != nil { if err != nil {
return err logrus.Warnf("Failed to resolve machine event sockets, machine events will not be published: %v", err)
}
// No sockets found, so no need to publish events...
if len(sockPaths) == 0 {
return nil
} }
for _, path := range sockPaths { for _, path := range sockPaths {
@ -101,7 +93,6 @@ func initMachineEvents(cmd *cobra.Command, _ []string) error {
logrus.Debugf("Machine event socket %q found", path) logrus.Debugf("Machine event socket %q found", path)
sockets = append(sockets, conn) sockets = append(sockets, conn)
} }
return nil
} }
func resolveEventSock() ([]string, error) { func resolveEventSock() ([]string, error) {
@ -145,22 +136,7 @@ func resolveEventSock() ([]string, error) {
} }
func newMachineEvent(status events.Status, event events.Event) { func newMachineEvent(status events.Status, event events.Event) {
openEventSock.Do(func() { openEventSock.Do(initMachineEvents)
// No sockets where found, so no need to publish events...
if len(sockPaths) == 0 {
return
}
for _, path := range sockPaths {
conn, err := (&net.Dialer{}).DialContext(registry.Context(), "unix", path)
if err != nil {
logrus.Warnf("Failed to open event socket %q: %v", path, err)
continue
}
logrus.Debugf("Machine event socket %q found", path)
sockets = append(sockets, conn)
}
})
event.Status = status event.Status = status
event.Time = time.Now() event.Time = time.Now()