main.go 4.6 KB

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