cluster.go 5.8 KB

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