Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #161 from kinvolk/robertgzr/v02-refactor
Browse files Browse the repository at this point in the history
Refactor + CLI restructure
  • Loading branch information
Dongsu Park authored Oct 26, 2017
2 parents 57ca23c + 7f6b7ef commit 3803ef6
Show file tree
Hide file tree
Showing 1,360 changed files with 588,386 additions and 1,360 deletions.
68 changes: 67 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
branch = "master"
name = "github.com/spf13/cobra"

[[constraint]]
branch = "master"
name = "github.com/spf13/viper"

[[constraint]]
branch = "master"
name = "github.com/vishvananda/netlink"
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ $ go get -d github.com/kinvolk/kube-spawn
$ cd $GOPATH/src/github.com/kinvolk/kube-spawn
$ make all
$ sudo GOPATH=$GOPATH CNI_PATH=$GOPATH/bin ./kube-spawn up --image=coreos --nodes=3
```
$ export CNI_PATH=$GOPATH/bin
The `up` subcommand pulls the image, sets up the nodes and then configures the cluster using [kubeadm](https://github.com/kubernetes/kubeadm).
# This generated a default 3 nodes cluster configuration
$ sudo -E ./kube-spawn create --nodes=3
$ sudo -E ./kube-spawn start
```

The `create` subcommand sets up a cluster environment in `/var/lib/kube-spawn`, and puts all the neccessary
scripts/configs into place for running the cluster.
Via `start` you bring up the nodes and `kube-spawn` configures the cluster using [kubeadm](https://github.com/kubernetes/kubeadm).
Now that you're up and running, you can start using it.

After stopping the cluster with `stop` you don't have to run `create` again, unless you want to change the cluster config in
`/var/lib/kube-spawn/CLUSTER_NAME/kspawn.toml`.

## How to..

### Deploy to your local cluster
Expand Down Expand Up @@ -100,10 +108,8 @@ Assuming you have built `kube-spawn` and pulled the CoreOS image, do:

```
# Spawn and provision nodes for the cluster
$ sudo GOPATH=$GOPATH CNI_PATH=$GOPATH/bin ./kube-spawn --kubernetes-version=dev setup --image=coreos --nodes=3
# Setup Kubernetes
$ sudo GOPATH=$GOPATH CNI_PATH=$GOPATH/bin ./kube-spawn --kubernetes-version=dev init
$ sudo -E ./kube-spawn create --dev
$ sudo -E ./kube-spawn start
```

### Access a kube-spawn node
Expand Down
165 changes: 165 additions & 0 deletions cmd/kube-spawn/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
Copyright 2017 Kinvolk GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"log"
"os"
"path"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/kinvolk/kube-spawn/pkg/bootstrap"
"github.com/kinvolk/kube-spawn/pkg/config"
"github.com/kinvolk/kube-spawn/pkg/utils"
)

var (
createCmd = &cobra.Command{
Use: "create",
Short: "Generates the environment for a cluster. If you change 'kspawn.toml' this needs to be run again.",
Run: runCreate,
}
)

func init() {
kubespawnCmd.AddCommand(createCmd)

// do not set defaults here
// intead use:
// pkg/config/defaults.go
// and call from the if uninitialized {} block below
//
createCmd.Flags().StringP("container-runtime", "r", "", "runtime to use for the spawned cluster (docker or rkt)")
createCmd.Flags().String("kubernetes-version", "", `version kubernetes used to initialize the cluster. Irrelevant if used wih --dev. Only accepts semantic version strings like "v1.7.5"`)
createCmd.Flags().Bool("dev", false, "create a cluster from a local build of Kubernetes")
createCmd.Flags().IntP("nodes", "n", 0, "number of nodes to spawn")
createCmd.Flags().StringP("image", "i", "", "base image for nodes")
viper.BindPFlags(createCmd.Flags())

viper.BindEnv("runtime-config.rkt.rkt-bin", "KUBE_SPAWN_RKT_BIN")
viper.BindEnv("runtime-config.rkt.stage1-image", "KUBE_SPAWN_RKT_STAGE1_IMAGE")
viper.BindEnv("runtime-config.rkt.rktlet-bin", "KUBE_SPAWN_RKTLET_BIN")

viper.BindEnv("runtime-config.crio.crio-bin", "KUBE_SPAWN_CRIO_BIN")
viper.BindEnv("runtime-config.crio.runc-bin", "KUBE_SPAWN_RUNC_BIN")
viper.BindEnv("runtime-config.crio.conmon-bin", "KUBE_SPAWN_CONMON_BIN")

config.SetDefaults_Viper(viper.GetViper())
}

