github.com/openebs/api@v1.12.0/pkg/kubernetes/core/volume.go (about)

     1  // Copyright © 2020 The OpenEBS Authors
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package core
    16  
    17  import (
    18  	corev1 "k8s.io/api/core/v1"
    19  )
    20  
    21  // Volume is a wrapper over named volume api object, used
    22  // within Pods. It provides build, validations and other common
    23  // logic to be used by various feature specific callers.
    24  type Volume struct {
    25  	*corev1.Volume
    26  }
    27  
    28  // NewVolume returns a new instance of volume
    29  func NewVolume() *Volume {
    30  	return &Volume{
    31  		&corev1.Volume{},
    32  	}
    33  }
    34  
    35  // WithName sets the Name field of Volume with provided value.
    36  func (v *Volume) WithName(name string) *Volume {
    37  	v.Name = name
    38  	return v
    39  }
    40  
    41  // WithHostDirectory sets the VolumeSource field of Volume with provided hostpath
    42  // as type directory.
    43  func (v *Volume) WithHostDirectory(path string) *Volume {
    44  	volumeSource := corev1.VolumeSource{
    45  		HostPath: &corev1.HostPathVolumeSource{
    46  			Path: path,
    47  		},
    48  	}
    49  
    50  	v.VolumeSource = volumeSource
    51  	return v
    52  }
    53  
    54  // WithHostPathAndType sets the VolumeSource field of Volume with provided
    55  // hostpath as directory path and type as directory type
    56  func (v *Volume) WithHostPathAndType(dirpath string, dirtype *corev1.HostPathType) *Volume {
    57  	volumeSource := corev1.VolumeSource{
    58  		HostPath: &corev1.HostPathVolumeSource{
    59  			Path: dirpath,
    60  			Type: dirtype,
    61  		},
    62  	}
    63  
    64  	v.VolumeSource = volumeSource
    65  	return v
    66  }
    67  
    68  // WithPVCSource sets the Volume field of Volume with provided pvc
    69  func (v *Volume) WithPVCSource(pvcName string) *Volume {
    70  	volumeSource := corev1.VolumeSource{
    71  		PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
    72  			ClaimName: pvcName,
    73  		},
    74  	}
    75  	v.VolumeSource = volumeSource
    76  	return v
    77  }
    78  
    79  // WithEmptyDir sets the EmptyDir field of the Volume with provided dir
    80  func (v *Volume) WithEmptyDir(dir *corev1.EmptyDirVolumeSource) *Volume {
    81  	v.EmptyDir = dir
    82  	return v
    83  }
    84  
    85  func (v *Volume) Build() *corev1.Volume {
    86  	return v.Volume
    87  }