Skip to content

Commit

Permalink
halfway through adding database stuff
Browse files Browse the repository at this point in the history
Signed-off-by: Yee Hing Tong <[email protected]>
  • Loading branch information
wild-endeavor committed Oct 12, 2023
1 parent f04aa07 commit 651aa2a
Show file tree
Hide file tree
Showing 16 changed files with 611 additions and 129 deletions.
75 changes: 2 additions & 73 deletions flyteartifacts/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,12 @@ import (
_ "net/http/pprof" // Required to serve application.
)

//
//var (
// cfgFile string
// configAccessor = viper.NewAccessor(config.Options{})
//)
//
//var serveCmd = &cobra.Command{
// Use: "serve",
// Short: "Launches the Flyte artifacts server",
// RunE: func(cmd *cobra.Command, args []string) error {
// ctx := context.Background()
// cfg := configuration.ApplicationConfig.GetConfig().(*configuration.ApplicationConfiguration)
// fmt.Printf("cfg: [%+v]\n", cfg)
// opts := make([]grpc.ServerOption, 0)
// return server.Serve(ctx, opts...)
// },
//}
//
//// RootCmd represents the base command when called without any subcommands
//var RootCmd = &cobra.Command{
// Use: "artifacts",
// Short: "Fill in later",
// Long: `
//To get started run the serve subcommand which will start a server on localhost:50051
//`,
// PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// return initConfig(cmd.Flags())
// },
//}
//
//func init() {
// pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
//
// // Add persistent flags - persistent flags persist through all sub-commands
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./artifact_config.yaml)")
//
// // Allow viper to read the value of the flags
// configAccessor.InitializePflags(RootCmd.PersistentFlags())
//
// // Command information
// RootCmd.AddCommand(serveCmd)
//
// err := flag.CommandLine.Parse([]string{})
// if err != nil {
// fmt.Println(err)
// os.Exit(-1)
// }
//
//}
//
//func initConfig(flags *pflag.FlagSet) error {
// configAccessor = viper.NewAccessor(config.Options{
// SearchPaths: []string{cfgFile, "./artifact_config.yaml", ".", "/etc/flyte/config", "$GOPATH/src/github.com/flyteorg/flyte/flyteartifacts"},
// StrictMode: false,
// })
//
// logger.Infof(context.TODO(), "Using config file: %v", configAccessor.ConfigFilesUsed())
//
// configAccessor.InitializePflags(flags)
//
// err := flag.CommandLine.Parse([]string{})
// if err != nil {
// fmt.Println(err)
// os.Exit(-1)
// }
//
// return configAccessor.UpdateConfig(context.TODO())
//}

