Skip to content

Commit

Permalink
Merge pull request #38 from homeport/init-tiller
Browse files Browse the repository at this point in the history
Install tiller or upgrade when using helm client
  • Loading branch information
HeavyWombat authored Dec 8, 2018
2 parents 610b416 + 7308e71 commit 87320ca
Show file tree
Hide file tree
Showing 177 changed files with 14,799 additions and 29 deletions.
42 changes: 40 additions & 2 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion internal/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var logsCmd = &cobra.Command{
Short: "Retrieve log files from pods",
Long: `Retrieve log files from pods`,
Run: func(cmd *cobra.Command, args []string) {
clientSet, restconfig, err := havener.OutOfClusterAuthentication()
clientSet, restconfig, err := havener.OutOfClusterAuthentication("")
if err != nil {
exitWithError("unable to get access to cluster", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/nexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ and respective pod will be deleted after the command was executed.

havener.VerboseMessage("Connecting to Kubernetes cluster...")

client, restconfig, err := havener.OutOfClusterAuthentication()
client, restconfig, err := havener.OutOfClusterAuthentication("")
if err != nil {
exitWithError("failed to connect to Kubernetes cluster", err)
}
Expand Down
8 changes: 2 additions & 6 deletions internal/cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"strings"

"github.com/homeport/gonvenience/pkg/v1/wait"
Expand Down Expand Up @@ -55,7 +54,7 @@ If multiple Helm Releases are specified, then they will deleted concurrently.
Run: func(cmd *cobra.Command, args []string) {
havener.VerboseMessage("Accessing cluster...")

client, _, err := havener.OutOfClusterAuthentication()
client, _, err := havener.OutOfClusterAuthentication("")
if err != nil {
exitWithError("unable to get access to cluster", err)
}
Expand All @@ -70,10 +69,7 @@ func getConfiguredHelmClient() *helm.Client {

havener.VerboseMessage("Reading kube config file...")

cfg, err := ioutil.ReadFile(viper.GetString("kubeconfig"))
if err != nil {
exitWithError("unable to read the kube config file", err)
}
cfg := viper.GetString("kubeconfig")

havener.VerboseMessage("Getting helm client...")

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var topCmd = &cobra.Command{

havener.VerboseMessage("Accessing Kubernetes cluster...")

clientSet, _, err := havener.OutOfClusterAuthentication()
clientSet, _, err := havener.OutOfClusterAuthentication("")
if err != nil {
exitWithError("unable to get access to cluster", err)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/havener/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ func getKubeConfig() string {
}

//OutOfClusterAuthentication for kube authentication from the outside
func OutOfClusterAuthentication() (*kubernetes.Clientset, *rest.Config, error) {
func OutOfClusterAuthentication(kubeConfig string) (*kubernetes.Clientset, *rest.Config, error) {
if kubeConfig == "" {
kubeConfig = getKubeConfig()
}

// BuildConfigFromFlags is a helper function that builds configs from a master
// url or a kubeconfig filepath.
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfig())
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, nil, err
}
Expand Down
54 changes: 39 additions & 15 deletions pkg/havener/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ package havener

import (
"fmt"
"io/ioutil"
"os"

"github.com/spf13/viper"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/helm/cmd/helm/installer"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/helm/portforwarder"
Expand All @@ -35,13 +37,16 @@ import (
rls "k8s.io/helm/pkg/proto/hapi/services"
)

// Hardcode tiller image because version.Version is overwritten when build helm release
// https://github.com/helm/helm/blob/99199c975236430fdf7599c69a956c6eb73b44e9/versioning.mk#L16-L19
const (
ImageSpec = "gcr.io/kubernetes-helm/tiller:v2.10.0"
)

// ListHelmReleases returns a list of releases
// Based on https://github.com/helm/helm/blob/7cad59091a9451b2aa4f95aa882ea27e6b195f98/pkg/proto/hapi/services/tiller.pb.go
func ListHelmReleases() (*rls.ListReleasesResponse, error) {
cfg, err := ioutil.ReadFile(viper.GetString("kubeconfig"))
if err != nil {
return nil, err
}
cfg := viper.GetString("kubeconfig")

helmClient, _ := GetHelmClient(cfg)
var sortBy = int32(2) //LAST_RELEASED
Expand Down Expand Up @@ -81,10 +86,7 @@ func GetHelmChart(path string) (requestedChart *chart.Chart, err error) {

//UpdateHelmRelease will upgrade an existing release with provided override values
func UpdateHelmRelease(chartname string, chartPath string, valueOverrides []byte, reuseVal bool) (*rls.UpdateReleaseResponse, error) {
cfg, err := ioutil.ReadFile(viper.GetString("kubeconfig"))
if err != nil {
return nil, err
}
cfg := viper.GetString("kubeconfig")

helmChartPath, err := PathToHelmChart(chartPath)
if err != nil {
Expand Down Expand Up @@ -116,10 +118,7 @@ func DeployHelmRelease(chartname string, namespace string, chartPath string, tim

VerboseMessage("Reading kube config file...")

cfg, err := ioutil.ReadFile(viper.GetString("kubeconfig"))
if err != nil {
return nil, err
}
cfg := viper.GetString("kubeconfig")

VerboseMessage("Locating helm chart location...")

Expand Down Expand Up @@ -161,10 +160,15 @@ func DeployHelmRelease(chartname string, namespace string, chartPath string, tim
}

// GetHelmClient creates a new client for the Helm-Tiller protocol
func GetHelmClient(kubeConfig []byte) (*helm.Client, error) {
func GetHelmClient(kubeConfig string) (*helm.Client, error) {
var tillerTunnel *kube.Tunnel

clientSet, config, err := OutOfClusterAuthentication()
clientSet, config, err := OutOfClusterAuthentication(kubeConfig)
if err != nil {
return nil, err
}

err = InitTiller("kube-system", clientSet)
if err != nil {
return nil, err
}
Expand All @@ -179,3 +183,23 @@ func GetHelmClient(kubeConfig []byte) (*helm.Client, error) {

return hClient, nil
}

// InitTiller installs Tiller or upgrade if needed
func InitTiller(namespace string, clientSet kubernetes.Interface) error {
VerboseMessage("Installing Tiller in namespace %s...", namespace)
if err := installer.Install(clientSet, &installer.Options{Namespace: namespace, ImageSpec: ImageSpec}); err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("error when installing: %s", err)
}
if err := installer.Upgrade(clientSet, &installer.Options{Namespace: namespace, ForceUpgrade: true, ImageSpec: ImageSpec}); err != nil {
return fmt.Errorf("error when upgrading: %s", err)
}

VerboseMessage("Tiller has been upgraded to the current version.")

} else {
VerboseMessage("Tiller has been installed.")
}

return nil
}
Loading

0 comments on commit 87320ca

Please sign in to comment.