...

Source file src/kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1/utils.go

Documentation: kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1

     1  /*
     2  Copyright 2020 The CDI Authors.
     3  
     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  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1beta1
    18  
    19  import (
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  // IsPopulated indicates if the persistent volume passed in has been fully populated. It follow the following logic
    25  // 1. If the PVC is not owned by a DataVolume, return true, we assume someone else has properly populated the image
    26  // 2. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase succeeded return true
    27  // 3. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase !succeeded return false
    28  func IsPopulated(pvc *corev1.PersistentVolumeClaim, getDvFunc func(name, namespace string) (*DataVolume, error)) (bool, error) {
    29  	pvcOwner := metav1.GetControllerOf(pvc)
    30  	if pvcOwner != nil && pvcOwner.Kind == "DataVolume" {
    31  		// Find the data volume:
    32  		dv, err := getDvFunc(pvcOwner.Name, pvc.Namespace)
    33  		if err != nil {
    34  			return false, err
    35  		}
    36  		if dv.Status.Phase != Succeeded {
    37  			return false, nil
    38  		}
    39  	}
    40  	return true, nil
    41  }
    42  
    43  // IsSucceededOrPendingPopulation indicates if the persistent volume passed in has been fully populated or is waiting for a consumer.
    44  // It follow the following logic
    45  // 1. If the PVC is not owned by a DataVolume, return true, we assume someone else has properly populated the image
    46  // 2. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase succeeded or pending population return true
    47  // 3. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase !succeeded return false
    48  func IsSucceededOrPendingPopulation(pvc *corev1.PersistentVolumeClaim, getDvFunc func(name, namespace string) (*DataVolume, error)) (bool, error) {
    49  	pvcOwner := metav1.GetControllerOf(pvc)
    50  	if pvcOwner != nil && pvcOwner.Kind == "DataVolume" {
    51  		// Find the data volume:
    52  		dv, err := getDvFunc(pvcOwner.Name, pvc.Namespace)
    53  		if err != nil {
    54  			return false, err
    55  		}
    56  		return dv.Status.Phase == Succeeded || dv.Status.Phase == PendingPopulation, nil
    57  	}
    58  	return true, nil
    59  }
    60  
    61  // IsWaitForFirstConsumerBeforePopulating indicates if the persistent volume passed in is in ClaimPending state and waiting for first consumer.
    62  // It follow the following logic
    63  // 1. If the PVC is not owned by a DataVolume, return false, we can not assume it will be populated
    64  // 2. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase WaitForFirstConsumer return true
    65  // 3. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase !WaitForFirstConsumer return false
    66  func IsWaitForFirstConsumerBeforePopulating(pvc *corev1.PersistentVolumeClaim, getDvFunc func(name, namespace string) (*DataVolume, error)) (bool, error) {
    67  	pvcOwner := metav1.GetControllerOf(pvc)
    68  	if pvcOwner != nil && pvcOwner.Kind == "DataVolume" {
    69  		// Find the data volume:
    70  		dv, err := getDvFunc(pvcOwner.Name, pvc.Namespace)
    71  		if err != nil {
    72  			return false, err
    73  		}
    74  		if dv.Status.Phase == WaitForFirstConsumer {
    75  			return true, nil
    76  		}
    77  	}
    78  	return false, nil
    79  }
    80  

View as plain text