cluster.go 6.2 KB

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