main.go 3.5 KB

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