Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first run of kubeapps up/down #1

Merged
merged 3 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
36 changes: 29 additions & 7 deletions cmd/down.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main
package cmd

import (
"github.com/Sirupsen/logrus"
"path/filepath"

"github.com/ksonnet/kubecfg/pkg/kubecfg"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/api"
)
Expand All @@ -10,20 +12,40 @@ var downCmd = &cobra.Command{
Use: "down FLAG",
Short: "uninstall KubeApps components",
Long: `uninstall KubeApps components`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
c := kubecfg.DeleteCmd{}
ns, err := cmd.Flags().GetString("namespace")
if err != nil {
logrus.Fatal(err.Error())
return err
}
c.DefaultNamespace = ns

c.GracePeriod, err = cmd.Flags().GetInt64("grace-period")
if err != nil {
return err
}

c.ClientPool, c.Discovery, err = restClientPool()
if err != nil {
return err
}

home, err := getHome()
if err != nil {
return err
}
kaManifestDir := filepath.Join(home, KUBEAPPS_DIR)

dryRun, err := cmd.Flags().GetBool("dry-run")
objs, err := parseObjects(kaManifestDir)
if err != nil {
logrus.Fatal(err.Error())
return err
}
return c.Run(objs)
},
}

func init() {
RootCmd.AddCommand(downCmd)
downCmd.Flags().StringP("namespace", "", api.NamespaceDefault, "Specify namespace for the KubeApps components")
downCmd.Flags().Bool("dry-run", false, "Provides output to be submitted to the server")
downCmd.Flags().Int64("grace-period", -1, "Number of seconds given to resources to terminate gracefully. A negative value is ignored")
}
24 changes: 0 additions & 24 deletions cmd/installer.go

This file was deleted.

105 changes: 105 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/ksonnet/kubecfg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

const KUBEAPPS_DIR = ".kubeapps"

// RootCmd is the root of cobra subcommand tree
var RootCmd = &cobra.Command{
Use: "kubeapps",
Short: "Manage KubeApps infrastructure",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
out := cmd.OutOrStderr()
logrus.SetOutput(out)
return nil
},
}

func appendObj(res *[]*unstructured.Unstructured) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
ext := filepath.Ext(path)
if ext == ".yaml" {
objs, err := utils.Read(nil, path)
if err != nil {
return fmt.Errorf("Error reading %s: %v", path, err)
}
*res = append(*res, utils.FlattenToV1(objs)...)
}
}

return nil
}
}

func parseObjects(dirPath string) ([]*unstructured.Unstructured, error) {
res := []*unstructured.Unstructured{}
err := filepath.Walk(dirPath, appendObj(&res))
if err != nil {
return nil, err
}
return res, nil
}

func restClientPool() (dynamic.ClientPool, discovery.DiscoveryInterface, error) {
conf, err := buildOutOfClusterConfig()
if err != nil {
return nil, nil, err
}

disco, err := discovery.NewDiscoveryClientForConfig(conf)
if err != nil {
return nil, nil, err
}

discoCache := utils.NewMemcachedDiscoveryClient(disco)
mapper := discovery.NewDeferredDiscoveryRESTMapper(discoCache, dynamic.VersionInterfaces)
pathresolver := dynamic.LegacyAPIPathResolverFunc

pool := dynamic.NewClientPool(conf, mapper, pathresolver)
return pool, discoCache, nil
}

func buildOutOfClusterConfig() (*rest.Config, error) {
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath == "" {
home, err := getHome()
if err != nil {
return nil, err
}
kubeconfigPath = filepath.Join(home, ".kube", "config")
}
return clientcmd.BuildConfigFromFlags("", kubeconfigPath)
}

func getHome() (string, error) {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
for _, h := range []string{"HOME", "USERPROFILE"} {
if home = os.Getenv(h); home != "" {
return home, nil
}
}
} else {
return home, nil
}

return "", errors.New("can't get home directory")
}
51 changes: 42 additions & 9 deletions cmd/up.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,68 @@
package main
package cmd

import (
"github.com/Sirupsen/logrus"
"os"

"github.com/ksonnet/kubecfg/metadata"
"github.com/ksonnet/kubecfg/pkg/kubecfg"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/api"
"path/filepath"
)

const (
GcTag = "bitnami/kubeapps"
)

var upCmd = &cobra.Command{
Use: "up FLAG",
Short: "install KubeApps components",
Long: `install KubeApps components`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
c := kubecfg.ApplyCmd{}
ns, err := cmd.Flags().GetString("namespace")
c.DefaultNamespace = ns
if err != nil {
return err
}

c.Create = true

c.DryRun, err = cmd.Flags().GetBool("dry-run")
if err != nil {
logrus.Fatal(err.Error())
return err
}

out, err := cmd.Flags().GetString("out")
c.GcTag = GcTag

c.ClientPool, c.Discovery, err = restClientPool()
if err != nil {
logrus.Fatal(err.Error())
return err
}

dryRun, err := cmd.Flags().GetBool("dry-run")
cwd, err := os.Getwd()
if err != nil {
logrus.Fatal(err.Error())
return err
}
wd := metadata.AbsPath(cwd)

home, err := getHome()
if err != nil {
return err
}
kaManifestDir := filepath.Join(home, KUBEAPPS_DIR)

objs, err := parseObjects(kaManifestDir)
if err != nil {
return err
}

return c.Run(objs, wd)
},
}

func init() {
RootCmd.AddCommand(upCmd)
upCmd.Flags().StringP("namespace", "", api.NamespaceDefault, "Specify namespace for the KubeApps components")
upCmd.Flags().Bool("dry-run", false, "Provides output to be submitted to the server")
upCmd.Flags().StringP("out", "o", "", "Output format. One of: json|yaml")
}
Loading