app.go 29 KB

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