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
use super::*;
pub trait ServiceExt: super::ResourceBuilder {
    fn new(name: impl ToString) -> Self;
    fn cluster_ip(name: impl ToString) -> Self;
    fn node_port(name: impl ToString) -> Self;
    fn load_balancer(name: impl ToString) -> Self;
    fn external_name(name: impl ToString, external_name: impl ToString) -> Self;
    fn with_labels(
        name: impl ToString,
        labels: impl IntoIterator<Item = (impl ToString, impl ToString)>,
    ) -> Self;
    fn spec(self, spec: corev1::ServiceSpec) -> Self;
    fn selector(
        self,
        match_labels: impl IntoIterator<Item = (impl ToString, impl ToString)>,
    ) -> Self;
}
impl ServiceExt for corev1::Service {
    fn new(name: impl ToString) -> Self {
        let metadata = metadata(name);
        Self {
            metadata,
            ..default()
        }
    }
    fn cluster_ip(name: impl ToString) -> Self {
        Self::with_type(name, "ClusterIP")
    }
    fn node_port(name: impl ToString) -> Self {
        Self::with_type(name, "NodePort")
    }
    fn load_balancer(name: impl ToString) -> Self {
        Self::with_type(name, "LoadBalancer")
    }
    fn external_name(name: impl ToString, external_name: impl ToString) -> Self {
        let service = Self::with_type(name, "ExternalName");
        let mut spec = service.spec.unwrap_or_default();
        spec.external_name = Some(external_name.to_string());
        Self {
            spec: Some(spec),
            ..service
        }
    }
    fn with_labels(
        name: impl ToString,
        labels: impl IntoIterator<Item = (impl ToString, impl ToString)>,
    ) -> Self {
        Self::new(name).labels(labels)
    }
    fn spec(self, spec: corev1::ServiceSpec) -> Self {
        Self {
            spec: Some(spec),
            ..self
        }
    }
    fn selector(self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self {
        let labels = labels
            .into_iter()
            .map(|(key, value)| (key.to_string(), value.to_string()))
            .collect();
        let mut spec = self.spec.unwrap_or_default();
        spec.selector = Some(labels);
        Self {
            spec: Some(spec),
            ..self
        }
    }
}
trait ServiceExtPrivate {
    fn with_type(name: impl ToString, r#type: impl ToString) -> Self;
}
impl ServiceExtPrivate for corev1::Service {
    fn with_type(name: impl ToString, r#type: impl ToString) -> Self {
        let type_ = Some(r#type.to_string());
        let spec = corev1::ServiceSpec {
            type_,
            ..corev1::ServiceSpec::default()
        };
        Self::new(name).spec(spec)
    }
}