main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "errors"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "github.com/go-kit/kit/log"
  22. "github.com/spf13/cobra"
  23. apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
  24. "k8s.io/client-go/kubernetes"
  25. "k8s.io/client-go/tools/clientcmd"
  26. "github.com/squat/kilo/pkg/k8s"
  27. kiloclient "github.com/squat/kilo/pkg/k8s/clientset/versioned"
  28. "github.com/squat/kilo/pkg/mesh"
  29. "github.com/squat/kilo/pkg/version"
  30. )
  31. const (
  32. logLevelAll = "all"
  33. logLevelDebug = "debug"
  34. logLevelInfo = "info"
  35. logLevelWarn = "warn"
  36. logLevelError = "error"
  37. logLevelNone = "none"
  38. )
  39. var (
  40. availableBackends = strings.Join([]string{
  41. k8s.Backend,
  42. }, ", ")
  43. availableGranularities = strings.Join([]string{
  44. string(mesh.LogicalGranularity),
  45. string(mesh.FullGranularity),
  46. string(mesh.AutoGranularity),
  47. }, ", ")
  48. availableLogLevels = strings.Join([]string{
  49. logLevelAll,
  50. logLevelDebug,
  51. logLevelInfo,
  52. logLevelWarn,
  53. logLevelError,
  54. logLevelNone,
  55. }, ", ")
  56. opts struct {
  57. backend mesh.Backend
  58. granularity mesh.Granularity
  59. port int
  60. }
  61. backend string
  62. granularity string
  63. kubeconfig string
  64. topologyLabel string
  65. )
  66. func runRoot(_ *cobra.Command, _ []string) error {
  67. if opts.port < 1 || opts.port > 1<<16-1 {
  68. return fmt.Errorf("invalid port: port mus be in range [%d:%d], but got %d", 1, 1<<16-1, opts.port)
  69. }
  70. opts.granularity = mesh.Granularity(granularity)
  71. switch opts.granularity {
  72. case mesh.LogicalGranularity:
  73. case mesh.FullGranularity:
  74. case mesh.AutoGranularity:
  75. default:
  76. return fmt.Errorf("mesh granularity %v unknown; posible values are: %s", granularity, availableGranularities)
  77. }
  78. switch backend {
  79. case k8s.Backend:
  80. config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
  81. if err != nil {
  82. return fmt.Errorf("failed to create Kubernetes config: %v", err)
  83. }
  84. c := kubernetes.NewForConfigOrDie(config)
  85. kc := kiloclient.NewForConfigOrDie(config)
  86. ec := apiextensions.NewForConfigOrDie(config)
  87. opts.backend = k8s.New(c, kc, ec, topologyLabel, log.NewNopLogger())
  88. default:
  89. return fmt.Errorf("backend %v unknown; posible values are: %s", backend, availableBackends)
  90. }
  91. if err := opts.backend.Nodes().Init(make(chan struct{})); err != nil {
  92. return fmt.Errorf("failed to initialize node backend: %v", err)
  93. }
  94. if err := opts.backend.Peers().Init(make(chan struct{})); err != nil {
  95. return fmt.Errorf("failed to initialize peer backend: %v", err)
  96. }
  97. return nil
  98. }
  99. func main() {
  100. cmd := &cobra.Command{
  101. Use: "kgctl",
  102. Short: "Manage a Kilo network",
  103. Long: "",
  104. PersistentPreRunE: runRoot,
  105. Version: version.Version,
  106. SilenceErrors: true,
  107. }
  108. cmd.PersistentFlags().StringVar(&backend, "backend", k8s.Backend, fmt.Sprintf("The backend for the mesh. Possible values: %s", availableBackends))
  109. cmd.PersistentFlags().StringVar(&granularity, "mesh-granularity", string(mesh.AutoGranularity), fmt.Sprintf("The granularity of the network mesh to create. Possible values: %s", availableGranularities))
  110. defaultKubeconfig := os.Getenv("KUBECONFIG")
  111. if _, err := os.Stat(defaultKubeconfig); os.IsNotExist(err) {
  112. defaultKubeconfig = filepath.Join(os.Getenv("HOME"), ".kube/config")
  113. }
  114. cmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", defaultKubeconfig, "Path to kubeconfig.")
  115. cmd.PersistentFlags().IntVar(&opts.port, "port", mesh.DefaultKiloPort, "The WireGuard port over which the nodes communicate.")
  116. cmd.PersistentFlags().StringVar(&topologyLabel, "topology-label", k8s.RegionLabelKey, "Kubernetes node label used to group nodes into logical locations.")
  117. for _, subCmd := range []*cobra.Command{
  118. graph(),
  119. showConf(),
  120. } {
  121. cmd.AddCommand(subCmd)
  122. }
  123. if err := cmd.Execute(); err != nil {
  124. fmt.Fprintf(os.Stderr, "%v\n", err)
  125. os.Exit(1)
  126. }
  127. }
  128. func optainGranularity(gr mesh.Granularity, ns []*mesh.Node) (mesh.Granularity, error) {
  129. if gr == mesh.AutoGranularity {
  130. if len(ns) == 0 {
  131. return gr, errors.New("could not get any nodes")
  132. }
  133. ret := mesh.Granularity(ns[0].Granularity)
  134. switch ret {
  135. case mesh.LogicalGranularity:
  136. case mesh.FullGranularity:
  137. default:
  138. return ret, fmt.Errorf("mesh granularity %v is not supported", opts.granularity)
  139. }
  140. return ret, nil
  141. }
  142. return gr, nil
  143. }