deploy_bluegreen.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package commands
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/porter-dev/porter/cli/cmd/config"
  8. v2 "github.com/porter-dev/porter/cli/cmd/v2"
  9. "github.com/fatih/color"
  10. api "github.com/porter-dev/porter/api/client"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/cli/cmd/deploy"
  13. "github.com/spf13/cobra"
  14. appsv1 "k8s.io/api/apps/v1"
  15. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  16. intstrutil "k8s.io/apimachinery/pkg/util/intstr"
  17. )
  18. func registerCommand_Deploy(cliConf config.CLIConfig) *cobra.Command {
  19. deployCmd := &cobra.Command{
  20. Use: "deploy",
  21. }
  22. bluegreenCmd := &cobra.Command{
  23. Use: "blue-green-switch",
  24. Short: "Automatically switches the traffic of a blue-green deployment once the new application is ready.",
  25. Run: func(cmd *cobra.Command, args []string) {
  26. err := checkLoginAndRunWithConfig(cmd, cliConf, args, bluegreenSwitch)
  27. if err != nil {
  28. os.Exit(1)
  29. }
  30. },
  31. }
  32. deployCmd.AddCommand(bluegreenCmd)
  33. bluegreenCmd.PersistentFlags().StringVar(
  34. &app,
  35. "app",
  36. "",
  37. "Application in the Porter dashboard",
  38. )
  39. bluegreenCmd.MarkPersistentFlagRequired("app")
  40. bluegreenCmd.PersistentFlags().StringVar(
  41. &tag,
  42. "tag",
  43. "",
  44. "The image tag to switch traffic to.",
  45. )
  46. bluegreenCmd.PersistentFlags().StringVar(
  47. &namespace,
  48. "namespace",
  49. "",
  50. "The namespace of the jobs.",
  51. )
  52. return deployCmd
  53. }
  54. func bluegreenSwitch(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConfig config.CLIConfig, _ config.FeatureFlags, args []string) error {
  55. project, err := client.GetProject(ctx, cliConfig.Project)
  56. if err != nil {
  57. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  58. }
  59. if project.ValidateApplyV2 {
  60. err = v2.BlueGreenSwitch(ctx)
  61. if err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. // get the web release
  67. webRelease, err := client.GetRelease(ctx, cliConfig.Project, cliConfig.Cluster, namespace, app)
  68. if err != nil {
  69. return err
  70. }
  71. // if this application is not a web chart, throw an error
  72. if webRelease.Chart.Name() != "web" {
  73. return fmt.Errorf("target application is not a web chart")
  74. }
  75. currActiveImage := deploy.GetCurrActiveBlueGreenImage(webRelease.Config)
  76. sharedConf := &PorterRunSharedConfig{
  77. Client: client,
  78. CLIConfig: cliConfig,
  79. }
  80. err = sharedConf.setSharedConfig(ctx)
  81. if err != nil {
  82. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  83. }
  84. // if no job exists with the given revision, wait up to 30 minutes
  85. timeWait := time.Now().Add(30 * time.Minute)
  86. prevRefresh := time.Now()
  87. success := false
  88. color.New(color.FgGreen).Printf("Waiting for the new version of the application %s to be ready\n", app)
  89. for time.Now().Before(timeWait) {
  90. // refresh the client every 10 minutes
  91. if time.Now().After(prevRefresh.Add(10 * time.Minute)) {
  92. err = sharedConf.setSharedConfig(ctx)
  93. if err != nil {
  94. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  95. }
  96. prevRefresh = time.Now()
  97. }
  98. depls, err := sharedConf.Clientset.AppsV1().Deployments(namespace).List(
  99. ctx,
  100. metav1.ListOptions{
  101. LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s", app),
  102. },
  103. )
  104. if err != nil {
  105. return fmt.Errorf("could not get deployments: %s", err.Error())
  106. }
  107. foundDeployment := false
  108. // get the deployment which matches the new image tag
  109. for _, depl := range depls.Items {
  110. if depl.ObjectMeta.Name == fmt.Sprintf("%s-web-%s", app, tag) || depl.ObjectMeta.Name == fmt.Sprintf("%s-%s", app, tag) {
  111. foundDeployment = true
  112. // determine if the deployment has an appropriate number of ready replicas
  113. minUnavailable := *(depl.Spec.Replicas) - getMaxUnavailable(depl)
  114. // if the number of ready replicas is greater than the number of min unavailable,
  115. // the controller is ready for a traffic switch
  116. if minUnavailable <= depl.Status.ReadyReplicas {
  117. // push the deployment
  118. color.New(color.FgGreen).Printf("Switching traffic for app %s\n", app)
  119. deployAgent, err := updateGetAgent(ctx, client, cliConfig)
  120. if err != nil {
  121. return err
  122. }
  123. if currActiveImage == "" {
  124. err = deployAgent.UpdateImageAndValues(ctx, map[string]interface{}{
  125. "bluegreen": map[string]interface{}{
  126. "enabled": true,
  127. "disablePrimaryDeployment": true,
  128. "activeImageTag": tag,
  129. "imageTags": []string{tag},
  130. },
  131. })
  132. } else {
  133. err = deployAgent.UpdateImageAndValues(ctx, map[string]interface{}{
  134. "bluegreen": map[string]interface{}{
  135. "enabled": true,
  136. "disablePrimaryDeployment": true,
  137. "activeImageTag": tag,
  138. "imageTags": []string{currActiveImage, tag},
  139. },
  140. })
  141. }
  142. if err != nil {
  143. return err
  144. } else {
  145. success = true
  146. }
  147. }
  148. }
  149. }
  150. if !foundDeployment {
  151. return fmt.Errorf("target deployment not found. Did you specify the correct tag?")
  152. }
  153. if success {
  154. break
  155. }
  156. // otherwise, return no error
  157. time.Sleep(2 * time.Second)
  158. }
  159. if !success {
  160. return fmt.Errorf("new application was not ready within 30 minutes")
  161. }
  162. // wait 30 seconds before removing old deployment
  163. time.Sleep(30 * time.Second)
  164. deployAgent, err := updateGetAgent(ctx, client, cliConfig)
  165. if err != nil {
  166. return err
  167. }
  168. err = deployAgent.UpdateImageAndValues( //nolint - do not want to change logic. New linter error
  169. ctx,
  170. map[string]interface{}{
  171. "bluegreen": map[string]interface{}{
  172. "enabled": true,
  173. "disablePrimaryDeployment": true,
  174. "activeImageTag": tag,
  175. "imageTags": []string{tag},
  176. },
  177. })
  178. return nil
  179. }
  180. func getMaxUnavailable(deployment appsv1.Deployment) int32 {
  181. if deployment.Spec.Strategy.Type != appsv1.RollingUpdateDeploymentStrategyType || *(deployment.Spec.Replicas) == 0 {
  182. return int32(0)
  183. }
  184. desired := *(deployment.Spec.Replicas)
  185. maxUnavailable := deployment.Spec.Strategy.RollingUpdate.MaxUnavailable
  186. unavailable, err := intstrutil.GetScaledValueFromIntOrPercent(intstrutil.ValueOrDefault(maxUnavailable, intstrutil.FromInt(0)), int(desired), false)
  187. if err != nil {
  188. return 0
  189. }
  190. return int32(unavailable)
  191. }