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
use super::*;

pub trait ServiceExt: super::ResourceBuilder {
    fn new(name: impl ToString) -> Self;
    fn cluster_ip(
        name: impl ToString,
        ports: impl IntoIterator<Item = corev1::ServicePort>,
    ) -> 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,
            // spec: todo!(),
            // status: todo!(),
            ..default()
        }
    }

    fn cluster_ip(
        name: impl ToString,
        ports: impl IntoIterator<Item = corev1::ServicePort>,
    ) -> Self {
        let mut service = Self::with_type(name, "ClusterIP");
        service
            .spec_mut()
            .ports
            .replace(ports.into_iter().collect());
        service
    }

    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 mut service = Self::with_type(name, "ExternalName");
        service
            .spec_mut()
            .external_name
            .replace(external_name.to_string());
        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(
        mut 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();
        self.spec_mut().selector.replace(labels);
        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_, ..default() };
        Self::new(name).spec(spec)
    }
}

impl HasSpec for corev1::Service {
    type Spec = corev1::ServiceSpec;

    fn spec_mut(&mut self) -> &mut Self::Spec {
        self.spec.get_or_insert_with(default)
    }
}