...

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

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

     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 utils
    18  
    19  import (
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  
    23  	cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
    24  )
    25  
    26  // IsPopulated indicates if the persistent volume passed in has been fully populated. It follow the following logic
    27  // 1. If the PVC is not owned by a DataVolume, return true, we assume someone else has properly populated the image
    28  // 2. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase succeeded return true
    29  // 3. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase !succeeded return false
    30  func IsPopulated(pvc *corev1.PersistentVolumeClaim, getDvFunc func(name, namespace string) (*cdiv1.DataVolume, error)) (bool, error) {
    31  	pvcOwner := metav1.GetControllerOf(pvc)
    32  	if pvcOwner != nil && pvcOwner.Kind == "DataVolume" {
    33  		// Find the data volume:
    34  		dv, err := getDvFunc(pvcOwner.Name, pvc.Namespace)
    35  		if err != nil {
    36  			return false, err
    37  		}
    38  		if dv.Status.Phase != cdiv1.Succeeded {
    39  			return false, nil
    40  		}
    41  	}
    42  	return true, nil
    43  }
    44  
    45  // IsWaitForFirstConsumerBeforePopulating indicates if the persistent volume passed in is in ClaimPending state and waiting for first consumer.
    46  // It follow the following logic
    47  // 1. If the PVC is not owned by a DataVolume, return false, we can not assume it will be populated
    48  // 2. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase WaitForFirstConsumer return true
    49  // 3. If the PVC is owned by a DataVolume, look up the DV and check the phase, if phase !WaitForFirstConsumer return false
    50  func IsWaitForFirstConsumerBeforePopulating(pvc *corev1.PersistentVolumeClaim, getDvFunc func(name, namespace string) (*cdiv1.DataVolume, error)) (bool, error) {
    51  	pvcOwner := metav1.GetControllerOf(pvc)
    52  	if pvcOwner != nil && pvcOwner.Kind == "DataVolume" {
    53  		// Find the data volume:
    54  		dv, err := getDvFunc(pvcOwner.Name, pvc.Namespace)
    55  		if err != nil {
    56  			return false, err
    57  		}
    58  		if dv.Status.Phase == cdiv1.WaitForFirstConsumer {
    59  			return true, nil
    60  		}
    61  	}
    62  	return false, nil
    63  }
    64  

View as plain text