GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

result.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package update
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "github.com/weaveworks/flux"
  7. "github.com/weaveworks/flux/image"
  8. )
  9. type ControllerUpdateStatus string
  10. const (
  11. ReleaseStatusSuccess ControllerUpdateStatus = "success"
  12. ReleaseStatusFailed ControllerUpdateStatus = "failed"
  13. ReleaseStatusSkipped ControllerUpdateStatus = "skipped"
  14. ReleaseStatusIgnored ControllerUpdateStatus = "ignored"
  15. ReleaseStatusUnknown ControllerUpdateStatus = "unknown"
  16. )
  17. type Result map[flux.ResourceID]ControllerResult
  18. func (r Result) ServiceIDs() []string {
  19. var result []string
  20. for id := range r {
  21. result = append(result, id.String())
  22. }
  23. sort.StringSlice(result).Sort()
  24. return result
  25. }
  26. func (r Result) AffectedResources() flux.ResourceIDs {
  27. ids := flux.ResourceIDs{}
  28. for id, result := range r {
  29. if result.Status == ReleaseStatusSuccess {
  30. ids = append(ids, id)
  31. }
  32. }
  33. return ids
  34. }
  35. func (r Result) ChangedImages() []string {
  36. images := map[image.Ref]struct{}{}
  37. for _, serviceResult := range r {
  38. if serviceResult.Status != ReleaseStatusSuccess {
  39. continue
  40. }
  41. for _, containerResult := range serviceResult.PerContainer {
  42. images[containerResult.Target] = struct{}{}
  43. }
  44. }
  45. var result []string
  46. for image := range images {
  47. result = append(result, image.String())
  48. }
  49. sort.StringSlice(result).Sort()
  50. return result
  51. }
  52. // Error returns the error for this release (if any)
  53. func (r Result) Error() string {
  54. var errIds []string
  55. var errStr string
  56. for id, serviceResult := range r {
  57. if serviceResult.Status == ReleaseStatusFailed {
  58. errIds = append(errIds, id.String())
  59. errStr = serviceResult.Error
  60. }
  61. }
  62. switch {
  63. case len(errIds) == 0:
  64. return ""
  65. case len(errIds) == 1:
  66. return fmt.Sprintf("%s failed: %s", errIds[0], errStr)
  67. default:
  68. return fmt.Sprintf("Multiple services failed: %s", strings.Join(errIds, ", "))
  69. }
  70. }
  71. type ControllerResult struct {
  72. Status ControllerUpdateStatus // summary of what happened, e.g., "incomplete", "ignored", "success"
  73. Error string `json:",omitempty"` // error if there was one finding the service (e.g., it doesn't exist in repo)
  74. PerContainer []ContainerUpdate // what happened with each container
  75. }
  76. type ContainerUpdate struct {
  77. Container string
  78. Current image.Ref
  79. Target image.Ref
  80. }