|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/ghodss/yaml" |
| 7 | + kstypes "github.com/linuxsuren/ks/kubectl-plugin/types" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 11 | + "k8s.io/client-go/dynamic" |
| 12 | + "k8s.io/client-go/rest" |
| 13 | + "k8s.io/client-go/tools/clientcmd" |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | +) |
| 17 | + |
| 18 | +func newUpdateCmd() (cmd *cobra.Command) { |
| 19 | + opt := &updateOption{} |
| 20 | + |
| 21 | + cmd = &cobra.Command{ |
| 22 | + Use: "update", |
| 23 | + Aliases: []string{"up"}, |
| 24 | + Short: "Update the config item", |
| 25 | + PreRunE: opt.preRunE, |
| 26 | + RunE: opt.runE, |
| 27 | + } |
| 28 | + return |
| 29 | +} |
| 30 | + |
| 31 | +type updateOption struct { |
| 32 | + name string |
| 33 | + token string |
| 34 | +} |
| 35 | + |
| 36 | +// kubeSphereConfig is the config object of KubeSphere |
| 37 | +// currently it's partial |
| 38 | +type kubeSphereConfig struct { |
| 39 | + DevOps struct { |
| 40 | + Password string `yaml:"password"` |
| 41 | + } `yaml:"devops"` |
| 42 | +} |
| 43 | + |
| 44 | +func (o *updateOption) preRunE(_ *cobra.Command, args []string) (err error) { |
| 45 | + if len(args) > 0 { |
| 46 | + o.name = args[0] |
| 47 | + } |
| 48 | + |
| 49 | + kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config") |
| 50 | + var config *rest.Config |
| 51 | + var client dynamic.Interface |
| 52 | + |
| 53 | + if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err != nil { |
| 54 | + fmt.Println(err) |
| 55 | + } else { |
| 56 | + if client, err = dynamic.NewForConfig(config); err != nil { |
| 57 | + return |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + ctx := context.TODO() |
| 62 | + var rawConfigMap *unstructured.Unstructured |
| 63 | + if rawConfigMap, err = client.Resource(kstypes.GetConfigMapSchema()).Namespace("kubesphere-system"). |
| 64 | + Get(ctx, "kubesphere-config", metav1.GetOptions{}); err == nil { |
| 65 | + data := rawConfigMap.Object["data"] |
| 66 | + dataMap := data.(map[string]interface{}) |
| 67 | + configStr := dataMap["kubesphere.yaml"].(string) |
| 68 | + |
| 69 | + ksCfg := kubeSphereConfig{} |
| 70 | + if err = yaml.Unmarshal([]byte(configStr), &ksCfg); err == nil { |
| 71 | + o.token = ksCfg.DevOps.Password |
| 72 | + } else { |
| 73 | + err = fmt.Errorf("unable to parse KubeSphere config from configmap, error: %v", err) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if o.token == "" { |
| 78 | + err = fmt.Errorf("unable to get Jenkins token from KubeSphere, please check if it was enabled") |
| 79 | + } |
| 80 | + return |
| 81 | +} |
| 82 | + |
| 83 | +func (o *updateOption) runE(_ *cobra.Command, args []string) (err error) { |
| 84 | + err = execCommand("jcli", "config", "update", "--token", o.token, o.name) |
| 85 | + return |
| 86 | +} |
0 commit comments