main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "path/filepath"
  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.LogicalGranularity),
  43. string(mesh.FullGranularity),
  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. port uint32
  57. }
  58. backend string
  59. granularity string
  60. kubeconfig string
  61. topologyLabel string
  62. )
  63. func runRoot(_ *cobra.Command, _ []string) error {
  64. opts.granularity = mesh.Granularity(granularity)
  65. switch opts.granularity {
  66. case mesh.LogicalGranularity:
  67. case mesh.FullGranularity:
  68. default:
  69. return fmt.Errorf("mesh granularity %v unknown; posible values are: %s", granularity, availableGranularities)
  70. }
  71. switch backend {
  72. case k8s.Backend:
  73. config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
  74. if err != nil {
  75. return fmt.Errorf("failed to create Kubernetes config: %v", err)
  76. }
  77. c := kubernetes.NewForConfigOrDie(config)
  78. kc := kiloclient.NewForConfigOrDie(config)
  79. ec := apiextensions.NewForConfigOrDie(config)
  80. opts.backend = k8s.New(c, kc, ec, topologyLabel)
  81. default:
  82. return fmt.Errorf("backend %v unknown; posible values are: %s", backend, availableBackends)
  83. }
  84. if err := opts.backend.Nodes().Init(make(chan struct{})); err != nil {
  85. return fmt.Errorf("failed to initialize node backend: %v", err)
  86. }
  87. if err := opts.backend.Peers().Init(make(chan struct{})); err != nil {
  88. return fmt.Errorf("failed to initialize peer backend: %v", err)
  89. }
  90. return nil
  91. }
  92. func main() {
  93. cmd := &cobra.Command{
  94. Use: "kgctl",
  95. Short: "Manage a Kilo network",
  96. Long: "",
  97. PersistentPreRunE: runRoot,
  98. Version: version.Version,
  99. }
  100. cmd.PersistentFlags().StringVar(&backend, "backend", k8s.Backend, fmt.Sprintf("The backend for the mesh. Possible values: %s", availableBackends))
  101. cmd.PersistentFlags().StringVar(&granularity, "mesh-granularity", string(mesh.LogicalGranularity), fmt.Sprintf("The granularity of the network mesh to create. Possible values: %s", availableGranularities))
  102. defaultKubeconfig := os.Getenv("KUBECONFIG")
  103. if _, err := os.Stat(defaultKubeconfig); os.IsNotExist(err) {
  104. defaultKubeconfig = filepath.Join(os.Getenv("HOME"), ".kube/config")
  105. }
  106. cmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", defaultKubeconfig, "Path to kubeconfig.")
  107. cmd.PersistentFlags().Uint32Var(&opts.port, "port", mesh.DefaultKiloPort, "The WireGuard port over which the nodes communicate.")
  108. cmd.PersistentFlags().StringVar(&topologyLabel, "topology-label", k8s.RegionLabelKey, "Kubernetes node label used to group nodes into logical locations.")
  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. }