candidate.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package forms
  2. import (
  3. "github.com/porter-dev/porter/internal/kubernetes"
  4. "github.com/porter-dev/porter/internal/models"
  5. )
  6. // CreateServiceAccountCandidatesForm represents the accepted values for
  7. // creating a list of ServiceAccountCandidates from a kubeconfig
  8. type CreateServiceAccountCandidatesForm 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. // ToServiceAccountCandidates creates a ServiceAccountCandidate from the kubeconfig and
  17. // project id
  18. func (csa *CreateServiceAccountCandidatesForm) ToServiceAccountCandidates(
  19. isServerLocal bool,
  20. ) ([]*models.ServiceAccountCandidate, error) {
  21. // can only use "local" auth mechanism if the server is running locally
  22. candidates, err := kubernetes.GetServiceAccountCandidates([]byte(csa.Kubeconfig), isServerLocal && csa.IsLocal)
  23. if err != nil {
  24. return nil, err
  25. }
  26. for _, saCandidate := range candidates {
  27. saCandidate.ProjectID = csa.ProjectID
  28. }
  29. return candidates, nil
  30. }