mirror of https://github.com/kubernetes/kops.git
Remove addons command
Encourage users to use kubectl instead
This commit is contained in:
parent
9f92e24f02
commit
928fd6161d
|
@ -85,6 +85,8 @@ You can now use kubernetes using the kubectl tool (after allowing a few minutes
|
|||
|
||||
## Cluster management
|
||||
|
||||
* Set up [add-ons](docs/addons.md), to add important functionality to k8s.
|
||||
|
||||
* Learn about [InstanceGroups](docs/instance_groups.md), which let you change instance types, cluster sizes etc.
|
||||
|
||||
## Delete the cluster
|
||||
|
|
|
@ -1,97 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"encoding/json"
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/kops/upup/pkg/fi/utils"
|
||||
"k8s.io/kops/upup/pkg/kutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AddonsCmd represents the addons command
|
||||
type AddonsCmd struct {
|
||||
//ClusterName string
|
||||
|
||||
cobraCommand *cobra.Command
|
||||
}
|
||||
|
||||
var addonsCmd = AddonsCmd{
|
||||
cobraCommand: &cobra.Command{
|
||||
Use: "addons",
|
||||
Short: "manage cluster addons",
|
||||
Long: `manage cluster addons`,
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
cmd := addonsCmd.cobraCommand
|
||||
rootCommand.cobraCommand.AddCommand(cmd)
|
||||
|
||||
//cmd.PersistentFlags().StringVar(&addonsCmd.ClusterName, "name", "", "cluster name")
|
||||
}
|
||||
|
||||
type kubectlConfig struct {
|
||||
Kind string `json:"kind`
|
||||
ApiVersion string `json:"apiVersion`
|
||||
Clusters []*kubectlClusterWithName `json:"clusters`
|
||||
}
|
||||
|
||||
type kubectlClusterWithName struct {
|
||||
Name string `json:"name`
|
||||
Cluster kubectlCluster `json:"cluster`
|
||||
}
|
||||
type kubectlCluster struct {
|
||||
Server string `json:"server`
|
||||
}
|
||||
|
||||
func (c *AddonsCmd) buildClusterAddons() (*kutil.ClusterAddons, error) {
|
||||
//if c.ClusterName == "" {
|
||||
// return fmt.Errorf("--name is required")
|
||||
//}
|
||||
|
||||
kubectl := &kutil.Kubectl{}
|
||||
//context, err := kubectl.GetCurrentContext()
|
||||
//if err != nil {
|
||||
// return nil, fmt.Errorf("error getting current context from kubectl: %v", err)
|
||||
//}
|
||||
//glog.V(4).Infof("context = %q", context)
|
||||
|
||||
configString, err := kubectl.GetConfig(true, "json")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting current config from kubectl: %v", err)
|
||||
}
|
||||
glog.V(8).Infof("config = %q", configString)
|
||||
|
||||
config := &kubectlConfig{}
|
||||
err = json.Unmarshal([]byte(configString), config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse current config from kubectl: %v", err)
|
||||
}
|
||||
|
||||
if len(config.Clusters) != 1 {
|
||||
return nil, fmt.Errorf("expected exactly one cluster in kubectl config, found %d", len(config.Clusters))
|
||||
}
|
||||
|
||||
namedCluster := config.Clusters[0]
|
||||
glog.V(4).Infof("using cluster name %q", namedCluster.Name)
|
||||
server := namedCluster.Cluster.Server
|
||||
server = strings.TrimSpace(server)
|
||||
if server == "" {
|
||||
return nil, fmt.Errorf("server was not set in kubectl config")
|
||||
}
|
||||
|
||||
k := &kutil.ClusterAddons{
|
||||
APIEndpoint: server,
|
||||
}
|
||||
|
||||
privateKeyFile := utils.ExpandPath("~/.ssh/id_rsa")
|
||||
err = kutil.AddSSHIdentity(&k.SSHConfig, privateKeyFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error adding SSH private key %q: %v", privateKeyFile, err)
|
||||
}
|
||||
|
||||
return k, nil
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/kops/upup/pkg/fi/vfs"
|
||||
)
|
||||
|
||||
type AddonsCreateCmd struct {
|
||||
cobraCommand *cobra.Command
|
||||
}
|
||||
|
||||
var addonsCreateCmd = AddonsCreateCmd{
|
||||
cobraCommand: &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Create an addons",
|
||||
Long: `Create an addon in a cluster.`,
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
cmd := addonsCreateCmd.cobraCommand
|
||||
addonsCmd.cobraCommand.AddCommand(cmd)
|
||||
|
||||
cmd.Run = func(cmd *cobra.Command, args []string) {
|
||||
err := addonsCreateCmd.Run(args)
|
||||
if err != nil {
|
||||
glog.Exitf("%v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *AddonsCreateCmd) Run(args []string) error {
|
||||
k, err := addonsCmd.buildClusterAddons()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
addonFiles := make(map[string][]vfs.Path)
|
||||
|
||||
for _, path := range args {
|
||||
vfsPath := vfs.NewFSPath(path)
|
||||
|
||||
files, err := vfsPath.ReadDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error listing path %s: %v", vfsPath, err)
|
||||
}
|
||||
|
||||
key := vfsPath.Base()
|
||||
addonFiles[key] = files
|
||||
}
|
||||
|
||||
for key, files := range addonFiles {
|
||||
glog.Infof("Creating addon %q", key)
|
||||
err := k.CreateAddon(key, files)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating addon %q: %v", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"bytes"
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/kops/upup/pkg/kutil"
|
||||
"os"
|
||||
"text/tabwriter"
|
||||
)
|
||||
|
||||
type AddonsGetCmd struct {
|
||||
cobraCommand *cobra.Command
|
||||
}
|
||||
|
||||
var addonsGetCmd = AddonsGetCmd{
|
||||
cobraCommand: &cobra.Command{
|
||||
Use: "get",
|
||||
Short: "Display one or many addons",
|
||||
Long: `Query a cluster, and list the addons.`,
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
cmd := addonsGetCmd.cobraCommand
|
||||
addonsCmd.cobraCommand.AddCommand(cmd)
|
||||
|
||||
cmd.Run = func(cmd *cobra.Command, args []string) {
|
||||
err := addonsGetCmd.Run()
|
||||
if err != nil {
|
||||
glog.Exitf("%v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *AddonsGetCmd) Run() error {
|
||||
k, err := addonsCmd.buildClusterAddons()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
addons, err := k.ListAddons()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.printAddons(addons)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *AddonsGetCmd) printAddons(addons map[string]*kutil.ClusterAddon) error {
|
||||
w := new(tabwriter.Writer)
|
||||
var b bytes.Buffer
|
||||
|
||||
// Format in tab-separated columns with a tab stop of 8.
|
||||
w.Init(os.Stdout, 0, 8, 0, '\t', tabwriter.StripEscape)
|
||||
for _, n := range addons {
|
||||
b.WriteByte(tabwriter.Escape)
|
||||
b.WriteString(n.Name)
|
||||
b.WriteByte(tabwriter.Escape)
|
||||
b.WriteByte('\t')
|
||||
b.WriteByte(tabwriter.Escape)
|
||||
b.WriteString(n.Path)
|
||||
b.WriteByte(tabwriter.Escape)
|
||||
b.WriteByte('\n')
|
||||
|
||||
_, err := w.Write(b.Bytes())
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing to output: %v", err)
|
||||
}
|
||||
b.Reset()
|
||||
}
|
||||
|
||||
return w.Flush()
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package main
|
||||
|
||||
type kubectlConfig struct {
|
||||
Kind string `json:"kind`
|
||||
ApiVersion string `json:"apiVersion`
|
||||
Clusters []*kubectlClusterWithName `json:"clusters`
|
||||
}
|
||||
|
||||
type kubectlClusterWithName struct {
|
||||
Name string `json:"name`
|
||||
Cluster kubectlCluster `json:"cluster`
|
||||
}
|
||||
|
||||
type kubectlCluster struct {
|
||||
Server string `json:"server`
|
||||
}
|
|
@ -1,102 +0,0 @@
|
|||
package kutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"k8s.io/kops/upup/pkg/fi/vfs"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ClusterAddons struct {
|
||||
APIEndpoint string
|
||||
SSHConfig ssh.ClientConfig
|
||||
}
|
||||
|
||||
type ClusterAddon struct {
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (c *ClusterAddons) AddonsPath() (vfs.Path, error) {
|
||||
// TODO: Close NodeSSH
|
||||
|
||||
// TODO: What if endpoint is a load balancer? Query cloud and try to find actual hosts?
|
||||
|
||||
// TODO: What if multiple masters?
|
||||
|
||||
hostname := c.APIEndpoint
|
||||
hostname = strings.TrimPrefix(hostname, "http://")
|
||||
hostname = strings.TrimPrefix(hostname, "https://")
|
||||
master := &NodeSSH{
|
||||
Hostname: hostname,
|
||||
}
|
||||
|
||||
master.SSHConfig = c.SSHConfig
|
||||
|
||||
root, err := master.Root()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
manifests := root.Join("etc", "kubernetes", "addons")
|
||||
return manifests, nil
|
||||
}
|
||||
|
||||
func (c *ClusterAddons) ListAddons() (map[string]*ClusterAddon, error) {
|
||||
addonsPath, err := c.AddonsPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files, err := addonsPath.ReadDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading addons: %v", err)
|
||||
}
|
||||
|
||||
addons := make(map[string]*ClusterAddon)
|
||||
|
||||
for _, f := range files {
|
||||
name := f.Base()
|
||||
|
||||
addon := &ClusterAddon{
|
||||
Name: name,
|
||||
Path: name,
|
||||
}
|
||||
addons[addon.Name] = addon
|
||||
}
|
||||
return addons, nil
|
||||
}
|
||||
|
||||
func (c *ClusterAddons) CreateAddon(key string, files []vfs.Path) error {
|
||||
addonsPath, err := c.AddonsPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
addonPath := addonsPath.Join(key)
|
||||
existingFiles, err := addonPath.ReadDir()
|
||||
if err == nil && len(existingFiles) != 0 {
|
||||
return fmt.Errorf("addon %q already exists", key)
|
||||
}
|
||||
|
||||
srcData := make(map[string][]byte)
|
||||
for _, f := range files {
|
||||
name := f.Base()
|
||||
|
||||
data, err := f.ReadFile()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading file %s: %v", f, err)
|
||||
}
|
||||
srcData[name] = data
|
||||
}
|
||||
|
||||
for k, data := range srcData {
|
||||
destPath := addonPath.Join(k)
|
||||
err := destPath.WriteFile(data)
|
||||
if err != nil {
|
||||
// TODO: Delete other files?
|
||||
return fmt.Errorf("error writing file %s: %v", destPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue