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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! # kubernix
#![deny(missing_docs)]

mod apiserver;
mod config;
mod controllermanager;
mod coredns;
mod crio;
mod encryptionconfig;
mod etcd;
mod kubeconfig;
mod kubelet;
mod pki;
mod process;
mod proxy;
mod scheduler;

pub use config::{Config, ConfigBuilder};

use apiserver::APIServer;
use controllermanager::ControllerManager;
use coredns::CoreDNS;
use crio::Crio;
use encryptionconfig::EncryptionConfig;
use etcd::Etcd;
use kubeconfig::KubeConfig;
use kubelet::Kubelet;
use pki::Pki;
use process::{Process, Startable};
use proxy::Proxy;
use scheduler::Scheduler;

use env_logger::Builder;
use failure::{bail, format_err, Fallible};
use ipnetwork::IpNetwork;
use log::{debug, error, info};
use nix::unistd::getuid;
use rayon::scope;
use std::{
    env::{current_exe, split_paths, var, var_os},
    fmt::Display,
    fs::{self, create_dir_all},
    net::{IpAddr, Ipv4Addr},
    path::{Path, PathBuf},
    process::Command,
};

const RUNTIME_ENV: &str = "CONTAINER_RUNTIME_ENDPOINT";
const KUBECONFIG_ENV: &str = "KUBECONFIG";
const NIX_SHELL_ENV: &str = "IN_NIX_SHELL";
const CRIO_DIR: &str = "crio";
const LOG_DIR: &str = "log";
const NIX_DIR: &str = "nix";
const KUBERNIX_ENV: &str = "kubernix.env";

type Stoppables = Vec<Startable>;

/// The main entry point for the application
pub struct Kubernix {
    config: Config,
    crio_socket: PathBuf,
    kubeconfig: KubeConfig,
    processes: Stoppables,
}

impl Kubernix {
    /// Start kubernix by consuming the provided configuration
    pub fn start(mut config: Config) -> Fallible<()> {
        Self::prepare_env(&mut config)?;

        // Bootstrap if we're not inside a nix shell
        if var(NIX_SHELL_ENV).is_err() {
            info!("Nix environment not found, bootstrapping one");
            Self::bootstrap_nix(config)
        } else {
            info!("Bootstrapping cluster inside nix environment");
            Self::bootstrap_cluster(config)
        }
    }

    /// Spawn a new shell into the provided configuration environment
    pub fn new_shell(mut config: Config) -> Fallible<()> {
        Self::prepare_env(&mut config)?;

        info!(
            "Spawning new kubernix shell in '{}'",
            config.root().display()
        );
        Command::new(Self::find_executable("nix-shell")?)
            .arg(&config.root().join(NIX_DIR))
            .arg("--pure")
            .arg("-Q")
            .arg(format!("-j{}", num_cpus::get()))
            .arg("--run")
            .arg(format!(
                "bash --init-file {}",
                config.root().join(KUBERNIX_ENV).display()
            ))
            .status()?;

        Ok(())
    }

    fn prepare_env(config: &mut Config) -> Fallible<()> {
        // Rootless is currently not supported
        if !getuid().is_root() {
            bail!("Please run kubernix as root")
        }

        // Prepare the configuration
        if config.root().exists() {
            config.update_from_file()?;
        } else {
            config.to_file()?;
        }
        config.canonicalize_root()?;

        // Setup the logger
        let mut builder = Builder::new();
        builder
            .format_timestamp(None)
            .filter(None, config.log_level())
            .try_init()?;

        Ok(())
    }

    /// Stop kubernix by cleaning up all running processes
    fn stop(&mut self) {
        for x in &mut self.processes {
            if let Err(e) = x.stop() {
                debug!("{}", e)
            }
        }
    }

    fn bootstrap_cluster(config: Config) -> Fallible<()> {
        // Retrieve the local IP
        let ip = Self::local_ip()?;
        let hostname =
            hostname::get_hostname().ok_or_else(|| format_err!("Unable to retrieve hostname"))?;
        info!("Using local IP {}", ip);

        // Setup the PKI
        let pki = Pki::new(&config, &ip, &hostname)?;

        // Setup the configs
        let kubeconfig = KubeConfig::new(&config, &pki, &ip, &hostname)?;
        let encryptionconfig = EncryptionConfig::new(&config)?;

        // Full path to the CRI socket
        let crio_socket = config.root().join(CRIO_DIR).join("crio.sock");

        // All processes
        let mut crio = Process::stopped();
        let mut etcd = Process::stopped();
        let mut apis = Process::stopped();
        let mut cont = Process::stopped();
        let mut sche = Process::stopped();
        let mut kube = Process::stopped();
        let mut prox = Process::stopped();

        // Spawn the processes
        info!("Starting processes");
        scope(|s| {
            s.spawn(|_| crio = Crio::start(&config, &crio_socket));
            s.spawn(|_| {
                etcd = Etcd::start(&config, &pki);
                apis = APIServer::start(&config, &ip, &pki, &encryptionconfig, &kubeconfig)
            });
            s.spawn(|_| cont = ControllerManager::start(&config, &pki, &kubeconfig));
            s.spawn(|_| sche = Scheduler::start(&config, &kubeconfig));
            s.spawn(|_| kube = Kubelet::start(&config, &pki, &kubeconfig, &crio_socket));
            s.spawn(|_| prox = Proxy::start(&config, &kubeconfig));
        });

        let mut processes = vec![];

        // This order is important since we will shut down the processes in its reverse
        let results = vec![kube, sche, prox, cont, apis, etcd, crio];
        let all_ok = results.iter().all(|x| x.is_ok());

        // Note: wait for `drain_filter()` to be stable and make it more straightforward
        for process in results {
            match process {
                Ok(p) => processes.push(p),
                Err(e) => error!("{}", e),
            }
        }

        // Setup the main instance
        let mut kubernix = Kubernix {
            config,
            crio_socket,
            kubeconfig,
            processes,
        };

        // No dead processes
        if all_ok {
            kubernix.apply_addons()?;

            info!("Everything is up and running");
            kubernix.spawn_shell()?;
        } else {
            error!("Unable to start all processes")
        }

        Ok(())
    }

