From 089aa239b864809826f299ccf466a87732a5769c Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Wed, 23 Mar 2022 00:00:29 +0800 Subject: [PATCH 1/9] =?UTF-8?q?k8s=E9=AA=8C=E8=AF=81=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=8C=BA=E5=88=86Config=E8=BF=98=E6=98=AFToken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/core/settings/settings.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/internal/core/settings/settings.go b/internal/core/settings/settings.go index 9beb27f5..9dc10e95 100644 --- a/internal/core/settings/settings.go +++ b/internal/core/settings/settings.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "io" + "k8s.io/client-go/rest" "os" "strings" "time" @@ -69,6 +70,9 @@ const ( KubernetesType = "kubernetes" RegistryType = "registry" JenkinsType = "jenkins" + + KubernetesConfig = "kubernetesConfig" + KubernetesToken = "kubernetesToken" ) type Config struct{} @@ -81,6 +85,7 @@ type BaseConfig struct { type KubeConfig struct { URL string `json:"url,omitempty"` Conf string `json:"conf,omitempty"` + Type string `json:"type,omitempty"` } type RegistryConfig struct { BaseConfig @@ -258,16 +263,30 @@ func (pm *SettingManager) VerifyIntegrateSetting(request *IntegrateSettingReq) V case KubernetesType: kube := &KubeConfig{} err := json.Unmarshal([]byte(config), kube) + if kube.Type == "" { + kube.Type = KubernetesConfig + } if err != nil { log.Log.Error("kuber conf format error: %v", err.Error()) resp.Error = err return resp } - k8sconf, err := clientcmd.RESTConfigFromKubeConfig([]byte(kube.Conf)) - if err != nil { - resp.Error = err - return resp + var k8sconf *rest.Config + switch kube.Type { + case KubernetesConfig: + k8sconf, err = clientcmd.RESTConfigFromKubeConfig([]byte(kube.Conf)) + if err != nil { + resp.Error = err + return resp + } + case KubernetesToken: + k8sconf = &rest.Config{ + BearerToken: kube.Conf, + TLSClientConfig: rest.TLSClientConfig{Insecure: true}, + Host: "https://81.68.216.88:6443", + } } + clientset, err := kubernetes.NewForConfig(k8sconf) if err != nil { resp.Error = err From 1581a16eb7c796a0d7b0d0e75a8cf471b5feba22 Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Wed, 23 Mar 2022 00:00:54 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=89=E6=8B=A9=E8=BE=93=E5=85=A5Config?= =?UTF-8?q?=E6=88=96=E8=80=85Token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../setting/components/IntegrateCreate.vue | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/web/src/views/setting/components/IntegrateCreate.vue b/web/src/views/setting/components/IntegrateCreate.vue index f2835b27..f5340203 100644 --- a/web/src/views/setting/components/IntegrateCreate.vue +++ b/web/src/views/setting/components/IntegrateCreate.vue @@ -22,18 +22,27 @@ - +
- + + + Kubernetes Config + Service Account Token + + + - + + + +
@@ -141,6 +150,9 @@ export default { 'config.token': [ { required: true, message: '请输入token信息', trigger: 'blur' }, ], + 'config.type': [ + { required: true, message: '请选择类型', trigger: 'blur' }, + ], description: [ { required: false, message: '描述信息不能为空', trigger: 'blur' }, ], @@ -155,7 +167,15 @@ export default { }, created() { }, + methods: { + selectChange(newVal){ + console.log("下拉改变",newVal) + if (this.form.type === 'kubernetes'){ + if (this.form.config.type == null) + this.$set(this.form.config,"type","kubernetesConfig") + } + }, handleClose(done) { this.$confirm('确认关闭?') .then(_ => { From 684ff7b3f0fcf6ef79528935483cf56e8708188f Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Wed, 23 Mar 2022 00:10:14 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=88=96=E4=BF=AE?= =?UTF-8?q?=E6=94=B9K8S=E9=85=8D=E7=BD=AE=E6=97=B6=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=B0=86Config=20/=20Token=20=E4=BF=9D=E5=AD=98=E5=88=B0?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/core/settings/settings.go | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/internal/core/settings/settings.go b/internal/core/settings/settings.go index 9dc10e95..a02fbb58 100644 --- a/internal/core/settings/settings.go +++ b/internal/core/settings/settings.go @@ -213,17 +213,9 @@ func (pm *SettingManager) UpdateIntegrateSetting(request *IntegrateSettingReq, s log.Log.Error("json marshal error: %s", err.Error()) return err } - //stageModel.Config = config + stageModel.CryptoConfig(config) - if request.Type == KubernetesType { - kube := &KubeConfig{} - err := json.Unmarshal([]byte(config), kube) - if err == nil { - pm.createOrupateKubernetesConfig(request.Name, kube.Conf) - } else { - log.Log.Error("kuber conf format error: %v", err.Error()) - } - } + return pm.model.UpdateIntegrateSetting(stageModel) } @@ -371,21 +363,6 @@ func (pm *SettingManager) CreateIntegrateSetting(request *IntegrateSettingReq, c newIntegrateSetting.CryptoConfig(config) - if request.Type == KubernetesType { - kube := &KubeConfig{} - err := json.Unmarshal([]byte(config), kube) - if err != nil { - msg := fmt.Sprintf("kuber conf format error: %v", err.Error()) - log.Log.Error(msg) - return fmt.Errorf(msg) - } - - if err := pm.createOrupateKubernetesConfig(request.Name, kube.Conf); err != nil { - log.Log.Error("create or update k8s config file error: %s", err.Error()) - } else { - log.Log.Debug("create or update k8s config file success.") - } - } return pm.model.CreateIntegrateSetting(newIntegrateSetting) } From 84ddbfacf125d48d60dd8c20e95aba7322b89c4f Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Wed, 23 Mar 2022 00:11:31 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E5=8A=A0=E5=AF=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/models/integrate.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/models/integrate.go b/internal/models/integrate.go index 6ce99d3c..0a8dcaf4 100644 --- a/internal/models/integrate.go +++ b/internal/models/integrate.go @@ -37,7 +37,6 @@ func (t *IntegrateSetting) TableName() string { } func (t *IntegrateSetting) CryptoConfig(raw string) { - t.crypto(raw) t.Config = t.crypto(raw) } From 5d7bdddc9cc8bc98d7f65ba255a156154807dc31 Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Wed, 23 Mar 2022 08:43:05 +0800 Subject: [PATCH 5/9] =?UTF-8?q?1.=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8name+?= =?UTF-8?q?type=E8=8E=B7=E5=8F=96integrate=E4=BF=A1=E6=81=AF=202.GetClient?= =?UTF-8?q?set=E4=BB=8E=E6=95=B0=E6=8D=AE=E5=BA=93=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E9=85=8D=E7=BD=AE=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/core/settings/settings.go | 12 ++++- internal/dao/integrate_settings.go | 11 ++++- pkg/kube/clientset.go | 71 ++++++++++++++---------------- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/internal/core/settings/settings.go b/internal/core/settings/settings.go index a02fbb58..a29abdef 100644 --- a/internal/core/settings/settings.go +++ b/internal/core/settings/settings.go @@ -174,6 +174,16 @@ func (pm *SettingManager) GetIntegrateSettingByID(id int64) (*IntegrateSettingRe return formatSignalIntegrateSetting(integrateSetting, config), err } +func (pm *SettingManager) GetIntegrateSettingByName(name string, integrateType string) (*IntegrateSettingResponse, error) { + integrateSetting, err := pm.model.GetIntegrateSettingByName(name, integrateType) + if err != nil { + log.Log.Error("when GetIntegrateSettingByName, get GetIntegrateSettingByName occur error: %s", err.Error()) + return nil, err + } + config := &Config{} + return formatSignalIntegrateSetting(integrateSetting, config), err +} + // GetIntegrateSettingsByPagination .. func (pm *SettingManager) GetIntegrateSettingsByPagination(filter *query.FilterQuery, intergrateTypes []string) (*query.QueryResult, error) { queryResult, settingsList, err := pm.model.GetIntegrateSettingsByPagination(filter, intergrateTypes) @@ -275,7 +285,7 @@ func (pm *SettingManager) VerifyIntegrateSetting(request *IntegrateSettingReq) V k8sconf = &rest.Config{ BearerToken: kube.Conf, TLSClientConfig: rest.TLSClientConfig{Insecure: true}, - Host: "https://81.68.216.88:6443", + Host: kube.URL, } } diff --git a/internal/dao/integrate_settings.go b/internal/dao/integrate_settings.go index 3958ae46..2d1c4665 100644 --- a/internal/dao/integrate_settings.go +++ b/internal/dao/integrate_settings.go @@ -49,9 +49,18 @@ func (model *SysSettingModel) GetIntegrateSettingByID(integrateSettingID int64) return &integrateSetting, nil } +func (model *SysSettingModel) GetIntegrateSettingByName(name string, integrateType string) (*models.IntegrateSetting, error) { + integrateSetting := models.IntegrateSetting{} + qs := model.ormer.QueryTable(model.IntegrateSettingTableName).Filter("deleted", false) + if err := qs.Filter("name", name).Filter("type", integrateType).One(&integrateSetting); err != nil { + return nil, err + } + return &integrateSetting, nil +} + // GetIntegrateSettings ... func (model *SysSettingModel) GetIntegrateSettings(integrateTypes []string) ([]*models.IntegrateSetting, error) { - integrateSettings := []*models.IntegrateSetting{} + var integrateSettings []*models.IntegrateSetting qs := model.ormer.QueryTable(model.IntegrateSettingTableName).Filter("deleted", false) if len(integrateTypes) > 0 { qs = qs.Filter("type__in", integrateTypes) diff --git a/pkg/kube/clientset.go b/pkg/kube/clientset.go index 28a9d08d..169a80bc 100644 --- a/pkg/kube/clientset.go +++ b/pkg/kube/clientset.go @@ -17,53 +17,46 @@ limitations under the License. package kube import ( - "sync" - + "encoding/json" + "github.com/go-atomci/atomci/internal/core/settings" + "github.com/go-atomci/atomci/internal/models" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" ) -var ( - clusterClientsetMapMutex sync.RWMutex - clusterClientsetMap = make(map[string]kubernetes.Interface) -) - -func findClientset(cluster string) (client kubernetes.Interface, ok bool) { - clusterClientsetMapMutex.RLock() - defer clusterClientsetMapMutex.RUnlock() - client, ok = clusterClientsetMap[cluster] - return client, ok -} +func GetClientset(cluster string) (client kubernetes.Interface, err error) { -func newClientset(cluster string) (client kubernetes.Interface, err error) { - var ok bool - clusterClientsetMapMutex.Lock() - defer clusterClientsetMapMutex.Unlock() - client, ok = clusterClientsetMap[cluster] - if !ok { - client, err = clientsetProvider(cluster) - if err == nil { - clusterClientsetMap[cluster] = client - } + pm := settings.NewSettingManager() + resp, err := pm.GetIntegrateSettingByName(cluster, settings.KubernetesType) + if err != nil { + return nil, err } - return client, err + return buildK8sClient(resp.IntegrateSettingReq.Config.(*models.IntegrateSetting)) } -func GetClientset(cluster string) (client kubernetes.Interface, err error) { - var ok bool - client, ok = findClientset(cluster) - if !ok { - client, err = newClientset(cluster) +func buildK8sClient(setting *models.IntegrateSetting) (client kubernetes.Interface, err error) { + kube := &settings.KubeConfig{} + err = json.Unmarshal([]byte(setting.Config), kube) + if err != nil { + return nil, err } - return client, err -} -func UpdateClientset(cluster string) (client kubernetes.Interface, err error) { - clusterClientsetMapMutex.Lock() - defer clusterClientsetMapMutex.Unlock() - client, err = clientsetProvider(cluster) - if err != nil { - return + var k8sConfig *rest.Config + switch kube.Type { + case settings.KubernetesConfig: + k8sConfig, err = clientcmd.RESTConfigFromKubeConfig([]byte(kube.Conf)) + if err != nil { + return nil, err + } + case settings.KubernetesToken: + k8sConfig = &rest.Config{ + BearerToken: kube.Conf, + TLSClientConfig: rest.TLSClientConfig{Insecure: true}, + Host: kube.URL, + } } - clusterClientsetMap[cluster] = client - return + + clientSet, err := kubernetes.NewForConfig(k8sConfig) + return clientSet, err } From f9986f0eca12b1fa8ac0526c80a166b2b32cb436 Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Fri, 25 Mar 2022 09:31:45 +0800 Subject: [PATCH 6/9] =?UTF-8?q?1.=20=E8=A1=A5=E5=85=85=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E6=96=87=E4=BB=B6=202.=20=E8=BF=81=E7=A7=BB=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=B8=BAinit=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/atomci/main.go | 1 + internal/migrations/migration20220324.go | 35 ++++++++++++++++++++++++ internal/migrations/migrations.go | 13 +++++++-- internal/models/models.go | 2 -- 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 internal/migrations/migration20220324.go diff --git a/cmd/atomci/main.go b/cmd/atomci/main.go index f954def7..95a3db56 100644 --- a/cmd/atomci/main.go +++ b/cmd/atomci/main.go @@ -24,6 +24,7 @@ import ( "github.com/go-atomci/atomci/internal/cronjob" _ "github.com/go-atomci/atomci/internal/initialize" + _ "github.com/go-atomci/atomci/internal/migrations" _ "github.com/go-atomci/atomci/internal/models" "github.com/go-atomci/atomci/internal/routers" "github.com/go-atomci/atomci/pkg/kube" diff --git a/internal/migrations/migration20220324.go b/internal/migrations/migration20220324.go new file mode 100644 index 00000000..ec784836 --- /dev/null +++ b/internal/migrations/migration20220324.go @@ -0,0 +1,35 @@ +package migrations + +import ( + "github.com/astaxie/beego/orm" + "github.com/go-atomci/atomci/internal/core/settings" + "time" +) + +type Migration20220324 struct { +} + +func (m Migration20220324) GetCreateAt() time.Time { + return time.Date(2022, 3, 24, 0, 0, 0, 0, time.Local) +} + +func (m Migration20220324) Upgrade(ormer orm.Ormer) error { + pm := settings.NewSettingManager() + k8sSettings, err := pm.GetIntegrateSettings([]string{"kubernetes"}) + if err != nil { + return err + } + for _, setting := range k8sSettings { + req := &setting.IntegrateSettingReq + cfg := req.Config.(*settings.KubeConfig) + if cfg.Type == "" { + cfg.Type = settings.KubernetesConfig + cfg.URL = "" + err = pm.UpdateIntegrateSetting(req, setting.ID) + if err != nil { + return err + } + } + } + return nil +} diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go index 85ca69f9..070dee85 100644 --- a/internal/migrations/migrations.go +++ b/internal/migrations/migrations.go @@ -2,6 +2,7 @@ package migrations import ( "github.com/astaxie/beego/orm" + "os" "sort" "time" ) @@ -29,11 +30,12 @@ func (t MigrationTypes) Swap(i, j int) { t[i], t[j] = t[j], t[i] } -// InitMigration db migration register -func InitMigration() { +// initMigration db migration register +func initMigration() { migrationTypes := MigrationTypes{ new(Migration20220101), new(Migration20220309), + new(Migration20220324), } migrateInTx(migrationTypes) @@ -95,3 +97,10 @@ func sureCreateTable(ormer orm.Ormer) { )` ormer.Raw(ddl).Exec() } + +func init() { + if len(os.Args) > 1 && os.Args[1][:5] == "-test" { + return + } + initMigration() +} diff --git a/internal/models/models.go b/internal/models/models.go index 64561e50..1a083cfe 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -18,7 +18,6 @@ package models import ( "fmt" - "github.com/go-atomci/atomci/internal/migrations" "os" "time" @@ -156,6 +155,5 @@ func init() { return } initOrm() - migrations.InitMigration() // orm.RunSyncdb("default", false, true) } From e0bcc01e6f75749124b6821785db69e189580442 Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Sun, 27 Mar 2022 13:02:59 +0800 Subject: [PATCH 7/9] =?UTF-8?q?k8s=E9=85=8D=E7=BD=AE=E8=AF=BB=E5=86=99?= =?UTF-8?q?=E5=9D=87=E4=BB=8E=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=AD=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/atomci/main.go | 5 --- internal/api/terminal.go | 15 +-------- internal/core/kuberes/application.go | 6 ++-- pkg/kube/clientset.go | 20 ++++-------- pkg/kube/kube.go | 46 ---------------------------- 5 files changed, 10 insertions(+), 82 deletions(-) delete mode 100644 pkg/kube/kube.go diff --git a/cmd/atomci/main.go b/cmd/atomci/main.go index 95a3db56..7cb4caf5 100644 --- a/cmd/atomci/main.go +++ b/cmd/atomci/main.go @@ -27,13 +27,8 @@ import ( _ "github.com/go-atomci/atomci/internal/migrations" _ "github.com/go-atomci/atomci/internal/models" "github.com/go-atomci/atomci/internal/routers" - "github.com/go-atomci/atomci/pkg/kube" ) -func init() { - kube.Init() -} - func main() { cronjob.RunPublishJobServer() beego.Info("Beego version:", beego.VERSION) diff --git a/internal/api/terminal.go b/internal/api/terminal.go index a7600bc3..2bb0e875 100644 --- a/internal/api/terminal.go +++ b/internal/api/terminal.go @@ -18,14 +18,9 @@ package api import ( "fmt" - "path" - "github.com/go-atomci/atomci/internal/core/podexec" "github.com/go-atomci/atomci/internal/middleware/log" "github.com/go-atomci/atomci/pkg/kube" - - "github.com/astaxie/beego" - "k8s.io/client-go/tools/clientcmd" ) type TerminalController struct { @@ -57,7 +52,7 @@ func (t *TerminalController) PodTerminal() { _ = pty.Close() }() - kubeCli, err := kube.GetClientset(cluster) + kubeCli, cfg, err := kube.GetClientset(cluster) if err != nil { msg := fmt.Sprintf("get kubecli err :%v", err) log.Log.Error(msg) @@ -79,14 +74,6 @@ func (t *TerminalController) PodTerminal() { return } - configFile := path.Join(beego.AppConfig.String("k8s::configPath"), cluster) - cfg, err := clientcmd.BuildConfigFromFlags("", configFile) - if err != nil { - msg := fmt.Sprintf("build config occur error: %s", err.Error()) - log.Log.Error(msg) - t.HandleInternalServerError(msg) - return - } err = podexec.ExecPod(kubeCli, cfg, []string{"/bin/sh"}, pty, namespace, podName, containerName) if err != nil { msg := fmt.Sprintf("Exec to pod error! err: %v", err) diff --git a/internal/core/kuberes/application.go b/internal/core/kuberes/application.go index f731a910..eefa5b9e 100644 --- a/internal/core/kuberes/application.go +++ b/internal/core/kuberes/application.go @@ -144,7 +144,7 @@ func NewAppRes(cluster string, envID, projectID int64) (*AppRes, error) { ProjectID: projectID, }, nil } - client, err := kube.GetClientset(cluster) + client, _, err := kube.GetClientset(cluster) if err != nil { if cluster != "" { return nil, errors.NewInternalServerError().SetCause(err) @@ -486,7 +486,7 @@ func (ar *AppRes) SetLabels(namespace, name string, labels map[string]string) er } func CreateK8sNamespace(cluster, namespace string) error { - client, err := kube.GetClientset(cluster) + client, _, err := kube.GetClientset(cluster) if err != nil { return err } @@ -505,7 +505,7 @@ func CreateK8sNamespace(cluster, namespace string) error { } func CreateRegistrySecret(cluster, namespace string, envID int64) error { - client, err := kube.GetClientset(cluster) + client, _, err := kube.GetClientset(cluster) if err != nil { log.Log.Warning(fmt.Sprintf("create registry secret failed: %v", err.Error())) return err diff --git a/pkg/kube/clientset.go b/pkg/kube/clientset.go index 169a80bc..d40fb24b 100644 --- a/pkg/kube/clientset.go +++ b/pkg/kube/clientset.go @@ -17,37 +17,29 @@ limitations under the License. package kube import ( - "encoding/json" "github.com/go-atomci/atomci/internal/core/settings" - "github.com/go-atomci/atomci/internal/models" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" ) -func GetClientset(cluster string) (client kubernetes.Interface, err error) { +func GetClientset(cluster string) (client kubernetes.Interface, cfg *rest.Config, err error) { pm := settings.NewSettingManager() resp, err := pm.GetIntegrateSettingByName(cluster, settings.KubernetesType) if err != nil { - return nil, err + return nil, nil, err } - return buildK8sClient(resp.IntegrateSettingReq.Config.(*models.IntegrateSetting)) + return buildK8sClient(resp.IntegrateSettingReq.Config.(*settings.KubeConfig)) } -func buildK8sClient(setting *models.IntegrateSetting) (client kubernetes.Interface, err error) { - kube := &settings.KubeConfig{} - err = json.Unmarshal([]byte(setting.Config), kube) - if err != nil { - return nil, err - } - +func buildK8sClient(kube *settings.KubeConfig) (client kubernetes.Interface, cfg *rest.Config, err error) { var k8sConfig *rest.Config switch kube.Type { case settings.KubernetesConfig: k8sConfig, err = clientcmd.RESTConfigFromKubeConfig([]byte(kube.Conf)) if err != nil { - return nil, err + return nil, nil, err } case settings.KubernetesToken: k8sConfig = &rest.Config{ @@ -58,5 +50,5 @@ func buildK8sClient(setting *models.IntegrateSetting) (client kubernetes.Interfa } clientSet, err := kubernetes.NewForConfig(k8sConfig) - return clientSet, err + return clientSet, k8sConfig, err } diff --git a/pkg/kube/kube.go b/pkg/kube/kube.go deleted file mode 100644 index df2bb2ef..00000000 --- a/pkg/kube/kube.go +++ /dev/null @@ -1,46 +0,0 @@ -/* -Copyright 2021 The AtomCI Group Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package kube - -import ( - "path" - - "github.com/astaxie/beego" - "github.com/astaxie/beego/config" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" -) - -var clientsetProvider func(cluster string) (kubernetes.Interface, error) - -var appConfigProvider func() config.Configer - -func Init() { - clientsetProvider = func(cluster string) (kubernetes.Interface, error) { - configPath := path.Join(beego.AppConfig.String("k8s::configPath"), cluster) - config, err := clientcmd.BuildConfigFromFlags("", configPath) - - if err != nil { - return nil, err - } - return kubernetes.NewForConfig(config) - } - - appConfigProvider = func() config.Configer { - return beego.AppConfig - } -} From 0a3063bd4b790eac957415fc72b8f4a70c78cc2f Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Sun, 27 Mar 2022 13:04:02 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E6=95=88?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/core/settings/settings.go | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/internal/core/settings/settings.go b/internal/core/settings/settings.go index a29abdef..3db77994 100644 --- a/internal/core/settings/settings.go +++ b/internal/core/settings/settings.go @@ -19,9 +19,7 @@ package settings import ( "encoding/json" "fmt" - "io" "k8s.io/client-go/rest" - "os" "strings" "time" @@ -31,7 +29,6 @@ import ( "github.com/go-atomci/atomci/utils/query" "github.com/go-atomci/atomci/utils/validate" - "github.com/astaxie/beego" "github.com/go-atomci/workflow/jenkins" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" @@ -229,28 +226,6 @@ func (pm *SettingManager) UpdateIntegrateSetting(request *IntegrateSettingReq, s return pm.model.UpdateIntegrateSetting(stageModel) } -func (pm *SettingManager) createOrupateKubernetesConfig(clusterName, config string) error { - configPath := beego.AppConfig.String("k8s::configPath") - - log.Log.Debug("configPath: %v", configPath) - err := os.MkdirAll(configPath, 0766) - if err != nil { - log.Log.Error(fmt.Sprintf("Failed to make the k8sconfig dir: %v", err.Error())) - return err - } - fileObj, err := os.OpenFile(configPath+"/"+clusterName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - log.Log.Error(fmt.Sprintf("Failed to open the file: %v", err.Error())) - return err - } - if _, err := io.WriteString(fileObj, config); err != nil { - log.Log.Error(fmt.Sprintf("init K8S cluster %v configure failed: %v", clusterName, err.Error())) - return err - } - log.Log.Debug(fmt.Sprintf("update K8S cluster %v configure successfully", clusterName)) - return nil -} - // VerifyIntegrateSetting .. func (pm *SettingManager) VerifyIntegrateSetting(request *IntegrateSettingReq) VerifyResponse { resp := VerifyResponse{} From 91ee0383fa192a7be5a87e78f2358357e8f62905 Mon Sep 17 00:00:00 2001 From: SampsonYe <271232507@qq.com> Date: Sun, 27 Mar 2022 13:07:44 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E6=95=88?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 -- conf/app.conf | 3 --- conf/app.conf.template | 4 ---- deploy/docker-compose/conf/app.conf | 3 --- 4 files changed, 12 deletions(-) diff --git a/README.md b/README.md index fde0e8e4..8f6511a4 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,5 @@ __AtomCI__ 因你而变。 |`ldap::baseDN`| OU=Xxx,DC=xx,DC=com | | | JWT 配置
| |`jwt::secret`| changemeforsecurity | jwt的加密使用的字段,建议修改 | -| K8s配置 
| -|`k8s::configPath`| ./conf/k8sconfig | k8s 配置文件存放路径,不建议修改| |
| |`atomci::url`| http://localhost:8080 | AtomCI 回调地址 | diff --git a/conf/app.conf b/conf/app.conf index 1d47ae1f..3bab7e3e 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -36,9 +36,6 @@ baseDN = OU=Xxx,DC=xx,DC=com [jwt] secret = changemeforsecurity -[k8s] -configPath = ./conf/k8sconfig - # build/deploy callback [atomci] url = http://localhost:8080 diff --git a/conf/app.conf.template b/conf/app.conf.template index eb56385a..52b2dbaf 100644 --- a/conf/app.conf.template +++ b/conf/app.conf.template @@ -40,10 +40,6 @@ baseDN = OU=Xxx,DC=xx,DC=com [jwt] secret = changemeforsecurity -[k8s] -# k8s相关配置文件默认保存地址,一般请不要修改 -configPath = ./conf/k8sconfig - [atomci] # atomci后端服务地址,用于k8s/jenkins进行回调,因此请确保地址是可以被k8s集群(jenkins agent)访问到 url = http://localhost:8080 diff --git a/deploy/docker-compose/conf/app.conf b/deploy/docker-compose/conf/app.conf index 48747b24..ce8426cb 100644 --- a/deploy/docker-compose/conf/app.conf +++ b/deploy/docker-compose/conf/app.conf @@ -36,9 +36,6 @@ baseDN = OU=Xxx,DC=xx,DC=com [jwt] secret = changemeforsecurity -[k8s] -configPath = ./conf/k8sconfig - # build/deploy callback [atomci] url = http://localhost:8080 \ No newline at end of file