graph.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "github.com/spf13/cobra"
  18. "github.com/squat/kilo/pkg/mesh"
  19. )
  20. func newGraph() *cobra.Command {
  21. return &cobra.Command{
  22. Use: "graph",
  23. Short: "Generates a graph of the Kilo network",
  24. Long: "",
  25. RunE: runGraph,
  26. }
  27. }
  28. func runGraph(_ *cobra.Command, _ []string) error {
  29. ns, err := opts.backend.List()
  30. if err != nil {
  31. return fmt.Errorf("failed to list nodes: %v", err)
  32. }
  33. var hostname string
  34. if len(ns) != 0 {
  35. hostname = ns[0].Name
  36. }
  37. nodes := make(map[string]*mesh.Node)
  38. for _, n := range ns {
  39. if n.Ready() {
  40. nodes[n.Name] = n
  41. }
  42. }
  43. t, err := mesh.NewTopology(nodes, opts.granularity, hostname, 0, []byte{}, opts.subnet)
  44. if err != nil {
  45. return fmt.Errorf("failed to create topology: %v", err)
  46. }
  47. g, err := t.Dot()
  48. if err != nil {
  49. return fmt.Errorf("failed to generate graph: %v", err)
  50. }
  51. fmt.Println(g)
  52. return nil
  53. }