main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. )
  61. func runRoot(_ *cobra.Command, _ []string) error {
  62. opts.granularity = mesh.Granularity(granularity)
  63. switch opts.granularity {
  64. case mesh.LogicalGranularity:
  65. case mesh.FullGranularity:
  66. default:
  67. return fmt.Errorf("mesh granularity %v unknown; posible values are: %s", granularity, availableGranularities)
  68. }
  69. switch backend {
  70. case k8s.Backend:
  71. config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
  72. if err != nil {
  73. return fmt.Errorf("failed to create Kubernetes config: %v", err)
  74. }
  75. c := kubernetes.NewForConfigOrDie(config)
  76. kc := kiloclient.NewForConfigOrDie(config)
  77. ec := apiextensions.NewForConfigOrDie(config)
  78. opts.backend = k8s.New(c, kc, ec)
  79. default:
  80. return fmt.Errorf("backend %v unknown; posible values are: %s", backend, availableBackends)
  81. }
  82. if err := opts.backend.Nodes().Init(make(chan struct{})); err != nil {
  83. return fmt.Errorf("failed to initialize node backend: %v", err)
  84. }
  85. if err := opts.backend.Peers().Init(make(chan struct{})); err != nil {
  86. return fmt.Errorf("failed to initialize peer backend: %v", err)
  87. }
  88. return nil
  89. }
  90. func main() {
  91. cmd := &cobra.Command{
  92. Use: "kgctl",
  93. Short: "Manage a Kilo network",
  94. Long: "",
  95. PersistentPreRunE: runRoot,
  96. Version: version.Version,
  97. }
  98. cmd.PersistentFlags().StringVar(&backend, "backend", k8s.Backend, fmt.Sprintf("The backend for the mesh. Possible values: %s", availableBackends))
  99. cmd.PersistentFlags().StringVar(&granularity, "mesh-granularity", string(mesh.LogicalGranularity), fmt.Sprintf("The granularity of the network mesh to create. Possible values: %s", availableGranularities))
  100. cmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"), "Path to kubeconfig.")
  101. cmd.PersistentFlags().Uint32Var(&opts.port, "port", mesh.DefaultKiloPort, "The WireGuard port over which the nodes communicate.")
  102. for _, subCmd := range []*cobra.Command{
  103. graph(),
  104. showConf(),
  105. } {
  106. cmd.AddCommand(subCmd)
  107. }
  108. if err := cmd.Execute(); err != nil {
  109. fmt.Fprintf(os.Stderr, "%v\n", err)
  110. os.Exit(1)
  111. }
  112. }