Skip to content

Agent Mesh

controlplane: name nodes whose roles fail in the unserved-egress warning

HosniBelfeki9125f6679946
diff --git a/internal/controlplane/egress_test.go b/internal/controlplane/egress_test.go
index 58dd2227..67831d2d 100644
--- a/internal/controlplane/egress_test.go
+++ b/internal/controlplane/egress_test.go
@@ -26,6 +26,7 @@ import (
"time"
"github.com/google/agentmesh/api"
+ "github.com/google/agentmesh/internal/storage"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/protobuf/proto"
@@ -242,7 +243,7 @@ func TestEgressPolicyIsDistributedToServingNodes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- unserved, err := srv.unservedEgress(ctx, &api.PolicyConfig{Roles: roles, Bindings: bindings, Egress: []*api.EgressDestination{
+ unserved, unresolved, err := srv.unservedEgress(ctx, &api.PolicyConfig{Roles: roles, Bindings: bindings, Egress: []*api.EgressDestination{
{Name: "api.github.com", ServedBy: []string{"pep"}},
{Name: "mam.internal.example.com", ServedBy: []string{"site=dc1"}},
{Name: "typo.example", ServedBy: []string{"site=dc-1"}},
@@ -258,6 +259,9 @@ func TestEgressPolicyIsDistributedToServingNodes(t *testing.T) {
if want := []string{"typo.example", "nobody.example"}; !slices.Equal(unservedNames, want) {
t.Errorf("unserved = %v, want %v", unservedNames, want)
}
+ if len(unresolved) != 0 {
+ t.Errorf("unresolved = %v, want none: every node here has roles that resolve", unresolved)
+ }
// Validation names the mistake.
for _, tc := range []struct{ name, body, want string }{
@@ -275,3 +279,52 @@ func TestEgressPolicyIsDistributedToServingNodes(t *testing.T) {
}
}
}
+
+// TestUnservedEgressNamesNodesWhoseRolesCannotBeResolved covers an admitted
+// node that a destination selects but whose roles cannot be resolved. GET
+// /egress refuses it its assignments, so the destination really is unserved;
+// what was missing is the reason. Without it the warning says the selector
+// matches no node, which sends the operator looking for a typo.
+func TestUnservedEgressNamesNodesWhoseRolesCannotBeResolved(t *testing.T) {
+ issuer, _ := startCustomMockOIDC(t)
+ srv, store, _ := setupTestServer(t, issuer)
+ t.Cleanup(func() {
+ _ = srv.Close()
+ _ = store.Close()
+ })
+ ctx := context.Background()
+
+ // The key and the biscuit only satisfy the schema: unservedEgress reads
+ // neither, only admission, roles and labels.
+ now := time.Now()
+ placeholder := []byte("unused")
+ for _, n := range []*storage.EnrolledNode{
+ {PeerID: "healthy", PublicKey: placeholder, Biscuit: placeholder, Role: api.RoleNode, EnrollmentType: "BOOTSTRAP",
+ Labels: map[string]string{"site": "dc1"}, EnrolledAt: now, ExpiresAt: now.Add(time.Hour)},
+ {PeerID: "unreadable-claims", PublicKey: placeholder, Biscuit: placeholder, Role: api.RoleNode, EnrollmentType: "OIDC", ClaimsJSON: "{not json",
+ Labels: map[string]string{"site": "dc9"}, EnrolledAt: now, ExpiresAt: now.Add(time.Hour)},
+ } {
+ if err := store.EnrollNode(ctx, n); err != nil {
+ t.Fatalf("EnrollNode(%s): %v", n.PeerID, err)
+ }
+ }
+
+ unserved, unresolved, err := srv.unservedEgress(ctx, &api.PolicyConfig{Egress: []*api.EgressDestination{
+ {Name: "dc1.example", ServedBy: []string{"site=dc1"}},
+ {Name: "dc9.example", ServedBy: []string{"site=dc9"}},
+ }})
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var unservedNames []string
+ for _, d := range unserved {
+ unservedNames = append(unservedNames, d.Name)
+ }
+ if want := []string{"dc9.example"}; !slices.Equal(unservedNames, want) {
+ t.Errorf("unserved = %v, want %v: a node refused its assignments serves nothing", unservedNames, want)
+ }
+ if len(unresolved) != 1 || unresolved[0].peerID != "unreadable-claims" || unresolved[0].err == nil {
+ t.Fatalf("unresolved = %v, want only unreadable-claims, with the error that excluded it", unresolved)
+ }
+}
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index 4322ae4f..26335a1c 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -1589,40 +1589,66 @@ func (s *Server) HandlePolicies(w http.ResponseWriter, r *http.Request) {
// valid in form and would otherwise fail silently, as a 404 to every caller.
// A warning and not an error: the node it selects may enroll later.
func (s *Server) warnUnservedEgress(ctx context.Context, policy *api.PolicyConfig) {
- unserved, err := s.unservedEgress(ctx, policy)
+ unserved, unresolved, err := s.unservedEgress(ctx, policy)
if err != nil {
logger.Warnf("mesh policy: cannot check egress selectors against enrolled nodes: %v", err)
return
}
+ // Named before the destinations: one that only this node would serve
+ // otherwise reads as a selector that matches nothing.
+ for _, n := range unresolved {
+ logger.Warnf("mesh policy: roles of node %s cannot be resolved, so it serves no egress destination: %v", n.peerID, n.err)
+ }
for _, d := range unserved {
logger.Warnf("mesh policy: egress destination %s is served by no enrolled node (served_by %v); callers get 404 until one matches", d.GetName(), d.GetServedBy())
}
}
+// unresolvedNode is an admitted node whose roles could not be resolved.
+type unresolvedNode struct {
+ peerID string
+ err error
+}
+
// unservedEgress returns the destinations of policy whose served_by selects
-// no enrolled, admitted node, with roles resolved as a refresh would.
-func (s *Server) unservedEgress(ctx context.Context, policy *api.PolicyConfig) ([]*api.EgressDestination, error) {
+// no enrolled, admitted node, with roles resolved as a refresh would. An
+// admitted node whose roles cannot be resolved is refused its assignments at
+// GET /egress, so it is not counted as serving anything either; it is
+// returned as unresolved so that the caller can say why.
+func (s *Server) unservedEgress(ctx context.Context, policy *api.PolicyConfig) ([]*api.EgressDestination, []unresolvedNode, error) {
if len(policy.GetEgress()) == 0 {
- return nil, nil
+ return nil, nil, nil
}
nodes, err := s.store.ListNodes(ctx)
if err != nil {
- return nil, err
+ return nil, nil, err
+ }
+ // Admission and roles belong to the node, not to the destination, so each
+ // node is settled once and an unresolved one is reported once.
+ type candidate struct {
+ roles []string
+ labels map[string]string
}
now := time.Now()
+ var candidates []candidate
+ var unresolved []unresolvedNode
+ for i := range nodes {
+ node := &nodes[i]
+ if node.CheckAdmission(now) != nil {
+ continue
+ }
+ roles, err := nodeRoles(node, policy.GetBindings())
+ if err != nil {
+ unresolved = append(unresolved, unresolvedNode{peerID: node.PeerID, err: err})
+ continue
+ }
+ candidates = append(candidates, candidate{roles: roles, labels: node.Labels})
+ }
var unserved []*api.EgressDestination
for _, d := range policy.GetEgress() {
served := false
- for i := range nodes {
- node := &nodes[i]
- if node.CheckAdmission(now) != nil {
- continue
- }
- roles, err := nodeRoles(node, policy.GetBindings())
- if err != nil {
- continue
- }
- if api.EgressServedBy(d, roles, node.Labels) {
+ for _, c := range candidates {
+ if api.EgressServedBy(d, c.roles, c.labels) {
served = true
break
}
@@ -1631,7 +1657,7 @@ func (s *Server) unservedEgress(ctx context.Context, policy *api.PolicyConfig) (
unserved = append(unserved, d)
}
}
- return unserved, nil
+ return unserved, unresolved, nil
}
// admittedNodeWithChallenge returns the enrolled, admitted node whose biscuit