agent.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. package kubernetes
  2. import (
  3. "bufio"
  4. "bytes"
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "strings"
  10. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  11. "github.com/porter-dev/porter/internal/kubernetes/provisioner/aws"
  12. "github.com/porter-dev/porter/internal/kubernetes/provisioner/aws/ecr"
  13. "github.com/porter-dev/porter/internal/kubernetes/provisioner/aws/eks"
  14. "github.com/porter-dev/porter/internal/kubernetes/provisioner/do"
  15. "github.com/porter-dev/porter/internal/kubernetes/provisioner/do/docr"
  16. "github.com/porter-dev/porter/internal/kubernetes/provisioner/do/doks"
  17. "github.com/porter-dev/porter/internal/kubernetes/provisioner/gcp"
  18. "github.com/porter-dev/porter/internal/kubernetes/provisioner/gcp/gke"
  19. "github.com/porter-dev/porter/internal/models"
  20. "github.com/porter-dev/porter/internal/models/integrations"
  21. "github.com/porter-dev/porter/internal/oauth"
  22. "github.com/porter-dev/porter/internal/registry"
  23. "github.com/porter-dev/porter/internal/repository"
  24. "golang.org/x/oauth2"
  25. "github.com/gorilla/websocket"
  26. "github.com/porter-dev/porter/internal/helm/grapher"
  27. appsv1 "k8s.io/api/apps/v1"
  28. batchv1 "k8s.io/api/batch/v1"
  29. batchv1beta1 "k8s.io/api/batch/v1beta1"
  30. v1 "k8s.io/api/core/v1"
  31. v1beta1 "k8s.io/api/extensions/v1beta1"
  32. "k8s.io/apimachinery/pkg/api/errors"
  33. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  34. "k8s.io/apimachinery/pkg/types"
  35. "k8s.io/cli-runtime/pkg/genericclioptions"
  36. "k8s.io/client-go/informers"
  37. "k8s.io/client-go/kubernetes"
  38. "k8s.io/client-go/tools/cache"
  39. "github.com/porter-dev/porter/internal/config"
  40. )
  41. // Agent is a Kubernetes agent for performing operations that interact with the
  42. // api server
  43. type Agent struct {
  44. RESTClientGetter genericclioptions.RESTClientGetter
  45. Clientset kubernetes.Interface
  46. }
  47. type Message struct {
  48. EventType string `json:"event_type"`
  49. Object interface{}
  50. Kind string
  51. }
  52. type ListOptions struct {
  53. FieldSelector string
  54. }
  55. // CreateConfigMap creates the configmap given the key-value pairs and namespace
  56. func (a *Agent) CreateConfigMap(name string, namespace string, configMap map[string]string) (*v1.ConfigMap, error) {
  57. return a.Clientset.CoreV1().ConfigMaps(namespace).Create(
  58. context.TODO(),
  59. &v1.ConfigMap{
  60. ObjectMeta: metav1.ObjectMeta{
  61. Name: name,
  62. Namespace: namespace,
  63. Labels: map[string]string{
  64. "porter": "true",
  65. },
  66. },
  67. Data: configMap,
  68. },
  69. metav1.CreateOptions{},
  70. )
  71. }
  72. // CreateLinkedSecret creates a secret given the key-value pairs and namespace. Values are
  73. // base64 encoded
  74. func (a *Agent) CreateLinkedSecret(name, namespace, cmName string, data map[string][]byte) (*v1.Secret, error) {
  75. return a.Clientset.CoreV1().Secrets(namespace).Create(
  76. context.TODO(),
  77. &v1.Secret{
  78. ObjectMeta: metav1.ObjectMeta{
  79. Name: name,
  80. Namespace: namespace,
  81. Labels: map[string]string{
  82. "porter": "true",
  83. "configmap": cmName,
  84. },
  85. },
  86. Data: data,
  87. },
  88. metav1.CreateOptions{},
  89. )
  90. }
  91. type mergeConfigMapData struct {
  92. Data map[string]*string `json:"data"`
  93. }
  94. // UpdateConfigMap updates the configmap given its name and namespace
  95. func (a *Agent) UpdateConfigMap(name string, namespace string, configMap map[string]string) error {
  96. cmData := make(map[string]*string)
  97. for key, val := range configMap {
  98. valCopy := val
  99. cmData[key] = &valCopy
  100. if len(val) == 0 {
  101. cmData[key] = nil
  102. }
  103. }
  104. mergeCM := &mergeConfigMapData{
  105. Data: cmData,
  106. }
  107. patchBytes, err := json.Marshal(mergeCM)
  108. if err != nil {
  109. return err
  110. }
  111. _, err = a.Clientset.CoreV1().ConfigMaps(namespace).Patch(
  112. context.Background(),
  113. name,
  114. types.MergePatchType,
  115. patchBytes,
  116. metav1.PatchOptions{},
  117. )
  118. return err
  119. }
  120. type mergeLinkedSecretData struct {
  121. Data map[string]*[]byte `json:"data"`
  122. }
  123. // UpdateLinkedSecret updates the secret given its name and namespace
  124. func (a *Agent) UpdateLinkedSecret(name, namespace, cmName string, data map[string][]byte) error {
  125. secretData := make(map[string]*[]byte)
  126. for key, val := range data {
  127. valCopy := val
  128. secretData[key] = &valCopy
  129. if len(val) == 0 {
  130. secretData[key] = nil
  131. }
  132. }
  133. mergeSecret := &mergeLinkedSecretData{
  134. Data: secretData,
  135. }
  136. patchBytes, err := json.Marshal(mergeSecret)
  137. if err != nil {
  138. return err
  139. }
  140. _, err = a.Clientset.CoreV1().Secrets(namespace).Patch(
  141. context.TODO(),
  142. name,
  143. types.MergePatchType,
  144. patchBytes,
  145. metav1.PatchOptions{},
  146. )
  147. return err
  148. }
  149. // DeleteConfigMap deletes the configmap given its name and namespace
  150. func (a *Agent) DeleteConfigMap(name string, namespace string) error {
  151. return a.Clientset.CoreV1().ConfigMaps(namespace).Delete(
  152. context.TODO(),
  153. name,
  154. metav1.DeleteOptions{},
  155. )
  156. }
  157. // DeleteLinkedSecret deletes the secret given its name and namespace
  158. func (a *Agent) DeleteLinkedSecret(name, namespace string) error {
  159. return a.Clientset.CoreV1().Secrets(namespace).Delete(
  160. context.TODO(),
  161. name,
  162. metav1.DeleteOptions{},
  163. )
  164. }
  165. // GetConfigMap retrieves the configmap given its name and namespace
  166. func (a *Agent) GetConfigMap(name string, namespace string) (*v1.ConfigMap, error) {
  167. return a.Clientset.CoreV1().ConfigMaps(namespace).Get(
  168. context.TODO(),
  169. name,
  170. metav1.GetOptions{},
  171. )
  172. }
  173. // ListConfigMaps simply lists namespaces
  174. func (a *Agent) ListConfigMaps(namespace string) (*v1.ConfigMapList, error) {
  175. return a.Clientset.CoreV1().ConfigMaps(namespace).List(
  176. context.TODO(),
  177. metav1.ListOptions{
  178. LabelSelector: "porter=true",
  179. },
  180. )
  181. }
  182. // ListNamespaces simply lists namespaces
  183. func (a *Agent) ListNamespaces() (*v1.NamespaceList, error) {
  184. return a.Clientset.CoreV1().Namespaces().List(
  185. context.TODO(),
  186. metav1.ListOptions{},
  187. )
  188. }
  189. // ListJobsByLabel lists jobs in a namespace matching a label
  190. type Label struct {
  191. Key string
  192. Val string
  193. }
  194. func (a *Agent) ListJobsByLabel(namespace string, labels ...Label) ([]batchv1.Job, error) {
  195. selectors := make([]string, 0)
  196. for _, label := range labels {
  197. selectors = append(selectors, fmt.Sprintf("%s=%s", label.Key, label.Val))
  198. }
  199. resp, err := a.Clientset.BatchV1().Jobs(namespace).List(
  200. context.TODO(),
  201. metav1.ListOptions{
  202. LabelSelector: strings.Join(selectors, ","),
  203. },
  204. )
  205. if err != nil {
  206. return nil, err
  207. }
  208. return resp.Items, nil
  209. }
  210. // GetJobPods lists all pods belonging to a job in a namespace
  211. func (a *Agent) GetJobPods(namespace, jobName string) ([]v1.Pod, error) {
  212. resp, err := a.Clientset.CoreV1().Pods(namespace).List(
  213. context.TODO(),
  214. metav1.ListOptions{
  215. LabelSelector: fmt.Sprintf("%s=%s", "job-name", jobName),
  216. },
  217. )
  218. if err != nil {
  219. return nil, err
  220. }
  221. return resp.Items, nil
  222. }
  223. // GetIngress gets ingress given the name and namespace
  224. func (a *Agent) GetIngress(namespace string, name string) (*v1beta1.Ingress, error) {
  225. return a.Clientset.ExtensionsV1beta1().Ingresses(namespace).Get(
  226. context.TODO(),
  227. name,
  228. metav1.GetOptions{},
  229. )
  230. }
  231. // GetDeployment gets the deployment given the name and namespace
  232. func (a *Agent) GetDeployment(c grapher.Object) (*appsv1.Deployment, error) {
  233. return a.Clientset.AppsV1().Deployments(c.Namespace).Get(
  234. context.TODO(),
  235. c.Name,
  236. metav1.GetOptions{},
  237. )
  238. }
  239. // GetStatefulSet gets the statefulset given the name and namespace
  240. func (a *Agent) GetStatefulSet(c grapher.Object) (*appsv1.StatefulSet, error) {
  241. return a.Clientset.AppsV1().StatefulSets(c.Namespace).Get(
  242. context.TODO(),
  243. c.Name,
  244. metav1.GetOptions{},
  245. )
  246. }
  247. // GetReplicaSet gets the replicaset given the name and namespace
  248. func (a *Agent) GetReplicaSet(c grapher.Object) (*appsv1.ReplicaSet, error) {
  249. return a.Clientset.AppsV1().ReplicaSets(c.Namespace).Get(
  250. context.TODO(),
  251. c.Name,
  252. metav1.GetOptions{},
  253. )
  254. }
  255. // GetDaemonSet gets the daemonset by name and namespace
  256. func (a *Agent) GetDaemonSet(c grapher.Object) (*appsv1.DaemonSet, error) {
  257. return a.Clientset.AppsV1().DaemonSets(c.Namespace).Get(
  258. context.TODO(),
  259. c.Name,
  260. metav1.GetOptions{},
  261. )
  262. }
  263. // GetJob gets the job by name and namespace
  264. func (a *Agent) GetJob(c grapher.Object) (*batchv1.Job, error) {
  265. return a.Clientset.BatchV1().Jobs(c.Namespace).Get(
  266. context.TODO(),
  267. c.Name,
  268. metav1.GetOptions{},
  269. )
  270. }
  271. // GetCronJob gets the CronJob by name and namespace
  272. func (a *Agent) GetCronJob(c grapher.Object) (*batchv1beta1.CronJob, error) {
  273. return a.Clientset.BatchV1beta1().CronJobs(c.Namespace).Get(
  274. context.TODO(),
  275. c.Name,
  276. metav1.GetOptions{},
  277. )
  278. }
  279. // GetPodsByLabel retrieves pods with matching labels
  280. func (a *Agent) GetPodsByLabel(selector string) (*v1.PodList, error) {
  281. // Search in all namespaces for matching pods
  282. return a.Clientset.CoreV1().Pods("").List(
  283. context.TODO(),
  284. metav1.ListOptions{
  285. LabelSelector: selector,
  286. },
  287. )
  288. }
  289. // DeletePod deletes a pod by name and namespace
  290. func (a *Agent) DeletePod(namespace string, name string) error {
  291. return a.Clientset.CoreV1().Pods(namespace).Delete(
  292. context.TODO(),
  293. name,
  294. metav1.DeleteOptions{},
  295. )
  296. }
  297. // GetPodLogs streams real-time logs from a given pod.
  298. func (a *Agent) GetPodLogs(namespace string, name string, conn *websocket.Conn) error {
  299. tails := int64(400)
  300. // follow logs
  301. podLogOpts := v1.PodLogOptions{
  302. Follow: true,
  303. TailLines: &tails,
  304. }
  305. req := a.Clientset.CoreV1().Pods(namespace).GetLogs(name, &podLogOpts)
  306. podLogs, err := req.Stream(context.TODO())
  307. if err != nil {
  308. return fmt.Errorf("Cannot open log stream for pod %s", name)
  309. }
  310. defer podLogs.Close()
  311. r := bufio.NewReader(podLogs)
  312. errorchan := make(chan error)
  313. go func() {
  314. // listens for websocket closing handshake
  315. for {
  316. if _, _, err := conn.ReadMessage(); err != nil {
  317. defer conn.Close()
  318. errorchan <- nil
  319. return
  320. }
  321. }
  322. }()
  323. go func() {
  324. for {
  325. select {
  326. case <-errorchan:
  327. defer close(errorchan)
  328. return
  329. default:
  330. }
  331. bytes, err := r.ReadBytes('\n')
  332. if writeErr := conn.WriteMessage(websocket.TextMessage, bytes); writeErr != nil {
  333. errorchan <- writeErr
  334. return
  335. }
  336. if err != nil {
  337. if err != io.EOF {
  338. errorchan <- err
  339. return
  340. }
  341. errorchan <- nil
  342. return
  343. }
  344. }
  345. }()
  346. for {
  347. select {
  348. case err = <-errorchan:
  349. return err
  350. }
  351. }
  352. }
  353. // StreamControllerStatus streams controller status. Supports Deployment, StatefulSet, ReplicaSet, and DaemonSet
  354. // TODO: Support Jobs
  355. func (a *Agent) StreamControllerStatus(conn *websocket.Conn, kind string) error {
  356. factory := informers.NewSharedInformerFactory(
  357. a.Clientset,
  358. 0,
  359. )
  360. var informer cache.SharedInformer
  361. // Spins up an informer depending on kind. Convert to lowercase for robustness
  362. switch strings.ToLower(kind) {
  363. case "deployment":
  364. informer = factory.Apps().V1().Deployments().Informer()
  365. case "statefulset":
  366. informer = factory.Apps().V1().StatefulSets().Informer()
  367. case "replicaset":
  368. informer = factory.Apps().V1().ReplicaSets().Informer()
  369. case "daemonset":
  370. informer = factory.Apps().V1().DaemonSets().Informer()
  371. case "job":
  372. informer = factory.Batch().V1().Jobs().Informer()
  373. }
  374. stopper := make(chan struct{})
  375. errorchan := make(chan error)
  376. defer close(errorchan)
  377. informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
  378. UpdateFunc: func(oldObj, newObj interface{}) {
  379. msg := Message{
  380. EventType: "UPDATE",
  381. Object: newObj,
  382. Kind: strings.ToLower(kind),
  383. }
  384. if writeErr := conn.WriteJSON(msg); writeErr != nil {
  385. errorchan <- writeErr
  386. return
  387. }
  388. },
  389. AddFunc: func(obj interface{}) {
  390. msg := Message{
  391. EventType: "ADD",
  392. Object: obj,
  393. Kind: strings.ToLower(kind),
  394. }
  395. if writeErr := conn.WriteJSON(msg); writeErr != nil {
  396. errorchan <- writeErr
  397. return
  398. }
  399. },
  400. DeleteFunc: func(obj interface{}) {
  401. msg := Message{
  402. EventType: "DELETE",
  403. Object: obj,
  404. Kind: strings.ToLower(kind),
  405. }
  406. if writeErr := conn.WriteJSON(msg); writeErr != nil {
  407. errorchan <- writeErr
  408. return
  409. }
  410. },
  411. })
  412. go func() {
  413. // listens for websocket closing handshake
  414. for {
  415. if _, _, err := conn.ReadMessage(); err != nil {
  416. defer conn.Close()
  417. defer close(stopper)
  418. errorchan <- nil
  419. return
  420. }
  421. }
  422. }()
  423. go informer.Run(stopper)
  424. for {
  425. select {
  426. case err := <-errorchan:
  427. return err
  428. }
  429. }
  430. }
  431. // ProvisionECR spawns a new provisioning pod that creates an ECR instance
  432. func (a *Agent) ProvisionECR(
  433. projectID uint,
  434. awsConf *integrations.AWSIntegration,
  435. ecrName string,
  436. repo repository.Repository,
  437. infra *models.Infra,
  438. operation provisioner.ProvisionerOperation,
  439. pgConf *config.DBConf,
  440. redisConf *config.RedisConf,
  441. provImageTag string,
  442. ) (*batchv1.Job, error) {
  443. id := infra.GetUniqueName()
  444. prov := &provisioner.Conf{
  445. ID: id,
  446. Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
  447. Kind: provisioner.ECR,
  448. Operation: operation,
  449. Redis: redisConf,
  450. Postgres: pgConf,
  451. ProvisionerImageTag: provImageTag,
  452. LastApplied: infra.LastApplied,
  453. AWS: &aws.Conf{
  454. AWSRegion: awsConf.AWSRegion,
  455. AWSAccessKeyID: string(awsConf.AWSAccessKeyID),
  456. AWSSecretAccessKey: string(awsConf.AWSSecretAccessKey),
  457. },
  458. ECR: &ecr.Conf{
  459. ECRName: ecrName,
  460. },
  461. }
  462. return a.provision(prov, infra, repo)
  463. }
  464. // ProvisionEKS spawns a new provisioning pod that creates an EKS instance
  465. func (a *Agent) ProvisionEKS(
  466. projectID uint,
  467. awsConf *integrations.AWSIntegration,
  468. eksName, machineType string,
  469. repo repository.Repository,
  470. infra *models.Infra,
  471. operation provisioner.ProvisionerOperation,
  472. pgConf *config.DBConf,
  473. redisConf *config.RedisConf,
  474. provImageTag string,
  475. ) (*batchv1.Job, error) {
  476. id := infra.GetUniqueName()
  477. prov := &provisioner.Conf{
  478. ID: id,
  479. Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
  480. Kind: provisioner.EKS,
  481. Operation: operation,
  482. Redis: redisConf,
  483. Postgres: pgConf,
  484. ProvisionerImageTag: provImageTag,
  485. LastApplied: infra.LastApplied,
  486. AWS: &aws.Conf{
  487. AWSRegion: awsConf.AWSRegion,
  488. AWSAccessKeyID: string(awsConf.AWSAccessKeyID),
  489. AWSSecretAccessKey: string(awsConf.AWSSecretAccessKey),
  490. },
  491. EKS: &eks.Conf{
  492. ClusterName: eksName,
  493. MachineType: machineType,
  494. },
  495. }
  496. return a.provision(prov, infra, repo)
  497. }
  498. // ProvisionGCR spawns a new provisioning pod that creates a GCR instance
  499. func (a *Agent) ProvisionGCR(
  500. projectID uint,
  501. gcpConf *integrations.GCPIntegration,
  502. repo repository.Repository,
  503. infra *models.Infra,
  504. operation provisioner.ProvisionerOperation,
  505. pgConf *config.DBConf,
  506. redisConf *config.RedisConf,
  507. provImageTag string,
  508. ) (*batchv1.Job, error) {
  509. id := infra.GetUniqueName()
  510. prov := &provisioner.Conf{
  511. ID: id,
  512. Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
  513. Kind: provisioner.GCR,
  514. Operation: operation,
  515. Redis: redisConf,
  516. Postgres: pgConf,
  517. ProvisionerImageTag: provImageTag,
  518. LastApplied: infra.LastApplied,
  519. GCP: &gcp.Conf{
  520. GCPRegion: gcpConf.GCPRegion,
  521. GCPProjectID: gcpConf.GCPProjectID,
  522. GCPKeyData: string(gcpConf.GCPKeyData),
  523. },
  524. }
  525. return a.provision(prov, infra, repo)
  526. }
  527. // ProvisionGKE spawns a new provisioning pod that creates a GKE instance
  528. func (a *Agent) ProvisionGKE(
  529. projectID uint,
  530. gcpConf *integrations.GCPIntegration,
  531. gkeName string,
  532. repo repository.Repository,
  533. infra *models.Infra,
  534. operation provisioner.ProvisionerOperation,
  535. pgConf *config.DBConf,
  536. redisConf *config.RedisConf,
  537. provImageTag string,
  538. ) (*batchv1.Job, error) {
  539. id := infra.GetUniqueName()
  540. prov := &provisioner.Conf{
  541. ID: id,
  542. Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
  543. Kind: provisioner.GKE,
  544. Operation: operation,
  545. Redis: redisConf,
  546. Postgres: pgConf,
  547. ProvisionerImageTag: provImageTag,
  548. LastApplied: infra.LastApplied,
  549. GCP: &gcp.Conf{
  550. GCPRegion: gcpConf.GCPRegion,
  551. GCPProjectID: gcpConf.GCPProjectID,
  552. GCPKeyData: string(gcpConf.GCPKeyData),
  553. },
  554. GKE: &gke.Conf{
  555. ClusterName: gkeName,
  556. },
  557. }
  558. return a.provision(prov, infra, repo)
  559. }
  560. // ProvisionDOCR spawns a new provisioning pod that creates a DOCR instance
  561. func (a *Agent) ProvisionDOCR(
  562. projectID uint,
  563. doConf *integrations.OAuthIntegration,
  564. doAuth *oauth2.Config,
  565. repo repository.Repository,
  566. docrName, docrSubscriptionTier string,
  567. infra *models.Infra,
  568. operation provisioner.ProvisionerOperation,
  569. pgConf *config.DBConf,
  570. redisConf *config.RedisConf,
  571. provImageTag string,
  572. ) (*batchv1.Job, error) {
  573. // get the token
  574. oauthInt, err := repo.OAuthIntegration.ReadOAuthIntegration(
  575. infra.DOIntegrationID,
  576. )
  577. if err != nil {
  578. return nil, err
  579. }
  580. tok, _, err := oauth.GetAccessToken(oauthInt, doAuth, repo)
  581. if err != nil {
  582. return nil, err
  583. }
  584. id := infra.GetUniqueName()
  585. prov := &provisioner.Conf{
  586. ID: id,
  587. Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
  588. Kind: provisioner.DOCR,
  589. Operation: operation,
  590. Redis: redisConf,
  591. Postgres: pgConf,
  592. ProvisionerImageTag: provImageTag,
  593. LastApplied: infra.LastApplied,
  594. DO: &do.Conf{
  595. DOToken: tok,
  596. },
  597. DOCR: &docr.Conf{
  598. DOCRName: docrName,
  599. DOCRSubscriptionTier: docrSubscriptionTier,
  600. },
  601. }
  602. return a.provision(prov, infra, repo)
  603. }
  604. // ProvisionDOKS spawns a new provisioning pod that creates a DOKS instance
  605. func (a *Agent) ProvisionDOKS(
  606. projectID uint,
  607. doConf *integrations.OAuthIntegration,
  608. doAuth *oauth2.Config,
  609. repo repository.Repository,
  610. doRegion, doksClusterName string,
  611. infra *models.Infra,
  612. operation provisioner.ProvisionerOperation,
  613. pgConf *config.DBConf,
  614. redisConf *config.RedisConf,
  615. provImageTag string,
  616. ) (*batchv1.Job, error) {
  617. // get the token
  618. oauthInt, err := repo.OAuthIntegration.ReadOAuthIntegration(
  619. infra.DOIntegrationID,
  620. )
  621. if err != nil {
  622. return nil, err
  623. }
  624. tok, _, err := oauth.GetAccessToken(oauthInt, doAuth, repo)
  625. if err != nil {
  626. return nil, err
  627. }
  628. id := infra.GetUniqueName()
  629. prov := &provisioner.Conf{
  630. ID: id,
  631. Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
  632. Kind: provisioner.DOKS,
  633. Operation: operation,
  634. Redis: redisConf,
  635. Postgres: pgConf,
  636. LastApplied: infra.LastApplied,
  637. ProvisionerImageTag: provImageTag,
  638. DO: &do.Conf{
  639. DOToken: tok,
  640. },
  641. DOKS: &doks.Conf{
  642. DORegion: doRegion,
  643. DOKSClusterName: doksClusterName,
  644. },
  645. }
  646. return a.provision(prov, infra, repo)
  647. }
  648. // ProvisionTest spawns a new provisioning pod that tests provisioning
  649. func (a *Agent) ProvisionTest(
  650. projectID uint,
  651. infra *models.Infra,
  652. repo repository.Repository,
  653. operation provisioner.ProvisionerOperation,
  654. pgConf *config.DBConf,
  655. redisConf *config.RedisConf,
  656. provImageTag string,
  657. ) (*batchv1.Job, error) {
  658. id := infra.GetUniqueName()
  659. prov := &provisioner.Conf{
  660. ID: id,
  661. Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
  662. Operation: operation,
  663. Kind: provisioner.Test,
  664. Redis: redisConf,
  665. Postgres: pgConf,
  666. ProvisionerImageTag: provImageTag,
  667. }
  668. return a.provision(prov, infra, repo)
  669. }
  670. func (a *Agent) provision(
  671. prov *provisioner.Conf,
  672. infra *models.Infra,
  673. repo repository.Repository,
  674. ) (*batchv1.Job, error) {
  675. prov.Namespace = "default"
  676. job, err := prov.GetProvisionerJobTemplate()
  677. if err != nil {
  678. return nil, err
  679. }
  680. job, err = a.Clientset.BatchV1().Jobs(prov.Namespace).Create(
  681. context.TODO(),
  682. job,
  683. metav1.CreateOptions{},
  684. )
  685. if err != nil {
  686. return nil, err
  687. }
  688. infra.LastApplied = prov.LastApplied
  689. infra, err = repo.Infra.UpdateInfra(infra)
  690. if err != nil {
  691. return nil, err
  692. }
  693. return job, nil
  694. }
  695. // CreateImagePullSecrets will create the required image pull secrets and
  696. // return a map from the registry name to the name of the secret.
  697. func (a *Agent) CreateImagePullSecrets(
  698. repo repository.Repository,
  699. namespace string,
  700. linkedRegs map[string]*models.Registry,
  701. doAuth *oauth2.Config,
  702. ) (map[string]string, error) {
  703. res := make(map[string]string)
  704. for key, val := range linkedRegs {
  705. _reg := registry.Registry(*val)
  706. data, err := _reg.GetDockerConfigJSON(repo, doAuth)
  707. if err != nil {
  708. return nil, err
  709. }
  710. secretName := fmt.Sprintf("porter-%s-%d", val.Externalize().Service, val.ID)
  711. secret, err := a.Clientset.CoreV1().Secrets(namespace).Get(
  712. context.TODO(),
  713. secretName,
  714. metav1.GetOptions{},
  715. )
  716. // if not found, create the secret
  717. if err != nil && errors.IsNotFound(err) {
  718. _, err = a.Clientset.CoreV1().Secrets(namespace).Create(
  719. context.TODO(),
  720. &v1.Secret{
  721. ObjectMeta: metav1.ObjectMeta{
  722. Name: secretName,
  723. },
  724. Data: map[string][]byte{
  725. string(v1.DockerConfigJsonKey): data,
  726. },
  727. Type: v1.SecretTypeDockerConfigJson,
  728. },
  729. metav1.CreateOptions{},
  730. )
  731. if err != nil {
  732. return nil, err
  733. }
  734. // add secret name to the map
  735. res[key] = secretName
  736. continue
  737. } else if err != nil {
  738. return nil, err
  739. }
  740. // otherwise, check that the secret contains the correct data: if
  741. // if doesn't, update it
  742. if !bytes.Equal(secret.Data[v1.DockerConfigJsonKey], data) {
  743. _, err := a.Clientset.CoreV1().Secrets(namespace).Update(
  744. context.TODO(),
  745. &v1.Secret{
  746. ObjectMeta: metav1.ObjectMeta{
  747. Name: secretName,
  748. },
  749. Data: map[string][]byte{
  750. string(v1.DockerConfigJsonKey): data,
  751. },
  752. Type: v1.SecretTypeDockerConfigJson,
  753. },
  754. metav1.UpdateOptions{},
  755. )
  756. if err != nil {
  757. return nil, err
  758. }
  759. }
  760. // add secret name to the map
  761. res[key] = secretName
  762. }
  763. return res, nil
  764. }