main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. kc kiloclient.Interface
  60. port int
  61. }
  62. backend string
  63. granularity string
  64. kubeconfig string
  65. topologyLabel string
  66. )
  67. func runRoot(_ *cobra.Command, _ []string) error {
  68. if opts.port < 1 || opts.port > 1<<16-1 {
  69. return fmt.Errorf("invalid port: port mus be in range [%d:%d], but got %d", 1, 1<<16-1, opts.port)
  70. }
  71. opts.granularity = mesh.Granularity(granularity)
  72. switch opts.granularity {
  73. case mesh.LogicalGranularity:
  74. case mesh.FullGranularity:
  75. case mesh.AutoGranularity:
  76. default:
  77. return fmt.Errorf("mesh granularity %s unknown; posible values are: %s", granularity, availableGranularities)
  78. }
  79. switch backend {
  80. case k8s.Backend:
  81. config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
  82. if err != nil {
  83. return fmt.Errorf("failed to create Kubernetes config: %w", err)
  84. }
  85. c := kubernetes.NewForConfigOrDie(config)
  86. opts.kc = kiloclient.NewForConfigOrDie(config)
  87. ec := apiextensions.NewForConfigOrDie(config)
  88. opts.backend = k8s.New(c, opts.kc, ec, topologyLabel, log.NewNopLogger())
  89. default:
  90. return fmt.Errorf("backend %s unknown; posible values are: %s", backend, availableBackends)
  91. }
  92. if err := opts.backend.Nodes().Init(make(chan struct{})); err != nil {
  93. return fmt.Errorf("failed to initialize node backend: %w", err)
  94. }
  95. if err := opts.backend.Peers().Init(make(chan struct{})); err != nil {
  96. return fmt.Errorf("failed to initialize peer backend: %w", err)
  97. }
  98. return nil
  99. }
  100. func main() {
  101. cmd := &cobra.Command{
  102. Use: "kgctl",
  103. Short: "Manage a Kilo network",
  104. Long: "",
  105. PersistentPreRunE: runRoot,
  106. Version: version.Version,
  107. SilenceErrors: true,
  108. }
  109. cmd.PersistentFlags().StringVar(&backend, "backend", k8s.Backend, fmt.Sprintf("The backend for the mesh. Possible values: %s", availableBackends))
  110. cmd.PersistentFlags().StringVar(&granularity, "mesh-granularity", string(mesh.AutoGranularity), fmt.Sprintf("The granularity of the network mesh to create. Possible values: %s", availableGranularities))
  111. defaultKubeconfig := os.Getenv("KUBECONFIG")
  112. if _, err := os.Stat(defaultKubeconfig); os.IsNotExist(err) {
  113. defaultKubeconfig = filepath.Join(os.Getenv("HOME"), ".kube/config")
  114. }
  115. cmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", defaultKubeconfig, "Path to kubeconfig.")
  116. cmd.PersistentFlags().IntVar(&opts.port, "port", mesh.DefaultKiloPort, "The WireGuard port over which the nodes communicate.")
  117. cmd.PersistentFlags().StringVar(&topologyLabel, "topology-label", k8s.RegionLabelKey, "Kubernetes node label used to group nodes into logical locations.")
  118. for _, subCmd := range []*cobra.Command{
  119. graph(),
  120. showConf(),
  121. connect(),
  122. } {
  123. cmd.AddCommand(subCmd)
  124. }
  125. if err := cmd.Execute(); err != nil {
  126. fmt.Fprintf(os.Stderr, "%v\n", err)
  127. os.Exit(1)
  128. }
  129. }
  130. func determineGranularity(gr mesh.Granularity, ns []*mesh.Node) (mesh.Granularity, error) {
  131. if gr == mesh.AutoGranularity {
  132. if len(ns) == 0 {
  133. return gr, errors.New("could not get any nodes")
  134. }
  135. ret := mesh.Granularity(ns[0].Granularity)
  136. switch ret {
  137. case mesh.LogicalGranularity:
  138. case mesh.FullGranularity:
  139. default:
  140. return ret, fmt.Errorf("mesh granularity %s is not supported", opts.granularity)
  141. }
  142. return ret, nil
  143. }
  144. return gr, nil
  145. }