main.go 3.8 KB

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