utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // +build kube
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package client
  15. import (
  16. "fmt"
  17. "time"
  18. "github.com/docker/compose-cli/pkg/api"
  19. "github.com/docker/compose-cli/pkg/utils"
  20. corev1 "k8s.io/api/core/v1"
  21. )
  22. func podToContainerSummary(pod corev1.Pod) api.ContainerSummary {
  23. state := api.RUNNING
  24. if pod.DeletionTimestamp != nil {
  25. state = api.REMOVING
  26. } else {
  27. for _, container := range pod.Status.ContainerStatuses {
  28. if container.State.Waiting != nil || container.State.Terminated != nil {
  29. state = api.UPDATING
  30. break
  31. }
  32. }
  33. if state == api.RUNNING && pod.Status.Phase != corev1.PodRunning {
  34. state = string(pod.Status.Phase)
  35. }
  36. }
  37. return api.ContainerSummary{
  38. ID: pod.GetObjectMeta().GetName(),
  39. Name: pod.GetObjectMeta().GetName(),
  40. Service: pod.GetObjectMeta().GetLabels()[api.ServiceLabel],
  41. State: state,
  42. Project: pod.GetObjectMeta().GetLabels()[api.ProjectLabel],
  43. }
  44. }
  45. func checkPodsState(services []string, pods []corev1.Pod, status string) (bool, map[string]string, error) {
  46. servicePods := map[string]string{}
  47. stateReached := true
  48. for _, pod := range pods {
  49. service := pod.Labels[api.ServiceLabel]
  50. if len(services) > 0 && !utils.StringContains(services, service) {
  51. continue
  52. }
  53. containersRunning := true
  54. for _, container := range pod.Status.ContainerStatuses {
  55. if container.State.Running == nil {
  56. containersRunning = false
  57. break
  58. }
  59. }
  60. servicePods[service] = pod.Status.Message
  61. if status == api.REMOVING {
  62. continue
  63. }
  64. if pod.Status.Phase == corev1.PodFailed {
  65. return false, servicePods, fmt.Errorf(pod.Status.Reason)
  66. }
  67. if status == api.RUNNING && (pod.Status.Phase != corev1.PodRunning || !containersRunning) {
  68. stateReached = false
  69. }
  70. }
  71. if status == api.REMOVING && len(servicePods) > 0 {
  72. stateReached = false
  73. }
  74. return stateReached, servicePods, nil
  75. }
  76. // LogFunc defines a custom logger function (progress writer events)
  77. type LogFunc func(pod string, stateReached bool, message string)
  78. // WaitForStatusOptions hold the state pods should reach
  79. type WaitForStatusOptions struct {
  80. ProjectName string
  81. Services []string
  82. Status string
  83. Timeout *time.Duration
  84. Log LogFunc
  85. }
  86. // Ports holds published ports data
  87. type Ports []api.PortPublisher
  88. // PortMappingOptions holds the port mapping for project services
  89. type PortMappingOptions struct {
  90. ProjectName string
  91. Services map[string]Ports
  92. }