candidate.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package forms
  2. import (
  3. "github.com/porter-dev/porter/internal/kubernetes"
  4. "github.com/porter-dev/porter/internal/models"
  5. )
  6. // CreateClusterCandidatesForm represents the accepted values for
  7. // creating a list of ClusterCandidates from a kubeconfig
  8. type CreateClusterCandidatesForm struct {
  9. ProjectID uint `json:"project_id"`
  10. Kubeconfig string `json:"kubeconfig"`
  11. // Represents whether the auth mechanism should be designated as
  12. // "local": if so, the auth mechanism uses local plugins/mechanisms purely from the
  13. // kubeconfig.
  14. IsLocal bool `json:"is_local"`
  15. }
  16. // ToClusterCandidates creates a ClusterCandidate from the kubeconfig and
  17. // project id
  18. func (csa *CreateClusterCandidatesForm) ToClusterCandidates(
  19. isServerLocal bool,
  20. ) ([]*models.ClusterCandidate, error) {
  21. candidates, err := kubernetes.GetClusterCandidatesFromKubeconfig(
  22. []byte(csa.Kubeconfig),
  23. csa.ProjectID,
  24. // can only use "local" auth mechanism if the server is running locally
  25. isServerLocal && csa.IsLocal,
  26. )
  27. if err != nil {
  28. return nil, err
  29. }
  30. for _, cc := range candidates {
  31. cc.ProjectID = csa.ProjectID
  32. }
  33. return candidates, nil
  34. }