forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve WorkflowTemplate lazily (argoproj#1655)
- Loading branch information
1 parent
d15994b
commit b5dcac8
Showing
5 changed files
with
75 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package templateresolution | ||
|
||
import ( | ||
"github.com/argoproj/argo/pkg/apis/workflow" | ||
v1alpha1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1" | ||
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
// lazyWorkflowTemplate retrieves WorkflowTemplate lazily. | ||
type lazyWorkflowTemplate struct { | ||
// wftmplGetter is a proxied WorkflowTemplate getter. | ||
wftmplGetter WorkflowTemplateNamespacedGetter | ||
// wftmpl is a cache of retrieved WorkflowTemplate. | ||
wftmpl *wfv1.WorkflowTemplate | ||
// namespace is the namespace of the WorkflowTemplate. | ||
namespace string | ||
// name is the name of the WorkflowTemplate. | ||
name string | ||
} | ||
|
||
var _ wfv1.TemplateGetter = &lazyWorkflowTemplate{} | ||
|
||
// NewLazyWorkflowTemplate is a public constructor of lazyWorkflowTemplate. | ||
func NewLazyWorkflowTemplate(wftmplGetter WorkflowTemplateNamespacedGetter, namespace, name string) *lazyWorkflowTemplate { | ||
return &lazyWorkflowTemplate{ | ||
wftmplGetter: wftmplGetter, | ||
namespace: namespace, | ||
name: name, | ||
} | ||
} | ||
|
||
// GetNamespace returns the namespace of the WorkflowTemplate. | ||
func (lwt *lazyWorkflowTemplate) GetNamespace() string { | ||
return lwt.namespace | ||
} | ||
|
||
// GetName returns the name of the WorkflowTemplate. | ||
func (lwt *lazyWorkflowTemplate) GetName() string { | ||
return lwt.name | ||
} | ||
|
||
// GroupVersionKind returns a GroupVersionKind of WorkflowTemplate. | ||
func (lwt *lazyWorkflowTemplate) GroupVersionKind() schema.GroupVersionKind { | ||
return v1alpha1.SchemeGroupVersion.WithKind(workflow.WorkflowTemplateKind) | ||
} | ||
|
||
// GetTemplateByName retrieves a defined template by its name | ||
func (lwt *lazyWorkflowTemplate) GetTemplateByName(name string) *wfv1.Template { | ||
err := lwt.ensureWorkflowTemplate() | ||
if err != nil { | ||
return nil | ||
} | ||
return lwt.wftmpl.GetTemplateByName(name) | ||
} | ||
|
||
func (lwt *lazyWorkflowTemplate) ensureWorkflowTemplate() error { | ||
if lwt.wftmpl == nil { | ||
wftmpl, err := lwt.wftmplGetter.Get(lwt.name) | ||
if err != nil { | ||
return err | ||
} | ||
lwt.wftmpl = wftmpl | ||
} | ||
return nil | ||
} |