Skip to content

Commit

Permalink
Check for host validation before Create (#538)
Browse files Browse the repository at this point in the history
* Check for host validation before Create

* feedback
  • Loading branch information
DanG100 authored May 9, 2024
1 parent 489aaeb commit f22c301
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 2 additions & 3 deletions topo/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Implementation interface {
// BackToBackLoop returns a bool if the node supports links directly between
// two ports on the same node.
BackToBackLoop() bool
// ValidateConstraints validates the host with the node's constraints.
ValidateConstraints() error
}

// Certer provides an interface for working with certs on nodes.
Expand Down Expand Up @@ -235,9 +237,6 @@ func ToResourceRequirements(kv map[string]string) corev1.ResourceRequirements {
// Create will create the node in the k8s cluster with all services and config
// maps.
func (n *Impl) Create(ctx context.Context) error {
if err := n.ValidateConstraints(); err != nil {
return fmt.Errorf("node %s failed to validate node with errors: %s", n.Name(), err)
}
if err := n.CreatePod(ctx); err != nil {
return fmt.Errorf("node %s failed to create pod %w", n.Name(), err)
}
Expand Down
6 changes: 6 additions & 0 deletions topo/topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,12 @@ func (m *Manager) topologySpecs(ctx context.Context) ([]*topologyv1.Topology, er

// push deploys the topology to the cluster.
func (m *Manager) push(ctx context.Context) error {
for _, n := range m.nodes {
if err := n.ValidateConstraints(); err != nil {
return fmt.Errorf("failed to validate node %s: %w", n, err)
}
}

if _, err := m.kClient.CoreV1().Namespaces().Get(ctx, m.topo.Name, metav1.GetOptions{}); err != nil {
log.Infof("Creating namespace for topology: %q", m.topo.Name)
ns := &corev1.Namespace{
Expand Down
24 changes: 24 additions & 0 deletions topo/topo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func (c *configurable) ConfigPush(_ context.Context, r io.Reader) error {
return nil
}

func (c *configurable) ValidateConstraints() error {
if len(c.Proto.HostConstraints) != 0 {
return fmt.Errorf("fake error for host constraints")
}
return nil
}

func NewConfigurable(impl *node.Impl) (node.Node, error) {
return &configurable{Impl: impl}, nil
}
Expand Down Expand Up @@ -588,6 +595,23 @@ func TestCreate(t *testing.T) {
},
},
},
}, {
desc: "failed node validation",
topo: &tpb.Topology{
Name: "test",
Nodes: []*tpb.Node{{
Name: "r1",
Vendor: tpb.Vendor(1002),
Services: map[uint32]*tpb.Service{
1000: {
Name: "ssh",
},
},
Config: &tpb.Config{},
HostConstraints: []*tpb.HostConstraint{{Constraint: &tpb.HostConstraint_KernelConstraint{}}},
}},
},
wantErr: "failed to validate node",
}}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
Expand Down

0 comments on commit f22c301

Please sign in to comment.