cluster.go 5.9 KB

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