2
0

run.go 24 KB

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