Skip to content

Commit

Permalink
Merge pull request #250 from cornelk/master
Browse files Browse the repository at this point in the history
Add Kubernetes Dashboard addon
  • Loading branch information
xetys authored Feb 20, 2019
2 parents 2cf3224 + 04f7414 commit ba6299b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
92 changes: 92 additions & 0 deletions pkg/addons/addon_dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package addons

import (
"fmt"

"github.com/xetys/hetzner-kube/pkg/clustermanager"
)

// DashboardAddon installs dashboard
type DashboardAddon struct {
masterNode *clustermanager.Node
communicator clustermanager.NodeCommunicator
}

// NewDashboardAddon installs dashboard to the cluster
func NewDashboardAddon(provider clustermanager.ClusterProvider, communicator clustermanager.NodeCommunicator) ClusterAddon {
masterNode, _ := provider.GetMasterNode()
return DashboardAddon{masterNode: masterNode, communicator: communicator}
}

func init() {
addAddon(NewDashboardAddon)
}

// Name returns the addons name
func (addon DashboardAddon) Name() string {
return "dashboard"
}

// Requires returns a slice with the name of required addons
func (addon DashboardAddon) Requires() []string {
return []string{}
}

// Description returns the addons description
func (addon DashboardAddon) Description() string {
return "Kubernetes Dashboard"
}

// URL returns the URL of the addons underlying project
func (addon DashboardAddon) URL() string {
return "https://github.com/kubernetes/dashboard"
}

// Install performs all steps to install the addon
func (addon DashboardAddon) Install(args ...string) {
node := *addon.masterNode
_, err := addon.communicator.RunCmd(node, "kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml")
FatalOnError(err)

serviceAccount := `apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kube-system`
err = addon.communicator.WriteFile(node, "/root/dashboard-service-account.yaml", serviceAccount, false)
FatalOnError(err)

_, err = addon.communicator.RunCmd(node, "kubectl apply -f dashboard-service-account.yaml")
FatalOnError(err)

token, err := addon.communicator.RunCmd(node, "kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}') | grep -E '^token' | cut -f2 -d':' | tr -d ' \t'")
FatalOnError(err)

fmt.Printf("Use the following token to login to the dashboard: %s\n", token)
fmt.Println("Dashboard installed")
}

// Uninstall performs all steps to remove the addon
func (addon DashboardAddon) Uninstall() {
node := *addon.masterNode
_, err := addon.communicator.RunCmd(node, "kubectl delete -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml")
FatalOnError(err)

_, err = addon.communicator.RunCmd(node, "kubectl delete -f dashboard-service-account.yaml")
FatalOnError(err)

fmt.Println("Dashboard uninstalled")
}
2 changes: 1 addition & 1 deletion pkg/addons/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package addons

import "log"

// FatalOnError is an helper function used to transfor error to fatal
// FatalOnError is an helper function used to transfer error to fatal
func FatalOnError(err error) {
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit ba6299b

Please sign in to comment.