Commit a757e6d1 authored by Christopher Harm's avatar Christopher Harm

Adding support for kind k8s provider

parent cdfc7cc9
Pipeline #86395 passed with stages
in 57 seconds
......@@ -46,16 +46,23 @@ Use "devtool [command] --help" for more information about a command.
The devtool commands can be configured through CLI arguments, through ENV variables, or a global configuration file (~/.devtool.yaml)
```yaml
k8s:
provider: kind
minikube:
memory: 8192
cpu: 6
k8s:
version: v1.15.0
kind:
name: devtool
workers: 2
k8s:
version: v1.15.7
sha1sum: e2df133f80ef633c53c0200114fce2ed5e1f6947477dbc83261a6a921169488d
build:
skipCompile: false
runTests: true
openServices: true
openServices: false
webaccess:
user: crh5255
```
......
......@@ -39,13 +39,19 @@ var buildCmd = &cobra.Command{
return
}
err := environment.Run(false, "minikube", "status")
if err != nil {
if minikubeProvider() && !minikubeRunning() {
color.Red("Minikube is not started... run `devtool start` first.")
return
}
environment.EvalDockerEnv()
if kindProvider() && !kindRunning() {
color.Red("kind cluster is not created... run `devtool start` first.")
return
}
if minikubeProvider() {
environment.EvalDockerEnv()
}
fixKubectlContext()
......@@ -263,5 +269,14 @@ func buildDockerImage(deployable config.Deployable) (string, string, error) {
if err != nil {
return "", "", err
}
if kindProvider() {
color.New(color.FgGreen).Printf("Pushing image into the kind cluster")
err = environment.Run(true, "kind", "load", "docker-image", "--name", viper.GetString("kind.name"), dockerTag)
if err != nil {
return "", "", err
}
}
return image, tag.String(), nil
}
......@@ -16,6 +16,8 @@ package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
......@@ -33,35 +35,36 @@ var startCmd = &cobra.Command{
Use: "start",
Short: "start the minikube environment",
Run: func(command *cobra.Command, args []string) {
color.Red(viper.GetString("example"))
memory := strconv.Itoa(viper.GetInt("minikube.memory"))
cpu := strconv.Itoa(viper.GetInt("minikube.cpu"))
k8sVersion := viper.GetString("minikube.k8s.version")
start(memory, cpu, k8sVersion)
start()
},
}
const (
helmVersionTwoTag = "SemVer"
helmVersionTwoTag = "SemVer"
kindConfigTemplate = `kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
image: kindest/node:%s@sha256:%s
`
kindConfigWorkerTemplate = `- role: worker
image: kindest/node:%s@sha256:%s
`
)
func init() {
rootCmd.AddCommand(startCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// startCmd.PersistentFlags().String("foo", "", "A help for foo")
startCmd.PersistentFlags().StringP("k8s-provider", "p", "", "kubernetes provider (minikube or kind)")
viper.SetDefault("k8s.provider", "minikube")
err := viper.BindPFlag("k8s.provider", startCmd.PersistentFlags().Lookup("k8s-provider"))
if err != nil {
panic("invalid arg")
}
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
startCmd.PersistentFlags().IntP("memory", "m", 0, "memory for minikube (default is 8192)")
viper.SetDefault("minikube.memory", 8192)
err := viper.BindPFlag("minikube.memory", startCmd.PersistentFlags().Lookup("memory"))
err = viper.BindPFlag("minikube.memory", startCmd.PersistentFlags().Lookup("memory"))
if err != nil {
panic("invalid arg")
}
......@@ -79,9 +82,44 @@ func init() {
panic("invalid arg")
}
startCmd.PersistentFlags().StringP("kind-k8s-version", "", "", "kubernetes version for kind")
err = viper.BindPFlag("kind.k8s.version", startCmd.PersistentFlags().Lookup("kind-k8s-version"))
if err != nil {
panic("invalid arg")
}
startCmd.PersistentFlags().StringP("kind-k8s-sha1sum", "", "", "kubernetes sha1sum for kind")
err = viper.BindPFlag("kind.k8s.sha1sum", startCmd.PersistentFlags().Lookup("kind-k8s-sha1sum"))
if err != nil {
panic("invalid arg")
}
startCmd.PersistentFlags().StringP("kind-name", "n", "", "kind cluster name")
viper.SetDefault("kind.name", "devtool")
err = viper.BindPFlag("kind.name", startCmd.PersistentFlags().Lookup("kind-name"))
if err != nil {
panic("invalid arg")
}
startCmd.PersistentFlags().IntP("kind-workers", "w", 0, "# of worker nodes in the kind cluster")
viper.SetDefault("kind.workers", 1)
err = viper.BindPFlag("kind.workers", startCmd.PersistentFlags().Lookup("kind-workers"))
if err != nil {
panic("invalid arg")
}
}
func start(memory, cpu, k8sVersion string) {
func start() {
if minikubeProvider() {
color.Blue("Using minikube Kubernetes Provider")
} else if kindProvider() {
color.Blue("Using kind Kubernetes Provider")
} else {
color.Red("Unsupported k8s provider was specified")
os.Exit(-1)
}
color.Blue("Checking Prerequisites.")
prereqsInstalled := checkLocalSetup()
......@@ -99,18 +137,60 @@ func start(memory, cpu, k8sVersion string) {
environment.RunRequired(true, "helm", "repo", "add", "stable", "https://kubernetes-charts.storage.googleapis.com")
}
color.Blue("Setting up Minikube environment.")
if minikubeProvider() {
memory := strconv.Itoa(viper.GetInt("minikube.memory"))
cpu := strconv.Itoa(viper.GetInt("minikube.cpu"))
k8sVersion := viper.GetString("minikube.k8s.version")
fmt.Printf("Using memory: %s and cpu: %s\n", memory, cpu)
fmt.Printf("Using k8s version: %s\n", k8sVersion)
color.Blue("Setting up minikube environment.")
err := environment.Run(true, "minikube", "status")
if err != nil {
args := []string{"start", "--memory", memory, "--cpus", cpu}
if k8sVersion != "" {
args = append(args, "--kubernetes-version", k8sVersion)
fmt.Printf("Using memory: %s and cpu: %s\n", memory, cpu)
fmt.Printf("Using k8s version: %s\n", k8sVersion)
if !minikubeRunning() {
args := []string{"start", "--memory", memory, "--cpus", cpu}
if k8sVersion != "" {
args = append(args, "--kubernetes-version", k8sVersion)
}
environment.RunRequired(true, "minikube", args...)
}
environment.RunRequired(true, "minikube", args...)
}
if kindProvider() {
color.Blue("Setting up kind environment.")
clusterName := viper.GetString("kind.name")
if !kindRunning() {
k8sVersion := viper.GetString("kind.k8s.version") //"v1.15.7"
k8sSha1sum := viper.GetString("kind.k8s.sha1sum") //"e2df133f80ef633c53c0200114fce2ed5e1f6947477dbc83261a6a921169488d"
args := []string{"create", "cluster", "--name", clusterName}
if k8sVersion != "" && k8sSha1sum != "" {
file, err := ioutil.TempFile("/tmp", "devtool")
if err != nil {
log.Fatal(err)
}
defer os.Remove(file.Name())
config := fmt.Sprintf(kindConfigTemplate, k8sVersion, k8sSha1sum)
for i := 0; i < viper.GetInt("kind.workers"); i++ {
config += fmt.Sprintf(kindConfigWorkerTemplate, k8sVersion, k8sSha1sum)
}
_, err = file.WriteString(config)
if err != nil {
color.New(color.FgRed).Printf("Failed to write kind config file: %v", err)
}
color.Blue("Using kind config: ")
fmt.Println(config)
args = append(args, "--config", file.Name())
}
environment.RunRequired(true, "kind", args...)
}
}
fixKubectlContext()
......@@ -142,6 +222,34 @@ func helmVersion2() bool {
return keyValues[0] == helmVersionTwoTag
}
func minikubeProvider() bool {
return viper.GetString("k8s.provider") == "minikube"
}
func minikubeRunning() bool {
err := environment.Run(true, "minikube", "status")
return err == nil
}
func kindProvider() bool {
return viper.GetString("k8s.provider") == "kind"
}
func kindRunning() bool {
output := environment.RunAndGetOutputRequired("kind", "get", "clusters")
found := false
clusterName := viper.GetString("kind.name")
for _, o := range output {
if clusterName == o {
found = true
break
}
}
return found
}
func checkLocalSetup() bool {
var prereqsInstalled = true
err := checkInstalled("kubectl")
......@@ -154,9 +262,18 @@ func checkLocalSetup() bool {
prereqsInstalled = false
}
err = checkInstalled("minikube")
if err != nil {
prereqsInstalled = false
if minikubeProvider() {
err = checkInstalled("minikube")
if err != nil {
prereqsInstalled = false
}
}
if kindProvider() {
err = checkInstalled("kind")
if err != nil {
prereqsInstalled = false
}
}
return prereqsInstalled
......@@ -175,11 +292,18 @@ func checkInstalled(app string) error {
func fixKubectlContext() {
color.Blue("Setting kubectl context.")
ctx := ""
if minikubeProvider() {
ctx = "minikube"
} else if kindProvider() {
ctx = fmt.Sprintf("kind-%s", viper.GetString("kind.name"))
}
_, err := exec.LookPath("kubectx")
if err != nil {
color.New(color.FgYellow).Printf("kubectx is not installed")
} else {
environment.RunRequired(true, "kubectx", "minikube")
environment.RunRequired(true, "kubectx", ctx)
}
_, err = exec.LookPath("kubens")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment