cluster.go 6.0 KB

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