GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

portforward.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/justinbarrick/go-k8s-portforward"
  6. "github.com/pkg/errors"
  7. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  8. )
  9. // Attempt to create PortForwards to fluxes that match the label selectors until a Flux
  10. // is found or an error is returned.
  11. func tryPortforwards(ns string, selectors ...metav1.LabelSelector) (p *portforward.PortForward, err error) {
  12. message := fmt.Sprintf("No pod found in namespace %q using the following selectors:", ns)
  13. for _, selector := range selectors {
  14. p, err = tryPortforward(ns, selector)
  15. if err == nil {
  16. return
  17. }
  18. if !strings.Contains(err.Error(), "Could not find running pod for selector") {
  19. return
  20. } else {
  21. message = fmt.Sprintf("%s\n %s", message, metav1.FormatLabelSelector(&selector))
  22. }
  23. }
  24. message = fmt.Sprintf("%s\n\nMake sure Flux is running in namespace %q.\n"+
  25. "If Flux is running in another different namespace, please supply it to --k8s-fwd-ns.", message, ns)
  26. if err != nil {
  27. err = errors.New(message)
  28. }
  29. return
  30. }
  31. // Attempt to create a portforward in the namespace for the provided LabelSelector
  32. func tryPortforward(ns string, selector metav1.LabelSelector) (*portforward.PortForward, error) {
  33. portforwarder, err := portforward.NewPortForwarder(ns, selector, 3030)
  34. if err != nil {
  35. return portforwarder, err
  36. }
  37. err = portforwarder.Start()
  38. if err != nil {
  39. return portforwarder, err
  40. }
  41. return portforwarder, nil
  42. }