app.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "os"
  9. "strings"
  10. "time"
  11. "github.com/fatih/color"
  12. api "github.com/porter-dev/porter/api/client"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/cli/cmd/utils"
  15. "github.com/spf13/cobra"
  16. batchv1 "k8s.io/api/batch/v1"
  17. v1 "k8s.io/api/core/v1"
  18. rbacv1 "k8s.io/api/rbac/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/fields"
  21. "k8s.io/apimachinery/pkg/watch"
  22. "k8s.io/kubectl/pkg/util/term"
  23. templaterUtils "github.com/porter-dev/porter/internal/templater/utils"
  24. "k8s.io/apimachinery/pkg/runtime"
  25. "k8s.io/apimachinery/pkg/runtime/schema"
  26. "k8s.io/client-go/kubernetes"
  27. "k8s.io/client-go/rest"
  28. "k8s.io/client-go/tools/clientcmd"
  29. "k8s.io/client-go/tools/remotecommand"
  30. )
  31. var (
  32. appNamespace string
  33. appVerbose bool
  34. appExistingPod bool
  35. appNonInteractive bool
  36. appContainerName string
  37. appTag string
  38. )
  39. // appCmd represents the "porter app" base command when called
  40. // without any subcommands
  41. var appCmd = &cobra.Command{
  42. Use: "app",
  43. Short: "Runs a command for your application.",
  44. }
  45. // appRunCmd represents the "porter app run" subcommand
  46. var appRunCmd = &cobra.Command{
  47. Use: "run [application] -- COMMAND [args...]",
  48. Args: cobra.MinimumNArgs(2),
  49. Short: "Runs a command inside a connected cluster container.",
  50. Run: func(cmd *cobra.Command, args []string) {
  51. err := checkLoginAndRun(args, appRun)
  52. if err != nil {
  53. os.Exit(1)
  54. }
  55. },
  56. }
  57. // appRunCleanupCmd represents the "porter app run cleanup" subcommand
  58. var appRunCleanupCmd = &cobra.Command{
  59. Use: "cleanup",
  60. Args: cobra.NoArgs,
  61. Short: "Delete any lingering ephemeral pods that were created with \"porter app run\".",
  62. Run: func(cmd *cobra.Command, args []string) {
  63. err := checkLoginAndRun(args, appCleanup)
  64. if err != nil {
  65. os.Exit(1)
  66. }
  67. },
  68. }
  69. // appUpdateTagCmd represents the "porter app update-tag" subcommand
  70. var appUpdateTagCmd = &cobra.Command{
  71. Use: "update-tag [application]",
  72. Args: cobra.MinimumNArgs(1),
  73. Short: "Updates the image tag for an application.",
  74. Run: func(cmd *cobra.Command, args []string) {
  75. err := checkLoginAndRun(args, appUpdateUpgrade)
  76. if err != nil {
  77. os.Exit(1)
  78. }
  79. },
  80. }
  81. func init() {
  82. rootCmd.AddCommand(appCmd)
  83. appRunCmd.PersistentFlags().BoolVarP(
  84. &appExistingPod,
  85. "existing_pod",
  86. "e",
  87. false,
  88. "whether to connect to an existing pod",
  89. )
  90. appRunCmd.PersistentFlags().BoolVarP(
  91. &appVerbose,
  92. "verbose",
  93. "v",
  94. false,
  95. "whether to print verbose output",
  96. )
  97. appRunCmd.PersistentFlags().BoolVar(
  98. &appNonInteractive,
  99. "non-interactive",
  100. true,
  101. "whether to run in non-interactive mode",
  102. )
  103. appRunCmd.PersistentFlags().StringVarP(
  104. &appContainerName,
  105. "container",
  106. "c",
  107. "",
  108. "name of the container inside pod to run the command in",
  109. )
  110. appRunCmd.AddCommand(appRunCleanupCmd)
  111. appUpdateTagCmd.PersistentFlags().StringVarP(
  112. &appTag,
  113. "tag",
  114. "t",
  115. "",
  116. "the specified tag to use, default is \"latest\"",
  117. )
  118. appCmd.AddCommand(appRunCmd)
  119. appCmd.AddCommand(appUpdateTagCmd)
  120. }
  121. func appRun(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  122. execArgs := args[1:]
  123. color.New(color.FgGreen).Println("Running", strings.Join(execArgs, " "), "for application", args[0])
  124. appNamespace = fmt.Sprintf("porter-stack-%s", args[0])
  125. if len(execArgs) > 0 {
  126. release, err := client.GetRelease(
  127. context.Background(), cliConf.Project, cliConf.Cluster, appNamespace, args[0],
  128. )
  129. if err != nil {
  130. return fmt.Errorf("error fetching release %s: %w", args[0], err)
  131. }
  132. if release.BuildConfig != nil &&
  133. (strings.Contains(release.BuildConfig.Builder, "heroku") ||
  134. strings.Contains(release.BuildConfig.Builder, "paketo")) &&
  135. execArgs[0] != "/cnb/lifecycle/launcher" &&
  136. execArgs[0] != "launcher" {
  137. // this is a buildpacks release using a heroku builder, prepend the launcher
  138. execArgs = append([]string{"/cnb/lifecycle/launcher"}, execArgs...)
  139. }
  140. }
  141. podsSimple, err := appGetPods(client, appNamespace, args[0])
  142. if err != nil {
  143. return fmt.Errorf("Could not retrieve list of pods: %s", err.Error())
  144. }
  145. // if length of pods is 0, throw error
  146. var selectedPod appPodSimple
  147. if len(podsSimple) == 0 {
  148. return fmt.Errorf("At least one pod must exist in this deployment.")
  149. } else if appNonInteractive || len(podsSimple) == 1 {
  150. selectedPod = podsSimple[0]
  151. } else {
  152. podNames := make([]string, 0)
  153. for _, podSimple := range podsSimple {
  154. podNames = append(podNames, podSimple.Name)
  155. }
  156. selectedPodName, err := utils.PromptSelect("Select the pod:", podNames)
  157. if err != nil {
  158. return err
  159. }
  160. // find selected pod
  161. for _, podSimple := range podsSimple {
  162. if selectedPodName == podSimple.Name {
  163. selectedPod = podSimple
  164. }
  165. }
  166. }
  167. var selectedContainerName string
  168. if len(selectedPod.ContainerNames) == 0 {
  169. return fmt.Errorf("At least one container must exist in the selected pod.")
  170. } else if len(selectedPod.ContainerNames) == 1 {
  171. if appContainerName != "" && appContainerName != selectedPod.ContainerNames[0] {
  172. return fmt.Errorf("provided container %s does not exist in pod %s", appContainerName, selectedPod.Name)
  173. }
  174. selectedContainerName = selectedPod.ContainerNames[0]
  175. }
  176. if appContainerName != "" && selectedContainerName == "" {
  177. // check if provided container name exists in the pod
  178. for _, name := range selectedPod.ContainerNames {
  179. if name == appContainerName {
  180. selectedContainerName = name
  181. break
  182. }
  183. }
  184. if selectedContainerName == "" {
  185. return fmt.Errorf("provided container %s does not exist in pod %s", appContainerName, selectedPod.Name)
  186. }
  187. }
  188. if selectedContainerName == "" {
  189. if appNonInteractive {
  190. return fmt.Errorf("container name must be specified using the --container flag when using non-interactive mode")
  191. }
  192. selectedContainer, err := utils.PromptSelect("Select the container:", selectedPod.ContainerNames)
  193. if err != nil {
  194. return err
  195. }
  196. selectedContainerName = selectedContainer
  197. }
  198. config := &AppPorterRunSharedConfig{
  199. Client: client,
  200. }
  201. err = config.setSharedConfig()
  202. if err != nil {
  203. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  204. }
  205. if appExistingPod {
  206. return appExecuteRun(config, appNamespace, selectedPod.Name, selectedContainerName, execArgs)
  207. }
  208. return appExecuteRunEphemeral(config, appNamespace, selectedPod.Name, selectedContainerName, execArgs)
  209. }
  210. func appCleanup(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  211. config := &AppPorterRunSharedConfig{
  212. Client: client,
  213. }
  214. err := config.setSharedConfig()
  215. if err != nil {
  216. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  217. }
  218. proceed, err := utils.PromptSelect(
  219. fmt.Sprintf("You have chosen the '%s' namespace for cleanup. Do you want to proceed?", appNamespace),
  220. []string{"Yes", "No", "All namespaces"},
  221. )
  222. if err != nil {
  223. return err
  224. }
  225. if proceed == "No" {
  226. return nil
  227. }
  228. var podNames []string
  229. color.New(color.FgGreen).Println("Fetching ephemeral pods for cleanup")
  230. if proceed == "All namespaces" {
  231. namespaces, err := config.Clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
  232. if err != nil {
  233. return err
  234. }
  235. for _, namespace := range namespaces.Items {
  236. if pods, err := appGetEphemeralPods(namespace.Name, config.Clientset); err == nil {
  237. podNames = append(podNames, pods...)
  238. } else {
  239. return err
  240. }
  241. }
  242. } else {
  243. if pods, err := appGetEphemeralPods(appNamespace, config.Clientset); err == nil {
  244. podNames = append(podNames, pods...)
  245. } else {
  246. return err
  247. }
  248. }
  249. if len(podNames) == 0 {
  250. color.New(color.FgBlue).Println("No ephemeral pods to delete")
  251. return nil
  252. }
  253. selectedPods, err := utils.PromptMultiselect("Select ephemeral pods to delete", podNames)
  254. if err != nil {
  255. return err
  256. }
  257. for _, podName := range selectedPods {
  258. color.New(color.FgBlue).Printf("Deleting ephemeral pod: %s\n", podName)
  259. err = config.Clientset.CoreV1().Pods(appNamespace).Delete(
  260. context.Background(), podName, metav1.DeleteOptions{},
  261. )
  262. if err != nil {
  263. return err
  264. }
  265. }
  266. return nil
  267. }
  268. func appGetEphemeralPods(namespace string, clientset *kubernetes.Clientset) ([]string, error) {
  269. var podNames []string
  270. pods, err := clientset.CoreV1().Pods(namespace).List(
  271. context.Background(), metav1.ListOptions{LabelSelector: "porter/ephemeral-pod"},
  272. )
  273. if err != nil {
  274. return nil, err
  275. }
  276. for _, pod := range pods.Items {
  277. podNames = append(podNames, pod.Name)
  278. }
  279. return podNames, nil
  280. }
  281. type AppPorterRunSharedConfig struct {
  282. Client *api.Client
  283. RestConf *rest.Config
  284. Clientset *kubernetes.Clientset
  285. RestClient *rest.RESTClient
  286. }
  287. func (p *AppPorterRunSharedConfig) setSharedConfig() error {
  288. pID := cliConf.Project
  289. cID := cliConf.Cluster
  290. kubeResp, err := p.Client.GetKubeconfig(context.Background(), pID, cID, cliConf.Kubeconfig)
  291. if err != nil {
  292. return err
  293. }
  294. kubeBytes := kubeResp.Kubeconfig
  295. cmdConf, err := clientcmd.NewClientConfigFromBytes(kubeBytes)
  296. if err != nil {
  297. return err
  298. }
  299. restConf, err := cmdConf.ClientConfig()
  300. if err != nil {
  301. return err
  302. }
  303. restConf.GroupVersion = &schema.GroupVersion{
  304. Group: "api",
  305. Version: "v1",
  306. }
  307. restConf.NegotiatedSerializer = runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{})
  308. p.RestConf = restConf
  309. clientset, err := kubernetes.NewForConfig(restConf)
  310. if err != nil {
  311. return err
  312. }
  313. p.Clientset = clientset
  314. restClient, err := rest.RESTClientFor(restConf)
  315. if err != nil {
  316. return err
  317. }
  318. p.RestClient = restClient
  319. return nil
  320. }
  321. type appPodSimple struct {
  322. Name string
  323. ContainerNames []string
  324. }
  325. func appGetPods(client *api.Client, namespace, releaseName string) ([]appPodSimple, error) {
  326. pID := cliConf.Project
  327. cID := cliConf.Cluster
  328. resp, err := client.GetK8sAllPods(context.TODO(), pID, cID, namespace, releaseName)
  329. if err != nil {
  330. return nil, err
  331. }
  332. pods := *resp
  333. res := make([]appPodSimple, 0)
  334. for _, pod := range pods {
  335. if pod.Status.Phase == v1.PodRunning {
  336. containerNames := make([]string, 0)
  337. for _, container := range pod.Spec.Containers {
  338. containerNames = append(containerNames, container.Name)
  339. }
  340. res = append(res, appPodSimple{
  341. Name: pod.ObjectMeta.Name,
  342. ContainerNames: containerNames,
  343. })
  344. }
  345. }
  346. return res, nil
  347. }
  348. func appExecuteRun(config *AppPorterRunSharedConfig, namespace, name, container string, args []string) error {
  349. req := config.RestClient.Post().
  350. Resource("pods").
  351. Name(name).
  352. Namespace(namespace).
  353. SubResource("exec")
  354. for _, arg := range args {
  355. req.Param("command", arg)
  356. }
  357. req.Param("stdin", "true")
  358. req.Param("stdout", "true")
  359. req.Param("tty", "true")
  360. req.Param("container", container)
  361. t := term.TTY{
  362. In: os.Stdin,
  363. Out: os.Stdout,
  364. Raw: true,
  365. }
  366. size := t.GetSize()
  367. sizeQueue := t.MonitorSize(size)
  368. return t.Safe(func() error {
  369. exec, err := remotecommand.NewSPDYExecutor(config.RestConf, "POST", req.URL())
  370. if err != nil {
  371. return err
  372. }
  373. return exec.Stream(remotecommand.StreamOptions{
  374. Stdin: os.Stdin,
  375. Stdout: os.Stdout,
  376. Stderr: os.Stderr,
  377. Tty: true,
  378. TerminalSizeQueue: sizeQueue,
  379. })
  380. })
  381. }
  382. func appExecuteRunEphemeral(config *AppPorterRunSharedConfig, namespace, name, container string, args []string) error {
  383. existing, err := appGetExistingPod(config, name, namespace)
  384. if err != nil {
  385. return err
  386. }
  387. newPod, err := appCreateEphemeralPodFromExisting(config, existing, container, args)
  388. if err != nil {
  389. return err
  390. }
  391. podName := newPod.ObjectMeta.Name
  392. // delete the ephemeral pod no matter what
  393. defer appDeletePod(config, podName, namespace)
  394. color.New(color.FgYellow).Printf("Waiting for pod %s to be ready...", podName)
  395. if err = appWaitForPod(config, newPod); err != nil {
  396. color.New(color.FgRed).Println("failed")
  397. return appHandlePodAttachError(err, config, namespace, podName, container)
  398. }
  399. err = appCheckForPodDeletionCronJob(config)
  400. if err != nil {
  401. return err
  402. }
  403. // refresh pod info for latest status
  404. newPod, err = config.Clientset.CoreV1().
  405. Pods(newPod.Namespace).
  406. Get(context.Background(), newPod.Name, metav1.GetOptions{})
  407. // pod exited while we were waiting. maybe an error maybe not.
  408. // we dont know if the user wanted an interactive shell or not.
  409. // if it was an error the logs hopefully say so.
  410. if appIsPodExited(newPod) {
  411. color.New(color.FgGreen).Println("complete!")
  412. var writtenBytes int64
  413. writtenBytes, _ = appPipePodLogsToStdout(config, namespace, podName, container, false)
  414. if appVerbose || writtenBytes == 0 {
  415. color.New(color.FgYellow).Println("Could not get logs. Pod events:")
  416. appPipeEventsToStdout(config, namespace, podName, container, false)
  417. }
  418. return nil
  419. }
  420. color.New(color.FgGreen).Println("ready!")
  421. color.New(color.FgYellow).Println("Attempting connection to the container. If you don't see a command prompt, try pressing enter.")
  422. req := config.RestClient.Post().
  423. Resource("pods").
  424. Name(podName).
  425. Namespace(namespace).
  426. SubResource("attach")
  427. req.Param("stdin", "true")
  428. req.Param("stdout", "true")
  429. req.Param("tty", "true")
  430. req.Param("container", container)
  431. t := term.TTY{
  432. In: os.Stdin,
  433. Out: os.Stdout,
  434. Raw: true,
  435. }
  436. size := t.GetSize()
  437. sizeQueue := t.MonitorSize(size)
  438. if err = t.Safe(func() error {
  439. exec, err := remotecommand.NewSPDYExecutor(config.RestConf, "POST", req.URL())
  440. if err != nil {
  441. return err
  442. }
  443. return exec.Stream(remotecommand.StreamOptions{
  444. Stdin: os.Stdin,
  445. Stdout: os.Stdout,
  446. Stderr: os.Stderr,
  447. Tty: true,
  448. TerminalSizeQueue: sizeQueue,
  449. })
  450. }); err != nil {
  451. // ugly way to catch no TTY errors, such as when running command "echo \"hello\""
  452. return appHandlePodAttachError(err, config, namespace, podName, container)
  453. }
  454. if appVerbose {
  455. color.New(color.FgYellow).Println("Pod events:")
  456. appPipeEventsToStdout(config, namespace, podName, container, false)
  457. }
  458. return err
  459. }
  460. func appCheckForPodDeletionCronJob(config *AppPorterRunSharedConfig) error {
  461. // try and create the cron job and all of the other required resources as necessary,
  462. // starting with the service account, then role and then a role binding
  463. err := appCheckForServiceAccount(config)
  464. if err != nil {
  465. return err
  466. }
  467. err = appCheckForClusterRole(config)
  468. if err != nil {
  469. return err
  470. }
  471. err = appCheckForRoleBinding(config)
  472. if err != nil {
  473. return err
  474. }
  475. namespaces, err := config.Clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
  476. if err != nil {
  477. return err
  478. }
  479. for _, namespace := range namespaces.Items {
  480. cronJobs, err := config.Clientset.BatchV1().CronJobs(namespace.Name).List(
  481. context.Background(), metav1.ListOptions{},
  482. )
  483. if err != nil {
  484. return err
  485. }
  486. if namespace.Name == "default" {
  487. for _, cronJob := range cronJobs.Items {
  488. if cronJob.Name == "porter-ephemeral-pod-deletion-cronjob" {
  489. return nil
  490. }
  491. }
  492. } else {
  493. for _, cronJob := range cronJobs.Items {
  494. if cronJob.Name == "porter-ephemeral-pod-deletion-cronjob" {
  495. err = config.Clientset.BatchV1().CronJobs(namespace.Name).Delete(
  496. context.Background(), cronJob.Name, metav1.DeleteOptions{},
  497. )
  498. if err != nil {
  499. return err
  500. }
  501. }
  502. }
  503. }
  504. }
  505. // create the cronjob
  506. cronJob := &batchv1.CronJob{
  507. ObjectMeta: metav1.ObjectMeta{
  508. Name: "porter-ephemeral-pod-deletion-cronjob",
  509. },
  510. Spec: batchv1.CronJobSpec{
  511. Schedule: "0 * * * *",
  512. JobTemplate: batchv1.JobTemplateSpec{
  513. Spec: batchv1.JobSpec{
  514. Template: v1.PodTemplateSpec{
  515. Spec: v1.PodSpec{
  516. ServiceAccountName: "porter-ephemeral-pod-deletion-service-account",
  517. RestartPolicy: v1.RestartPolicyNever,
  518. Containers: []v1.Container{
  519. {
  520. Name: "ephemeral-pods-manager",
  521. Image: "public.ecr.aws/o1j4x7p4/porter-ephemeral-pods-manager:latest",
  522. ImagePullPolicy: v1.PullAlways,
  523. Args: []string{"delete"},
  524. },
  525. },
  526. },
  527. },
  528. },
  529. },
  530. },
  531. }
  532. _, err = config.Clientset.BatchV1().CronJobs("default").Create(
  533. context.Background(), cronJob, metav1.CreateOptions{},
  534. )
  535. if err != nil {
  536. return err
  537. }
  538. return nil
  539. }
  540. func appCheckForServiceAccount(config *AppPorterRunSharedConfig) error {
  541. namespaces, err := config.Clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
  542. if err != nil {
  543. return err
  544. }
  545. for _, namespace := range namespaces.Items {
  546. serviceAccounts, err := config.Clientset.CoreV1().ServiceAccounts(namespace.Name).List(
  547. context.Background(), metav1.ListOptions{},
  548. )
  549. if err != nil {
  550. return err
  551. }
  552. if namespace.Name == "default" {
  553. for _, svcAccount := range serviceAccounts.Items {
  554. if svcAccount.Name == "porter-ephemeral-pod-deletion-service-account" {
  555. return nil
  556. }
  557. }
  558. } else {
  559. for _, svcAccount := range serviceAccounts.Items {
  560. if svcAccount.Name == "porter-ephemeral-pod-deletion-service-account" {
  561. err = config.Clientset.CoreV1().ServiceAccounts(namespace.Name).Delete(
  562. context.Background(), svcAccount.Name, metav1.DeleteOptions{},
  563. )
  564. if err != nil {
  565. return err
  566. }
  567. }
  568. }
  569. }
  570. }
  571. serviceAccount := &v1.ServiceAccount{
  572. ObjectMeta: metav1.ObjectMeta{
  573. Name: "porter-ephemeral-pod-deletion-service-account",
  574. },
  575. }
  576. _, err = config.Clientset.CoreV1().ServiceAccounts("default").Create(
  577. context.Background(), serviceAccount, metav1.CreateOptions{},
  578. )
  579. if err != nil {
  580. return err
  581. }
  582. return nil
  583. }
  584. func appCheckForClusterRole(config *AppPorterRunSharedConfig) error {
  585. roles, err := config.Clientset.RbacV1().ClusterRoles().List(
  586. context.Background(), metav1.ListOptions{},
  587. )
  588. if err != nil {
  589. return err
  590. }
  591. for _, role := range roles.Items {
  592. if role.Name == "porter-ephemeral-pod-deletion-cluster-role" {
  593. return nil
  594. }
  595. }
  596. role := &rbacv1.ClusterRole{
  597. ObjectMeta: metav1.ObjectMeta{
  598. Name: "porter-ephemeral-pod-deletion-cluster-role",
  599. },
  600. Rules: []rbacv1.PolicyRule{
  601. {
  602. APIGroups: []string{""},
  603. Resources: []string{"pods"},
  604. Verbs: []string{"list", "delete"},
  605. },
  606. {
  607. APIGroups: []string{""},
  608. Resources: []string{"namespaces"},
  609. Verbs: []string{"list"},
  610. },
  611. },
  612. }
  613. _, err = config.Clientset.RbacV1().ClusterRoles().Create(
  614. context.Background(), role, metav1.CreateOptions{},
  615. )
  616. if err != nil {
  617. return err
  618. }
  619. return nil
  620. }
  621. func appCheckForRoleBinding(config *AppPorterRunSharedConfig) error {
  622. bindings, err := config.Clientset.RbacV1().ClusterRoleBindings().List(
  623. context.Background(), metav1.ListOptions{},
  624. )
  625. if err != nil {
  626. return err
  627. }
  628. for _, binding := range bindings.Items {
  629. if binding.Name == "porter-ephemeral-pod-deletion-cluster-rolebinding" {
  630. return nil
  631. }
  632. }
  633. binding := &rbacv1.ClusterRoleBinding{
  634. ObjectMeta: metav1.ObjectMeta{
  635. Name: "porter-ephemeral-pod-deletion-cluster-rolebinding",
  636. },
  637. RoleRef: rbacv1.RoleRef{
  638. APIGroup: "rbac.authorization.k8s.io",
  639. Kind: "ClusterRole",
  640. Name: "porter-ephemeral-pod-deletion-cluster-role",
  641. },
  642. Subjects: []rbacv1.Subject{
  643. {
  644. APIGroup: "",
  645. Kind: "ServiceAccount",
  646. Name: "porter-ephemeral-pod-deletion-service-account",
  647. Namespace: "default",
  648. },
  649. },
  650. }
  651. _, err = config.Clientset.RbacV1().ClusterRoleBindings().Create(
  652. context.Background(), binding, metav1.CreateOptions{},
  653. )
  654. if err != nil {
  655. return err
  656. }
  657. return nil
  658. }
  659. func appWaitForPod(config *AppPorterRunSharedConfig, pod *v1.Pod) error {
  660. var (
  661. w watch.Interface
  662. err error
  663. ok bool
  664. )
  665. // immediately after creating a pod, the API may return a 404. heuristically 1
  666. // second seems to be plenty.
  667. watchRetries := 3
  668. for i := 0; i < watchRetries; i++ {
  669. selector := fields.OneTermEqualSelector("metadata.name", pod.Name).String()
  670. w, err = config.Clientset.CoreV1().
  671. Pods(pod.Namespace).
  672. Watch(context.Background(), metav1.ListOptions{FieldSelector: selector})
  673. if err == nil {
  674. break
  675. }
  676. time.Sleep(time.Second)
  677. }
  678. if err != nil {
  679. return err
  680. }
  681. defer w.Stop()
  682. for {
  683. select {
  684. case <-time.Tick(time.Second):
  685. // poll every second in case we already missed the ready event while
  686. // creating the listener.
  687. pod, err = config.Clientset.CoreV1().
  688. Pods(pod.Namespace).
  689. Get(context.Background(), pod.Name, metav1.GetOptions{})
  690. if appIsPodReady(pod) || appIsPodExited(pod) {
  691. return nil
  692. }
  693. case evt := <-w.ResultChan():
  694. pod, ok = evt.Object.(*v1.Pod)
  695. if !ok {
  696. return fmt.Errorf("unexpected object type: %T", evt.Object)
  697. }
  698. if appIsPodReady(pod) || appIsPodExited(pod) {
  699. return nil
  700. }
  701. case <-time.After(time.Second * 10):
  702. return errors.New("timed out waiting for pod")
  703. }
  704. }
  705. }
  706. func appIsPodReady(pod *v1.Pod) bool {
  707. ready := false
  708. conditions := pod.Status.Conditions
  709. for i := range conditions {
  710. if conditions[i].Type == v1.PodReady {
  711. ready = pod.Status.Conditions[i].Status == v1.ConditionTrue
  712. }
  713. }
  714. return ready
  715. }
  716. func appIsPodExited(pod *v1.Pod) bool {
  717. return pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed
  718. }
  719. func appHandlePodAttachError(err error, config *AppPorterRunSharedConfig, namespace, podName, container string) error {
  720. if appVerbose {
  721. color.New(color.FgYellow).Fprintf(os.Stderr, "Error: %s\n", err)
  722. }
  723. color.New(color.FgYellow).Fprintln(os.Stderr, "Could not open a shell to this container. Container logs:")
  724. var writtenBytes int64
  725. writtenBytes, _ = appPipePodLogsToStdout(config, namespace, podName, container, false)
  726. if appVerbose || writtenBytes == 0 {
  727. color.New(color.FgYellow).Fprintln(os.Stderr, "Could not get logs. Pod events:")
  728. appPipeEventsToStdout(config, namespace, podName, container, false)
  729. }
  730. return err
  731. }
  732. func appPipePodLogsToStdout(config *AppPorterRunSharedConfig, namespace, name, container string, follow bool) (int64, error) {
  733. podLogOpts := v1.PodLogOptions{
  734. Container: container,
  735. Follow: follow,
  736. }
  737. req := config.Clientset.CoreV1().Pods(namespace).GetLogs(name, &podLogOpts)
  738. podLogs, err := req.Stream(
  739. context.Background(),
  740. )
  741. if err != nil {
  742. return 0, err
  743. }
  744. defer podLogs.Close()
  745. return io.Copy(os.Stdout, podLogs)
  746. }
  747. func appPipeEventsToStdout(config *AppPorterRunSharedConfig, namespace, name, container string, follow bool) error {
  748. // update the config in case the operation has taken longer than token expiry time
  749. config.setSharedConfig()
  750. // creates the clientset
  751. resp, err := config.Clientset.CoreV1().Events(namespace).List(
  752. context.TODO(),
  753. metav1.ListOptions{
  754. FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=%s", name, namespace),
  755. },
  756. )
  757. if err != nil {
  758. return err
  759. }
  760. for _, event := range resp.Items {
  761. color.New(color.FgRed).Println(event.Message)
  762. }
  763. return nil
  764. }
  765. func appGetExistingPod(config *AppPorterRunSharedConfig, name, namespace string) (*v1.Pod, error) {
  766. return config.Clientset.CoreV1().Pods(namespace).Get(
  767. context.Background(),
  768. name,
  769. metav1.GetOptions{},
  770. )
  771. }
  772. func appDeletePod(config *AppPorterRunSharedConfig, name, namespace string) error {
  773. // update the config in case the operation has taken longer than token expiry time
  774. config.setSharedConfig()
  775. err := config.Clientset.CoreV1().Pods(namespace).Delete(
  776. context.Background(),
  777. name,
  778. metav1.DeleteOptions{},
  779. )
  780. if err != nil {
  781. color.New(color.FgRed).Fprintf(os.Stderr, "Could not delete ephemeral pod: %s\n", err.Error())
  782. return err
  783. }
  784. color.New(color.FgGreen).Println("Sucessfully deleted ephemeral pod")
  785. return nil
  786. }
  787. func appCreateEphemeralPodFromExisting(
  788. config *AppPorterRunSharedConfig,
  789. existing *v1.Pod,
  790. container string,
  791. args []string,
  792. ) (*v1.Pod, error) {
  793. newPod := existing.DeepCopy()
  794. // only copy the pod spec, overwrite metadata
  795. newPod.ObjectMeta = metav1.ObjectMeta{
  796. Name: strings.ToLower(fmt.Sprintf("%s-copy-%s", existing.ObjectMeta.Name, utils.String(4))),
  797. Namespace: existing.ObjectMeta.Namespace,
  798. }
  799. newPod.Status = v1.PodStatus{}
  800. // set restart policy to never
  801. newPod.Spec.RestartPolicy = v1.RestartPolicyNever
  802. // change the command in the pod to the passed in pod command
  803. cmdRoot := args[0]
  804. cmdArgs := make([]string, 0)
  805. // annotate with the ephemeral pod tag
  806. newPod.Labels = make(map[string]string)
  807. newPod.Labels["porter/ephemeral-pod"] = "true"
  808. if len(args) > 1 {
  809. cmdArgs = args[1:]
  810. }
  811. for i := 0; i < len(newPod.Spec.Containers); i++ {
  812. if newPod.Spec.Containers[i].Name == container {
  813. newPod.Spec.Containers[i].Command = []string{cmdRoot}
  814. newPod.Spec.Containers[i].Args = cmdArgs
  815. newPod.Spec.Containers[i].TTY = true
  816. newPod.Spec.Containers[i].Stdin = true
  817. newPod.Spec.Containers[i].StdinOnce = true
  818. }
  819. // remove health checks and probes
  820. newPod.Spec.Containers[i].LivenessProbe = nil
  821. newPod.Spec.Containers[i].ReadinessProbe = nil
  822. newPod.Spec.Containers[i].StartupProbe = nil
  823. }
  824. newPod.Spec.NodeName = ""
  825. // create the pod and return it
  826. return config.Clientset.CoreV1().Pods(existing.ObjectMeta.Namespace).Create(
  827. context.Background(),
  828. newPod,
  829. metav1.CreateOptions{},
  830. )
  831. }
  832. func appUpdateUpgrade(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  833. namespace := fmt.Sprintf("porter-stack-%s", args[0])
  834. if appTag == "" {
  835. appTag = "latest"
  836. }
  837. release, err := client.GetRelease(context.TODO(), cliConf.Project, cliConf.Cluster, namespace, args[0])
  838. if err != nil {
  839. return fmt.Errorf("Unable to find application %s", args[0])
  840. }
  841. // check for the post-deploy job associated with the release
  842. postDeployReleaseName := fmt.Sprintf("%s-r", args[0])
  843. postDeployRelease, postDeployReleaseErr := client.GetRelease(context.TODO(), cliConf.Project, cliConf.Cluster, namespace, postDeployReleaseName)
  844. color.New(color.FgGreen).Printf("Updating application %s to build using tag \"%s\"\n", args[0], appTag)
  845. overrideValues := map[string]interface{}{
  846. "global": map[string]interface{}{
  847. "image": map[string]interface{}{
  848. "tag": appTag,
  849. },
  850. },
  851. }
  852. mergedValues := templaterUtils.CoalesceValues(release.Config, overrideValues)
  853. bytes, err := json.Marshal(mergedValues)
  854. if err != nil {
  855. return fmt.Errorf("Unable to update application %s: %w", args[0], err)
  856. }
  857. err = client.UpgradeRelease(
  858. context.Background(),
  859. cliConf.Project,
  860. cliConf.Cluster,
  861. namespace,
  862. args[0],
  863. &types.UpgradeReleaseRequest{
  864. Values: string(bytes),
  865. IgnoreDependencies: true,
  866. },
  867. )
  868. if err != nil {
  869. return fmt.Errorf("Unable to update application %s: %w", args[0], err)
  870. }
  871. color.New(color.FgGreen).Printf("Successfully updated application %s\n", args[0])
  872. if postDeployReleaseErr != nil {
  873. // didn't find a post-deploy job, so we're done
  874. return nil
  875. }
  876. color.New(color.FgGreen).Printf("Post-deploy job for application %s found. Updating job to build using tag \"%s\" as well\n", args[0], appTag)
  877. overrideValues = map[string]interface{}{
  878. "image": map[string]interface{}{
  879. "tag": appTag,
  880. },
  881. }
  882. mergedValues = templaterUtils.CoalesceValues(postDeployRelease.Config, overrideValues)
  883. bytes, err = json.Marshal(mergedValues)
  884. if err != nil {
  885. return fmt.Errorf("Unable to update post-deploy job for application %s: %w", args[0], err)
  886. }
  887. err = client.UpgradeRelease(
  888. context.Background(),
  889. cliConf.Project,
  890. cliConf.Cluster,
  891. namespace,
  892. postDeployReleaseName,
  893. &types.UpgradeReleaseRequest{
  894. Values: string(bytes),
  895. },
  896. )
  897. if err != nil {
  898. return fmt.Errorf("Unable to update post-deploy job for application %s: %w", args[0], err)
  899. }
  900. return nil
  901. }