From 3b89b466742bfdba017b1a6bddacb5e737d85622 Mon Sep 17 00:00:00 2001 From: fanyangyang Date: Fri, 15 May 2020 20:31:34 +0800 Subject: [PATCH] custom namespace --- builder/build/build.go | 1 + builder/build/code_build.go | 3 ++- builder/exector/build_from_sourcecode_run.go | 1 + builder/exector/exector.go | 2 +- builder/job/job.go | 5 ++-- grctl/cmd/build.go | 19 +++++++++++-- grctl/cmd/cluster.go | 28 +++++++++++++++++--- grctl/cmd/config.go | 10 ++++++- grctl/cmd/install.go | 10 +++++-- webcli/cmd/main.go | 13 +++++---- 10 files changed, 72 insertions(+), 20 deletions(-) diff --git a/builder/build/build.go b/builder/build/build.go index 2bfe65df9..107d1455b 100644 --- a/builder/build/build.go +++ b/builder/build/build.go @@ -74,6 +74,7 @@ type Response struct { //Request build input type Request struct { + RbdNamespace string GRDataPVCName string CachePVCName string CacheSource string diff --git a/builder/build/code_build.go b/builder/build/code_build.go index 779fdcca5..08d62206f 100644 --- a/builder/build/code_build.go +++ b/builder/build/code_build.go @@ -271,7 +271,7 @@ func (s *slugBuild) runBuildJob(re *Request) error { os.Remove(sourceTarFileName) }() name := fmt.Sprintf("%s-%s", re.ServiceID, re.DeployVersion) - namespace := "rbd-system" + namespace := re.RbdNamespace envs := []corev1.EnvVar{ {Name: "SLUG_VERSION", Value: re.DeployVersion}, {Name: "SERVICE_ID", Value: re.ServiceID}, @@ -363,6 +363,7 @@ func (s *slugBuild) runBuildJob(re *Request) error { reChan := channels.NewRingChannel(10) ctx, cancel := context.WithCancel(context.Background()) defer cancel() + logrus.Debugf("create job[name: %s; namespace: %s]", job.Name, job.Namespace) err = jobc.GetJobController().ExecJob(ctx, &job, writer, reChan) if err != nil { logrus.Errorf("create new job:%s failed: %s", name, err.Error()) diff --git a/builder/exector/build_from_sourcecode_run.go b/builder/exector/build_from_sourcecode_run.go index e9161e22b..ea3b1b184 100644 --- a/builder/exector/build_from_sourcecode_run.go +++ b/builder/exector/build_from_sourcecode_run.go @@ -236,6 +236,7 @@ func (i *SourceCodeBuildItem) codeBuild() (*build.Response, error) { return nil, err } buildReq := &build.Request{ + RbdNamespace: i.RbdNamespace, SourceDir: i.RepoInfo.GetCodeBuildAbsPath(), CacheDir: i.CacheDir, TGZDir: i.TGZDir, diff --git a/builder/exector/exector.go b/builder/exector/exector.go index ccf98437f..25e76ab05 100644 --- a/builder/exector/exector.go +++ b/builder/exector/exector.go @@ -105,7 +105,7 @@ func NewManager(conf option.Config, mqc mqclient.MQClient) (Manager, error) { maxConcurrentTask = conf.MaxTasks } stop := make(chan struct{}) - if err := job.InitJobController(stop, kubeClient); err != nil { + if err := job.InitJobController(conf.RbdNamespace, stop, kubeClient); err != nil { cancel() return nil, err } diff --git a/builder/job/job.go b/builder/job/job.go index 16e33e50e..5c9be14a6 100644 --- a/builder/job/job.go +++ b/builder/job/job.go @@ -56,11 +56,12 @@ type controller struct { var jobController *controller //InitJobController init job controller -func InitJobController(stop chan struct{}, kubeClient kubernetes.Interface) error { +func InitJobController(rbdNamespace string, stop chan struct{}, kubeClient kubernetes.Interface) error { jobController = &controller{ KubeClient: kubeClient, - namespace: "rbd-system", + namespace: rbdNamespace, } + logrus.Infof("watch namespace[%s] job ", rbdNamespace) eventHandler := cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { job, _ := obj.(*corev1.Pod) diff --git a/grctl/cmd/build.go b/grctl/cmd/build.go index e3c7f8690..7e7508ba9 100644 --- a/grctl/cmd/build.go +++ b/grctl/cmd/build.go @@ -68,8 +68,18 @@ func NewSourceBuildCmd() cli.Command { cli.Command{ Name: "list", Usage: "Lists the building tasks pod currently being performed", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "namespace,ns", + Usage: "rainbond build job namespace", + }, + }, Action: func(ctx *cli.Context) { - cmd := exec.Command("kubectl", "get", "pod", "-l", "job=codebuild", "-o", "wide", "-n", "rbd-system") + namespace := ctx.Args().First() + if namespace == "" { + namespace = "rbd-system" + } + cmd := exec.Command("kubectl", "get", "pod", "-l", "job=codebuild", "-o", "wide", "-n", namespace) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Run() @@ -83,7 +93,12 @@ func NewSourceBuildCmd() cli.Command { if name == "" { showError("Please specify the task pod name") } - cmd := exec.Command("kubectl", "logs", "-f", name, "-n", "rbd-system") + + namespace := ctx.Args().Get(1) + if namespace == "" { + namespace = "rbd-system" + } + cmd := exec.Command("kubectl", "logs", "-f", name, "-n", namespace) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Run() diff --git a/grctl/cmd/cluster.go b/grctl/cmd/cluster.go index 4e76cc036..19605c2a8 100644 --- a/grctl/cmd/cluster.go +++ b/grctl/cmd/cluster.go @@ -43,12 +43,24 @@ func NewCmdCluster() cli.Command { cli.Command{ Name: "config", Usage: "prints the current cluster configuration", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "namespace, ns", + Usage: "rainbond build job namespace", + }, + }, Action: func(c *cli.Context) error { Common(c) return printConfig(c) }, }, }, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "namespace,ns", + Usage: "rainbond build job namespace", + }, + }, Action: func(c *cli.Context) error { Common(c) return getClusterInfo(c) @@ -58,6 +70,10 @@ func NewCmdCluster() cli.Command { } func getClusterInfo(c *cli.Context) error { + namespace := c.Args().First() + if namespace == "" { + namespace = "rbd-system" + } //show cluster resource detail clusterInfo, err := clients.RegionClient.Cluster().GetClusterInfo() if err != nil { @@ -100,7 +116,7 @@ func getClusterInfo(c *cli.Context) error { fmt.Println(table) //show component health status - printComponentStatus() + printComponentStatus(namespace) //show node detail serviceTable := termtables.CreateTable() serviceTable.AddHeaders("Uid", "IP", "HostName", "NodeRole", "Status") @@ -230,10 +246,10 @@ func clusterStatus(roleList []map[string]string, ReadyList []map[string]string) return clusterStatus, errMessage } -func printComponentStatus() { +func printComponentStatus(namespace string) { fmt.Println("----------------------------------------------------------------------------------") fmt.Println() - cmd := exec.Command("kubectl", "get", "pod", "-n", "rbd-system", "-o", "wide") + cmd := exec.Command("kubectl", "get", "pod", "-n", namespace, "-o", "wide") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Run() @@ -241,7 +257,11 @@ func printComponentStatus() { } func printConfig(c *cli.Context) error { - config, err := clients.RainbondKubeClient.RainbondV1alpha1().RainbondClusters("rbd-system").Get("rainbondcluster", metav1.GetOptions{}) + namespace := c.Args().First() + if namespace == "" { + namespace = "rbd-system" + } + config, err := clients.RainbondKubeClient.RainbondV1alpha1().RainbondClusters(namespace).Get("rainbondcluster", metav1.GetOptions{}) if err != nil { showError(err.Error()) } diff --git a/grctl/cmd/config.go b/grctl/cmd/config.go index daef0000c..e10bd9417 100644 --- a/grctl/cmd/config.go +++ b/grctl/cmd/config.go @@ -37,11 +37,19 @@ func NewCmdConfig() cli.Command { Name: "output,o", Usage: "write region api config to file", }, + cli.StringFlag{ + Name: "namespace,ns", + Usage: "rainbond build job namespace", + }, }, Usage: "show region config file", Action: func(c *cli.Context) { Common(c) - configMap, err := clients.K8SClient.CoreV1().ConfigMaps("rbd-system").Get("region-config", metav1.GetOptions{}) + namespace := c.GlobalString("namespace") + if namespace == "" { + namespace = "rbd-system" + } + configMap, err := clients.K8SClient.CoreV1().ConfigMaps(namespace).Get("region-config", metav1.GetOptions{}) if err != nil { showError(err.Error()) } diff --git a/grctl/cmd/install.go b/grctl/cmd/install.go index b7631a089..f907f1e0b 100644 --- a/grctl/cmd/install.go +++ b/grctl/cmd/install.go @@ -40,18 +40,24 @@ func NewCmdInstall() cli.Command { Usage: "all gateway ip of this cluster, use it to access the region api", EnvVar: "GatewayIP", }, + cli.StringFlag{ + Name: "namespace,ns", + Usage: "rainbond namespace", + EnvVar: "RBDNamespace", + }, }, Usage: "grctl install", Action: func(c *cli.Context) error { fmt.Println("Start install, please waiting!") CommonWithoutRegion(c) - apiClientSecrit, err := clients.K8SClient.CoreV1().Secrets("rbd-system").Get("rbd-api-client-cert", metav1.GetOptions{}) + namespace := c.GlobalString("namespace") + apiClientSecrit, err := clients.K8SClient.CoreV1().Secrets(namespace).Get("rbd-api-client-cert", metav1.GetOptions{}) if err != nil { showError(fmt.Sprintf("get region api tls secret failure %s", err.Error())) } regionAPIIP := c.StringSlice("gateway-ip") if len(regionAPIIP) == 0 { - cluster, err := clients.RainbondKubeClient.RainbondV1alpha1().RainbondClusters("rbd-system").Get("rainbondcluster", metav1.GetOptions{}) + cluster, err := clients.RainbondKubeClient.RainbondV1alpha1().RainbondClusters(namespace).Get("rainbondcluster", metav1.GetOptions{}) if err != nil { showError(fmt.Sprintf("get rainbond cluster config failure %s", err.Error())) } diff --git a/webcli/cmd/main.go b/webcli/cmd/main.go index d7bd0b5db..cd2faf6c9 100644 --- a/webcli/cmd/main.go +++ b/webcli/cmd/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "github.com/Sirupsen/logrus" k8sutil "github.com/goodrain/rainbond/util/k8s" @@ -12,12 +13,6 @@ import ( func main() { option := app.DefaultOptions option.K8SConfPath = "/root/.kube/config" - ap, err := app.New(&option) - if err != nil { - logrus.Error(err) - return - } - logrus.Info(ap.GetDefaultContainerName("rbd-system", "rainbond-operator-0")) config, err := k8sutil.NewRestConfig(option.K8SConfPath) if err != nil { logrus.Error(err) @@ -28,11 +23,15 @@ func main() { if err != nil { logrus.Error(err) } + namespace := os.Getenv("namespace") + if namespace == "" { + namespace = "rbd-system" + } commands := []string{"sh"} req := restClient.Post(). Resource("pods"). Name("rainbond-operator-0"). - Namespace("rbd-system"). + Namespace(namespace). SubResource("exec"). Param("container", "operator"). Param("stdin", "true").