diff --git a/api/git.go b/api/git.go index 69b0ddb..eef176c 100644 --- a/api/git.go +++ b/api/git.go @@ -44,7 +44,7 @@ func (f *Git) Create(c *gin.Context) { return } // create git to k8s - err = f.k8sc.CreateGitSSH(ginheader.MutateContext(c), r.KnownHosts, r.SSH) + err = f.k8sc.CreateGitSSH(ginheader.MutateContext(c), r.KnownHosts, r.KeyScanKnownHosts, r.SSH) if err != nil { resp.Format(res, err).Context(c) return diff --git a/api/router.go b/api/router.go index f4f114e..774ab6a 100644 --- a/api/router.go +++ b/api/router.go @@ -7,7 +7,6 @@ import ( mysql2 "github.com/quanxiang-cloud/cabin/tailormade/db/mysql" redis2 "github.com/quanxiang-cloud/cabin/tailormade/db/redis" - "github.com/quanxiang-cloud/faas/pkg/basic/define/consts" "github.com/quanxiang-cloud/faas/pkg/basic/event" "github.com/quanxiang-cloud/faas/pkg/basic/k8s" @@ -45,7 +44,7 @@ func NewRouter(ctx context.Context, c *config.Config, log logger.AdaptedLogger) if err != nil { return nil, err } - k8sClient := k8s.NewClient(c, consts.BuildNamespace) + k8sClient := k8s.NewClient(c) engine, err := newRouter(c) if err != nil { diff --git a/configs/config.yml b/configs/config.yml index 2c3ed09..097fcac 100644 --- a/configs/config.yml +++ b/configs/config.yml @@ -33,7 +33,9 @@ mysql: log: true docker: - namespace: faas + builder: builder + serving: serving + # ---------- redis ---------- @@ -59,82 +61,78 @@ graph: - build-trusted - push steps: - - - - start - - - - clone - - - - prepare + - - start + - - clone + - - prepare - create - - - - push + - - push build-images: go1.16: qxcr.xyz/faas/build-go:v1alpha1 -templates: -- full_name: handle.go - branch: master - commit: init repo - content: | - package handle - - import ( - "fmt" - "net/http" - - "github.com/quanxiang-cloud/faas-lowcode-interface/lowcode" - ) - - type Request struct { - Foo string - } - - type Response struct { - Bar string - } - - // Handler function as a server handler - // @tags handler - // @Summary TODO: write your function name - // @Description handler description - // @Produce json - // @Param request body Request true "request" - // @Success 200 {object} Response - // @Router / [post] - func Handle(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - _, ok := ctx.Value(lowcode.LOWCODE).(lowcode.Lowcode) - if !ok { - return +templates: + - full_name: handle.go + branch: master + commit: init repo + content: | + package handle + + import ( + "fmt" + "net/http" + + "github.com/quanxiang-cloud/faas-lowcode-interface/lowcode" + ) + + type Request struct { + Foo string + } + + type Response struct { + Bar string + } + + // Handler function as a server handler + // @tags handler + // @Summary TODO: write your function name + // @Description handler description + // @Produce json + // @Param request body Request true "request" + // @Success 200 {object} Response + // @Router / [post] + func Handle(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + _, ok := ctx.Value(lowcode.LOWCODE).(lowcode.Lowcode) + if !ok { + return + } + + // code there + w.Write([]byte("hello faas!")) } - // code there - w.Write([]byte("hello faas!")) - } - -- full_name: go.mod - branch: master - commit: init repo - content: | - module quanxiang.lowcode/faas - - go 1.16 - - require github.com/quanxiang-cloud/faas-lowcode-interface v0.0.0-20220505034554-fae42a4892ad - -- full_name: go.sum - branch: master - commit: init repo - content: | - github.com/quanxiang-cloud/faas-lowcode-interface v0.0.0-20220505034554-fae42a4892ad h1:ynmSpZkZf8oGV6EhEXfiQY5Wy9lrTJxBw/g6WH8KlZw= - github.com/quanxiang-cloud/faas-lowcode-interface v0.0.0-20220505034554-fae42a4892ad/go.mod h1:rwhMXhcV96F1l+frwbgqazzepMTwRC1f0HaFEPNHscQ= - -- full_name: info.faas - branch: master - commit: init repo - content: | - projectName: demo - language: go - version: 1.16 + - full_name: go.mod + branch: master + commit: init repo + content: | + module quanxiang.lowcode/faas + + go 1.16 + + require github.com/quanxiang-cloud/faas-lowcode-interface v0.0.0-20220505034554-fae42a4892ad + + - full_name: go.sum + branch: master + commit: init repo + content: | + github.com/quanxiang-cloud/faas-lowcode-interface v0.0.0-20220505034554-fae42a4892ad h1:ynmSpZkZf8oGV6EhEXfiQY5Wy9lrTJxBw/g6WH8KlZw= + github.com/quanxiang-cloud/faas-lowcode-interface v0.0.0-20220505034554-fae42a4892ad/go.mod h1:rwhMXhcV96F1l+frwbgqazzepMTwRC1f0HaFEPNHscQ= + + - full_name: info.faas + branch: master + commit: init repo + content: | + projectName: demo + language: go + version: 1.16 diff --git a/internal/logic/git.go b/internal/logic/git.go index 689c0d4..239024b 100644 --- a/internal/logic/git.go +++ b/internal/logic/git.go @@ -33,10 +33,11 @@ func NewGit(c context.Context, db *gorm.DB) Git { } type CreateGitRequest struct { - Host string `json:"host"` - KnownHosts string `json:"knownHosts"` - Token string `json:"token"` - SSH string `json:"ssh"` + Host string `json:"host"` + KnownHosts string `json:"knownHosts"` + KeyScanKnownHosts string `json:"keyScanKnownHosts"` + Token string `json:"token"` + SSH string `json:"ssh"` } type CreateGitResponse struct { } @@ -52,6 +53,7 @@ func (g *git) Create(c context.Context, r *CreateGitRequest) (*CreateGitResponse data.Token = r.Token data.SSH = r.SSH data.KnownHosts = r.KnownHosts + data.KeyScanKnownHosts = r.KeyScanKnownHosts unix := time.NowUnix() data.CreatedAt = unix data.UpdatedAt = unix @@ -59,9 +61,12 @@ func (g *git) Create(c context.Context, r *CreateGitRequest) (*CreateGitResponse } type UpdateGitRequest struct { - ID string `json:"id"` - Host string `json:"host"` - Token string `json:"token"` + ID string `json:"id"` + Host string `json:"host"` + Token string `json:"token"` + SSH string `json:"ssh"` + KnownHosts string `json:"knownHosts"` + KeyScanKnownHosts string `json:"keyScanKnownHosts"` } type UpdateGitResponse struct { } @@ -73,6 +78,9 @@ func (g *git) Update(c context.Context, r *UpdateGitRequest) (*UpdateGitResponse } data.Host = r.Host data.Token = r.Token + data.SSH = r.SSH + data.KnownHosts = r.KnownHosts + data.KeyScanKnownHosts = r.KeyScanKnownHosts unix := time.NowUnix() data.UpdatedAt = unix return &UpdateGitResponse{}, g.gitRepo.Update(c, g.db, data) @@ -95,10 +103,13 @@ func (g *git) Delete(c context.Context, r *DeleteGitRequest) (*DeleteGitResponse type GetGitRequest struct { } type GetGitResponse struct { - ID string `json:"id"` - Host string `json:"host"` - Token string `json:"token"` - UpdatedAt int64 `json:"updatedAt"` + ID string `json:"id"` + Host string `json:"host"` + Token string `json:"token"` + UpdatedAt int64 `json:"updatedAt"` + SSH string `json:"ssh"` + KnownHosts string `json:"knownHosts"` + KeyScanKnownHosts string `json:"keyScanKnownHosts"` } func (g *git) Get(c context.Context, r *GetGitRequest) (*GetGitResponse, error) { @@ -107,10 +118,13 @@ func (g *git) Get(c context.Context, r *GetGitRequest) (*GetGitResponse, error) return nil, nil } res := &GetGitResponse{ - ID: data.ID, - Host: data.Host, - Token: data.Token, - UpdatedAt: data.UpdatedAt, + ID: data.ID, + Host: data.Host, + Token: data.Token, + UpdatedAt: data.UpdatedAt, + SSH: data.SSH, + KnownHosts: data.KnownHosts, + KeyScanKnownHosts: data.KeyScanKnownHosts, } return res, nil diff --git a/internal/logic/serving.go b/internal/logic/serving.go index 6640f42..5fbb2b2 100644 --- a/internal/logic/serving.go +++ b/internal/logic/serving.go @@ -9,7 +9,6 @@ import ( "github.com/quanxiang-cloud/faas/internal/models" "github.com/quanxiang-cloud/faas/internal/models/mysql" "github.com/quanxiang-cloud/faas/pkg/basic/define/code" - "github.com/quanxiang-cloud/faas/pkg/basic/define/consts" "github.com/quanxiang-cloud/faas/pkg/basic/k8s" "github.com/quanxiang-cloud/faas/pkg/config" "gorm.io/gorm" @@ -40,7 +39,7 @@ func NewServing(db *gorm.DB, log logger.AdaptedLogger, conf *config.Config) Serv dockerRepo: mysql.NewDockerRepo(), projectRepo: mysql.NewProjectRepo(), groupRepo: mysql.NewGroupRepo(), - k8sc: k8s.NewClient(conf, consts.ServingNamespace), + k8sc: k8s.NewClient(conf), log: log, } } diff --git a/internal/models/git.go b/internal/models/git.go index 068fd2c..2cd2a0f 100644 --- a/internal/models/git.go +++ b/internal/models/git.go @@ -6,12 +6,13 @@ import ( ) type Git struct { - ID string `gorm:"column:id;type:varchar(64);PRIMARY_KEY" json:"id"` - Host string `gorm:"column:host;type:varchar(200);" json:"host"` - KnownHosts string `gorm:"column:known_hosts;type:text;" json:"knownHosts"` - SSH string `gorm:"column:ssh;type:text;" json:"ssh"` - Token string `gorm:"column:token;type:text;" json:"token"` - Name string `gorm:"column:name;type:varchar(200);" json:"name"` + ID string `gorm:"column:id;type:varchar(64);PRIMARY_KEY" json:"id"` + Host string `gorm:"column:host;type:varchar(200);" json:"host"` + KnownHosts string `gorm:"column:known_hosts;type:text;" json:"knownHosts"` + KeyScanKnownHosts string `gorm:"column:key_scan_known_hosts;type:text;" json:"keyScanKnownHosts"` + SSH string `gorm:"column:ssh;type:text;" json:"ssh"` + Token string `gorm:"column:token;type:text;" json:"token"` + Name string `gorm:"column:name;type:varchar(200);" json:"name"` CreatedAt int64 `gorm:"column:created_at;type:bigint; " json:"createdAt,omitempty" ` UpdatedAt int64 `gorm:"column:updated_at;type:bigint; " json:"updatedAt,omitempty" ` diff --git a/pkg/basic/k8s/client.go b/pkg/basic/k8s/client.go index 2af2c41..2970c0a 100644 --- a/pkg/basic/k8s/client.go +++ b/pkg/basic/k8s/client.go @@ -27,7 +27,7 @@ import ( // Client Client type Client interface { CreateGitToken(ctx context.Context, host, token string) error - CreateGitSSH(ctx context.Context, host, ssh string) error + CreateGitSSH(ctx context.Context, host, keyScanHost, ssh string) error CreateDocker(ctx context.Context, host, username, secret string) error Build(ctx context.Context, data *Function) error DelFunction(ctx context.Context, data *DelFunction) error @@ -39,32 +39,34 @@ type Client interface { } type client struct { - client *kubernetes.Clientset - tekton *tektonClient.Clientset - ofn versioned.Interface - serving serving.ServiceInterface - k8sNamespace string - buildImages map[string]string + client *kubernetes.Clientset + tekton *tektonClient.Clientset + ofn versioned.Interface + serving serving.ServiceInterface + k8sBuilderNamespace string + k8sServingNamespace string + buildImages map[string]string } // NewClient NewClient -func NewClient(config *config.Config, namespace string) Client { +func NewClient(config *config.Config) Client { ctrlConfig := ctrl.GetConfigOrDie() clientset := kubernetes.NewForConfigOrDie(ctrlConfig) ofn := versioned.NewForConfigOrDie(ctrlConfig) return &client{ - client: clientset, - k8sNamespace: namespace, - ofn: ofn, - serving: serving.NewForConfigOrDie(ctrlConfig).Services(namespace), - tekton: tektonClient.NewForConfigOrDie(ctrlConfig), - buildImages: config.BuildImages, + client: clientset, + k8sBuilderNamespace: config.Docker.BuilderNameSpace, + k8sServingNamespace: config.Docker.ServingNameSpace, + ofn: ofn, + serving: serving.NewForConfigOrDie(ctrlConfig).Services(config.Docker.ServingNameSpace), + tekton: tektonClient.NewForConfigOrDie(ctrlConfig), + buildImages: config.BuildImages, } } func (c *client) CreateGitToken(ctx context.Context, host, token string) error { - secret := c.client.CoreV1().Secrets(c.k8sNamespace) + secret := c.client.CoreV1().Secrets(c.k8sBuilderNamespace) _, tenantID := ginheader.GetTenantID(ctx).Wreck() if tenantID == "" || tenantID == TenantUnexpectedType { tenantID = TenantDefault @@ -79,7 +81,7 @@ func (c *client) CreateGitToken(ctx context.Context, host, token string) error { Type: v1.SecretTypeOpaque, ObjectMeta: ctrl.ObjectMeta{ Name: strutil.GenName(tenantID, SecretGitSuffix), - Namespace: c.k8sNamespace, + Namespace: c.k8sBuilderNamespace, Annotations: tekton, }, Data: data, @@ -93,15 +95,15 @@ func (c *client) CreateGitToken(ctx context.Context, host, token string) error { } // CreateGitSSH CreateGitSSH -func (c *client) CreateGitSSH(ctx context.Context, host, ssh string) error { - secret := c.client.CoreV1().Secrets(c.k8sNamespace) +func (c *client) CreateGitSSH(ctx context.Context, host, keyScanHost, ssh string) error { + secret := c.client.CoreV1().Secrets(c.k8sBuilderNamespace) _, tenantID := ginheader.GetTenantID(ctx).Wreck() if tenantID == "" || tenantID == TenantUnexpectedType { tenantID = TenantDefault } data := make(map[string][]byte) // ssh-keyscan github.com | base64 - data["known_hosts"] = []byte(host) + data["known_hosts"] = []byte(keyScanHost) data["ssh-privatekey"] = []byte(ssh) tekton := make(map[string]string) @@ -111,7 +113,7 @@ func (c *client) CreateGitSSH(ctx context.Context, host, ssh string) error { Type: v1.SecretTypeSSHAuth, ObjectMeta: ctrl.ObjectMeta{ Name: strutil.GenName(tenantID, SecretGitSuffix), - Namespace: c.k8sNamespace, + Namespace: c.k8sBuilderNamespace, Annotations: tekton, }, Data: data, @@ -125,7 +127,8 @@ func (c *client) CreateGitSSH(ctx context.Context, host, ssh string) error { } func (c *client) CreateDocker(ctx context.Context, host, username, secret string) error { - sc := c.client.CoreV1().Secrets(c.k8sNamespace) + sc1 := c.client.CoreV1().Secrets(c.k8sBuilderNamespace) + sc2 := c.client.CoreV1().Secrets(c.k8sServingNamespace) _, tenantID := ginheader.GetTenantID(ctx).Wreck() if tenantID == "" || tenantID == TenantUnexpectedType { tenantID = TenantDefault @@ -159,12 +162,17 @@ func (c *client) CreateDocker(ctx context.Context, host, username, secret string Type: v1.SecretTypeDockerConfigJson, ObjectMeta: ctrl.ObjectMeta{ Name: strutil.GenName(tenantID, SecretDockerSuffix), - Namespace: c.k8sNamespace, + Namespace: c.k8sBuilderNamespace, }, Data: data, } options := metav1.CreateOptions{} - _, err = sc.Create(ctx, s, options) + _, err = sc1.Create(ctx, s, options) + if err != nil { + return err + } + s.ObjectMeta.Namespace = c.k8sServingNamespace + _, err = sc2.Create(ctx, s, options) if err != nil { return err } @@ -172,7 +180,7 @@ func (c *client) CreateDocker(ctx context.Context, host, username, secret string } func (c *client) Build(ctx context.Context, data *Function) error { - fn := c.ofn.CoreV1beta1().Functions(c.k8sNamespace) + fn := c.ofn.CoreV1beta1().Functions(c.k8sBuilderNamespace) function := &v1beta1.Function{ ObjectMeta: metav1.ObjectMeta{ Name: strutil.GenName(data.GroupName, data.Project, data.Version), @@ -219,7 +227,7 @@ type DelFunction struct { } func (c *client) DelFunction(ctx context.Context, data *DelFunction) error { - fn := c.ofn.CoreV1beta1().Functions(c.k8sNamespace) + fn := c.ofn.CoreV1beta1().Functions(c.k8sBuilderNamespace) return fn.Delete(ctx, data.Name, metav1.DeleteOptions{}) } @@ -227,7 +235,7 @@ func (c *client) CreateServing(ctx context.Context, fn *Function) error { ksvc := &ksvc.Service{ ObjectMeta: metav1.ObjectMeta{ Name: strutil.GenName(fn.Version, fn.Project, fn.GroupName), - Namespace: c.k8sNamespace, + Namespace: c.k8sServingNamespace, }, Spec: ksvc.ServiceSpec{ ConfigurationSpec: ksvc.ConfigurationSpec{ @@ -288,7 +296,7 @@ func genServingEnv(c *client, fn *Function) []v1.EnvVar { }, v1.EnvVar{ Name: "POD_NAMESPACE", - Value: c.k8sNamespace, + Value: c.k8sServingNamespace, }, v1.EnvVar{ Name: "LOWCODE_NAMESPACE", @@ -343,10 +351,10 @@ func (c *client) RegistAPI(ctx context.Context, fn *Function, appId string) erro }, }, } - _, err := c.tekton.TektonV1beta1().PipelineRuns(c.k8sNamespace).Create(ctx, pipeRun, metav1.CreateOptions{}) + _, err := c.tekton.TektonV1beta1().PipelineRuns(c.k8sBuilderNamespace).Create(ctx, pipeRun, metav1.CreateOptions{}) return err } func (c *client) DeleteReigstRun(ctx context.Context, name string) error { - return c.tekton.TektonV1beta1().PipelineRuns(c.k8sNamespace).Delete(ctx, name, metav1.DeleteOptions{}) + return c.tekton.TektonV1beta1().PipelineRuns(c.k8sBuilderNamespace).Delete(ctx, name, metav1.DeleteOptions{}) } diff --git a/pkg/basic/k8s/client_test.go b/pkg/basic/k8s/client_test.go index 2e8e901..6de7682 100644 --- a/pkg/basic/k8s/client_test.go +++ b/pkg/basic/k8s/client_test.go @@ -1,64 +1,80 @@ package k8s -// import ( -// "context" -// "testing" -// ) - -// func TestCreatGit(t *testing.T) { -// c := NewClient("faas") -// ctx := context.Background() -// ctx = context.WithValue(ctx, "Tenant-Id", "vvlgo") - -// err := c.CreateGitToken(ctx, "http://vvlgo.com", "123") -// if err != nil { -// panic(err) -// } -// } - -// func TestCreatDocker(t *testing.T) { -// c := NewClient("faas") - -// ctx := context.Background() -// ctx = context.WithValue(ctx, "Tenant-Id", "vvlgo") - -// err := c.CreateDocker(ctx, "http://vvlgo.com", "root", "vvlgo") -// if err != nil { -// panic(err) -// } -// } - -// func TestBuilder(t *testing.T) { -// c := NewClient("builder") - -// env := make(map[string]string) -// env["FUNC_NAME"] = "HelloWorld" -// env["FUNC_CLEAR_SOURCE"] = "true" -// env["GOPROXY"] = "https://goproxy.cn,direct" -// ctx := context.Background() -// ctx = context.WithValue(ctx, "Tenant-Id", "qxcr") -// function := &Function{ -// ID: "1", -// Version: "v007", -// Project: "faasdemo", -// GroupName: "root", -// Git: &Git{ -// Name: "demo", -// Host: "ssh://git@192.168.201.3:30679/", -// }, -// Docker: &Docker{ -// NameSpace: "privitetest/", -// Name: "qxcr", -// Host: "qxcr.xyz/", -// }, -// Builder: "openfunction/builder-go:latest", -// ENV: env, -// } -// err := c.Build(ctx, function) -// if err != nil { -// panic(err) -// } -// } +import ( + "context" + "github.com/quanxiang-cloud/faas/pkg/config" + "testing" +) + +func TestCreatGit(t *testing.T) { + newConfig, err := config.NewConfig("../../../configs/config.yml") + if err != nil { + panic(err) + } + c := NewClient(newConfig) + ctx := context.Background() + ctx = context.WithValue(ctx, "Tenant-Id", "justtest") + + var host = "http://192.168.208.3:10022" + var keyGenHost = "[192.168.208.3]:10022 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEuuYInZAnp5BFjUv1gSTjxD2nwbvslUIFFkfvMtMEK5qTNsDTPjVz9UJfCJWgwjcqJdEvVaVMfZL7wVRZT2rky2CRRO6qVtaOyjDOzWK0aoWnsn5/eEvUE3NRljjZOH+cPdHsbInSIeoQESIA3JDVYs2IC2wYG4+2UrCwsOHpHYepues+6jxQtJOkiBoy+s9DX1Fg4eDD4uQI2h7k7EaUruFApBvmnSFKsixo18SjW12nhJ+HmWWrX88NCFEs+2li4VHJEobpkZzcOMleTAWpU7PPq4DXNsLiLdH8L+Z/B9tXf6+IPrwIWjHYhYKmhl5vtEuG1Ms9M+wQRjMKhBtb\n[192.168.208.3]:10022 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBK7xuRcLe7nBpeZTsBy+V+7gSftTqF3WztTayAsawhzADSviesm69fNIpO6AHptLFuexTku6ZrOYiYVpvkTBLuI=\n[192.168.208.3]:10022 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOSLwxuzmsnCdfpCh4aniFBT1vSN4QKTqAYbXUtrG7I8\n" + var ssh = "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn\nNhAAAAAwEAAQAAAQEA7jOgHwIfF1+GLxiiVNbVA3n2pHc6mXNs0rExHMsB4yxH8UGyBoEq\nGhMJsftqWfrQMozSXVaxEoV6EjxdZWJ+NeTEppqNL5BdgNzkvBYmXEKil0YNi39ylSkean\nZUd3B/LTNMxcgMfSh4DJyBS46FSpjw46JwjPX249T9j6/RNO7r57jtHCzm5uy53h+A8Bcp\nxqxRPF0DcKNKArX8Fn9+f+NQSyN74OVSLASF9fdVVmKuIpP766QkGBREp+EUH5dZ1qVuju\nAG72ypO5eTZv28zOUGI+2eUBWukxRQjx2X6DyYuLiuvw0SQ5YwuOtdMlmjN+8R3UZOCBO5\nnlsjC9IXgQAAA9hWvRQLVr0UCwAAAAdzc2gtcnNhAAABAQDuM6AfAh8XX4YvGKJU1tUDef\nakdzqZc2zSsTEcywHjLEfxQbIGgSoaEwmx+2pZ+tAyjNJdVrEShXoSPF1lYn415MSmmo0v\nkF2A3OS8FiZcQqKXRg2Lf3KVKR5qdlR3cH8tM0zFyAx9KHgMnIFLjoVKmPDjonCM9fbj1P\n2Pr9E07uvnuO0cLObm7LneH4DwFynGrFE8XQNwo0oCtfwWf35/41BLI3vg5VIsBIX191VW\nYq4ik/vrpCQYFESn4RQfl1nWpW6O4AbvbKk7l5Nm/bzM5QYj7Z5QFa6TFFCPHZfoPJi4uK\n6/DRJDljC4610yWaM37xHdRk4IE7meWyML0heBAAAAAwEAAQAAAQEAksirVhBXmm6Z4HG8\nrq0H7LF+hFCYgrD3EUpxaj3K9vN1jbye+JUlnZW3tr6oBbSiEVRn2W6vhStNUulx4CT2gz\n1q1QOOFw5OIDd+mEzJ7LZ/PNrFQ/4QXMxO2un6GPPw2qivGX2x/HowqAzVO/siNhrS7mNO\nGf8D2deJPL7qG0QjB1axasUYzk5rRQyPg3zWl0aKDcbqfAzFt4L32jGw1jV2w61Jm92LiL\n8FzNz8oX/16sk/HC4W/Ps6L+x6h82jP0nZj2QOyp0K3zi6+peIz1FAeJS8wbuu8/uUpXeu\n0EMpgQb0eiHZyXldW89ddzEhPtVBhN1cvy54aCiNpMF3VQAAAIAXmdwFRB2nt4j5Qq4ZrB\nIzB1Kxj7lENscWGIfXNTj2XQj3h3tlzEp0vpkPeTCHTetpBJitWpnRwVDo1s609ZYuJQ4f\n2Nn6VIS6VJejXWaXYKvC0qThCxMhFW7GMZwuSlvVgHQhELsS5C1zBL5NsHorkMIupIWfzk\nP3BLML5vtc+gAAAIEA/yo/JDbmdu9Cvx6nVlTF28XzcLr8EvN68lOF9d8aOixV3SvLl1i5\neOQrHx0K0NvuTPD/og8xrCjvX9kfJRWggZEYiKtB7QCRTrVDdkLc5S6miuJFRtmNfqsLR6\n0ZAjOsa0Uh/NTuaP3nR1a0hB0UkQkYVv+arb9vLcYMomm5TmsAAACBAO77KyNzkckHFDee\nRfBm3diMCISWi3QOBvTgcYqwAQjeCDrOVKuyEi3+N+ZgNj/cr6cjwMz31I6R+okc/TaMGK\nM73SGrrA8AF/bEMxlfrGlaNrLTOYpUlXtjBvu0LbkYow54wIQ0GznbKXVAlnHKxs7tMgWV\nMPGdqO3auNi5YRTDAAAAHHZ2bGdvQFZWTEdPLU1hY2Jvb2tQcm8ubG9jYWwBAgMEBQY=\n-----END OPENSSH PRIVATE KEY-----\n" + err = c.CreateGitSSH(ctx, host, keyGenHost, ssh) + if err != nil { + panic(err) + } +} + +func TestCreatDocker(t *testing.T) { + newConfig, err := config.NewConfig("../../../configs/config.yml") + if err != nil { + panic(err) + } + c := NewClient(newConfig) + + ctx := context.Background() + ctx = context.WithValue(ctx, "Tenant-Id", "justdocker") + + err = c.CreateDocker(ctx, "http://qxcr.xyz", "qxptest", "ZHU**jie9") + if err != nil { + panic(err) + } +} + +func TestBuilder(t *testing.T) { + newConfig, err := config.NewConfig("../../../configs/config.yml") + if err != nil { + panic(err) + } + c := NewClient(newConfig) + + env := make(map[string]string) + env["FUNC_NAME"] = "HelloWorld" + env["FUNC_CLEAR_SOURCE"] = "true" + env["GOPROXY"] = "https://goproxy.cn,direct" + ctx := context.Background() + ctx = context.WithValue(ctx, "Tenant-Id", "justtest") + function := &Function{ + ID: "1", + Version: "v007", + Project: "faasdemo", + GroupName: "root", + Git: &Git{ + Name: "demo", + Host: "ssh://git@192.168.208.3:10022/", + }, + Docker: &Docker{ + NameSpace: "testcloud/", + Name: "qxcr", + Host: "qxcr.xyz/", + }, + Builder: "openfunction/builder-go:latest", + ENV: env, + } + err = c.Build(ctx, function) + if err != nil { + panic(err) + } +} // func TestDelFunction(t *testing.T) { // c := NewClient("builder") diff --git a/pkg/config/config.go b/pkg/config/config.go index eff2d54..1f79b2c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -36,7 +36,8 @@ type Config struct { // Docker docker type Docker struct { - NameSpace string `yaml:"namespace"` + BuilderNameSpace string `yaml:"builder"` + ServingNameSpace string `yaml:"serving"` } type Template struct { diff --git a/schema/full/faas.sql b/schema/full/faas.sql index 53aca49..a122268 100644 --- a/schema/full/faas.sql +++ b/schema/full/faas.sql @@ -1,91 +1,93 @@ -CREATE DATABASE faas DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; +CREATE +DATABASE faas DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; create table dockers ( - id varchar(64) not null + id varchar(64) not null primary key, host varchar(200) null, - user_name varchar(64) null, - name_space varchar(64) null, - secret text null, - name varchar(64) null, - created_at bigint null, - updated_at bigint null, - deleted_at bigint null, - created_by varchar(64) null, - updated_by varchar(64) null, - deleted_by varchar(64) null, - tenant_id varchar(64) null + user_name varchar(64) null, + name_space varchar(64) null, + secret text null, + name varchar(64) null, + created_at bigint null, + updated_at bigint null, + deleted_at bigint null, + created_by varchar(64) null, + updated_by varchar(64) null, + deleted_by varchar(64) null, + tenant_id varchar(64) null ); create table event ( - id varchar(64) not null comment 'unique id' + id varchar(64) not null comment 'unique id' primary key, name varchar(200) null comment 'name of event', - type varchar(64) null comment 'type of event', - state varchar(64) null comment 'state of event', + type varchar(64) null comment 'type of event', + state varchar(64) null comment 'state of event', message varchar(512) null comment 'msg of async', - create_by varchar(64) null, - create_at bigint null comment 'create time', - update_at bigint null comment 'update time', - delete_at bigint null comment 'delete time' + create_by varchar(64) null, + create_at bigint null comment 'create time', + update_at bigint null comment 'update time', + delete_at bigint null comment 'delete time' ); create table functions ( - id varchar(64) not null + id varchar(64) not null primary key, - group_id varchar(64) null, - project_id varchar(64) null, + group_id varchar(64) null, + project_id varchar(64) null, version varchar(200) null, - `describe` text null, + `describe` text null, status varchar(200) null, - doc_status int null, - doc int null comment 'status of function doc', - env text null, - created_at bigint null, - updated_at bigint null, - deleted_at bigint null, - created_by varchar(64) null, - updated_by varchar(64) null, - deleted_by varchar(64) null, - tenant_id varchar(64) null, + doc_status int null, + doc int null comment 'status of function doc', + env text null, + created_at bigint null, + updated_at bigint null, + deleted_at bigint null, + created_by varchar(64) null, + updated_by varchar(64) null, + deleted_by varchar(64) null, + tenant_id varchar(64) null, resource_ref varchar(200) null, name varchar(200) null, - built_at bigint null, + built_at bigint null, constraint functions_name_uindex unique (name) ); create table gits ( - id varchar(64) not null + id varchar(64) not null primary key, - host varchar(200) null, - token text null, - name varchar(200) null, - created_at bigint null, - updated_at bigint null, - deleted_at bigint null, - created_by varchar(64) null, - updated_by varchar(64) null, - deleted_by varchar(64) null, - tenant_id varchar(64) null, - known_hosts varchar(250) null, - ssh text null + host varchar(200) null, + token text null, + name varchar(200) null, + created_at bigint null, + updated_at bigint null, + deleted_at bigint null, + created_by varchar(64) null, + updated_by varchar(64) null, + deleted_by varchar(64) null, + tenant_id varchar(64) null, + known_hosts text null, + key_scan_known_hosts text null, + ssh text null ); create table groups ( id varchar(64) not null primary key, - group_id int null, + group_id int null, group_name varchar(40) null, Title varchar(64), - `describe` text null, - created_at bigint null, - updated_at bigint null, + `describe` text null, + created_at bigint null, + updated_at bigint null, created_by varchar(64) null, updated_by varchar(64) null, deleted_by varchar(64) null, @@ -94,22 +96,22 @@ create table groups create table projects ( - id varchar(64) not null + id varchar(64) not null primary key, - group_id varchar(64) null, - project_id int null, - project_name varchar(40) null, - alias varchar(40) null, - `describe` text null, - created_at bigint null, - updated_at bigint null, - created_by varchar(64) null, - updated_by varchar(64) null, - deleted_by varchar(64) null, - language varchar(20) null, - version varchar(30) null, - status int null, - user_id varchar(64) null, + group_id varchar(64) null, + project_id int null, + project_name varchar(40) null, + alias varchar(40) null, + `describe` text null, + created_at bigint null, + updated_at bigint null, + created_by varchar(64) null, + updated_by varchar(64) null, + deleted_by varchar(64) null, + language varchar(20) null, + version varchar(30) null, + status int null, + user_id varchar(64) null, repo_url varchar(200) null ); @@ -119,9 +121,9 @@ create table users primary key, user_id varchar(64) null, git_name varchar(64) null, - git_id int null, - created_at bigint null, - updated_at bigint null, + git_id int null, + created_at bigint null, + updated_at bigint null, created_by varchar(64) null, updated_by varchar(64) null, deleted_by varchar(64) null, @@ -133,10 +135,10 @@ create table user_group id varchar(64) not null primary key, user_id varchar(64) null, - git_id int null, + git_id int null, group_id varchar(64) null, - created_at bigint null, - updated_at bigint null, + created_at bigint null, + updated_at bigint null, created_by varchar(64) null, updated_by varchar(64) null, deleted_by varchar(64) null