forked from kanisterio/kanister
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phase.go
211 lines (183 loc) · 6.08 KB
/
phase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2019 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kanister
import (
"context"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
"k8s.io/utils/strings/slices"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/log"
"github.com/kanisterio/kanister/pkg/param"
)
// Phase is an atomic unit of execution.
type Phase struct {
name string
args map[string]interface{}
objects map[string]crv1alpha1.ObjectReference
f Func
}
// Name returns the name of this phase.
func (p *Phase) Name() string {
return p.name
}
// Objects returns the phase object references
func (p *Phase) Objects() map[string]crv1alpha1.ObjectReference {
return p.objects
}
// Exec renders the argument templates in this Phase's Func and executes with
// those arguments.
func (p *Phase) Exec(ctx context.Context, bp crv1alpha1.Blueprint, action string, tp param.TemplateParams) (map[string]interface{}, error) {
if p.args == nil {
// Get the action from Blueprint
a, ok := bp.Actions[action]
if !ok {
return nil, errors.Errorf("Action {%s} not found in action map", action)
}
// Render the argument templates for the Phase's function
phases := []crv1alpha1.BlueprintPhase{}
phases = append(phases, a.Phases...)
if a.DeferPhase != nil {
phases = append(phases, *a.DeferPhase)
}
for _, ap := range phases {
if ap.Name != p.name {
continue
}
args, err := param.RenderArgs(ap.Args, tp)
if err != nil {
return nil, err
}
if err = checkRequiredArgs(p.f.RequiredArgs(), args); err != nil {
return nil, errors.Wrapf(err, "Required args missing for function %s", p.f.Name())
}
if err = checkSupportedArgs(p.f.Arguments(), args); err != nil {
return nil, errors.Wrapf(err, "Checking supported args for function %s.", p.f.Name())
}
p.args = args
}
}
// Execute the function
return p.f.Exec(ctx, tp, p.args)
}
func checkSupportedArgs(supportedArgs []string, args map[string]interface{}) error {
for a := range args {
if !slices.Contains(supportedArgs, a) {
return errors.Errorf("argument %s is not supported", a)
}
}
return nil
}
func GetDeferPhase(bp crv1alpha1.Blueprint, action, version string, tp param.TemplateParams) (*Phase, error) {
a, ok := bp.Actions[action]
if !ok {
return nil, errors.Errorf("Action {%s} not found in blueprint actions", action)
}
if a.DeferPhase == nil {
return nil, nil
}
regVersion, err := regFuncVersion(a.DeferPhase.Func, version)
if err != nil {
return nil, err
}
objs, err := param.RenderObjectRefs(a.DeferPhase.ObjectRefs, tp)
if err != nil {
return nil, err
}
return &Phase{
name: a.DeferPhase.Name,
objects: objs,
f: funcs[a.DeferPhase.Func][regVersion],
}, nil
}
func regFuncVersion(f, version string) (semver.Version, error) {
funcMu.RLock()
defer funcMu.RUnlock()
defaultVersion, funcVersion, err := getFunctionVersion(version)
if err != nil {
return semver.Version{}, errors.Wrapf(err, "Failed to get function version")
}
regVersion := *funcVersion
if _, ok := funcs[f]; !ok {
return semver.Version{}, errors.Errorf("Requested function {%s} has not been registered", f)
}
if _, ok := funcs[f][regVersion]; !ok {
if funcVersion.Equal(defaultVersion) {
return semver.Version{}, errors.Errorf("Requested function {%s} has not been registered with version {%s}", f, version)
}
if _, ok := funcs[f][*defaultVersion]; !ok {
return semver.Version{}, errors.Errorf("Requested function {%s} has not been registered with versions {%s} or {%s}", f, version, DefaultVersion)
}
log.Info().Print("Falling back to default version of the function", field.M{"Function": f, "PreferredVersion": version, "FallbackVersion": DefaultVersion})
return *defaultVersion, nil
}
return *funcVersion, nil
}
// GetPhases renders the returns a list of Phases with pre-rendered arguments.
func GetPhases(bp crv1alpha1.Blueprint, action, version string, tp param.TemplateParams) ([]*Phase, error) {
a, ok := bp.Actions[action]
if !ok {
return nil, errors.Errorf("Action {%s} not found in action map", action)
}
phases := make([]*Phase, 0, len(a.Phases))
// Check that all requested phases are registered and render object refs
for _, p := range a.Phases {
regVersion, err := regFuncVersion(p.Func, version)
if err != nil {
return nil, err
}
objs, err := param.RenderObjectRefs(p.ObjectRefs, tp)
if err != nil {
return nil, err
}
phases = append(phases, &Phase{
name: p.Name,
objects: objs,
f: funcs[p.Func][regVersion],
})
}
return phases, nil
}
// Validate gets the provided arguments from a blueprint and verifies that the required arguments are present
func (p *Phase) Validate(args map[string]interface{}) error {
if err := checkSupportedArgs(p.f.Arguments(), args); err != nil {
return err
}
return checkRequiredArgs(p.f.RequiredArgs(), args)
}
func checkRequiredArgs(reqArgs []string, args map[string]interface{}) error {
for _, a := range reqArgs {
if _, ok := args[a]; !ok {
return errors.Errorf("Required arg missing: %s", a)
}
}
return nil
}
func getFunctionVersion(version string) (*semver.Version, *semver.Version, error) {
dv, err := semver.NewVersion(DefaultVersion)
if err != nil {
return nil, nil, errors.Wrap(err, "Failed to parse default function version")
}
switch version {
case DefaultVersion, "":
return dv, dv, nil
default:
fv, err := semver.NewVersion(version)
if err != nil {
return nil, nil, errors.Wrapf(err, "Failed to parse function version {%s}", version)
}
return dv, fv, nil
}
}