update_image.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package v2
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "github.com/fatih/color"
  7. api "github.com/porter-dev/porter/api/client"
  8. )
  9. // UpdateImageInput is the input for the UpdateImage function
  10. type UpdateImageInput struct {
  11. ProjectID uint
  12. ClusterID uint
  13. AppName string
  14. DeploymentTargetName string
  15. Tag string
  16. Description string
  17. Client api.Client
  18. WaitForSuccessfulDeployment bool
  19. }
  20. // UpdateImage updates the image of an application
  21. func UpdateImage(ctx context.Context, input UpdateImageInput) error {
  22. tag := input.Tag
  23. if tag == "" {
  24. tag = "latest"
  25. }
  26. var base64Description string
  27. if input.Description != "" {
  28. base64Description = base64.StdEncoding.EncodeToString([]byte(input.Description))
  29. }
  30. resp, err := input.Client.UpdateImage(ctx, input.ProjectID, input.ClusterID, input.AppName, input.DeploymentTargetName, tag, base64Description)
  31. if err != nil {
  32. return fmt.Errorf("unable to update image: %w", err)
  33. }
  34. triggeredBackgroundColor := color.FgGreen
  35. _, _ = color.New(triggeredBackgroundColor).Printf("Updated application %s to use tag \"%s\"\n", input.AppName, tag)
  36. if input.WaitForSuccessfulDeployment {
  37. return waitForAppRevisionStatus(ctx, waitForAppRevisionStatusInput{
  38. ProjectID: input.ProjectID,
  39. ClusterID: input.ClusterID,
  40. AppName: input.AppName,
  41. RevisionID: resp.RevisionID,
  42. Client: input.Client,
  43. })
  44. }
  45. return nil
  46. }