Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SVID check to agent health check #5298

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const (
)

type Agent struct {
c *Config
c *Config
sto storage.Storage
}

// Run the agent
Expand All @@ -56,7 +57,8 @@ func (a *Agent) Run(ctx context.Context) error {
return err
}

sto, err := storage.Open(a.c.DataDir)
var err error
a.sto, err = storage.Open(a.c.DataDir)
if err != nil {
return fmt.Errorf("failed to open storage: %w", err)
}
Expand Down Expand Up @@ -109,7 +111,7 @@ func (a *Agent) Run(ctx context.Context) error {
)

for {
as, err = a.attest(ctx, sto, cat, metrics, nodeAttestor)
as, err = a.attest(ctx, a.sto, cat, metrics, nodeAttestor)
if err == nil {
break
}
Expand All @@ -136,15 +138,15 @@ func (a *Agent) Run(ctx context.Context) error {
}
}
} else {
as, err = a.attest(ctx, sto, cat, metrics, nodeAttestor)
as, err = a.attest(ctx, a.sto, cat, metrics, nodeAttestor)
if err != nil {
return err
}
}

svidStoreCache := a.newSVIDStoreCache()

manager, err := a.newManager(ctx, sto, cat, metrics, as, svidStoreCache, nodeAttestor)
manager, err := a.newManager(ctx, a.sto, cat, metrics, as, svidStoreCache, nodeAttestor)
if err != nil {
return err
}
Expand Down Expand Up @@ -389,11 +391,14 @@ func (a *Agent) waitForTestDial(ctx context.Context) error {

// CheckHealth is used as a top-level health check for the agent.
func (a *Agent) CheckHealth() health.State {
err := a.checkWorkloadAPI()

// Both liveness and readiness checks are done by
// agents ability to create new Workload API client
// for the X509SVID service.
err := errors.Join(
a.checkWorkloadAPI(),
a.checkSVID(),
)

// Both liveness and readiness checks verify that:
// - the workload API endpoint is available
// - the agent has an SVID
// TODO: Better live check for agent.
return health.State{
Ready: err == nil,
Expand All @@ -407,6 +412,20 @@ func (a *Agent) CheckHealth() health.State {
}
}

func (a *Agent) checkSVID() error {
if a.sto == nil {
return errors.New("storage not initialized")
}
svid, _, err := a.sto.LoadSVID()
if err != nil {
return fmt.Errorf("loading SVID: %w", err)
}
if svid == nil {
return errors.New("SVID is nil")
}
return nil
}

func (a *Agent) checkWorkloadAPI() error {
clientOption, err := util.GetWorkloadAPIClientOption(a.c.BindAddress)
if err != nil {
Expand Down
Loading