agent.go 23 KB

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