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: add flink module #110

Merged
merged 8 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/goto/entropy/internal/store/postgres"
"github.com/goto/entropy/modules"
"github.com/goto/entropy/modules/firehose"
"github.com/goto/entropy/modules/flink"
"github.com/goto/entropy/modules/job"
"github.com/goto/entropy/modules/kafka"
"github.com/goto/entropy/modules/kubernetes"
Expand Down Expand Up @@ -92,6 +93,7 @@ func setupRegistry() module.Registry {
firehose.Module,
job.Module,
kafka.Module,
flink.Module,
}

registry := &modules.Registry{}
Expand Down
52 changes: 52 additions & 0 deletions modules/flink/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package flink

import (
_ "embed"
"encoding/json"

"github.com/goto/entropy/core/resource"
"github.com/goto/entropy/pkg/errors"
"github.com/goto/entropy/pkg/validator"
)

var (
//go:embed schema/config.json
configSchemaRaw []byte

validateConfig = validator.FromJSONSchema(configSchemaRaw)
)

type Influx struct {
URL string `json:"url,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}

type Config struct {
KubeNamespace string `json:"kube_namespace,omitempty"`
Influx Influx `json:"influx,omitempty"`
SinkKafkaStream string `json:"sink_kafka_stream,omitempty"`
}

func readConfig(_ resource.Resource, confJSON json.RawMessage, dc driverConf) (*Config, error) {
var cfg Config
if err := json.Unmarshal(confJSON, &cfg); err != nil {
return nil, errors.ErrInvalid.WithMsgf("invalid config json").WithCausef(err.Error())
}

if cfg.Influx.URL == "" {
cfg.Influx.URL = dc.Influx.URL
cfg.Influx.Username = dc.Influx.Username
cfg.Influx.Password = dc.Influx.Password
}

if cfg.SinkKafkaStream == "" {
cfg.SinkKafkaStream = dc.SinkKafkaStream
}

if cfg.KubeNamespace == "" {
cfg.KubeNamespace = dc.KubeNamespace
}

return &cfg, nil
}
37 changes: 37 additions & 0 deletions modules/flink/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package flink

import (
"encoding/json"

"github.com/goto/entropy/core/module"
"github.com/goto/entropy/modules/kubernetes"
"github.com/goto/entropy/pkg/errors"
)

type flinkDriver struct {
conf driverConf
}

type driverConf struct {
Influx Influx `json:"influx,omitempty"`
SinkKafkaStream string `json:"sink_kafka_stream,omitempty"`
KubeNamespace string `json:"kube_namespace,omitempty"`
}

type Output struct {
KubeCluster kubernetes.Output `json:"kube_cluster,omitempty"`
KubeNamespace string `json:"kube_namespace,omitempty"`
Influx Influx `json:"influx,omitempty"`
SinkKafkaStream string `json:"sink_kafka_stream,omitempty"`
}

func readOutputData(exr module.ExpandedResource) (*Output, error) {
var curOut Output
if len(exr.Resource.State.Output) == 0 {
return &curOut, nil
}
if err := json.Unmarshal(exr.Resource.State.Output, &curOut); err != nil {
return nil, errors.ErrInternal.WithMsgf("corrupted output").WithCausef(err.Error())
}
return &curOut, nil
}
38 changes: 38 additions & 0 deletions modules/flink/driver_output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package flink

import (
"context"
"encoding/json"

"github.com/goto/entropy/core/module"
"github.com/goto/entropy/modules"
"github.com/goto/entropy/modules/kubernetes"
"github.com/goto/entropy/pkg/errors"
)

func (fd *flinkDriver) Output(ctx context.Context, exr module.ExpandedResource) (json.RawMessage, error) {
output, err := readOutputData(exr)
if err != nil {
return nil, err
}

conf, err := readConfig(exr.Resource, exr.Resource.Spec.Configs, fd.conf)
if err != nil {
if errors.Is(err, errors.ErrInvalid) {
return nil, err
}
return nil, errors.ErrInternal.WithCausef(err.Error())
}

var kubeOut kubernetes.Output
if err := json.Unmarshal(exr.Dependencies[keyKubeDependency].Output, &kubeOut); err != nil {
return nil, errors.ErrInternal.WithMsgf("invalid kube state").WithCausef(err.Error())
}

output.KubeCluster = kubeOut
output.Influx = conf.Influx
output.KubeNamespace = conf.KubeNamespace
output.SinkKafkaStream = conf.SinkKafkaStream

return modules.MustJSON(output), nil
}
27 changes: 27 additions & 0 deletions modules/flink/driver_plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package flink

import (
"context"

"github.com/goto/entropy/core/module"
"github.com/goto/entropy/core/resource"
)

func (fd *flinkDriver) Plan(ctx context.Context, res module.ExpandedResource, act module.ActionRequest) (*resource.Resource, error) {
res.Resource.Spec = resource.Spec{
Configs: act.Params,
Dependencies: res.Spec.Dependencies,
}

output, err := fd.Output(ctx, res)
if err != nil {
return nil, err
}

res.Resource.State = resource.State{
Status: resource.StatusCompleted,
Output: output,
}

return &res.Resource, nil
}
16 changes: 16 additions & 0 deletions modules/flink/driver_sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package flink

import (
"context"

"github.com/goto/entropy/core/module"
"github.com/goto/entropy/core/resource"
)

func (*flinkDriver) Sync(_ context.Context, res module.ExpandedResource) (*resource.State, error) {
return &resource.State{
Status: resource.StatusCompleted,
Output: res.Resource.State.Output,
ModuleData: nil,
}, nil
}
33 changes: 33 additions & 0 deletions modules/flink/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package flink

import (
_ "embed"
"encoding/json"

"github.com/goto/entropy/core/module"
"github.com/goto/entropy/pkg/errors"
)

const (
keyKubeDependency = "kube_cluster"
)

var Module = module.Descriptor{
Kind: "flink",
Actions: []module.ActionDesc{
{
Name: module.CreateAction,
},
{
Name: module.UpdateAction,
},
},
DriverFactory: func(conf json.RawMessage) (module.Driver, error) {
fd := &flinkDriver{}
err := json.Unmarshal(conf, &fd)
if err != nil {
return nil, errors.ErrInvalid.WithMsgf("failed to unmarshal module config: %v", err)
}
return fd, nil
},
}
27 changes: 27 additions & 0 deletions modules/flink/schema/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"kube_namespace": {
"type": "string"
},
"influx": {
"type": "object",
"properties": {
"url": {
"type": "string"
},
"username": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"sink_kafka_stream_name": {
"type": "string"
}
}
}
2 changes: 1 addition & 1 deletion test/e2e_test/firehose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *FirehoseTestSuite) SetupTest() {

modules, err := s.moduleClient.ListModules(s.ctx, &entropyv1beta1.ListModulesRequest{})
s.Require().NoError(err)
s.Require().Equal(6, len(modules.GetModules()))
s.Require().Equal(9, len(modules.GetModules()))

resources, err := s.resourceClient.ListResources(s.ctx, &entropyv1beta1.ListResourcesRequest{
Kind: "kubernetes",
Expand Down
Loading
Loading