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.

images.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package daemon
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/go-kit/kit/log"
  6. "github.com/pkg/errors"
  7. "github.com/weaveworks/flux"
  8. "github.com/weaveworks/flux/git"
  9. "github.com/weaveworks/flux/policy"
  10. "github.com/weaveworks/flux/update"
  11. )
  12. func (d *Daemon) pollForNewImages(logger log.Logger) {
  13. logger.Log("msg", "polling images")
  14. ctx := context.Background()
  15. candidateServices, err := d.unlockedAutomatedServices(ctx)
  16. if err != nil {
  17. logger.Log("error", errors.Wrap(err, "getting unlocked automated services"))
  18. return
  19. }
  20. if len(candidateServices) == 0 {
  21. logger.Log("msg", "no automated services")
  22. return
  23. }
  24. // Find images to check
  25. services, err := d.Cluster.SomeControllers(candidateServices.ToSlice())
  26. if err != nil {
  27. logger.Log("error", errors.Wrap(err, "checking services for new images"))
  28. return
  29. }
  30. // Check the latest available image(s) for each service
  31. imageMap, err := update.CollectAvailableImages(d.Registry, services, logger)
  32. if err != nil {
  33. logger.Log("error", errors.Wrap(err, "fetching image updates"))
  34. return
  35. }
  36. changes := &update.Automated{}
  37. for _, service := range services {
  38. for _, container := range service.ContainersOrNil() {
  39. logger := log.With(logger, "service", service.ID, "container", container.Name, "currentimage", container.Image)
  40. currentImageID := container.Image
  41. if err != nil {
  42. logger.Log("error", err)
  43. continue
  44. }
  45. pattern := getTagPattern(candidateServices, service.ID, container.Name)
  46. repo := currentImageID.Name
  47. logger.Log("repo", repo, "pattern", pattern)
  48. if latest, ok := imageMap.LatestImage(repo, pattern); ok && latest.ID != currentImageID {
  49. newImage := currentImageID.WithNewTag(latest.ID.Tag)
  50. changes.Add(service.ID, container, newImage)
  51. logger.Log("msg", "added image to changes", "newimage", newImage)
  52. }
  53. }
  54. }
  55. if len(changes.Changes) > 0 {
  56. d.UpdateManifests(ctx, update.Spec{Type: update.Auto, Spec: changes})
  57. }
  58. }
  59. func getTagPattern(services policy.ResourceMap, service flux.ResourceID, container string) string {
  60. policies := services[service]
  61. if pattern, ok := policies.Get(policy.TagPrefix(container)); ok {
  62. return strings.TrimPrefix(pattern, "glob:")
  63. }
  64. return "*"
  65. }
  66. func (d *Daemon) unlockedAutomatedServices(ctx context.Context) (policy.ResourceMap, error) {
  67. var services policy.ResourceMap
  68. err := d.WithClone(ctx, func(checkout *git.Checkout) error {
  69. var err error
  70. services, err = d.Manifests.ServicesWithPolicies(checkout.ManifestDir())
  71. return err
  72. })
  73. if err != nil {
  74. return nil, err
  75. }
  76. automatedServices := services.OnlyWithPolicy(policy.Automated)
  77. lockedServices := services.OnlyWithPolicy(policy.Locked)
  78. return automatedServices.Without(lockedServices), nil
  79. }