Skip to content

Commit

Permalink
feat: support --env flag in start cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Oct 28, 2024
1 parent 2ae5b4f commit ca874a0
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 78 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ To run a basic HTTP request handler example using [ping.yaml](./examples/ping.ya
- kind: listener
name: listener
protocol: http
port: 8000
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand All @@ -71,7 +74,7 @@ To run a basic HTTP request handler example using [ping.yaml](./examples/ping.ya
To start the workflow, run:
```sh
uniflow start --from-specs example/ping.yaml
./dist/uniflow start --from-specs ./examples/ping.yaml --env=PORT=8000
```

Verify it's running by calling the HTTP endpoint:
Expand Down
7 changes: 5 additions & 2 deletions README_kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ make build
- kind: listener
name: listener
protocol: http
port: 8000
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand All @@ -71,7 +74,7 @@ make build
다음 명령어로 워크플로우를 실행합니다:
```sh
uniflow start --from-specs example/ping.yaml
./dist/uniflow start --from-specs ./examples/ping.yaml --env=PORT=8000
```

정상 작동 여부를 확인하려면 HTTP 엔드포인트를 호출하세요:
Expand Down
3 changes: 2 additions & 1 deletion cmd/pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const (
flagFromSecrets = "from-secrets"
flagFromCharts = "from-charts"

flagDebug = "debug"
flagDebug = "debug"
flagEnvironment = "env"

flagCPUProfile = "cpuprofile"
flagMemProfile = "memprofile"
Expand Down
16 changes: 11 additions & 5 deletions cmd/pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ func NewStartCommand(config StartConfig) *cobra.Command {
RunE: runStartCommand(config),
}

cmd.PersistentFlags().StringP(flagNamespace, toShorthand(flagNamespace), resourcebase.DefaultNamespace, "Set the namespace for running")
cmd.PersistentFlags().String(flagFromSpecs, "", "Specify the file path containing specs")
cmd.PersistentFlags().String(flagFromSecrets, "", "Specify the file path containing secrets")
cmd.PersistentFlags().String(flagFromCharts, "", "Specify the file path containing charts")
cmd.PersistentFlags().Bool(flagDebug, false, "Enable debug mode")
cmd.PersistentFlags().StringP(flagNamespace, toShorthand(flagNamespace), resourcebase.DefaultNamespace, "Set the namespace for running the workflow")
cmd.PersistentFlags().String(flagFromSpecs, "", "Specify the file path containing workflow specifications")
cmd.PersistentFlags().String(flagFromSecrets, "", "Specify the file path containing secrets for the workflow")
cmd.PersistentFlags().String(flagFromCharts, "", "Specify the file path containing charts for the workflow")
cmd.PersistentFlags().Bool(flagDebug, false, "Enable debug mode for detailed output during execution")
cmd.PersistentFlags().StringToString(flagEnvironment, nil, "Set environment variables for the workflow execution")

return cmd
}
Expand All @@ -62,6 +63,10 @@ func runStartCommand(config StartConfig) func(cmd *cobra.Command, args []string)
if err != nil {
return err
}
environment, err := cmd.Flags().GetStringToString(flagEnvironment)
if err != nil {
return err
}

if err := applySpecs(cmd); err != nil {
return err
Expand All @@ -80,6 +85,7 @@ func runStartCommand(config StartConfig) func(cmd *cobra.Command, args []string)

r := runtime.New(runtime.Config{
Namespace: namespace,
Environment: environment,
Scheme: config.Scheme,
Hook: h,
SpecStore: config.SpecStore,
Expand Down
5 changes: 4 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ Users can update node specifications by using a Command-Line Interface (CLI) or
- kind: listener
name: listener
protocol: http
port: 8000
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
error:
- name: catch
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
5 changes: 4 additions & 1 deletion docs/architecture_kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@
- kind: listener
name: listener
protocol: http
port: 8000
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
error:
- name: catch
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
2 changes: 1 addition & 1 deletion docs/key_concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ports:
env:
PORT:
- name: network
value: "{{ .PORT }}"
data: "{{ .PORT }}"
```
- `id`: A unique identifier in UUID format. UUID V7 is recommended.
Expand Down
2 changes: 1 addition & 1 deletion docs/key_concepts_kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ports:
env:
PORT:
- name: network
value: "{{ .PORT }}"
data: "{{ .PORT }}"
```
- `id`: UUID 형식의 고유 식별자입니다. UUID V7을 권장합니다.
Expand Down
8 changes: 5 additions & 3 deletions examples/httpproxy.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
- kind: listener
name: listener
protocol: http
port: 8000
port: '{{ .PORT }}'
ports:
out:
- name: proxy
- name: router
port: in

env:
PORT:
data: '{{ .PORT }}'
- kind: proxy
name: proxy
urls: [https://www.google.com/]
5 changes: 4 additions & 1 deletion examples/ping.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
- kind: listener
name: listener
protocol: http
port: 8000
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
5 changes: 4 additions & 1 deletion examples/system.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
- kind: listener
name: listener
protocol: http
port: 8000
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
error:
- name: catch
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
7 changes: 5 additions & 2 deletions examples/wsproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
# --header "Sec-WebSocket-Key: hqGkRtGkn4CjeomM99bTUw==" \
# --header "Sec-WebSocket-Version: 13" \
# http://localhost:8000/ws

- kind: listener
name: listener
protocol: http
port: 8000
port: '{{ .PORT }}'
ports:
out:
- name: router
port: in
env:
PORT:
data: '{{ .PORT }}'

- kind: router
name: router
Expand Down
11 changes: 8 additions & 3 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *Chart) IsBound(secrets ...*secret.Secret) bool {
func (c *Chart) Bind(secrets ...*secret.Secret) error {
for _, vals := range c.Env {
for i, val := range vals {
if val.ID != uuid.Nil || val.Name != "" {
if val.IsIdentified() {
example := &secret.Secret{
ID: val.ID,
Namespace: c.GetNamespace(),
Expand Down Expand Up @@ -139,14 +139,14 @@ func (c *Chart) Build(sp spec.Spec) ([]spec.Spec, error) {
env := map[string][]spec.Value{}
for key, vals := range c.Env {
for _, val := range vals {
if val.ID == uuid.Nil && val.Name == "" {
if val.IsIdentified() {
v, err := template.Execute(val.Value, data)
if err != nil {
return nil, err
}
val.Value = v
}
env[key] = append(env[key], spec.Value{Value: val.Value})
env[key] = append(env[key], spec.Value{Data: val.Value})
}
}

Expand Down Expand Up @@ -243,3 +243,8 @@ func (c *Chart) GetEnv() map[string][]Value {
func (c *Chart) SetEnv(val map[string][]Value) {
c.Env = val
}

// IsIdentified checks whether the Value instance has a unique identifier or name.
func (v *Value) IsIdentified() bool {
return v.ID != uuid.Nil || v.Name != ""
}
14 changes: 8 additions & 6 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (

// Config defines configuration options for the Runtime.
type Config struct {
Namespace string // Namespace defines the isolated execution environment for workflows.
Hook *hook.Hook // Hook is a collection of hook functions for managing symbols.
Scheme *scheme.Scheme // Scheme defines the scheme and behaviors for symbols.
SpecStore spec.Store // SpecStore is responsible for persisting specifications.
SecretStore secret.Store // SecretStore is responsible for persisting secrets.
ChartStore chart.Store // ChartStore is responsible for persisting charts.
Namespace string // Namespace defines the isolated execution environment for workflows.
Environment map[string]string // Environment holds the variables for the loader.
Hook *hook.Hook // Hook is a collection of hook functions for managing symbols.
Scheme *scheme.Scheme // Scheme defines the scheme and behaviors for symbols.
SpecStore spec.Store // SpecStore is responsible for persisting specifications.
SecretStore secret.Store // SecretStore is responsible for persisting secrets.
ChartStore chart.Store // ChartStore is responsible for persisting charts.
}

// Runtime represents an environment for executing Workflows.
Expand Down Expand Up @@ -68,6 +69,7 @@ func New(config Config) *Runtime {
UnloadHooks: []symbol.UnloadHook{config.Hook},
})
symbolLoader := symbol.NewLoader(symbol.LoaderConfig{
Environment: config.Environment,
Table: symbolTable,
Scheme: config.Scheme,
SpecStore: config.SpecStore,
Expand Down
6 changes: 3 additions & 3 deletions pkg/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ func TestRuntime_Reconcile(t *testing.T) {
Env: map[string][]spec.Value{
"key": {
{
ID: scrt.GetID(),
Value: "{{ . }}",
ID: scrt.GetID(),
Data: "{{ . }}",
},
},
},
Expand All @@ -186,7 +186,7 @@ func TestRuntime_Reconcile(t *testing.T) {
select {
case sb := <-symbols:
assert.Equal(t, meta.GetID(), sb.ID())
assert.Equal(t, scrt.Data, sb.Env()["key"][0].Value)
assert.Equal(t, scrt.Data, sb.Env()["key"][0].Data)
case <-ctx.Done():
assert.NoError(t, ctx.Err())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scheme/scheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestScheme_Decode(t *testing.T) {
Env: map[string][]spec.Value{
"FOO": {
{
Value: "foo",
Data: "foo",
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ func (s *Secret) GetData() any {
func (s *Secret) SetData(val any) {
s.Data = val
}

// IsIdentified checks whether the Value instance has a unique identifier or name.
func (s *Secret) IsIdentified() bool {
return s.ID != uuid.Nil || s.Name != ""
}
1 change: 1 addition & 0 deletions pkg/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ func TestSecret_Get(t *testing.T) {
assert.Equal(t, scrt.Name, scrt.GetName())
assert.Equal(t, scrt.Annotations, scrt.GetAnnotations())
assert.Equal(t, scrt.Data, scrt.GetData())
assert.True(t, scrt.IsIdentified())
}
Loading

0 comments on commit ca874a0

Please sign in to comment.