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

Narrow-down the download TypeInstance details #707

Merged
merged 3 commits into from
May 3, 2022
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
2 changes: 1 addition & 1 deletion cmd/terraform-runner/example-input/args.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:
# so setting it directly
- GOOGLE_PROJECT=capact
output:
goTemplate:
goTemplate: |
host: "{{ .instance_ip_addr }}"
port: 5432
defaultDBName: postgres
Expand Down
15 changes: 13 additions & 2 deletions cmd/ti-value-fetcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"fmt"
"log"
"time"

"google.golang.org/grpc"

"capact.io/capact/internal/logger"
tivaluefetcher "capact.io/capact/internal/ti-value-fetcher"
"github.com/vrischmann/envconfig"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"

"capact.io/capact/internal/logger"
tivaluefetcher "capact.io/capact/internal/ti-value-fetcher"
)

// Config holds TypeInstance Value resolver configuration.
Expand All @@ -26,6 +28,8 @@ type Config struct {
const appName = "ti-value-fetcher"

func main() {
start := time.Now()

var cfg Config
err := envconfig.InitWithPrefix(&cfg, "APP")
exitOnError(err, "while loading configuration")
Expand All @@ -48,6 +52,13 @@ func main() {

err = tiValueFetcher.SaveToFile(cfg.OutputFilePath, res)
exitOnError(err, fmt.Sprintf("while saving output to file %q", cfg.OutputFilePath))

// Argo doesn't like when a Pod exits too fast
// and this may happen when value already provided and no further logic is executed
minTime := start.Add(time.Second)
if time.Now().Before(minTime) {
time.Sleep(time.Second)
}
}

func exitOnError(err error, context string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ spec:
- GOOGLE_PROJECT=<@ providercredentials.project_id @>
- GOOGLE_APPLICATION_CREDENTIALS=/additional{{else}}[]{{end}}
output:
goTemplate:
goTemplate: |
{{- range $index, $output := .Outputs }}
{{ $output.Name }}: "{{`{{`}} .{{ $output.Name }} {{`}}`}}"
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ spec:
- AWS_ACCESS_KEY_ID=<@ providercredentials.accessKeyID @>
- AWS_SECRET_ACCESS_KEY=<@ providercredentials.secretAccessKey @>
output:
goTemplate:
goTemplate: |
instance_ids: "{{ .instance_ids }}"
random_number: "{{ .random_number }}"
variables: |+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ spec:
- GOOGLE_PROJECT=<@ providercredentials.project_id @>
- GOOGLE_APPLICATION_CREDENTIALS=/additional
output:
goTemplate:
goTemplate: |
instance_ids: "{{ .instance_ids }}"
random_number: "{{ .random_number }}"
variables: |+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ spec:
source: "https://example.com/module.tgz"
env: []
output:
goTemplate:
goTemplate: |
instance_ids: "{{ .instance_ids }}"
random_number: "{{ .random_number }}"
variables: |+
Expand Down

This file was deleted.

8 changes: 5 additions & 3 deletions pkg/argo-actions/download_type_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"fmt"

hubclient "capact.io/capact/pkg/hub/client"
"capact.io/capact/pkg/runner"
"github.com/pkg/errors"
"go.uber.org/zap"
"sigs.k8s.io/yaml"

hubclient "capact.io/capact/pkg/hub/client"
"capact.io/capact/pkg/hub/client/local"
"capact.io/capact/pkg/runner"
)

// DownloadAction represents the download TypeInstance action.
Expand Down Expand Up @@ -52,7 +54,7 @@ func NewDownloadAction(log *zap.Logger, client *hubclient.Client, cfg []Download
func (d *Download) Do(ctx context.Context) error {
for _, config := range d.cfg {
d.log.Info("Downloading TypeInstance", zap.String("ID", config.ID), zap.String("Path", config.Path))
typeInstance, err := d.client.FindTypeInstance(ctx, config.ID)
typeInstance, err := d.client.FindTypeInstance(ctx, config.ID, local.WithFields(local.TypeInstanceLatestResourceVersionFields))
if err != nil {
return err
}
Expand Down
11 changes: 2 additions & 9 deletions pkg/runner/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,14 @@ func (t *terraform) variables() ([]byte, error) {
}

func (t *terraform) renderOutput() ([]byte, error) {
if t.args.Output.GoTemplate == nil {
if t.args.Output.GoTemplate == "" {
return []byte{}, nil
}
if len(t.runOutput) == 0 {
return []byte{}, nil
}

// yaml.Unmarshal converts YAML to JSON then uses JSON to unmarshal into an object
// but the GoTemplate is defined via YAML, so we need to revert that change
artifactTemplate, err := yaml.JSONToYAML(t.args.Output.GoTemplate)
if err != nil {
return nil, errors.Wrap(err, "while converting GoTemplate property from JSON to YAML")
}

tmpl, err := template.New("output").Parse(string(artifactTemplate))
tmpl, err := template.New("output").Parse(t.args.Output.GoTemplate)
if err != nil {
return nil, errors.Wrap(err, "failed to load template")
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/runner/terraform/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package terraform

import "encoding/json"

// CommandType represents the operation type to be performed by the runner.
type CommandType string

Expand Down Expand Up @@ -33,7 +31,7 @@ type Module struct {

// AdditionalOutput stores input arguments for generating the additional output.
type AdditionalOutput struct {
GoTemplate json.RawMessage `yaml:"goTemplate"`
GoTemplate string `yaml:"goTemplate"`
}

// Config holds Runner related configuration.
Expand Down