/
update.go
273 lines (223 loc) · 8.85 KB
/
update.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Copyright 2019 The Fission 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 function
import (
"fmt"
"github.com/pkg/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
_package "github.com/fission/fission/pkg/fission-cli/cmd/package"
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type UpdateSubCommand struct {
cmd.CommandActioner
function *fv1.Function
specFile string
}
func Update(input cli.Input) error {
return (&UpdateSubCommand{}).do(input)
}
func (opts *UpdateSubCommand) do(input cli.Input) error {
err := opts.complete(input)
if err != nil {
return err
}
return opts.run(input)
}
func (opts *UpdateSubCommand) complete(input cli.Input) error {
fnName := input.String(flagkey.FnName)
_, fnNamespace, err := opts.GetResourceNamespace(input, flagkey.NamespaceFunction)
if err != nil {
return errors.Wrap(err, "error in updating function ")
}
if input.Bool(flagkey.SpecSave) {
opts.specFile = fmt.Sprintf("function-%s.yaml", fnName)
}
function, err := opts.Client().FissionClientSet.CoreV1().Functions(fnNamespace).Get(input.Context(), input.String(flagkey.FnName), metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("read function '%v'", fnName))
}
envName := input.String(flagkey.FnEnvironmentName)
envNamespace := input.String(flagkey.NamespaceEnvironment)
// if the new env specified is the same as the old one, no need to update package
// same is true for all update parameters, but, for now, we don't check all of them - because, its ok to
// re-write the object with same old values, we just end up getting a new resource version for the object.
if len(envName) > 0 && envName == function.Spec.Environment.Name {
envName = ""
}
if envNamespace == function.Spec.Environment.Namespace {
envNamespace = ""
}
pkgName := input.String(flagkey.FnPackageName)
entrypoint := input.String(flagkey.FnEntrypoint)
secretNames := input.StringSlice(flagkey.FnSecret)
cfgMapNames := input.StringSlice(flagkey.FnCfgMap)
var secrets []fv1.SecretReference
var configMaps []fv1.ConfigMapReference
if len(secretNames) > 0 {
// check that the referenced secret is in the same ns as the function, if not give a warning.
for _, secretName := range secretNames {
err := util.SecretExists(input.Context(), &metav1.ObjectMeta{Namespace: fnNamespace, Name: secretName}, opts.Client().KubernetesClient)
if k8serrors.IsNotFound(err) {
console.Warn(fmt.Sprintf("secret %s not found in Namespace: %s. Secret needs to be present in the same namespace as function", secretName, fnNamespace))
}
}
for _, secretName := range secretNames {
newSecret := fv1.SecretReference{
Name: secretName,
Namespace: fnNamespace,
}
secrets = append(secrets, newSecret)
}
function.Spec.Secrets = secrets
}
if len(cfgMapNames) > 0 {
// check that the referenced cfgmap is in the same ns as the function, if not give a warning.
for _, cfgMapName := range cfgMapNames {
err := util.ConfigMapExists(input.Context(), &metav1.ObjectMeta{Namespace: fnNamespace, Name: cfgMapName}, opts.Client().KubernetesClient)
if k8serrors.IsNotFound(err) {
console.Warn(fmt.Sprintf("ConfigMap %s not found in Namespace: %s. ConfigMap needs to be present in the same namespace as the function", cfgMapName, fnNamespace))
}
}
for _, cfgMapName := range cfgMapNames {
newCfgMap := fv1.ConfigMapReference{
Name: cfgMapName,
Namespace: fnNamespace,
}
configMaps = append(configMaps, newCfgMap)
}
function.Spec.ConfigMaps = configMaps
}
if len(envName) > 0 {
function.Spec.Environment.Name = envName
}
if len(envNamespace) > 0 {
function.Spec.Environment.Namespace = envNamespace
}
if len(entrypoint) > 0 {
function.Spec.Package.FunctionName = entrypoint
}
if input.IsSet(flagkey.FnExecutionTimeout) {
fnTimeout := input.Int(flagkey.FnExecutionTimeout)
if fnTimeout <= 0 {
return errors.Errorf("--%v must be greater than 0", flagkey.FnExecutionTimeout)
}
function.Spec.FunctionTimeout = fnTimeout
}
if input.IsSet(flagkey.FnIdleTimeout) {
fnTimeout := input.Int(flagkey.FnIdleTimeout)
function.Spec.IdleTimeout = &fnTimeout
}
if input.IsSet(flagkey.FnConcurrency) {
function.Spec.Concurrency = input.Int(flagkey.FnConcurrency)
}
if input.IsSet(flagkey.FnRequestsPerPod) {
function.Spec.RequestsPerPod = input.Int(flagkey.FnRequestsPerPod)
}
if input.IsSet(flagkey.FnRetainPods) {
function.Spec.RetainPods = input.Int(flagkey.FnRetainPods)
}
if input.IsSet(flagkey.FnOnceOnly) {
function.Spec.OnceOnly = input.Bool(flagkey.FnOnceOnly)
}
if len(pkgName) == 0 {
pkgName = function.Spec.Package.PackageRef.Name
}
strategy, err := getInvokeStrategy(input, &function.Spec.InvokeStrategy)
if err != nil {
return err
}
function.Spec.InvokeStrategy = *strategy
resReqs, err := util.GetResourceReqs(input, &function.Spec.Resources)
if err != nil {
return err
}
function.Spec.Resources = *resReqs
pkg, err := opts.Client().FissionClientSet.CoreV1().Packages(fnNamespace).Get(input.Context(), pkgName, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("read package '%v.%v'. Pkg should be present in the same ns as the function", pkgName, fnNamespace))
}
forceUpdate := input.Bool(flagkey.PkgForce)
fnList, err := _package.GetFunctionsByPackage(input.Context(), opts.Client(), pkg.ObjectMeta.Name, pkg.ObjectMeta.Namespace)
if err != nil {
return errors.Wrap(err, "error getting function list")
}
if !forceUpdate && len(fnList) > 1 {
return errors.Errorf("Package is used by multiple functions, use --%v to force update", flagkey.PkgForce)
}
newPkgMeta, err := _package.UpdatePackage(input, opts.Client(), opts.specFile, pkg)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error updating package '%v'", pkgName))
}
// the package resource version of function has been changed,
// we need to update function resource version to prevent conflict.
// TODO: remove this block when deprecating pkg flags of function command.
if pkg.ObjectMeta.ResourceVersion != newPkgMeta.ResourceVersion {
var fns []fv1.Function
// don't update the package resource version of the function we are currently
// updating to prevent update conflict.
for _, fn := range fnList {
if fn.ObjectMeta.UID != function.ObjectMeta.UID {
fns = append(fns, fn)
}
}
err = _package.UpdateFunctionPackageResourceVersion(input.Context(), opts.Client(), newPkgMeta, fns...)
if err != nil {
return errors.Wrap(err, "error updating function package reference resource version")
}
}
// TODO : One corner case where user just updates the pkg reference with fnUpdate, but internally this new pkg reference
// references a diff env than the spec
// update function spec with new package metadata
function.Spec.Package.PackageRef = fv1.PackageRef{
Namespace: newPkgMeta.Namespace,
Name: newPkgMeta.Name,
ResourceVersion: newPkgMeta.ResourceVersion,
}
if function.Spec.Environment.Name != pkg.Spec.Environment.Name {
console.Warn("Function's environment is different than package's environment, package's environment will be used for updating function")
function.Spec.Environment.Name = pkg.Spec.Environment.Name
function.Spec.Environment.Namespace = pkg.Spec.Environment.Namespace
}
opts.function = function
err = util.ApplyLabelsAndAnnotations(input, &opts.function.ObjectMeta)
if err != nil {
return err
}
return nil
}
func (opts *UpdateSubCommand) run(input cli.Input) error {
if input.Bool(flagkey.SpecSave) {
err := opts.function.Validate()
if err != nil {
return fv1.AggregateValidationErrors("Function", err)
}
err = spec.SpecSave(*opts.function, opts.specFile, false)
if err != nil {
return errors.Wrap(err, "error saving function spec")
}
return nil
}
_, err := opts.Client().FissionClientSet.CoreV1().Functions(opts.function.Namespace).Update(input.Context(), opts.function, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "error updating function")
}
fmt.Printf("Function '%v' updated\n", opts.function.ObjectMeta.Name)
return nil
}