func runCreate(cmd *cobra.Command, args []string) {
cfg, err := config.LoadConfig()
if err != nil {
// ignore if config not found
// it means we started from scratch and need to generate one
if !config.IsNotFound(err) {
log.Fatal(errors.Wrap(err, "unable to load config"))
}
}
log.Printf("creating cluster environment %q", cfg.Name)
if cfg.DevCluster {
log.Printf("spawning from local kubernetes build")
} else {
log.Printf("spawning kubernetes version %q", cfg.KubernetesVersion)
}
if cfg.ContainerRuntime != config.RuntimeDocker {
log.Printf("spawning with container runtime %q", cfg.ContainerRuntime)
}

if utils.CheckVersionConstraint(cfg.KubernetesVersion, "<1.7.5") {
log.Fatal("minimum supported version is 'v1.7.5'")
}

// download files into cache
if !cfg.DevCluster {
if err := bootstrap.DownloadK8sBins(cfg); err != nil {
log.Fatal(err)
}
}
if err := bootstrap.DownloadSocatBin(cfg); err != nil {
log.Fatal(err)
}

if err := config.SetDefaults_Kubernetes(cfg); err != nil {
log.Fatal(errors.Wrap(err, "error settting kubernetes defaults"))
}

if err := config.SetDefaults_BindmountConfiguration(cfg); err != nil {
log.Fatal(errors.Wrap(err, "error setting bindmount defaults"))
}

if err := config.SetDefaults_RuntimeConfiguration(cfg); err != nil {
log.Fatal(errors.Wrap(err, "error setting container runtime defaults"))
}

// note: this is a workaround the keyctl issue with runc
// can be removed when systemd v235 is common
// TODO: move this somewhere else and reuse code from utils pkg
goPath := os.Getenv("GOPATH")
if goPath == "" {
log.Fatal("GOPATH was not set")
}
cfg.Copymap = append(cfg.Copymap, config.Pathmap{
Dst: "/usr/bin/kube-spawn-runc",
Src: path.Join(goPath, "src/github.com/kinvolk/kube-spawn/kube-spawn-runc"),
})

if cfg.Image == config.DefaultBaseImage {
if err := bootstrap.PrepareCoreosImage(); err != nil {
log.Fatal(errors.Wrap(err, "error setting up default base image"))
}
}

// TODO: check config + env
// - check version
// - check version of k8s binaries
// - cni bridge works
// - base image exists
// - ??? coreos version correct
// - overlayfs works
// - conntrack hashsize
// - iptables setup correct
// - selinux setup correct
// if err := checks.RunCreateChecks(cfg); err != nil {
// log.Fatal(errors.Wrap(err, "check failed"))
// }

if err := bootstrap.PathSupportsOverlay(cfg.KubeSpawnDir); err != nil {
log.Fatalf("unable to use overlayfs on %q: %v. Try to pass a directory with a different filesystem (like ext4 or XFS) to --dir.", cfg.KubeSpawnDir, err)
}

log.Print("generating scripts")
if err := bootstrap.GenerateScripts(cfg); err != nil {
log.Fatal(errors.Wrap(err, "error generating files"))
}

log.Print("copy files into environment")
if err := bootstrap.CopyFiles(cfg); err != nil {
log.Fatal(errors.Wrap(err, "error copying files"))
}

log.Print("ensuring environment")
if err := bootstrap.EnsureRequirements(cfg); err != nil {
log.Fatal(err)
}

saveConfig(cfg)
log.Println("created cluster config")
}
58 changes: 58 additions & 0 deletions cmd/kube-spawn/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2017 Kinvolk GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"log"
"os"
"path"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/kinvolk/kube-spawn/pkg/config"
)

var (
destroyCmd = &cobra.Command{
Use: "destroy",
// Aliases: []string{"setup, up"},
Short: "Start the nodes of a generated cluster. You should have run `kube-spawn create` before this",
Run: runDestroy,
}
)

func init() {
kubespawnCmd.AddCommand(destroyCmd)
}

func runDestroy(cmd *cobra.Command, args []string) {
cfg := loadConfig()
doDestroy(cfg)
}

func doDestroy(cfg *config.ClusterConfiguration) {
log.Printf("destroying cluster %q", cfg.Name)

doStop(cfg, true)

cDir := path.Join(cfg.KubeSpawnDir, cfg.Name)
if err := os.RemoveAll(cDir); err != nil {
log.Fatal(errors.Wrapf(err, "error removing cluster dir at %q", cDir))
}
log.Printf("%q destroyed", cfg.Name)
}
Loading

0 comments on commit 3803ef6

Please sign in to comment.