main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "os"
  18. "strings"
  19. "github.com/spf13/cobra"
  20. apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
  21. "k8s.io/client-go/kubernetes"
  22. "k8s.io/client-go/tools/clientcmd"
  23. "github.com/squat/kilo/pkg/k8s"
  24. kiloclient "github.com/squat/kilo/pkg/k8s/clientset/versioned"
  25. "github.com/squat/kilo/pkg/mesh"
  26. "github.com/squat/kilo/pkg/version"
  27. )
  28. const (
  29. logLevelAll = "all"
  30. logLevelDebug = "debug"
  31. logLevelInfo = "info"
  32. logLevelWarn = "warn"
  33. logLevelError = "error"
  34. logLevelNone = "none"
  35. )
  36. var (
  37. availableBackends = strings.Join([]string{
  38. k8s.Backend,
  39. }, ", ")
  40. availableGranularities = strings.Join([]string{
  41. string(mesh.LogicalGranularity),
  42. string(mesh.FullGranularity),
  43. }, ", ")
  44. availableLogLevels = strings.Join([]string{
  45. logLevelAll,
  46. logLevelDebug,
  47. logLevelInfo,
  48. logLevelWarn,
  49. logLevelError,
  50. logLevelNone,
  51. }, ", ")
  52. opts struct {
  53. backend mesh.Backend
  54. granularity mesh.Granularity
  55. port uint32
  56. }
  57. backend string
  58. granularity string
  59. kubeconfig string
  60. topologyLabel string
  61. )
  62. func runRoot(_ *cobra.Command, _ []string) error {
  63. opts.granularity = mesh.Granularity(granularity)
  64. switch opts.granularity {
  65. case mesh.LogicalGranularity:
  66. case mesh.FullGranularity:
  67. default:
  68. return fmt.Errorf("mesh granularity %v unknown; posible values are: %s", granularity, availableGranularities)
  69. }
  70. switch backend {
  71. case k8s.Backend:
  72. config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
  73. if err != nil {
  74. return fmt.Errorf("failed to create Kubernetes config: %v", err)
  75. }
  76. c := kubernetes.NewForConfigOrDie(config)
  77. kc := kiloclient.NewForConfigOrDie(config)
  78. ec := apiextensions.NewForConfigOrDie(config)
  79. opts.backend = k8s.New(c, kc, ec, topologyLabel)
  80. default:
  81. return fmt.Errorf("backend %v unknown; posible values are: %s", backend, availableBackends)
  82. }
  83. if err := opts.backend.Nodes().Init(make(chan struct{})); err != nil {
  84. return fmt.Errorf("failed to initialize node backend: %v", err)
  85. }
  86. if err := opts.backend.Peers().Init(make(chan struct{})); err != nil {
  87. return fmt.Errorf("failed to initialize peer backend: %v", err)
  88. }
  89. return nil
  90. }
  91. func main() {
  92. cmd := &cobra.Command{
  93. Use: "kgctl",
  94. Short: "Manage a Kilo network",
  95. Long: "",
  96. PersistentPreRunE: runRoot,
  97. Version: version.Version,
  98. }
  99. cmd.PersistentFlags().StringVar(&backend, "backend", k8s.Backend, fmt.Sprintf("The backend for the mesh. Possible values: %s", availableBackends))
  100. cmd.PersistentFlags().StringVar(&granularity, "mesh-granularity", string(mesh.LogicalGranularity), fmt.Sprintf("The granularity of the network mesh to create. Possible values: %s", availableGranularities))
  101. cmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), "Path to kubeconfig.")
  102. cmd.PersistentFlags().Uint32Var(&opts.port, "port", mesh.DefaultKiloPort, "The WireGuard port over which the nodes communicate.")
  103. cmd.PersistentFlags().StringVar(&topologyLabel, "topology-label", k8s.RegionLabelKey, "Kubernetes node label used to group nodes into logical locations.")
  104. for _, subCmd := range []*cobra.Command{
  105. graph(),
  106. showConf(),
  107. } {
  108. cmd.AddCommand(subCmd)
  109. }
  110. if err := cmd.Execute(); err != nil {
  111. fmt.Fprintf(os.Stderr, "%v\n", err)
  112. os.Exit(1)
  113. }
  114. }