cluster.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package models
  2. import (
  3. "encoding/json"
  4. "github.com/porter-dev/porter/api/types"
  5. "github.com/porter-dev/porter/internal/models/integrations"
  6. "gorm.io/gorm"
  7. )
  8. // ClusterAuth is an auth mechanism that a cluster candidate can resolve
  9. type ClusterAuth string
  10. // The support cluster candidate auth mechanisms
  11. const (
  12. X509 ClusterAuth = "x509"
  13. Basic ClusterAuth = "basic"
  14. Bearer ClusterAuth = "bearerToken"
  15. OIDC ClusterAuth = "oidc"
  16. GCP ClusterAuth = "gcp-sa"
  17. AWS ClusterAuth = "aws-sa"
  18. DO ClusterAuth = "do-oauth"
  19. Local ClusterAuth = "local"
  20. )
  21. // Cluster is an integration that can connect to a Kubernetes cluster via
  22. // a specific auth mechanism
  23. type Cluster struct {
  24. gorm.Model
  25. // The auth mechanism that this cluster will use
  26. AuthMechanism ClusterAuth `json:"auth_mechanism"`
  27. // The project that this integration belongs to
  28. ProjectID uint `json:"project_id"`
  29. // Name of the cluster
  30. Name string `json:"name"`
  31. // Server endpoint for the cluster
  32. Server string `json:"server"`
  33. // Additional fields optionally used by the kube client
  34. ClusterLocationOfOrigin string `json:"location_of_origin,omitempty"`
  35. TLSServerName string `json:"tls-server-name,omitempty"`
  36. InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify,omitempty"`
  37. ProxyURL string `json:"proxy-url,omitempty"`
  38. UserLocationOfOrigin string
  39. UserImpersonate string `json:"act-as,omitempty"`
  40. UserImpersonateGroups string `json:"act-as-groups,omitempty"`
  41. InfraID uint `json:"infra_id"`
  42. // ------------------------------------------------------------------
  43. // All fields below this line are encrypted before storage
  44. // ------------------------------------------------------------------
  45. // The various auth mechanisms available to the integration
  46. KubeIntegrationID uint
  47. OIDCIntegrationID uint
  48. GCPIntegrationID uint
  49. AWSIntegrationID uint
  50. DOIntegrationID uint
  51. // A token cache that can be used by an auth mechanism, if desired
  52. TokenCache integrations.ClusterTokenCache `json:"token_cache"`
  53. // CertificateAuthorityData for the cluster, encrypted at rest
  54. CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"`
  55. }
  56. // ToProjectType generates an external types.Project to be shared over REST
  57. func (c *Cluster) ToClusterType() *types.Cluster {
  58. serv := types.Kube
  59. if c.AWSIntegrationID != 0 {
  60. serv = types.EKS
  61. } else if c.GCPIntegrationID != 0 {
  62. serv = types.GKE
  63. } else if c.DOIntegrationID != 0 {
  64. serv = types.DOKS
  65. }
  66. return &types.Cluster{
  67. ID: c.ID,
  68. ProjectID: c.ProjectID,
  69. Name: c.Name,
  70. Server: c.Server,
  71. Service: serv,
  72. InfraID: c.InfraID,
  73. AWSIntegrationID: c.AWSIntegrationID,
  74. }
  75. }
  76. // ClusterCandidate is a cluster integration that requires additional action
  77. // from the user to set up.
  78. type ClusterCandidate struct {
  79. gorm.Model
  80. // The auth mechanism that this candidate will parse for
  81. AuthMechanism ClusterAuth `json:"auth_mechanism"`
  82. // The project that this integration belongs to
  83. ProjectID uint `json:"project_id"`
  84. // CreatedClusterID is the ID of the cluster that's eventually
  85. // created
  86. CreatedClusterID uint `json:"created_cluster_id"`
  87. // Resolvers are the list of resolvers: once all resolvers are "resolved," the
  88. // cluster will be created
  89. Resolvers []ClusterResolver `json:"resolvers"`
  90. // Name of the cluster
  91. Name string `json:"name"`
  92. // Server endpoint for the cluster
  93. Server string `json:"server"`
  94. // Name of the context that this was created from, if it exists
  95. ContextName string `json:"context_name"`
  96. // ------------------------------------------------------------------
  97. // All fields below this line are encrypted before storage
  98. // ------------------------------------------------------------------
  99. // The best-guess for the AWSClusterID, which is required by aws auth mechanisms
  100. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  101. AWSClusterIDGuess []byte `json:"aws_cluster_id_guess"`
  102. // The raw kubeconfig
  103. Kubeconfig []byte `json:"kubeconfig"`
  104. }
  105. func (cc *ClusterCandidate) ToClusterCandidateType() *types.ClusterCandidate {
  106. resolvers := make([]types.ClusterResolver, 0)
  107. for _, resolver := range cc.Resolvers {
  108. resolvers = append(resolvers, *resolver.ToClusterResolverType())
  109. }
  110. return &types.ClusterCandidate{
  111. ID: cc.ID,
  112. ProjectID: cc.ProjectID,
  113. CreatedClusterID: cc.CreatedClusterID,
  114. Name: cc.Name,
  115. Server: cc.Server,
  116. ContextName: cc.ContextName,
  117. Resolvers: resolvers,
  118. AWSClusterIDGuess: string(cc.AWSClusterIDGuess),
  119. }
  120. }
  121. // ClusterResolver is an action that must be resolved to set up
  122. // a Cluster
  123. type ClusterResolver struct {
  124. gorm.Model
  125. // The ClusterCandidate that this is resolving
  126. ClusterCandidateID uint `json:"cluster_candidate_id"`
  127. // One of the ClusterResolverNames
  128. Name types.ClusterResolverName `json:"name"`
  129. // Resolved is true if this has been resolved, false otherwise
  130. Resolved bool `json:"resolved"`
  131. // Data is additional data for resolving the action, for example a file name,
  132. // context name, etc
  133. Data []byte `json:"data,omitempty"`
  134. }
  135. func (cr *ClusterResolver) ToClusterResolverType() *types.ClusterResolver {
  136. info := types.ClusterResolverInfos[cr.Name]
  137. data := make(types.ClusterResolverData)
  138. json.Unmarshal(cr.Data, &data)
  139. return &types.ClusterResolver{
  140. ID: cr.ID,
  141. ClusterCandidateID: cr.ClusterCandidateID,
  142. Name: cr.Name,
  143. Resolved: cr.Resolved,
  144. Docs: info.Docs,
  145. Fields: info.Fields,
  146. Data: data,
  147. }
  148. }