1
0

events.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "strings"
  17. "time"
  18. moby "github.com/docker/docker/api/types"
  19. "github.com/docker/docker/api/types/filters"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/docker/compose/v2/pkg/utils"
  22. )
  23. func (s *composeService) Events(ctx context.Context, project string, options api.EventsOptions) error {
  24. events, errors := s.apiClient.Events(ctx, moby.EventsOptions{
  25. Filters: filters.NewArgs(projectFilter(project)),
  26. })
  27. for {
  28. select {
  29. case event := <-events:
  30. // TODO: support other event types
  31. if event.Type != "container" {
  32. continue
  33. }
  34. oneOff := event.Actor.Attributes[api.OneoffLabel]
  35. if oneOff == "True" {
  36. // ignore
  37. continue
  38. }
  39. service := event.Actor.Attributes[api.ServiceLabel]
  40. if len(options.Services) > 0 && !utils.StringContains(options.Services, service) {
  41. continue
  42. }
  43. attributes := map[string]string{}
  44. for k, v := range event.Actor.Attributes {
  45. if strings.HasPrefix(k, "com.docker.compose.") {
  46. continue
  47. }
  48. attributes[k] = v
  49. }
  50. timestamp := time.Unix(event.Time, 0)
  51. if event.TimeNano != 0 {
  52. timestamp = time.Unix(0, event.TimeNano)
  53. }
  54. err := options.Consumer(api.Event{
  55. Timestamp: timestamp,
  56. Service: service,
  57. Container: event.ID,
  58. Status: event.Status,
  59. Attributes: attributes,
  60. })
  61. if err != nil {
  62. return err
  63. }
  64. case err := <-errors:
  65. return err
  66. }
  67. }
  68. }