Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update #6

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,66 @@ module opennaslab.io/bifrost
go 1.20

require (
github.com/docker/docker v24.0.6+incompatible
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/go-git/go-git/v5 v5.9.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/klog v1.0.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.9.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/invopop/jsonschema v0.9.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog v1.0.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
77 changes: 54 additions & 23 deletions go.sum

Large diffs are not rendered by default.

Binary file added main
Binary file not shown.
48 changes: 28 additions & 20 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,30 @@ type RemoteConfigDefinition ConfigStepDefinition
type DNSConfigDefinition ConfigStepDefinition

type ConfigStepDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Image string `json:"image"`
Parameters StepParameter `json:"parameters"`
Name string `json:"name"`
Description string `json:"description"`
Image string `json:"image"`
Parameters StepParameterDefinition `json:"parametersDefinition"`
}

type StepParameter struct {
In []Parameter `json:"in"`
Out []Parameter `json:"out"`
type StepParameterDefinition struct {
In []ParameterDefinition `json:"in,omitempty"`
Out []ParameterDefinition `json:"out,omitempty"`
}

type Parameter struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Required bool `json:"required"`
Items []Parameter `json:"items"`
const (
ParameterTypeString = "string"
ParameterTypeInteger = "integer"
ParameterTypeBoolean = "boolean"
ParameterTypeArray = "array"
)

type ParameterDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Required bool `json:"required"`
Items []ParameterDefinition `json:"items,omitempty"`
}

type ConfigurationWorkflow struct {
Expand All @@ -46,13 +53,13 @@ type ConfigurationWorkflow struct {
LocalConfigurationSteps []ConfigurationStep `json:"localConfigurationSteps"`
RemoteConfigurationSteps []ConfigurationStep `json:"remoteConfigurationSteps"`
DNSConfigurationSteps []ConfigurationStep `json:"dnsConfigurationSteps"`
Status ConfigurationWorkflowStatus `json:"status"`
Status ConfigurationWorkflowStatus `json:"status,omitempty"`
}

type ConfigurationStep struct {
Name string `json:"name"`
Use string `json:"use"`
In []interface{} `json:"in"`
Name string `json:"name"`
Use string `json:"use"`
In string `json:"in"`
}

type ConfigurationWorkflowState string
Expand All @@ -71,7 +78,8 @@ type ConfigurationWorkflowStatus struct {
}

type ConfigurationStepStatus struct {
Name string `json:"name"`
State ConfigurationWorkflowState `json:"state"`
Message string `json:"message"`
Name string `json:"name"`
ContainerId string `json:"containerId"`
State ConfigurationWorkflowState `json:"state"`
Message string `json:"message"`
}
27 changes: 27 additions & 0 deletions pkg/container/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package container

import (
"context"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)

func CreateContainer(workflowName, stepName, image string) (string, error) {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return "", err
}
_, err = cli.ImagePull(context.Background(), image, types.ImagePullOptions{})
if err != nil {
return "", err
}
resp, err := cli.ContainerCreate(context.Background(), &container.Config{
Image: image,
Tty: false}, nil, nil, nil, "")
if err != nil {
return "", err
}
return resp.ID, nil
}
Empty file added pkg/container/delete.go
Empty file.
51 changes: 51 additions & 0 deletions pkg/customapi/frpc_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2023 The opennaslab 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 applicabl e 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 customapi

import (
"fmt"
)

type FrpcParameterIn struct {
ServerAddr string `json:"serverAddr" description:"the frps server address" required:"true"`
ServerPort int `json:"serverPort" description:"the frps server port" required:"true"`
Service []FrpService `json:"service" description:"the service list" required:"true"`
}

type FrpService struct {
ServiceName string `json:"serviceName" description:"the service name" required:"true"`
LocalIP string `json:"localIP" description:"the local ip address" required:"true"`
LocalPort int `json:"localPort" description:"the local port" required:"true"`
RemotePort string `json:"remoteIP" description:"the remote port" required:"true"`
}

func (f FrpcParameterIn) Validate() error {
if f.ServerAddr == "" {
return fmt.Errorf("serverAddr is required")
}
if f.ServerPort == 0 {
return fmt.Errorf("serverPort is required")
}
if f.Service == nil || len(f.Service) == 0 {
return fmt.Errorf("service is required")
}
return nil
}

func (f FrpcParameterIn) GetExecutionConfig() ([]byte, error) {
return nil, nil
}
110 changes: 110 additions & 0 deletions pkg/customapi/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package customapi

import (
"encoding/json"
"fmt"
"reflect"

"opennaslab.io/bifrost/pkg/api"
)

var StepsInfoMap = map[string]StepsInfo{
"frpc-config": {
Name: "frpc-config",
Image: "opennaslab/frpc-config:latest",
Description: "install/config frpc in local host",
},
}

var StepsDocStruct = map[string]interface{}{
"frpc-config": FrpcParameterIn{Service: []FrpService{{}}},
}

var TypedInterfaceMap = map[string]TypedInterface{
"frpc-config": &FrpcParameterIn{},
}

type TypedInterface interface {
Validate() error
GetExecutionConfig() ([]byte, error)
}

func GetTypedConfig(step *api.ConfigurationStep) (TypedInterface, error) {
if _, ok := TypedInterfaceMap[step.Use]; !ok {
return nil, fmt.Errorf("not found")
}
// TODO: there is currency problem
ret := TypedInterfaceMap[step.Use]
if err := json.Unmarshal([]byte(step.In), ret); err != nil {
return nil, err
}
return ret, nil
}

type Documentation struct {
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Required bool `json:"required"`
Items []Documentation `json:"items,omitempty"`
}

func GenerateDocumentation(obj interface{}) []Documentation {
doc := []Documentation{}

t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)

for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
value := v.Field(i)

docField := Documentation{
Name: field.Tag.Get("json"),
Type: field.Type.Name(),
Description: field.Tag.Get("description"),
Required: field.Tag.Get("required") == "true",
}

if field.Type.Kind() == reflect.Slice {
docField.Type = "array"
docField.Items = append(docField.Items, GenerateDocumentation(value.Index(0).Interface())...)
}

doc = append(doc, docField)
}

return doc
}

type StepsInfo struct {
Name string `json:"name"`
Description string `json:"description"`
Image string `json:"image"`
Parameters StepParameter `json:"parameters"`
}

type StepParameter struct {
In []Documentation `json:"in"`
}

func GetLocalStepDefinition(name string) *StepsInfo {
if _, ok := StepsInfoMap[name]; !ok {
return nil
}
paraInDoc := GenerateDocumentation(StepsDocStruct[name])
ret := StepsInfoMap[name]
ret.Parameters.In = paraInDoc
return &ret
}

func ListLocalStepDefinitions() []StepsInfo {
ret := []StepsInfo{}
for name := range StepsInfoMap {
paraInDoc := GenerateDocumentation(StepsDocStruct[name])
ele := StepsInfoMap[name]
ele.Parameters.In = paraInDoc
ret = append(ret, ele)
}
return ret
}
Loading
Loading