run.go 24 KB

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