run.go 24 KB

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