K8s Go

Manage K8S service with Go script

2

Votes

package cmd

import (
    "fmt"
    "io"
    "sync"

    "github.com/deis/deis/deisctl/backend"
    "github.com/deis/deis/pkg/prettyprint"
)

// Installk8s loads all Mesos units for StartMesos
func InstallK8s(b backend.Backend) error {

    var wg sync.WaitGroup

    io.WriteString(Stdout, prettyprint.DeisIfy("Installing Mesos..."))

    installK8sServices(b, &wg, Stdout, Stderr)

    wg.Wait()

    fmt.Fprintln(Stdout, "Done.")
    fmt.Fprintln(Stdout, "")
    fmt.Fprintln(Stdout, "Please run `deisctl start mesos` to boot up Mesos.")
    return nil
}

func installK8sServices(b backend.Backend, wg *sync.WaitGroup, out, err io.Writer) {

    fmt.Fprintln(out, "K8s API Server...")
    b.Create([]string{"kube-apiserver"}, wg, out, err)
    wg.Wait()

    fmt.Fprintln(out, "K8s controller and scheduler...")
    b.Create([]string{"kube-controller-manager", "kube-scheduler"}, wg, out, err)
    wg.Wait()

    fmt.Fprintln(out, "K8s proxy and kubelet...")
    b.Create([]string{"kube-proxy", "kube-kubelet"}, wg, out, err)
    wg.Wait()
}

// UninstallK8s unloads and uninstalls all Mesos component definitions
func UninstallK8s(b backend.Backend) error {

    var wg sync.WaitGroup

    io.WriteString(Stdout, prettyprint.DeisIfy("Uninstalling Kubernetes..."))

    uninstallk8sServices(b, &wg, Stdout, Stderr)

    wg.Wait()

    fmt.Fprintln(Stdout, "Done.")
    return nil
}

func uninstallk8sServices(b backend.Backend, wg *sync.WaitGroup, out, err io.Writer) error {

    fmt.Fprintln(out, "K8s proxy and kubelet ...")
    b.Destroy([]string{"kube-proxy", "kube-kubelet"}, wg, out, err)
    wg.Wait()

    fmt.Fprintln(out, "K8s controller and scheduler...")
    b.Destroy([]string{"kube-controller-manager", "kube-scheduler"}, wg, out, err)
    wg.Wait()

    fmt.Fprintln(out, "K8s API Server ...")
    b.Destroy([]string{"kube-apiserver"}, wg, out, err)
    wg.Wait()
    return nil
}

// StartK8s activates all Mesos components.
func StartK8s(b backend.Backend) error {

    var wg sync.WaitGroup

    io.WriteString(Stdout, prettyprint.DeisIfy("Starting Kubernetes..."))

    startK8sServices(b, &wg, Stdout, Stderr)

    wg.Wait()

    fmt.Fprintln(Stdout, "Done.")
    fmt.Fprintln(Stdout, "")
    fmt.Fprintln(Stdout, "Please use `deisctl config controller set schedulerModule=k8s`")
    return nil
}

func startK8sServices(b backend.Backend, wg *sync.WaitGroup, out, err io.Writer) {

    fmt.Fprintln(out, "K8s API Server...")
    b.Start([]string{"kube-apiserver"}, wg, out, err)
    wg.Wait()

    fmt.Fprintln(out, "K8s controller and scheduler...")
    b.Start([]string{"kube-controller-manager", "kube-scheduler"}, wg, out, err)
    wg.Wait()

    fmt.Fprintln(out, "K8s proxy and kubelet...")
    b.Start([]string{"kube-proxy", "kube-kubelet"}, wg, out, err)
    wg.Wait()
}

// StopK8s deactivates all Mesos components.
func StopK8s(b backend.Backend) error {

    var wg sync.WaitGroup

    io.WriteString(Stdout, prettyprint.DeisIfy("Stopping Kubernetes..."))

    stopK8sServices(b, &wg, Stdout, Stderr)

    wg.Wait()

    fmt.Fprintln(Stdout, "Done.")
    fmt.Fprintln(Stdout, "")
    fmt.Fprintln(Stdout, "Please run `deisctl start k8s` to restart K8s.")
    return nil
}

func stopK8sServices(b backend.Backend, wg *sync.WaitGroup, out, err io.Writer) {

    fmt.Fprintln(out, "K8s API Server...")
    b.Stop([]string{"kube-apiserver"}, wg, out, err)
    wg.Wait()

    fmt.Fprintln(out, "K8s controller and scheduler...")
    b.Stop([]string{"kube-controller-manager", "kube-scheduler"}, wg, out, err)
    wg.Wait()

    fmt.Fprintln(out, "K8s proxy and kubelet...")
    b.Stop([]string{"kube-proxy", "kube-kubelet"}, wg, out, err)
    wg.Wait()
}

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png