Skip to content
Draft
Show file tree
Hide file tree
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
78 changes: 78 additions & 0 deletions src/go/types/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,87 @@ func DecodeTopologyFromConfig(c store.Config) (ifaces.TopologySpec, error) {
return nil, fmt.Errorf("invalid spec in config")
}

// Process includeTopologies if this is a v1 topology
if v1Spec, ok := iface.(*v1.TopologySpec); ok {
if err := processIncludedTopologies(v1Spec); err != nil {
return nil, fmt.Errorf("processing included topologies: %w", err)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure if doing this here is the right way to do it


return spec, nil
}

func processIncludedTopologies(spec *v1.TopologySpec) error {
if len(spec.IncludeTopologiesF) == 0 {
return nil
}

var allErrors error

for _, path := range spec.IncludeTopologiesF {
// Check if it's an absolute path to a file
if filepath.IsAbs(path) {
if err := loadTopologyFromFile(spec, path); err != nil {
allErrors = fmt.Errorf("%w; loading topology from %s: %v", allErrors, path, err)
}
continue
}

// Otherwise, try to load from store by name
if err := loadTopologyFromStore(spec, path); err != nil {
allErrors = fmt.Errorf("%w; loading topology %s from store: %v", allErrors, path, err)
}
}

return allErrors
}

func loadTopologyFromFile(spec *v1.TopologySpec, path string) error {
c, err := store.NewConfigFromFile(path)
if err != nil {
return fmt.Errorf("loading config from file: %w", err)
}

var includedSpec struct {
Nodes []*v1.Node `mapstructure:"nodes"`
}

if err := mapstructure.Decode(c.Spec, &includedSpec); err != nil {
return fmt.Errorf("decoding topology spec: %w", err)
}

// Append nodes from included topology to this topology
spec.NodesF = append(spec.NodesF, includedSpec.Nodes...)

return nil
}

func loadTopologyFromStore(spec *v1.TopologySpec, name string) error {
c := &store.Config{
Kind: "Topology",
Metadata: store.ConfigMetadata{
Name: name,
},
}

if err := store.Get(c); err != nil {
return fmt.Errorf("getting topology from store: %w", err)
}

var includedSpec struct {
Nodes []*v1.Node `mapstructure:"nodes"`
}

if err := mapstructure.Decode(c.Spec, &includedSpec); err != nil {
return fmt.Errorf("decoding topology spec: %w", err)
}

// Append nodes from included topology to this topology
spec.NodesF = append(spec.NodesF, includedSpec.Nodes...)

return nil
}

type topology struct{}

func (topology) Upgrade(version string, spec map[string]interface{}, md store.ConfigMetadata) (interface{}, error) {
Expand Down
10 changes: 8 additions & 2 deletions src/go/types/version/v1/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,21 @@ components:
example: [email protected]
Topology:
type: object
required:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to require one of nodes or includeTopologies?

- nodes
properties:
nodes:
type: array
items:
oneOf:
- $ref: '#/components/schemas/minimega_node'
- $ref: '#/components/schemas/external_node'
includeTopologies:
type: array
items:
type: string
description: Array of topology file paths or store names to include
example:
- /phenix/topologies/base-topology.yml
- common-nodes
Scenario:
type: object
required:
Expand Down
11 changes: 10 additions & 1 deletion src/go/types/version/v1/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

type TopologySpec struct {
NodesF []*Node `json:"nodes" yaml:"nodes" structs:"nodes" mapstructure:"nodes"`
NodesF []*Node `json:"nodes" yaml:"nodes" structs:"nodes" mapstructure:"nodes"`
IncludeTopologiesF []string `json:"includeTopologies,omitempty" yaml:"includeTopologies,omitempty" structs:"includeTopologies,omitempty" mapstructure:"includeTopologies"`
}

func (this *TopologySpec) Nodes() []ifaces.NodeSpec {
Expand All @@ -25,6 +26,14 @@ func (this *TopologySpec) Nodes() []ifaces.NodeSpec {
return nodes
}

func (this *TopologySpec) IncludeTopologies() []string {
if this == nil {
return nil
}

return this.IncludeTopologiesF
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this function is useful. Should I add it to the interface (and return nil for v0 topology) ?


func (this *TopologySpec) BootableNodes() []ifaces.NodeSpec {
if this == nil {
return nil
Expand Down
10 changes: 8 additions & 2 deletions src/go/types/version/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,21 @@ components:
example: [email protected]
Topology:
type: object
required:
- nodes
properties:
nodes:
type: array
items:
oneOf:
- $ref: '#/components/schemas/minimega_node'
- $ref: '#/components/schemas/external_node'
includeTopologies:
type: array
items:
type: string
description: Array of topology file paths or store names to include
example:
- /phenix/topologies/base-topology.yml
- common-nodes
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have to add this to the v2 spec, even though there is no v2 topology.

Scenario:
type: object
nullable: true
Expand Down