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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use anyhow::Error;
use clap::{Arg, ArgMatches};
use k8s_openapi::api::apps::v1::Deployment;
use k8s_openapi::api::core::v1::{Secret, ServiceAccount};
use k8s_openapi::api::rbac::v1::{ClusterRole, ClusterRoleBinding};
use kube::api::{DeleteParams, PostParams};
use kube::client::APIClient;
use kube::Api;

use async_trait::async_trait;

#[derive(Debug, PartialEq, Clone)]
pub struct Targets {
    namespace: String,
    registry_ulrs: Vec<String>,
}

impl Targets {
    pub fn args() -> Vec<Arg<'static, 'static>> {
        vec![
            Arg::with_name("registry_url")
                .multiple(true)
                .short("r")
                .long("registry-url")
                .number_of_values(1)
                .default_value("https://gcr.io")
                .value_name("URL")
                .help("The registry to forward authentication for"),
            Arg::with_name("namespace")
                .short("n")
                .long("namespace")
                .takes_value(true)
                .value_name("NAMESPACE")
                .help("The namespace to create the secret in, if not current namespace"),
        ]
    }

    pub fn new(namespace: &str, registry_urls: &[&str]) -> Self {
        Self {
            namespace: namespace.into(),
            registry_ulrs: registry_urls.iter().map(|s| (*s).to_string()).collect(),
        }
    }

    pub fn from_matches(matches: &ArgMatches, default_namespace: &str) -> Self {
        Self {
            namespace: matches
                .value_of("namespace")
                .map(From::from)
                .unwrap_or_else(|| default_namespace.into()),
            registry_ulrs: matches
                .values_of("registry_url")
                .map(|v| v.map(|s| s.to_string()).collect())
                .unwrap_or_else(Vec::new),
        }
    }

    pub fn namespace(&self) -> &str {
        &self.namespace
    }

    pub fn registry_urls(&self) -> Vec<&str> {
        self.registry_ulrs.iter().map(AsRef::as_ref).collect()
    }
}

#[async_trait]
pub trait KubeCrud {
    async fn upsert(&self, client: &APIClient, namespace: &str, name: &str) -> anyhow::Result<()>;
    async fn delete(client: &APIClient, namespace: &str, name: &str) -> anyhow::Result<()>;
}

#[async_trait]
impl KubeCrud for Secret {
    async fn upsert(&self, client: &APIClient, namespace: &str, name: &str) -> anyhow::Result<()> {
        let pp = PostParams::default();
        let secrets: Api<Secret> = Api::namespaced(client.clone(), namespace);
        if secrets.get(&name).await.is_ok() {
            secrets.replace(&name, &pp, &self).await?;
            debug!("Secret {} updated", name);
        } else {
            secrets.create(&pp, &self).await?;
            debug!("Secret {} created", name);
        }
        Ok(())
    }

    async fn delete(client: &APIClient, namespace: &str, name: &str) -> anyhow::Result<()> {
        let dp = DeleteParams::default();
        let objects: Api<Secret> = Api::namespaced(client.clone(), namespace);
        if objects.delete(&name, &dp).await.is_ok() {
            debug!("Secret {} deleted", name);
        }
        Ok(())
    }
}

#[async_trait]
impl KubeCrud for ServiceAccount {
    async fn upsert(&self, client: &APIClient, namespace: &str, name: &str) -> anyhow::Result<()> {
        let pp = PostParams::default();
        let accounts: Api<ServiceAccount> = Api::namespaced(client.clone(), namespace);
        if let Ok(existing) = accounts.get(&name).await {
            let object = self.clone();
            let object = ServiceAccount {
                metadata: object.metadata.or(existing.metadata),
                automount_service_account_token: object
                    .automount_service_account_token
                    .or(existing.automount_service_account_token),
                secrets: object.secrets.or(existing.secrets),
                image_pull_secrets: object.image_pull_secrets.or(existing.image_pull_secrets),
            };
            accounts.replace(&name, &pp, &object).await?;
            debug!("ServiceAccount {} updated", name);
        } else {
            accounts.create(&pp, &self).await?;
            debug!("ServiceAccount {} created", name);
        }
        Ok(())
    }
    async fn delete(client: &APIClient, namespace: &str, name: &str) -> anyhow::Result<()> {
        let dp = DeleteParams::default();
        let objects: Api<ServiceAccount> = Api::namespaced(client.clone(), namespace);
        if objects.delete(&name, &dp).await.is_ok() {
            debug!("ServiceAccount {} deleted", name);
        }
        Ok(())
    }
}

#[async_trait]
impl KubeCrud for Deployment {
    async fn upsert(&self, client: &APIClient, namespace: &str, name: &str) -> Result<(), Error> {
        let pp = PostParams::default();
        let deployments: Api<Deployment> = Api::namespaced(client.clone(), namespace);
        if deployments.get(&name).await.is_ok() {
            if deployments.replace(&name, &pp, &self).await.is_err() {
                debug!(
                    "Deployment {} cannot be updated, trying to delete and recreate",
                    name
                );
                deployments.delete(&name, &DeleteParams::default()).await?;
                deployments.create(&pp, &self).await?;
            };
            debug!("Deployment {} updated", name);
        } else {
            deployments.create(&pp, &self).await?;
            debug!("Deployment {} created", name);
        }
        Ok(())
    }
    async fn delete(client: &APIClient, namespace: &str, name: &str) -> anyhow::Result<()> {
        let dp = DeleteParams::default();
        let objects: Api<Deployment> = Api::namespaced(client.clone(), namespace);
        if objects.delete(&name, &dp).await.is_ok() {
            debug!("Deployment {} deleted", name);
        }
        Ok(())
    }
}

#[async_trait]
impl KubeCrud for ClusterRoleBinding {
    async fn upsert(&self, client: &APIClient, _namespace: &str, name: &str) -> Result<(), Error> {
        let pp = PostParams::default();
        let bindings: Api<ClusterRoleBinding> = Api::all(client.clone());
        if bindings.get(&name).await.is_ok() {
            bindings.replace(&name, &pp, &self).await?;
            debug!("ClusterRoleBinding {} updated", name);
        } else {
            bindings.create(&pp, &self).await?;
            debug!("ClusterRoleBinding {} created", name);
        }
        Ok(())
    }
    async fn delete(client: &APIClient, _namespace: &str, name: &str) -> anyhow::Result<()> {
        let dp = DeleteParams::default();
        let objects: Api<ClusterRoleBinding> = Api::all(client.clone());
        if objects.delete(&name, &dp).await.is_ok() {
            debug!("ClusterRoleBinding {} deleted", name);
        }
        Ok(())
    }
}

#[async_trait]
impl KubeCrud for ClusterRole {
    async fn upsert(&self, client: &APIClient, _namespace: &str, name: &str) -> Result<(), Error> {
        let pp = PostParams::default();
        let bindings: Api<ClusterRole> = Api::all(client.clone());
        if bindings.get(&name).await.is_ok() {
            bindings.replace(&name, &pp, &self).await?;
            debug!("ClusterRole {} updated", name);
        } else {
            bindings.create(&pp, &self).await?;
            debug!("ClusterRole {} created", name);
        }
        Ok(())
    }
    async fn delete(client: &APIClient, _namespace: &str, name: &str) -> anyhow::Result<()> {
        let dp = DeleteParams::default();
        let objects: Api<ClusterRole> = Api::all(client.clone());
        if objects.delete(&name, &dp).await.is_ok() {
            debug!("ClusterRole {} deleted", name);
        }
        Ok(())
    }
}