Skip to content

Commit

Permalink
addon support
Browse files Browse the repository at this point in the history
close #18
  • Loading branch information
xetys committed Feb 27, 2018
1 parent 5382f7d commit 7216cbc
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 0 deletions.
129 changes: 129 additions & 0 deletions cmd/addons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package cmd

import (
"fmt"
"time"
)

type ClusterAddon interface {
Install(args ... string)
Uninstall()
}

func AddonExists(addonName string) bool {
switch addonName {
case "helm", "rook":
return true
default:
return false
}
}

func (cluster Cluster) GetAddon(addonName string) ClusterAddon {
switch addonName {
case "helm":
return NewHelmAddon(cluster)
case "rook":
return NewRookAddon(cluster)
default:
return nil
}
}

type HelmAddon struct {
masterNode *Node
}

func NewHelmAddon(cluster Cluster) ClusterAddon {
masterNode, _ := cluster.GetMasterNode()
return HelmAddon{masterNode: masterNode}
}

func (addon HelmAddon) Install(args ... string) {

node := *addon.masterNode
_, err := runCmd(node, "curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get | bash")
FatalOnError(err)
serviceAccount := `apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system`
err = writeNodeFile(node, "/root/helm-service-account", serviceAccount, false)
FatalOnError(err)

_, err = runCmd(node, "helm init --service-account tiller")
FatalOnError(err)

fmt.Println("Helm installed")
}

func (addon HelmAddon) Uninstall() {
node := *addon.masterNode
_, err := runCmd(node, "helm init --service-account tiller")
FatalOnError(err)

fmt.Println("Helm uninstalled")
}

type RookAddon struct {
masterNode *Node
}

func NewRookAddon(cluster Cluster) ClusterAddon {
masterNode, _ := cluster.GetMasterNode()
return RookAddon{masterNode: masterNode}
}

func (addon RookAddon) Install(args ... string) {
node := *addon.masterNode

_, err := runCmd(node, "kubectl apply -f https://github.com/rook/rook/raw/master/cluster/examples/kubernetes/rook-operator.yaml")
FatalOnError(err)
time.Sleep(15 * time.Second)
_, err = runCmd(node, "kubectl apply -f https://github.com/rook/rook/raw/master/cluster/examples/kubernetes/rook-cluster.yaml")
FatalOnError(err)
_, err = runCmd(node, "kubectl apply -f https://github.com/rook/rook/raw/master/cluster/examples/kubernetes/rook-storageclass.yaml")
FatalOnError(err)

fmt.Println("Rook installed")
}

func (addon RookAddon) Uninstall() {
node := *addon.masterNode
_, err := runCmd(node, "kubectl delete -n rook pool replicapool")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete storageclass rook-block")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete -n kube-system secret rook-admin")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete thirdpartyresources cluster.rook.io pool.rook.io objectstore.rook.io filesystem.rook.io volumeattachment.rook.io # ignore errors if on K8s 1.7+")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete crd clusters.rook.io pools.rook.io objectstores.rook.io filesystems.rook.io volumeattachments.rook.io # ignore errors if on K8s 1.5 and 1.6")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete -n rook-system daemonset rook-agent")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete -f rook-operator.yaml")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete clusterroles rook-agent")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete clusterrolebindings rook-agent")
FatalOnError(err)
_, err = runCmd(node, "kubectl delete namespace rook")
FatalOnError(err)

fmt.Println("Rook uninstalled")
}
33 changes: 33 additions & 0 deletions cmd/clusterAddon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// 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 cmd

import (
"github.com/spf13/cobra"
)

// clusterAddonCmd represents the cluster addon command
var clusterAddonCmd = &cobra.Command{
Use: "addon",
Short: "manages addons for kubernetes clusters",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}

func init() {
clusterCmd.AddCommand(clusterAddonCmd)

}
69 changes: 69 additions & 0 deletions cmd/clusterAddonInstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// 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 cmd

import (
"fmt"

"github.com/spf13/cobra"
"errors"
"log"
)

// clusterAddonInstallCmd represents the clusterAddonInstall command
var clusterAddonInstallCmd = &cobra.Command{
Use: "install",
Short: "installs an addon to a cluster",
PreRunE: func(cmd *cobra.Command, args []string) error {
name, err := cmd.Flags().GetString("name")
if err != nil {
return nil
}

if name == "" {
return errors.New("flag --name is required")
}

idx, _ := AppConf.Config.FindClusterByName(name)

if idx == -1 {
return errors.New(fmt.Sprintf("cluster '%s' not found", name))
}
if len(args) != 1 {
return errors.New("exactly one argument expected")
}
addonName := args[0]
if !AddonExists(addonName) {
return errors.New(fmt.Sprintf("addon %s not found", addonName))
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
addonName := args[0]

_, cluster := AppConf.Config.FindClusterByName(name)

addon := cluster.GetAddon(addonName)
addon.Install()

log.Printf("addon %s successfully installed", addonName)
},
}

func init() {
clusterAddonCmd.AddCommand(clusterAddonInstallCmd)
clusterAddonInstallCmd.Flags().StringP("name", "n", "", "Name of the cluster")
}
69 changes: 69 additions & 0 deletions cmd/clusterAddonUninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// 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 cmd

import (
"fmt"

"github.com/spf13/cobra"
"errors"
"log"
)

// clusterAddonInstallCmd represents the clusterAddonInstall command
var clusterAddonUninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "removes an addon to a cluster",
PreRunE: func(cmd *cobra.Command, args []string) error {
name, err := cmd.Flags().GetString("name")
if err != nil {
return nil
}

if name == "" {
return errors.New("flag --name is required")
}

idx, _ := AppConf.Config.FindClusterByName(name)

if idx == -1 {
return errors.New(fmt.Sprintf("cluster '%s' not found", name))
}
if len(args) != 1 {
return errors.New("exactly one argument expected")
}
addonName := args[0]
if !AddonExists(addonName) {
return errors.New(fmt.Sprintf("addon %s not found", addonName))
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
addonName := args[0]

_, cluster := AppConf.Config.FindClusterByName(name)

addon := cluster.GetAddon(addonName)
addon.Uninstall()

log.Printf("addon %s successfully removed", addonName)
},
}

func init() {
clusterAddonCmd.AddCommand(clusterAddonUninstallCmd)
clusterAddonUninstallCmd.Flags().StringP("name", "n", "", "Name of the cluster")
}

0 comments on commit 7216cbc

Please sign in to comment.