app.go 29 KB

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