Merge pull request #9071 from zexi/feature/k8s-cluster-addon-opt

feat(climc): k8s cluster addon add enable native ip alloc option
This commit is contained in:
Zexi Li
2021-02-22 19:51:04 +08:00
committed by GitHub
6 changed files with 168 additions and 21 deletions
+73 -3
View File
@@ -16,6 +16,9 @@ package k8s
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -36,9 +39,10 @@ func initKubeCluster() {
cmd.ShowEvent()
cmd.List(new(o.ClusterListOptions))
cmd.Show(new(o.IdentOptions))
cmd.Create(new(o.KubeClusterCreateOptions))
cmd.Create(new(o.K8SClusterCreateOptions))
cmd.Perform("sync", new(o.ClusterSyncOptions))
cmd.Perform("syncstatus", new(o.IdentOptions))
cmd.Perform("deploy", new(o.IdentOptions))
cmd.Get("components-status", new(o.IdentOptions))
cmd.Get("api-resources", new(o.IdentOptions))
cmd.Get("cluster-users", new(o.IdentOptions))
@@ -80,8 +84,12 @@ func initKubeCluster() {
return nil
})
R(&o.IdentOptions{}, cmdN("addons"), "Get addon manifest of a cluster", func(s *mcclient.ClientSession, args *o.IdentOptions) error {
ret, err := k8s.KubeClusters.GetSpecific(s, args.ID, "addons", nil)
R(&o.ClusterGetAddonsOpt{}, cmdN("addons"), "Get addon manifest of a cluster", func(s *mcclient.ClientSession, args *o.ClusterGetAddonsOpt) error {
params, err := args.Params()
if err != nil {
return err
}
ret, err := k8s.KubeClusters.GetSpecific(s, args.ID, "addons", params)
if err != nil {
return err
}
@@ -280,4 +288,66 @@ func initKubeCluster() {
printObject(ret)
return nil
})
type GetKubesprayConfigOpt struct {
o.IdentOptions
OUTPUT string `help:"Output directory to store config files"`
}
R(&GetKubesprayConfigOpt{}, cmdN("kubespray-config"), "Get cluster kubespray config", func(s *mcclient.ClientSession, args *GetKubesprayConfigOpt) error {
conf, err := k8s.KubeClusters.GetSpecific(s, args.ID, "kubespray-config", nil)
if err != nil {
return err
}
inventoryContent, err := conf.GetString("inventory_content")
if err != nil {
return errors.Wrap(err, "get inventory content")
}
vars, err := conf.Get("vars")
if err != nil {
return errors.Wrap(err, "get variables")
}
privateKey, err := conf.GetString("private_key")
if err != nil {
return errors.Wrap(err, "get private key")
}
if err := os.MkdirAll(args.OUTPUT, 0755); err != nil {
return errors.Wrap(err, "mkdir")
}
writeFile := func(name, content string) error {
fp := filepath.Join(args.OUTPUT, name)
log.Infof("Write file: %s", fp)
if err := ioutil.WriteFile(fp, []byte(content), 0644); err != nil {
return errors.Wrap(err, "write file")
}
if err := os.Chmod(fp, 0600); err != nil {
return errors.Wrap(err, "chmod")
}
return nil
}
iPath := filepath.Join(args.OUTPUT, "hosts.ini")
vPath := filepath.Join(args.OUTPUT, "vars.json")
kPath := filepath.Join(args.OUTPUT, "private_key")
for fPath, content := range map[string]string{
iPath: inventoryContent,
vPath: vars.PrettyString(),
kPath: privateKey,
} {
name := filepath.Base(fPath)
if err := writeFile(name, content); err != nil {
return errors.Wrapf(err, "write file %s", name)
}
}
fmt.Printf("try cmd:\nansible-playbook -i %s cluster.yml -b -v --private-key %s --extra-vars @%s\n", iPath, kPath, vPath)
return nil
})
}
+2
View File
@@ -29,4 +29,6 @@ func initKubeMachine() {
cmd.BatchDelete(new(o.IdentsOptions))
cmd.Perform("recreate", new(o.IdentOptions))
cmd.Perform("terminate", new(o.IdentOptions))
cmd.Get("networkaddress", new(o.MachineListNetworkAddressOptions))
cmd.Perform("attach-networkaddress", new(o.MachineAttachNetworkAddressOptions))
}