agent.go 23 KB

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