-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add status subscription service
the shim ttrpc service now exposes a SubscribeStatus function which the manager subscribes to for receiving zeropod status updates, such as when checkpointing and restoring.
- Loading branch information
Showing
8 changed files
with
631 additions
and
68 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package manager | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/containerd/ttrpc" | ||
v1 "github.com/ctrox/zeropod/api/shim/v1" | ||
"github.com/ctrox/zeropod/runc/task" | ||
"github.com/fsnotify/fsnotify" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
) | ||
|
||
func StartSubscribers(ctx context.Context) error { | ||
socks, err := os.ReadDir(task.ShimSocketPath) | ||
if err != nil { | ||
return fmt.Errorf("error listing file in shim socket path: %s", err) | ||
} | ||
|
||
for _, sock := range socks { | ||
sock := sock | ||
go func() { | ||
if err := subscribe(ctx, filepath.Join(task.ShimSocketPath, sock.Name())); err != nil { | ||
slog.Error("error subscribing", "sock", sock.Name(), "err", err) | ||
} | ||
}() | ||
} | ||
|
||
go watchForShims(ctx) | ||
|
||
return nil | ||
} | ||
|
||
func subscribe(ctx context.Context, sock string) error { | ||
log := slog.With("sock", sock) | ||
log.Info("subscribing to status events") | ||
|
||
conn, err := net.Dial("unix", sock) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
shimClient := v1.NewShimClient(ttrpc.NewClient(conn)) | ||
// not sure why but the emptypb needs to be set in order for the subscribe | ||
// to be received | ||
client, err := shimClient.SubscribeStatus(ctx, &v1.SubscribeStatusRequest{Empty: &emptypb.Empty{}}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
status, err := client.Recv() | ||
if err != nil { | ||
if err == io.EOF || errors.Is(err, ttrpc.ErrClosed) { | ||
log.Info("subscribe closed") | ||
} else { | ||
log.Error("subscribe closed", "err", err) | ||
} | ||
break | ||
} | ||
slog.Info("received status", | ||
"container", status.Name, "pod", status.PodName, | ||
"namespace", status.PodNamespace, "phase", status.Phase) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func watchForShims(ctx context.Context) error { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return err | ||
} | ||
defer watcher.Close() | ||
|
||
if err := watcher.Add(task.ShimSocketPath); err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
select { | ||
case event := <-watcher.Events: | ||
switch event.Op { | ||
case fsnotify.Create: | ||
if err := subscribe(ctx, event.Name); err != nil { | ||
slog.Error("error subscribing", "sock", event.Name, "err", err) | ||
} | ||
} | ||
case err := <-watcher.Errors: | ||
slog.Error("watch error", "err", err) | ||
case <-ctx.Done(): | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.