utils.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/api/compose"
  19. "github.com/docker/compose-cli/utils"
  20. corev1 "k8s.io/api/core/v1"
  21. )
  22. func podToContainerSummary(pod corev1.Pod) compose.ContainerSummary {
  23. return compose.ContainerSummary{
  24. ID: pod.GetObjectMeta().GetName(),
  25. Name: pod.GetObjectMeta().GetName(),
  26. Service: pod.GetObjectMeta().GetLabels()[compose.ServiceTag],
  27. State: string(pod.Status.Phase),
  28. Project: pod.GetObjectMeta().GetLabels()[compose.ProjectTag],
  29. }
  30. }
  31. func checkPodsState(services []string, pods []corev1.Pod, status string) (bool, map[string]string, error) {
  32. servicePods := map[string]string{}
  33. stateReached := true
  34. for _, pod := range pods {
  35. service := pod.Labels[compose.ServiceTag]
  36. if len(services) > 0 && !utils.StringContains(services, service) {
  37. continue
  38. }
  39. servicePods[service] = pod.Status.Message
  40. if status == compose.REMOVING {
  41. continue
  42. }
  43. if pod.Status.Phase == corev1.PodFailed {
  44. return false, servicePods, fmt.Errorf(pod.Status.Reason)
  45. }
  46. if status == compose.RUNNING && pod.Status.Phase != corev1.PodRunning {
  47. stateReached = false
  48. }
  49. }
  50. if status == compose.REMOVING && len(servicePods) > 0 {
  51. stateReached = false
  52. }
  53. return stateReached, servicePods, nil
  54. }
  55. // LogFunc defines a custom logger function (progress writer events)
  56. type LogFunc func(pod string, stateReached bool, message string)
  57. // WaitForStatusOptions hold the state pods should reach
  58. type WaitForStatusOptions struct {
  59. ProjectName string
  60. Services []string
  61. Status string
  62. Timeout *time.Duration
  63. Log LogFunc
  64. }