pod.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright 2015 The Kubernetes 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 format
  14. import (
  15. "fmt"
  16. "strings"
  17. "time"
  18. "k8s.io/kubernetes/pkg/api"
  19. )
  20. type podHandler func(*api.Pod) string
  21. // Pod returns a string reprenetating a pod in a human readable format,
  22. // with pod UID as part of the string.
  23. func Pod(pod *api.Pod) string {
  24. // Use underscore as the delimiter because it is not allowed in pod name
  25. // (DNS subdomain format), while allowed in the container name format.
  26. return fmt.Sprintf("%s_%s(%s)", pod.Name, pod.Namespace, pod.UID)
  27. }
  28. // PodWithDeletionTimestamp is the same as Pod. In addition, it prints the
  29. // deletion timestamp of the pod if it's not nil.
  30. func PodWithDeletionTimestamp(pod *api.Pod) string {
  31. var deletionTimestamp string
  32. if pod.DeletionTimestamp != nil {
  33. deletionTimestamp = ":DeletionTimestamp=" + pod.DeletionTimestamp.UTC().Format(time.RFC3339)
  34. }
  35. return Pod(pod) + deletionTimestamp
  36. }
  37. // Pods returns a string representating a list of pods in a human
  38. // readable format.
  39. func Pods(pods []*api.Pod) string {
  40. return aggregatePods(pods, Pod)
  41. }
  42. // PodsWithDeletiontimestamps is the same as Pods. In addition, it prints the
  43. // deletion timestamps of the pods if they are not nil.
  44. func PodsWithDeletiontimestamps(pods []*api.Pod) string {
  45. return aggregatePods(pods, PodWithDeletionTimestamp)
  46. }
  47. func aggregatePods(pods []*api.Pod, handler podHandler) string {
  48. podStrings := make([]string, 0, len(pods))
  49. for _, pod := range pods {
  50. podStrings = append(podStrings, handler(pod))
  51. }
  52. return fmt.Sprintf(strings.Join(podStrings, ", "))
  53. }