1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2020 Jens Reimann and others.
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 */
use crate::utils::UseOrCreate;

use anyhow::Result;

use k8s_openapi::api::core::v1::{Container, PodSpec, Volume, VolumeMount};

pub trait ApplyVolume {
    fn apply_volume<F, S>(&mut self, name: S, mutator: F) -> Result<()>
    where
        F: FnOnce(&mut Volume) -> Result<()>,
        S: AsRef<str>;
}

impl ApplyVolume for Vec<Volume> {
    fn apply_volume<F, S>(&mut self, name: S, mutator: F) -> Result<()>
    where
        F: FnOnce(&mut Volume) -> Result<()>,
        S: AsRef<str>,
    {
        let c = self.iter_mut().find(|c| &c.name == name.as_ref());
        match c {
            Some(c) => {
                mutator(c)?;
            }
            None => {
                let mut item: Volume = Default::default();
                item.name = name.as_ref().to_string();
                mutator(&mut item)?;
                self.push(item);
            }
        }
        Ok(())
    }
}

impl ApplyVolume for PodSpec {
    fn apply_volume<F, S>(&mut self, name: S, mutator: F) -> Result<()>
    where
        F: FnOnce(&mut Volume) -> Result<()>,
        S: AsRef<str>,
    {
        self.volumes
            .use_or_create(|volumes| volumes.apply_volume(name, mutator))
    }
}

pub trait ApplyVolumeMount {
    fn apply_volume_mount<F, S>(&mut self, name: S, mutator: F) -> Result<()>
    where
        F: FnOnce(&mut VolumeMount) -> Result<()>,
        S: AsRef<str>;

    fn apply_volume_mount_simple<S1, S2>(
        &mut self,
        name: S1,
        path: S2,
        read_only: bool,
    ) -> Result<()>
    where
        S1: AsRef<str>,
        S2: ToString,
    {
        self.apply_volume_mount(name, |mount| {
            mount.mount_path = path.to_string();
            mount.read_only = Some(read_only);
            mount.mount_propagation = None;
            mount.sub_path = None;
            mount.sub_path_expr = None;
            Ok(())
        })
    }
}

impl ApplyVolumeMount for Vec<VolumeMount> {
    fn apply_volume_mount<F, S>(&mut self, name: S, mutator: F) -> Result<()>
    where
        F: FnOnce(&mut VolumeMount) -> Result<()>,
        S: AsRef<str>,
    {
        let c = self.iter_mut().find(|c| &c.name == name.as_ref());
        match c {
            Some(c) => {
                mutator(c)?;
            }
            None => {
                let mut item: VolumeMount = Default::default();
                item.name = name.as_ref().to_string();
                mutator(&mut item)?;
                self.push(item);
            }
        }
        Ok(())
    }
}

impl ApplyVolumeMount for Container {
    fn apply_volume_mount<F, S>(&mut self, name: S, mutator: F) -> Result<()>
    where
        F: FnOnce(&mut VolumeMount) -> Result<()>,
        S: AsRef<str>,
    {
        self.volume_mounts
            .use_or_create(|volume_mounts| volume_mounts.apply_volume_mount(name, mutator))
    }
}