cluster.go 5.6 KB

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