Skip to content

Commit

Permalink
Merge branch 'main' into fix_typeinstance
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Kuziemko committed Feb 5, 2022
2 parents ae8b3dc + 49defde commit 5ce9612
Show file tree
Hide file tree
Showing 24 changed files with 560 additions and 214 deletions.
2 changes: 1 addition & 1 deletion cmd/argo-actions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {

case argoactions.UpdateAction:
log := logger.With(zap.String("Action", argoactions.UpdateAction))
action = argoactions.NewUpdateAction(log, localClient, cfg.UpdateConfig)
action = argoactions.NewUpdateAction(log, localClient, publicClient, cfg.UpdateConfig)

default:
err := fmt.Errorf("Invalid action: %s", cfg.Action)
Expand Down
19 changes: 13 additions & 6 deletions cmd/cli/cmd/typeinstance/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"capact.io/capact/internal/cli/heredoc"
"capact.io/capact/internal/cli/printer"
gqllocalapi "capact.io/capact/pkg/hub/api/graphql/local"

"capact.io/capact/pkg/sdk/validation/manifest"
"capact.io/capact/pkg/sdk/apis/0.0.1/types"
"capact.io/capact/pkg/sdk/validation"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -100,12 +100,19 @@ func createTI(ctx context.Context, opts createOptions, resourcePrinter *printer.
}

for _, ti := range out.TypeInstances {
validationResult, err := manifest.ValidateTI(ctx, ti, hubCli)
validationResult, err := validation.ValidateTI(ctx, &validation.TypeInstanceValidation{
Alias: ti.Alias,
Value: ti.Value,
TypeRef: types.TypeRef{
Path: ti.TypeRef.Path,
Revision: ti.TypeRef.Revision,
},
}, hubCli)
if err != nil {
return err
return errors.Wrap(err, "while validating TypeInstance")
}
if len(validationResult.Errors) > 0 {
return fmt.Errorf("%s", validationResult.Errors)
if validationResult.Len() > 0 {
return validationResult.ErrorOrNil()
}
}

Expand Down
27 changes: 27 additions & 0 deletions cmd/cli/cmd/typeinstance/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (
"capact.io/capact/internal/cli/client"
"capact.io/capact/internal/cli/config"
gqllocalapi "capact.io/capact/pkg/hub/api/graphql/local"
"capact.io/capact/pkg/sdk/apis/0.0.1/types"
"capact.io/capact/pkg/sdk/validation"

"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -58,6 +61,30 @@ func editTI(ctx context.Context, opts editOptions, w io.Writer) error {
return err
}

for _, ti := range typeInstanceToUpdate {
if ti.TypeInstance == nil {
continue
}
currentTI, err := hubCli.FindTypeInstance(ctx, ti.ID)
if err != nil {
return errors.Wrapf(err, "while finding TypeInstance %s", ti.ID)
}

validationResult, err := validation.ValidateTI(ctx, &validation.TypeInstanceValidation{
Value: ti.TypeInstance.Value,
TypeRef: types.TypeRef{
Path: currentTI.TypeRef.Path,
Revision: currentTI.TypeRef.Revision,
},
}, hubCli)
if err != nil {
return errors.Wrap(err, "while validating TypeInstance")
}
if validationResult.Len() > 0 {
return validationResult.ErrorOrNil()
}
}

_, err = hubCli.UpdateTypeInstances(ctx, typeInstanceToUpdate)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions internal/cli/client/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type Hub interface {
ListInterfaces(ctx context.Context, opts ...public.InterfaceOption) ([]*gqlpublicapi.Interface, error)
ListTypeInstances(ctx context.Context, filter *gqllocalapi.TypeInstanceFilter, opts ...local.TypeInstancesOption) ([]gqllocalapi.TypeInstance, error)
ListImplementationRevisions(ctx context.Context, opts ...public.ListImplementationRevisionsOption) ([]*gqlpublicapi.ImplementationRevision, error)
FindTypeRevision(ctx context.Context, ref gqlpublicapi.TypeReference, opts ...public.TypeRevisionOption) (*gqlpublicapi.TypeRevision, error)
FindTypeInstance(ctx context.Context, id string, opts ...local.TypeInstancesOption) (*gqllocalapi.TypeInstance, error)
CreateTypeInstance(ctx context.Context, in *gqllocalapi.CreateTypeInstanceInput, opts ...local.TypeInstancesOption) (*gqllocalapi.TypeInstance, error)
CreateTypeInstances(ctx context.Context, in *gqllocalapi.CreateTypeInstancesInput) ([]gqllocalapi.CreateTypeInstanceOutput, error)
Expand All @@ -34,9 +33,6 @@ type Hub interface {
FindInterfaceRevision(ctx context.Context, ref gqlpublicapi.InterfaceReference, opts ...public.InterfaceRevisionOption) (*gqlpublicapi.InterfaceRevision, error)
FindTypeInstancesTypeRef(ctx context.Context, ids []string) (map[string]gqllocalapi.TypeInstanceTypeReference, error)
CheckManifestRevisionsExist(ctx context.Context, manifestRefs []gqlpublicapi.ManifestReference) (map[gqlpublicapi.ManifestReference]bool, error)
// not implemented
GetInterfaceLatestRevisionString(ctx context.Context, ref gqlpublicapi.InterfaceReference) (string, error)
ListImplementationRevisionsForInterface(ctx context.Context, ref gqlpublicapi.InterfaceReference, opts ...public.ListImplementationRevisionsForInterfaceOption) ([]gqlpublicapi.ImplementationRevision, error)
}

// NewHub returns client for Capact Hub configured with saved credentials for a given server URL.
Expand Down
4 changes: 0 additions & 4 deletions internal/cli/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ func (r *ValidationResult) Error() string {
errMsgs = append(errMsgs, err.Error())
}

if r.Path == "" {
return fmt.Sprintf("\n * %s\n", strings.Join(errMsgs, "\n * "))
}

return fmt.Sprintf("%q:\n * %s\n", r.Path, strings.Join(errMsgs, "\n * "))
}

Expand Down
48 changes: 40 additions & 8 deletions pkg/argo-actions/update_type_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (
"path"
"path/filepath"

"capact.io/capact/pkg/sdk/apis/0.0.1/types"
"capact.io/capact/pkg/sdk/validation"

graphqllocal "capact.io/capact/pkg/hub/api/graphql/local"
"capact.io/capact/pkg/hub/client/local"
"capact.io/capact/pkg/hub/client/public"
"github.com/pkg/errors"
"go.uber.org/zap"
"sigs.k8s.io/yaml"
Expand All @@ -25,17 +29,19 @@ type UpdateConfig struct {
// Update implements the Action interface.
// It is used to update existing TypeInstances in the Local Hub.
type Update struct {
log *zap.Logger
client *local.Client
cfg UpdateConfig
log *zap.Logger
localClient *local.Client
publicClient *public.Client
cfg UpdateConfig
}

// NewUpdateAction returns a new Update instance.
func NewUpdateAction(log *zap.Logger, client *local.Client, cfg UpdateConfig) Action {
func NewUpdateAction(log *zap.Logger, localClient *local.Client, publicClient *public.Client, cfg UpdateConfig) Action {
return &Update{
log: log,
client: client,
cfg: cfg,
log: log,
localClient: localClient,
publicClient: publicClient,
cfg: cfg,
}
}

Expand Down Expand Up @@ -83,6 +89,32 @@ func (u *Update) Do(ctx context.Context) error {
return errors.Wrap(err, "while rendering UpdateTypeInstancesInput")
}

u.log.Info("Validating TypeInstances")

for _, ti := range payload {
if ti.TypeInstance == nil {
continue
}
currentTI, err := u.localClient.FindTypeInstance(ctx, ti.ID)
if err != nil {
return errors.Wrapf(err, "while finding TypeInstance %s", ti.ID)
}

validationResult, err := validation.ValidateTI(ctx, &validation.TypeInstanceValidation{
Value: ti.TypeInstance.Value,
TypeRef: types.TypeRef{
Path: currentTI.TypeRef.Path,
Revision: currentTI.TypeRef.Revision,
},
}, u.publicClient)
if err != nil {
return errors.Wrap(err, "while validating TypeInstance")
}
if validationResult.Len() > 0 {
return validationResult.ErrorOrNil()
}
}

u.log.Info("Updating TypeInstances in Hub...", zap.Int("TypeInstance count", len(payload)))

uploadOutput, err := u.updateTypeInstances(ctx, payload)
Expand Down Expand Up @@ -110,5 +142,5 @@ func (u *Update) render(payload []graphqllocal.UpdateTypeInstancesInput, values
}

func (u *Update) updateTypeInstances(ctx context.Context, in []graphqllocal.UpdateTypeInstancesInput) ([]graphqllocal.TypeInstance, error) {
return u.client.UpdateTypeInstances(ctx, in)
return u.localClient.UpdateTypeInstances(ctx, in)
}
22 changes: 14 additions & 8 deletions pkg/argo-actions/upload_type_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"path/filepath"

graphqllocal "capact.io/capact/pkg/hub/api/graphql/local"
"capact.io/capact/pkg/sdk/validation/manifest"
"capact.io/capact/pkg/sdk/apis/0.0.1/types"
"capact.io/capact/pkg/sdk/validation"

"capact.io/capact/pkg/hub/client/local"
"capact.io/capact/pkg/hub/client/public"
Expand Down Expand Up @@ -88,16 +89,21 @@ func (u *Upload) Do(ctx context.Context) error {
return errors.Wrap(err, "while rendering CreateTypeInstancesInput")
}

u.log.Info("Validating TypeInstances")

for _, ti := range payload.TypeInstances {
u.log.Info(fmt.Sprintf("Validating TypeInstance... %s", *ti.Alias))
u.log.Info(fmt.Sprintf("ti %+v", *ti))
u.log.Info(fmt.Sprintf("TypeRef %+v", *ti.TypeRef))
validationResult, err := manifest.ValidateTI(ctx, ti, u.publicClient)
validationResult, err := validation.ValidateTI(ctx, &validation.TypeInstanceValidation{
Value: ti.Value,
TypeRef: types.TypeRef{
Path: ti.TypeRef.Path,
Revision: ti.TypeRef.Revision,
},
}, u.publicClient)
if err != nil {
return err
return errors.Wrap(err, "while validating TypeInstance")
}
if len(validationResult.Errors) > 0 {
return fmt.Errorf("%s", validationResult.Errors)
if validationResult.Len() > 0 {
return validationResult.ErrorOrNil()
}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/hub/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type Local interface {
// Public interface aggregates methods to interact with Capact Public Hub.
type Public interface {
ListTypes(ctx context.Context, opts ...public.TypeOption) ([]*hubpublicgraphql.Type, error)
FindTypeRevision(ctx context.Context, ref hubpublicgraphql.TypeReference, opts ...public.TypeRevisionOption) (*hubpublicgraphql.TypeRevision, error)
GetInterfaceLatestRevisionString(ctx context.Context, ref hubpublicgraphql.InterfaceReference) (string, error)
FindInterfaceRevision(ctx context.Context, ref hubpublicgraphql.InterfaceReference, opts ...public.InterfaceRevisionOption) (*hubpublicgraphql.InterfaceRevision, error)
ListImplementationRevisionsForInterface(ctx context.Context, ref hubpublicgraphql.InterfaceReference, opts ...public.ListImplementationRevisionsForInterfaceOption) ([]hubpublicgraphql.ImplementationRevision, error)
Expand Down
58 changes: 0 additions & 58 deletions pkg/hub/client/public/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,6 @@ func (c *Client) ListTypes(ctx context.Context, opts ...TypeOption) ([]*gqlpubli
return resp.Types, nil
}

func (c *Client) FindTypeRevision(ctx context.Context, ref gqlpublicapi.TypeReference, opts ...TypeRevisionOption) (*gqlpublicapi.TypeRevision, error) {
findOpts := &TypeRevisionOptions{}
findOpts.Apply(opts...)

query, params := c.typeQueryForRef(findOpts.fields, ref)
req := graphql.NewRequest(fmt.Sprintf(`query FindTypeRevision($typePath: NodePath!, %s) {
type(path: $typePath) {
%s
}
}`, params.Query(), query))

req.Var("typePath", ref.Path)
params.PopulateVars(req)

var resp struct {
Type struct {
Revision *gqlpublicapi.TypeRevision `json:"rev"`
} `json:"type"`
}
err := retry.Do(func() error {
return c.client.Run(ctx, req, &resp)
}, retry.Attempts(retryAttempts))

if err != nil {
return nil, errors.Wrap(err, "while executing query to fetch Hub Type Revision")
}

return resp.Type.Revision, nil
}

// ListInterfaces returns all Interfaces. By default, only root fields are populated. Use options to add
// latestRevision fields or apply additional filtering.
func (c *Client) ListInterfaces(ctx context.Context, opts ...InterfaceOption) ([]*gqlpublicapi.Interface, error) {
Expand Down Expand Up @@ -396,31 +366,3 @@ func (c *Client) specificInterfaceRevision(fields string, rev string) (string, A
"$interfaceRev: Version!": rev,
}
}

func (c *Client) typeQueryForRef(fields string, ref gqlpublicapi.TypeReference) (string, Args) {
if ref.Revision == "" {
return c.latestTypeRevision(fields)
}

return c.specificTypeRevision(fields, ref.Revision)
}

func (c *Client) latestTypeRevision(fields string) (string, Args) {
latestRevision := fmt.Sprintf(`
rev: latestRevision {
%s
}`, fields)

return latestRevision, Args{}
}

func (c *Client) specificTypeRevision(fields string, rev string) (string, Args) {
specificRevision := fmt.Sprintf(`
rev: revision(revision: $typeRev) {
%s
}`, fields)

return specificRevision, Args{
"$typeRev: Version!": rev,
}
}
12 changes: 9 additions & 3 deletions pkg/sdk/renderer/argo/dedicated_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ func newDedicatedRenderer(maxDepth int, policyEnforcedCli PolicyEnforcedHubClien
return r
}

func (r *dedicatedRenderer) WrapEntrypointWithRootStep(workflow *Workflow) (*Workflow, *WorkflowStep) {
func (r *dedicatedRenderer) WrapEntrypointWithRootStep(workflow *Workflow) (*Workflow, *WorkflowStep, error) {
if workflow == nil || workflow.WorkflowSpec == nil || workflow.Entrypoint == "" {
return nil, nil, errors.New("workflow and its entrypoint cannot be empty")
}

r.entrypointStep = &WorkflowStep{
WorkflowStep: &wfv1.WorkflowStep{
Name: "start-entrypoint",
Expand All @@ -98,7 +102,7 @@ func (r *dedicatedRenderer) WrapEntrypointWithRootStep(workflow *Workflow) (*Wor
workflow.Entrypoint = r.rootTemplate.Name
workflow.Templates = append(workflow.Templates, r.rootTemplate)

return workflow, r.entrypointStep
return workflow, r.entrypointStep, nil
}

func (r *dedicatedRenderer) AppendAdditionalInputTypeInstances(typeInstances []types.InputTypeInstanceRef) {
Expand Down Expand Up @@ -396,7 +400,9 @@ func (r *dedicatedRenderer) UnmarshalWorkflowFromImplementation(prefix string, i
if err != nil {
return nil, nil, errors.Wrap(err, "while unmarshaling Argo Workflow from OCF Implementation")
}

if workflow == nil || workflow.WorkflowSpec == nil || workflow.Entrypoint == "" {
return nil, nil, errors.New("workflow and its entrypoint cannot be empty")
}
artifactsNameMapping := map[string]string{}

for i := range workflow.Templates {
Expand Down
3 changes: 2 additions & 1 deletion pkg/sdk/renderer/argo/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func interfaceRefToHub(in types.InterfaceRef) hubpublicgraphql.InterfaceReferenc
}
}

func getEntrypointWorkflowIndex(w *Workflow) (int, error) {
// GetEntrypointWorkflowIndex returns workflow entrypoint index
func GetEntrypointWorkflowIndex(w *Workflow) (int, error) {
if w == nil {
return 0, NewWorkflowNilError()
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/sdk/renderer/argo/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func (r *Renderer) Render(ctx context.Context, input *RenderInput) (*RenderOutpu
}

// 3.1 Add our own root step and replace entrypoint
rootWorkflow, entrypointStep := dedicatedRenderer.WrapEntrypointWithRootStep(rootWorkflow)
rootWorkflow, entrypointStep, err := dedicatedRenderer.WrapEntrypointWithRootStep(rootWorkflow)
if err != nil {
return nil, errors.Wrap(err, "while wrapping entrypoint with root step")
}

// 4. Add user input
if err := dedicatedRenderer.AddUserInputSecretRefIfProvided(rootWorkflow); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/renderer/argo/typeinstance_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (r *TypeInstanceHandler) AddInputTypeInstances(rootWorkflow *Workflow, inst
return nil
}

idx, err := getEntrypointWorkflowIndex(rootWorkflow)
idx, err := GetEntrypointWorkflowIndex(rootWorkflow)
if err != nil {
return err
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func (r *TypeInstanceHandler) AddUploadTypeInstancesStep(rootWorkflow *Workflow,
},
}

idx, err := getEntrypointWorkflowIndex(rootWorkflow)
idx, err := GetEntrypointWorkflowIndex(rootWorkflow)
if err != nil {
return err
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func (r *TypeInstanceHandler) AddUpdateTypeInstancesStep(rootWorkflow *Workflow,
},
}

idx, err := getEntrypointWorkflowIndex(rootWorkflow)
idx, err := GetEntrypointWorkflowIndex(rootWorkflow)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 5ce9612

Please sign in to comment.