cluster.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. Azure ClusterAuth = "azure-sp"
  20. Local ClusterAuth = "local"
  21. InCluster ClusterAuth = "in-cluster"
  22. )
  23. // Cluster is an integration that can connect to a Kubernetes cluster via
  24. // a specific auth mechanism
  25. type Cluster struct {
  26. gorm.Model
  27. // The auth mechanism that this cluster will use
  28. AuthMechanism ClusterAuth `json:"auth_mechanism"`
  29. // The project that this integration belongs to
  30. ProjectID uint `json:"project_id"`
  31. // Whether or not the Porter agent integration is enabled on the cluster
  32. AgentIntegrationEnabled bool
  33. // Name of the cluster
  34. Name string `json:"name"`
  35. // Server endpoint for the cluster
  36. Server string `json:"server"`
  37. // Additional fields optionally used by the kube client
  38. ClusterLocationOfOrigin string `json:"location_of_origin,omitempty"`
  39. TLSServerName string `json:"tls-server-name,omitempty"`
  40. InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify,omitempty"`
  41. ProxyURL string `json:"proxy-url,omitempty"`
  42. UserLocationOfOrigin string
  43. UserImpersonate string `json:"act-as,omitempty"`
  44. UserImpersonateGroups string `json:"act-as-groups,omitempty"`
  45. InfraID uint `json:"infra_id"`
  46. NotificationsDisabled bool `json:"notifications_disabled"`
  47. PreviewEnvsEnabled bool
  48. AWSClusterID string
  49. // ------------------------------------------------------------------
  50. // All fields below this line are encrypted before storage
  51. // ------------------------------------------------------------------
  52. // The various auth mechanisms available to the integration
  53. KubeIntegrationID uint
  54. OIDCIntegrationID uint
  55. GCPIntegrationID uint
  56. AWSIntegrationID uint
  57. DOIntegrationID uint
  58. AzureIntegrationID uint
  59. // A token cache that can be used by an auth mechanism, if desired
  60. TokenCache integrations.ClusterTokenCache `json:"token_cache" gorm:"-" sql:"-"`
  61. TokenCacheID uint `gorm:"token_cache_id"`
  62. // CertificateAuthorityData for the cluster, encrypted at rest
  63. CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"`
  64. // MonitorHelmReleases to trim down the number of revisions per release
  65. MonitorHelmReleases bool
  66. }
  67. // ToProjectType generates an external types.Project to be shared over REST
  68. func (c *Cluster) ToClusterType() *types.Cluster {
  69. serv := types.Kube
  70. if c.AWSIntegrationID != 0 {
  71. serv = types.EKS
  72. } else if c.GCPIntegrationID != 0 {
  73. serv = types.GKE
  74. } else if c.DOIntegrationID != 0 {
  75. serv = types.DOKS
  76. } else if c.AzureIntegrationID != 0 {
  77. serv = types.AKS
  78. }
  79. return &types.Cluster{
  80. ID: c.ID,
  81. ProjectID: c.ProjectID,
  82. Name: c.Name,
  83. Server: c.Server,
  84. Service: serv,
  85. AgentIntegrationEnabled: c.AgentIntegrationEnabled,
  86. InfraID: c.InfraID,
  87. AWSIntegrationID: c.AWSIntegrationID,
  88. AWSClusterID: c.AWSClusterID,
  89. PreviewEnvsEnabled: c.PreviewEnvsEnabled,
  90. }
  91. }
  92. // ClusterCandidate is a cluster integration that requires additional action
  93. // from the user to set up.
  94. type ClusterCandidate struct {
  95. gorm.Model
  96. // The auth mechanism that this candidate will parse for
  97. AuthMechanism ClusterAuth `json:"auth_mechanism"`
  98. // The project that this integration belongs to
  99. ProjectID uint `json:"project_id"`
  100. // CreatedClusterID is the ID of the cluster that's eventually
  101. // created
  102. CreatedClusterID uint `json:"created_cluster_id"`
  103. // Resolvers are the list of resolvers: once all resolvers are "resolved," the
  104. // cluster will be created
  105. Resolvers []ClusterResolver `json:"resolvers"`
  106. // Name of the cluster
  107. Name string `json:"name"`
  108. // Server endpoint for the cluster
  109. Server string `json:"server"`
  110. // Name of the context that this was created from, if it exists
  111. ContextName string `json:"context_name"`
  112. // ------------------------------------------------------------------
  113. // All fields below this line are encrypted before storage
  114. // ------------------------------------------------------------------
  115. // The best-guess for the AWSClusterID, which is required by aws auth mechanisms
  116. // See https://github.com/kubernetes-sigs/aws-iam-authenticator#what-is-a-cluster-id
  117. AWSClusterIDGuess []byte `json:"aws_cluster_id_guess"`
  118. // The raw kubeconfig
  119. Kubeconfig []byte `json:"kubeconfig"`
  120. }
  121. func (cc *ClusterCandidate) ToClusterCandidateType() *types.ClusterCandidate {
  122. resolvers := make([]types.ClusterResolver, 0)
  123. for _, resolver := range cc.Resolvers {
  124. resolvers = append(resolvers, *resolver.ToClusterResolverType())
  125. }
  126. return &types.ClusterCandidate{
  127. ID: cc.ID,
  128. ProjectID: cc.ProjectID,
  129. CreatedClusterID: cc.CreatedClusterID,
  130. Name: cc.Name,
  131. Server: cc.Server,
  132. ContextName: cc.ContextName,
  133. Resolvers: resolvers,
  134. AWSClusterIDGuess: string(cc.AWSClusterIDGuess),
  135. }
  136. }
  137. // ClusterResolver is an action that must be resolved to set up
  138. // a Cluster
  139. type ClusterResolver struct {
  140. gorm.Model
  141. // The ClusterCandidate that this is resolving
  142. ClusterCandidateID uint `json:"cluster_candidate_id"`
  143. // One of the ClusterResolverNames
  144. Name types.ClusterResolverName `json:"name"`
  145. // Resolved is true if this has been resolved, false otherwise
  146. Resolved bool `json:"resolved"`
  147. // Data is additional data for resolving the action, for example a file name,
  148. // context name, etc
  149. Data []byte `json:"data,omitempty"`
  150. }
  151. func (cr *ClusterResolver) ToClusterResolverType() *types.ClusterResolver {
  152. info := types.ClusterResolverInfos[cr.Name]
  153. data := make(types.ClusterResolverData)
  154. json.Unmarshal(cr.Data, &data)
  155. return &types.ClusterResolver{
  156. ID: cr.ID,
  157. ClusterCandidateID: cr.ClusterCandidateID,
  158. Name: cr.Name,
  159. Resolved: cr.Resolved,
  160. Docs: info.Docs,
  161. Fields: info.Fields,
  162. Data: data,
  163. }
  164. }