app.go 28 KB

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