github.com/verrazzano/verrazzano@v1.7.0/pkg/k8s/ready/pod_ready.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package ready 5 6 import ( 7 "context" 8 "github.com/verrazzano/verrazzano/pkg/log/vzlog" 9 corev1 "k8s.io/api/core/v1" 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 "k8s.io/apimachinery/pkg/types" 12 clipkg "sigs.k8s.io/controller-runtime/pkg/client" 13 ) 14 15 // pod label used to identify the replicaset resource for deployments 16 const podTemplateHashLabel = "pod-template-hash" 17 18 // annotation used to identify the revision of a replicaset 19 const deploymentRevisionAnnotation = "deployment.kubernetes.io/revision" 20 21 // GetPodsList retrieves a list of pods for a given namespace and labels selector 22 func GetPodsList(log vzlog.VerrazzanoLogger, client clipkg.Client, namespacedName types.NamespacedName, selector *metav1.LabelSelector) *corev1.PodList { 23 labelSelector, err := metav1.LabelSelectorAsSelector(selector) 24 if err != nil { 25 logErrorf(log, "Failed to convert LabelSelector %v for %v: %v", selector, namespacedName, err) 26 return nil 27 } 28 var pods corev1.PodList 29 err = client.List(context.TODO(), &pods, 30 &clipkg.ListOptions{Namespace: namespacedName.Namespace, LabelSelector: labelSelector}) 31 if err != nil { 32 logErrorf(log, "Failed listing pods in namespace %s: %v", namespacedName.Namespace, err) 33 return nil 34 } 35 36 return &pods 37 } 38 39 // EnsurePodsAreReady makes sure pods using the latest workload revision are ready. 40 // A list of pods using the latest revision are passed to this function. 41 func EnsurePodsAreReady(log vzlog.VerrazzanoLogger, podsToCheck []corev1.Pod, expectedPods int32, prefix string) (int32, bool) { 42 var podsReady int32 43 for _, pod := range podsToCheck { 44 // Check that init containers are ready 45 for _, initContainerStatus := range pod.Status.InitContainerStatuses { 46 if !initContainerStatus.Ready { 47 logProgressf(log, "%s is waiting for init container of pod %s to be ready", prefix, pod.Name) 48 return 0, false 49 } 50 } 51 // Check that containers are ready 52 for _, containerStatus := range pod.Status.ContainerStatuses { 53 if !containerStatus.Ready { 54 logProgressf(log, "%s is waiting for container of pod %s to be ready", prefix, pod.Name) 55 return 0, false 56 } 57 } 58 59 podsReady++ 60 61 // No need to look at other pods if the expected pods are ready 62 if podsReady == expectedPods { 63 break 64 } 65 } 66 return podsReady, true 67 } 68 69 func logOncef(log vzlog.VerrazzanoLogger, template string, args ...interface{}) { 70 if log != nil { 71 log.Oncef(template, args...) 72 } 73 } 74 75 func logErrorf(log vzlog.VerrazzanoLogger, template string, args ...interface{}) { 76 if log != nil { 77 log.Errorf(template, args...) 78 } 79 } 80 81 func logProgressf(log vzlog.VerrazzanoLogger, template string, args ...interface{}) { 82 if log != nil { 83 log.Progressf(template, args...) 84 } 85 }