2
0

main.go 3.4 KB

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