    // Apply needed workloads to the running cluster. This method stops the cluster on any error.
    fn apply_addons(&mut self) -> Fallible<()> {
        if let Err(e) = CoreDNS::apply(&self.config, &self.kubeconfig) {
            bail!("Unable to apply CoreDNS: {}", e);
        }
        Ok(())
    }

    fn bootstrap_nix(config: Config) -> Fallible<()> {
        // Prepare the nix dir
        let nix_dir = config.root().join(NIX_DIR);
        create_dir_all(&nix_dir)?;

        // Write the configuration
        fs::write(
            nix_dir.join("nixpkgs.json"),
            include_str!("../nix/nixpkgs.json"),
        )?;
        fs::write(
            nix_dir.join("nixpkgs.nix"),
            include_str!("../nix/nixpkgs.nix"),
        )?;
        fs::write(
            nix_dir.join("default.nix"),
            include_str!("../nix/default.nix"),
        )?;
        fs::write(nix_dir.join("deps.nix"), include_str!("../nix/deps.nix"))?;

        // Run the shell
        let status = Command::new(Self::find_executable("nix-shell")?)
            .arg(nix_dir)
            .arg("--pure")
            .arg("-Q")
            .arg(format!("-j{}", num_cpus::get()))
            .arg("--run")
            .arg(
                &[
                    &current_exe()?.display().to_string(),
                    "--root",
                    &config.root().display().to_string(),
                    "--log-level",
                    &config.log_level().to_string().to_lowercase(),
                    "--crio-cidr",
                    &config.crio_cidr().to_string(),
                    "--cluster-cidr",
                    &config.cluster_cidr().to_string(),
                    "--service-cidr",
                    &config.service_cidr().to_string(),
                ]
                .join(" "),
            )
            .status()?;
        if !status.success() {
            bail!("nix-shell command failed");
        }
        Ok(())
    }

    fn spawn_shell(&self) -> Fallible<()> {
        info!("Spawning interactive shell");
        info!("Please be aware that the cluster gets destroyed if you exit the shell");
        let env_file = self.config.root().join(KUBERNIX_ENV);
        fs::write(
            &env_file,
            format!(
                "PS1='> '\nexport {}={}\nexport {}={}",
                RUNTIME_ENV,
                format!("unix://{}", self.crio_socket.display()),
                KUBECONFIG_ENV,
                self.kubeconfig.admin.display(),
            ),
        )?;

        Command::new("bash")
            .current_dir(&self.config.root().join(LOG_DIR))
            .arg("--init-file")
            .arg(env_file)
            .status()?;
        Ok(())
    }

    /// Retrieve the local hosts IP via the default route
    fn local_ip() -> Fallible<String> {
        let cmd = Command::new("ip")
            .arg("route")
            .arg("get")
            .arg("1.2.3.4")
            .output()?;
        if !cmd.status.success() {
            bail!("Unable to obtain `ip` output")
        }
        let output = String::from_utf8(cmd.stdout)?;
        let ip = output
            .split_whitespace()
            .nth(6)
            .ok_or_else(|| format_err!("Different `ip` command output expected"))?;
        if let Err(e) = ip.parse::<IpAddr>() {
            bail!("Unable to parse IP '{}': {}", ip, e);
        }
        Ok(ip.to_owned())
    }

    /// Find an executable inside the current $PATH environment
    fn find_executable<P>(name: P) -> Fallible<PathBuf>
    where
        P: AsRef<Path> + Display,
    {
        var_os("PATH")
            .and_then(|paths| {
                split_paths(&paths)
                    .filter_map(|dir| {
                        let full_path = dir.join(&name);
                        if full_path.is_file() {
                            Some(full_path)
                        } else {
                            None
                        }
                    })
                    .next()
            })
            .ok_or_else(|| format_err!("Unable to find {} in $PATH", name))
    }

    // Retrieve the DNS address from the config
    fn dns(config: &Config) -> Fallible<Ipv4Addr> {
        match config.service_cidr() {
            IpNetwork::V4(n) => Ok(n.nth(2).ok_or_else(|| {
                format_err!(
                    "Unable to retrieve second IP from service CIDR: {}",
                    config.service_cidr()
                )
            })?),
            _ => bail!("Service CIDR is not for IPv4"),
        }
    }
}

impl Drop for Kubernix {
    fn drop(&mut self) {
        info!("Cleaning up");
        self.stop();
    }
}