func main() {
ctx := context.Background()
logger.Infof(ctx, "Beginning Flyte Artifacts Service")
rootCmd := sharedCmd.NewRootCmd("artifacts", server.GrpcRegistrationHook, server.HttpRegistrationHook)
//if err := RootCmd.ExecuteC(); err != nil {
// fmt.Println(err)
// panic(err)
//}

rootCmd.AddCommand(sharedCmd.NewMigrateCmd(migrations))
err := rootCmd.ExecuteContext(ctx)
if err != nil {
panic(err)
Expand Down
19 changes: 19 additions & 0 deletions flyteartifacts/cmd/shared/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package shared

import (
stdlibLogger "github.com/flyteorg/flyte/flytestdlib/logger"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/flyteorg/flyte/flytestdlib/promutils"
"github.com/spf13/cobra"
)
\
// NewMigrateCmd represents the migrate command
func NewMigrateCmd(migs []*gormigrate.Migration) *cobra.Command {
return &cobra.Command{
Use: "migrate",
Short: "This command will run all the migrations for the database",
RunE: func(cmd *cobra.Command, args []string) error {
return migrions.Migrate(confi.GetDBConfig(), stdlibLogger.GetConfig(), promutils.NewScope("dbmigrate"), migrs)
},
}
}
9 changes: 0 additions & 9 deletions flyteartifacts/cmd/shared/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,8 @@ var (
configAccessor = viper.NewAccessor(config.Options{})
)

//var XXRootCmd = &cobra.Command{
// Use: "artifacts",
// Short: "Fill in later",
// PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// return initConfig(cmd.Flags())
// },
//}

// NewRootCmd represents the base command when called without any subcommands
func NewRootCmd(rootUse string, grpcHook GrpcRegistrationHook, httpHook HttpRegistrationHook) *cobra.Command {

rootCmd := &cobra.Command{
Use: rootUse,
Short: "Short description",
Expand Down
2 changes: 1 addition & 1 deletion flyteartifacts/cmd/shared/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func newGRPCServer(ctx context.Context, serviceName string, serverCfg *sharedCfg
grpcHook GrpcRegistrationHook, opts ...grpc.ServerOption) (*grpc.Server, error) {

scope := promutils.NewScope(serverCfg.Metrics.MetricsScope)
scope = scope.NewSubScope(serviceName)

var grpcUnaryInterceptors = make([]grpc.UnaryServerInterceptor, 0)
var streamServerInterceptors = make([]grpc.StreamServerInterceptor, 0)
Expand Down Expand Up @@ -202,7 +203,6 @@ func newHTTPServer(_ sharedCfg.ServerSecurityOptions) (*http.ServeMux, *runtime.
gwmux := runtime.NewServeMux(gwmuxOptions...)

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
//ctx := GetOrGenerateRequestIDForRequest(r)
ctx := r.Context()
logger.Debugf(ctx, "Running identity interceptor for http endpoint [%s]", r.URL.String())

Expand Down
7 changes: 5 additions & 2 deletions flyteartifacts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ go 1.19
require (
github.com/flyteorg/flyte/flyteidl v0.0.0-00010101000000-000000000000
github.com/flyteorg/flyte/flytestdlib v1.9.5
github.com/go-gormigrate/gormigrate/v2 v2.1.1
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
google.golang.org/grpc v1.56.1

gorm.io/gorm v1.25.4
)

require (
Expand Down Expand Up @@ -51,6 +52,8 @@ require (
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand All @@ -71,7 +74,7 @@ require (
github.com/spf13/viper v1.11.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.7.0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions flyteartifacts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gormigrate/gormigrate/v2 v2.1.1 h1:eGS0WTFRV30r103lU8JNXY27KbviRnqqIDobW3EV3iY=
github.com/go-gormigrate/gormigrate/v2 v2.1.1/go.mod h1:L7nJ620PFDKei9QOhJzqA8kRCk+E3UbV2f5gv+1ndLc=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
Expand Down Expand Up @@ -268,6 +270,10 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down Expand Up @@ -434,8 +440,8 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f h1:OeJjE6G4dgCY4PIXvIRQbE8+RX+uXZyGhUy/ksMGJoc=
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b h1:huxqepDufQpLLIRXiVkTvnxrzJlpwmIWAObmcCcUFr0=
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -791,6 +797,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
29 changes: 27 additions & 2 deletions flyteartifacts/pkg/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,37 @@ import (

const artifactsServer = "artifactsServer"

type PostgresConfig struct {
Host string `json:"host" pflag:",The host name of the database server"`
Port int `json:"port" pflag:",The port name of the database server"`
DbName string `json:"dbname" pflag:",The database name"`
User string `json:"username" pflag:",The database user who is connecting to the server."`
// Either Password or PasswordPath must be set.
Password string `json:"password" pflag:",The database password."`
PasswordPath string `json:"passwordPath" pflag:",Points to the file containing the database password."`
ExtraOptions string `json:"options" pflag:",See http://gorm.io/docs/connecting_to_the_database.html for available options passed, in addition to the above."`
Debug bool `json:"debug" pflag:" Whether or not to start the database connection with debug mode enabled."`
}

type DbConfig struct {
Postgres PostgresConfig `json:"postgres" pflag:",Postgres database configuration"`
}

type ApplicationConfiguration struct {
MyTestValue string `json:"myTestValue" pflag:",Test config value"`
Database DbConfig `json:"database" pflag:",Database configuration"`
}

var defaultApplicationConfiguration = ApplicationConfiguration{
MyTestValue: "default",
Database: DbConfig{
Postgres: PostgresConfig{
// These values are suitable for local sandbox development
Host: "localhost",
Port: 30001,
DbName: "artifacts",
User: "postgres",
Password: "postgres",
},
},
}

var ApplicationConfig = config.MustRegisterSection(artifactsServer, &defaultApplicationConfiguration)
28 changes: 28 additions & 0 deletions flyteartifacts/pkg/db/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package shared

import (
stdlibLogger "github.com/flyteorg/flyte/flytestdlib/logger"
"github.com/flyteorg/flyte/flytestdlib/promutils"
"github.com/go-gormigrate/gormigrate/v2"
"google.golang.org/grpc/codes"
)

func PGMigrate(cfg *DbConfig, logCfg *stdlibLogger.Config, _ promutils.Scope, migrations []*gormigrate.Migration) error {
db, err := connection.OpenDbConnection(connection.NewPostgresDialector(cfg), cfg, logCfg)
if err != nil {
return serverErr.NewServerErrorf(codes.InvalidArgument, "Failed to open postgres connection to [%+s] with err: %+v", cfg.DbName, err)
}

if err := sharedmigrations.CreateDB(db, cfg.DbName); err != nil {
return serverErr.NewServerErrorf(codes.Internal, "Failed to ensure db [%s] exists wth err: %v", cfg.DbName, err)
}

if len(migrations) > 0 {
migrator := gormigrate.New(db, gormigrate.DefaultOptions, migrations)
err = migrator.Migrate()
if err != nil {
return serverErr.NewServerErrorf(codes.Internal, "Failed to migrate database: %v", err)
}
}
return nil
}
24 changes: 24 additions & 0 deletions flyteartifacts/pkg/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gormimpl

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

var Migrations = []*gormigrate.Migration{
{
ID: "2023-10-12-inits",
Migrate: func(tx *gorm.DB) error {
type Marker struct {
}
return tx.AutoMigrate(
&Marker{},
)
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable(
"markers",
)
},
},
}
29 changes: 29 additions & 0 deletions flyteartifacts/pkg/server/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package server

import (
"context"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/artifact"
)

type ArtifactHandler interface {
CreateArtifact(ctx context.Context, request *artifact.CreateArtifactRequest) (*artifact.CreateArtifactResponse, error)
GetArtifact(ctx context.Context, request *artifact.GetArtifactRequest) (*artifact.GetArtifactResponse, error)
}

type TriggerHandler interface {
CreateTrigger(ctx context.Context, request *artifact.CreateTriggerRequest) (*artifact.CreateTriggerResponse, error)
DeleteTrigger(ctx context.Context, request *artifact.DeleteTriggerRequest) (*artifact.DeleteTriggerResponse, error)
}

type TagHandler interface {
AddTag(ctx context.Context, request *artifact.AddTagRequest) (*artifact.AddTagResponse, error)
}

type LineageHandler interface {
RegisterProducer(ctx context.Context, request *artifact.RegisterProducerRequest) (*artifact.RegisterResponse, error)
RegisterConsumer(ctx context.Context, request *artifact.RegisterConsumerRequest) (*artifact.RegisterResponse, error)
}

type SearchHandler interface {
SearchArtifacts(ctx context.Context, request *artifact.SearchArtifactsRequest) (*artifact.SearchArtifactsResponse, error)
}
Loading

0 comments on commit 651aa2a

Please sign in to comment.