/
apply.go
1299 lines (1134 loc) · 40.2 KB
/
apply.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 spec
import (
"context"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/go-git/go-git/v5"
"github.com/mholt/archiver/v3"
"github.com/pkg/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"
pkgutil "github.com/fission/fission/pkg/fission-cli/cmd/package/util"
spectypes "github.com/fission/fission/pkg/fission-cli/cmd/spec/types"
"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"
"github.com/fission/fission/pkg/utils"
)
type ApplySubCommand struct {
cmd.CommandActioner
}
// Apply compares the specs in the spec/config/ directory to the
// deployed resources on the cluster, and reconciles the differences
// by creating, updating or deleting resources on the cluster.
//
// Apply is idempotent.
//
// Apply is *not* transactional -- if the user hits Ctrl-C, or their laptop dies
// etc, while doing an apply, they will get a partially applied deployment. However,
// they can retry their apply command once they're back online.
func Apply(input cli.Input) error {
return (&ApplySubCommand{}).do(input)
}
func (opts *ApplySubCommand) do(input cli.Input) error {
return opts.run(input)
}
// insertNamespace inserts the Namespace value if it was not provided at the time of `spec save`.
// we make sure that all component of a resource should be present in the same Namespace. i.e.
// Function's env and package should be present in same namespace
func (opts *ApplySubCommand) insertNamespace(input cli.Input, fr *FissionResources) error {
result := utils.MultiErrorWithFormat()
_, currentNS, err := opts.GetResourceNamespace(input, flagkey.NamespaceEnvironment)
if err != nil {
return fv1.AggregateValidationErrors("Environment", err)
}
for i := range fr.Functions {
if fr.Functions[i].Namespace == "" || input.Bool(flagkey.ForceNamespace) {
fr.Functions[i].Namespace = currentNS
fr.Functions[i].Spec.Package.PackageRef.Namespace = currentNS
fr.Functions[i].Spec.Environment.Namespace = currentNS
for j := range fr.Functions[i].Spec.ConfigMaps {
fr.Functions[i].Spec.ConfigMaps[j].Namespace = currentNS
}
for j := range fr.Functions[i].Spec.Secrets {
fr.Functions[i].Spec.Secrets[j].Namespace = currentNS
}
}
}
for i := range fr.Environments {
if fr.Environments[i].Namespace == "" || input.Bool(flagkey.ForceNamespace) {
fr.Environments[i].Namespace = currentNS
}
}
for i := range fr.Packages {
if fr.Packages[i].Namespace == "" || input.Bool(flagkey.ForceNamespace) {
fr.Packages[i].Namespace = currentNS
fr.Packages[i].Spec.Environment.Namespace = currentNS
fr.Packages[i].ObjectMeta.Namespace = currentNS
}
}
for i := range fr.HttpTriggers {
if fr.HttpTriggers[i].Namespace == "" || input.Bool(flagkey.ForceNamespace) {
fr.HttpTriggers[i].Namespace = currentNS
}
}
for i := range fr.MessageQueueTriggers {
if fr.MessageQueueTriggers[i].Namespace == "" || input.Bool(flagkey.ForceNamespace) {
fr.MessageQueueTriggers[i].Namespace = currentNS
}
}
for i := range fr.TimeTriggers {
if fr.TimeTriggers[i].Namespace == "" || input.Bool(flagkey.ForceNamespace) {
fr.TimeTriggers[i].Namespace = currentNS
}
}
for i := range fr.KubernetesWatchTriggers {
if fr.KubernetesWatchTriggers[i].Namespace == "" || input.Bool(flagkey.ForceNamespace) {
fr.KubernetesWatchTriggers[i].Namespace = currentNS
}
}
return result.ErrorOrNil()
}
func (opts *ApplySubCommand) run(input cli.Input) error {
specDir := util.GetSpecDir(input)
specIgnore := util.GetSpecIgnore(input)
applyCommitLabel := input.Bool(flagkey.SpecApplyCommitLabel)
deleteResources := input.Bool(flagkey.SpecDelete)
watchResources := input.Bool(flagkey.SpecWatch)
waitForBuild := input.Bool(flagkey.SpecWait)
validateSpecs := util.GetValidationFlag(input)
var watcher *fsnotify.Watcher
var pbw *packageBuildWatcher
if watchResources || waitForBuild {
// init package build watcher
pbw = makePackageBuildWatcher(opts.Client())
}
if watchResources {
var err error
watcher, err = fsnotify.NewWatcher()
if err != nil {
return errors.Wrap(err, "error creating file watcher")
}
// add watches
rootDir := filepath.Clean(specDir + "/..")
err = filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err, "error scanning project files")
}
if ignoreFile(path) {
return nil
}
err = watcher.Add(path)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error watching path %v", path))
}
return nil
})
if err != nil {
return errors.Wrap(err, "error scanning files to watch")
}
}
for {
// read all specs
fr, err := ReadSpecs(specDir, specIgnore, applyCommitLabel)
if err != nil {
return errors.Wrap(err, "error reading specs")
}
err = opts.insertNamespace(input, fr)
if err != nil {
return errors.Wrap(err, "error reading specs")
}
if validateSpecs {
err = validateForApply(input, fr)
if err != nil {
return errors.Wrap(err, "abort applying resources")
}
}
err = warnIfDirtyWorkTree(filepath.Clean(specDir + "/.."))
if err != nil {
console.Warn(err.Error())
}
// make changes to the cluster based on the specs
pkgMetas, as, err := applyResources(input, opts.Client(), specDir, fr, deleteResources, input.Bool(flagkey.SpecAllowConflicts))
if err != nil {
return errors.Wrap(err, "error applying specs")
}
printApplyStatus(as)
if watchResources || waitForBuild {
// watch package builds
pbw.addPackages(pkgMetas)
}
ctx, pkgWatchCancel := context.WithCancel(input.Context())
if watchResources {
// if we're watching for files, we don't need to wait for builds to complete
go pbw.watch(ctx)
} else if waitForBuild {
// synchronously wait for build if --wait was specified
pbw.watch(ctx)
}
if !watchResources {
pkgWatchCancel()
break
}
// listen for file watch events
fmt.Println("Watching files for changes...")
waitloop:
for {
select {
case e := <-watcher.Events:
if ignoreFile(e.Name) {
continue waitloop
}
fmt.Printf("Noticed a file change, reapplying specs...\n")
// Builds that finish after this cancellation will be
// printed in the next watchPackageBuildStatus call.
pkgWatchCancel()
err = waitForFileWatcherToSettleDown(watcher)
if err != nil {
return errors.Wrap(err, "error watching files")
}
break waitloop
case err := <-watcher.Errors:
pkgWatchCancel()
if err != nil {
return errors.Wrap(err, "error watching files")
}
}
}
}
return nil
}
func warnIfDirtyWorkTree(path string) error {
repo, err := git.PlainOpen(path)
if err != nil {
console.Info("Spec doesn't belong to Git Tree.")
return nil
}
workTree, err := repo.Worktree()
if err != nil {
return err
}
status, err := workTree.Status()
if err != nil {
return err
}
if !status.IsClean() {
console.Warn("Worktree is not clean, please ensure you have committed the changes to git.")
}
return nil
}
func ignoreFile(path string) bool {
return (strings.Contains(path, "/.#") || // editor autosave files
strings.HasSuffix(path, "~")) // editor backups, usually
}
func waitForFileWatcherToSettleDown(watcher *fsnotify.Watcher) error {
// Wait a bit for things to settle down in case a bunch of
// files changed; also drain all events that queue up during
// the wait interval.
time.Sleep(500 * time.Millisecond)
for {
select {
case <-watcher.Events:
time.Sleep(200 * time.Millisecond)
continue
case err := <-watcher.Errors:
return err
default:
return nil
}
}
}
// printApplyStatus prints a summary of what changed on the
// cluster as the result of a spec apply operation.
func printApplyStatus(applyStatus map[string]ResourceApplyStatus) {
changed := false
for typ, ras := range applyStatus {
n := len(ras.Created)
if n > 0 {
changed = true
fmt.Printf("%v %v created: %v\n", n, pluralize(n, typ), strings.Join(metadataNames(ras.Created), ", "))
}
n = len(ras.Updated)
if n > 0 {
changed = true
fmt.Printf("%v %v updated: %v\n", n, pluralize(n, typ), strings.Join(metadataNames(ras.Updated), ", "))
}
n = len(ras.Deleted)
if n > 0 {
changed = true
fmt.Printf("%v %v deleted: %v\n", n, pluralize(n, typ), strings.Join(metadataNames(ras.Deleted), ", "))
}
}
if !changed {
fmt.Println("Everything up to date.")
}
}
// metadataNames extracts a slice of names from a slice of object metadata.
func metadataNames(ms []*metav1.ObjectMeta) []string {
s := make([]string, len(ms))
for i, m := range ms {
s[i] = m.Name
}
return s
}
// pluralize returns the plural of word if num is zero or more than one.
func pluralize(num int, word string) string {
if num == 1 {
return word
}
return word + "s"
}
// applyArchives figures out the set of archives that need to be uploaded, and uploads them.
func applyArchives(input cli.Input, fclient cmd.Client, specDir string, fr *FissionResources) error {
// archive:// URL -> archive map.
archiveFiles := make(map[string]fv1.Archive)
// We'll first populate archiveFiles with references to local files, and then modify it to
// point at archive URLs.
// create archives locally and calculate checksums
for _, aus := range fr.ArchiveUploadSpecs {
ar, err := localArchiveFromSpec(specDir, &aus)
if err != nil {
return err
}
archiveUrl := fmt.Sprintf("%v%v", ARCHIVE_URL_PREFIX, aus.Name)
archiveFiles[archiveUrl] = *ar
}
// get list of packages, make content-indexed map of available archives
availableArchives := make(map[string]string) // (sha256 -> url)
pkgs, err := fclient.FissionClientSet.CoreV1().Packages(metav1.NamespaceAll).List(input.Context(), metav1.ListOptions{})
if err != nil {
return err
}
for _, pkg := range pkgs.Items {
for _, ar := range []fv1.Archive{pkg.Spec.Source, pkg.Spec.Deployment} {
if ar.Type == fv1.ArchiveTypeUrl && len(ar.URL) > 0 {
availableArchives[ar.Checksum.Sum] = ar.URL
}
}
}
// upload archives that we need to, updating the map
for name, ar := range archiveFiles {
if ar.Type == fv1.ArchiveTypeLiteral {
continue
}
// does the archive exist already?
if url, ok := availableArchives[ar.Checksum.Sum]; ok {
fmt.Printf("archive %v exists, not uploading\n", name)
ar.URL = url
archiveFiles[name] = ar
} else {
// doesn't exist, upload
fmt.Printf("uploading archive %v\n", name)
// ar.URL is actually a local filename at this stage
uploadedAr, err := pkgutil.UploadArchiveFile(input.Context(), fclient, ar.URL)
if err != nil {
return err
}
archiveFiles[name] = *uploadedAr
}
}
// resolve references to urls in packages to be applied
for i := range fr.Packages {
for _, ar := range []*fv1.Archive{&fr.Packages[i].Spec.Source, &fr.Packages[i].Spec.Deployment} {
if strings.HasPrefix(ar.URL, ARCHIVE_URL_PREFIX) {
availableAr, ok := archiveFiles[ar.URL]
if !ok {
return errors.Errorf("unknown archive name %v", strings.TrimPrefix(ar.URL, ARCHIVE_URL_PREFIX))
}
ar.Type = availableAr.Type
ar.Literal = availableAr.Literal
ar.URL = availableAr.URL
ar.Checksum = availableAr.Checksum
}
}
}
return nil
}
// applyResources applies the given set of fission resources.
func applyResources(input cli.Input, fclient cmd.Client, specDir string, fr *FissionResources, delete bool, specAllowConflicts bool) (map[string]metav1.ObjectMeta, map[string]ResourceApplyStatus, error) {
applyStatus := make(map[string]ResourceApplyStatus)
// upload archives that need to be uploaded. Changes archive references in fr.Packages.
err := applyArchives(input, fclient, specDir, fr)
if err != nil {
return nil, nil, err
}
_, ras, err := applyEnvironments(input.Context(), fclient, fr, delete, specAllowConflicts)
if err != nil {
return nil, nil, errors.Wrap(err, "environment apply failed")
}
applyStatus["environment"] = *ras
pkgMeta, ras, err := applyPackages(input.Context(), fclient, fr, delete, specAllowConflicts)
if err != nil {
return nil, nil, errors.Wrap(err, "package apply failed")
}
applyStatus["package"] = *ras
// Each reference to a package from a function must contain the resource version
// of the package. This ensures that various caches can invalidate themselves
// when the package changes.
for i, f := range fr.Functions {
if f.Spec.InvokeStrategy.ExecutionStrategy.ExecutorType == fv1.ExecutorTypeContainer {
continue
}
k := mapKey(&metav1.ObjectMeta{
Namespace: f.Spec.Package.PackageRef.Namespace,
Name: f.Spec.Package.PackageRef.Name,
})
m, ok := pkgMeta[k]
if !ok {
// the function references a package that doesn't exist in the
// spec. It may exist outside the spec, but we're going to treat
// that as an error, so that we encourage self-contained specs.
// Is there a good use case for non-self contained specs?
return nil, nil, errors.Errorf("function %v/%v references package %v/%v, which doesn't exist in the specs",
f.ObjectMeta.Namespace, f.ObjectMeta.Name, f.Spec.Package.PackageRef.Namespace, f.Spec.Package.PackageRef.Name)
}
fr.Functions[i].Spec.Package.PackageRef.ResourceVersion = m.ResourceVersion
}
_, ras, err = applyFunctions(input.Context(), fclient, fr, delete, specAllowConflicts)
if err != nil {
return nil, nil, errors.Wrap(err, "function apply failed")
}
applyStatus["function"] = *ras
_, ras, err = applyHTTPTriggers(input.Context(), fclient, fr, delete, specAllowConflicts)
if err != nil {
return nil, nil, errors.Wrap(err, "HTTPTrigger apply failed")
}
applyStatus["HTTPTrigger"] = *ras
_, ras, err = applyKubernetesWatchTriggers(input.Context(), fclient, fr, delete, specAllowConflicts)
if err != nil {
return nil, nil, errors.Wrap(err, "KubernetesWatchTrigger apply failed")
}
applyStatus["KubernetesWatchTrigger"] = *ras
_, ras, err = applyTimeTriggers(input.Context(), fclient, fr, delete, specAllowConflicts)
if err != nil {
return nil, nil, errors.Wrap(err, "TimeTrigger apply failed")
}
applyStatus["TimeTrigger"] = *ras
_, ras, err = applyMessageQueueTriggers(input.Context(), fclient, fr, delete, specAllowConflicts)
if err != nil {
return nil, nil, errors.Wrap(err, "MessageQueueTrigger apply failed")
}
applyStatus["MessageQueueTrigger"] = *ras
return pkgMeta, applyStatus, nil
}
// localArchiveFromSpec creates an archive on the local filesystem from the given spec,
// and returns its path and checksum.
func localArchiveFromSpec(specDir string, aus *spectypes.ArchiveUploadSpec) (*fv1.Archive, error) {
// get root dir
var rootDir string
if len(aus.RootDir) == 0 {
rootDir = filepath.Clean(specDir + "/..")
} else {
rootDir = aus.RootDir
}
// get a list of files from the include/exclude globs.
//
// XXX if there are lots of globs it's probably more efficient
// to do a filepath.Walk and call path.Match on each path...
files := make([]string, 0)
// checking if file is a zip
if match, _ := utils.IsZip(aus.IncludeGlobs[0]); match && len(aus.IncludeGlobs) == 1 {
files = append(files, aus.IncludeGlobs[0])
} else {
for _, relativeGlob := range aus.IncludeGlobs {
absGlob := filepath.Join(rootDir, relativeGlob)
console.Verbose(2, "try to find globs in path '%v'", absGlob)
fs, err := utils.FindAllGlobs(absGlob)
if err != nil {
return nil, errors.Wrapf(err, "Invalid glob in archive %v: %v", aus.Name, relativeGlob)
}
files = append(files, fs...)
}
}
if len(files) == 0 {
return nil, errors.Errorf("archive '%v' is empty", aus.Name)
}
// if it's just one file, use its path directly
var archiveFileName string
var isSingleFile bool
if len(files) == 1 {
// check whether a path destination is file or directory
f, err := os.Stat(files[0])
if err != nil {
return nil, err
}
if !f.IsDir() {
isSingleFile = true
archiveFileName = files[0]
}
}
if len(files) > 1 || !isSingleFile {
// Generate archive name with .zip extension and pack all files under it.
archiveFile, err := os.CreateTemp("", fmt.Sprintf("fission-archive-%v-*.zip", aus.Name))
if err != nil {
return nil, err
}
archiveFileName = archiveFile.Name()
// This instance is required to allow overwriting and not changing DefaultZip
zipOverwrite := archiver.Zip{OverwriteExisting: true}
err = zipOverwrite.Archive(files, archiveFileName)
if err != nil {
return nil, err
}
}
size, err := utils.FileSize(archiveFileName)
if err != nil {
return nil, err
}
// figure out if we're making a literal or a URL-based archive
if size < fv1.ArchiveLiteralSizeLimit {
contents, err := pkgutil.GetContents(archiveFileName)
if err != nil {
return nil, err
}
return &fv1.Archive{
Type: fv1.ArchiveTypeLiteral,
Literal: contents,
}, nil
} else {
// checksum
csum, err := utils.GetFileChecksum(archiveFileName)
if err != nil {
return nil, errors.Errorf("failed to calculate archive checksum for %v (%v): %v", aus.Name, archiveFileName, err)
}
// archive object
return &fv1.Archive{
Type: fv1.ArchiveTypeUrl,
// we should be actually be adding a "file://" prefix, but this archive is only an
// intermediate step, so just the path works fine.
URL: archiveFileName,
Checksum: *csum,
}, nil
}
}
func mapKey(m *metav1.ObjectMeta) string {
return fmt.Sprintf("%v:%v", m.Namespace, m.Name)
}
func applyDeploymentConfig(m *metav1.ObjectMeta, fr *FissionResources) {
if m.Annotations == nil {
m.Annotations = make(map[string]string)
}
m.Annotations[FISSION_DEPLOYMENT_NAME_KEY] = fr.DeploymentConfig.Name
m.Annotations[FISSION_DEPLOYMENT_UID_KEY] = fr.DeploymentConfig.UID
}
func hasDeploymentConfig(m *metav1.ObjectMeta, fr *FissionResources) bool {
if m.Annotations == nil {
return false
}
uid, ok := m.Annotations[FISSION_DEPLOYMENT_UID_KEY]
if ok && uid == fr.DeploymentConfig.UID {
return true
}
return false
}
func waitForPackageBuild(ctx context.Context, fclient cmd.Client, pkg *fv1.Package) (*fv1.Package, error) {
start := time.Now()
for {
if pkg.Status.BuildStatus != fv1.BuildStatusRunning {
return pkg, nil
}
if time.Since(start) > 5*time.Minute {
return nil, errors.Errorf("package %v has been building for a while, giving up on waiting for it", pkg.ObjectMeta.Name)
}
// TODO watch instead
time.Sleep(time.Second)
var err error
pkg, err = fclient.FissionClientSet.CoreV1().Packages(pkg.ObjectMeta.Namespace).Get(ctx, pkg.ObjectMeta.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
}
}
func applyPackages(ctx context.Context, fclient cmd.Client, fr *FissionResources, delete bool, specAllowConflicts bool) (map[string]metav1.ObjectMeta, *ResourceApplyStatus, error) {
// get list
allObjs, err := fclient.FissionClientSet.CoreV1().Packages(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, nil, err
}
// filter
objs := make([]fv1.Package, 0)
if specAllowConflicts {
objs = allObjs.Items
} else {
for _, o := range allObjs.Items {
if hasDeploymentConfig(&o.ObjectMeta, fr) {
objs = append(objs, o)
}
}
}
// index
existent := make(map[string]fv1.Package)
for _, obj := range objs {
existent[mapKey(&obj.ObjectMeta)] = obj
}
metadataMap := make(map[string]metav1.ObjectMeta)
// desired set. used to compute the set to delete.
desired := make(map[string]bool)
var ras ResourceApplyStatus
// create or update desired state
for _, o := range fr.Packages {
// apply deploymentConfig so we can find our objects on future apply invocations
applyDeploymentConfig(&o.ObjectMeta, fr)
console.Verbose(2, fmt.Sprintf("Package is here '%s','%s','%s','%s'", o.Namespace, o.Name, o.Spec.Environment.Namespace, o.Spec.Environment.Name))
// index desired state
desired[mapKey(&o.ObjectMeta)] = true
// exists?
existingObj, ok := existent[mapKey(&o.ObjectMeta)]
if ok {
// ok, a resource with the same name exists, is it the same?
keep := false
if reflect.DeepEqual(existingObj.Spec, o.Spec) {
keep = true
} else if reflect.DeepEqual(existingObj.Spec.Environment, o.Spec.Environment) &&
!reflect.DeepEqual(existingObj.Spec.Source, fv1.Archive{}) &&
reflect.DeepEqual(existingObj.Spec.Source, o.Spec.Source) &&
existingObj.Spec.BuildCommand == o.Spec.BuildCommand {
keep = true
}
if keep && isObjectMetaEqual(existingObj.ObjectMeta, o.ObjectMeta) && existingObj.Status.BuildStatus == fv1.BuildStatusSucceeded {
// nothing to do on the server
metadataMap[mapKey(&o.ObjectMeta)] = existingObj.ObjectMeta
} else {
// update
o.ObjectMeta.ResourceVersion = existingObj.ObjectMeta.ResourceVersion
// We may be racing against the package builder to update the
// package (a previous version might have been getting built). So,
// wait for the package to have a non-running build status.
pkg, err := waitForPackageBuild(ctx, fclient, &o)
if err != nil {
// log and ignore
console.Warn(fmt.Sprintf("Error waiting for package '%v' build, ignoring", o.ObjectMeta.Name))
pkg = &o
}
// update status in order to rebuild the package again
if pkg.Status.BuildStatus == fv1.BuildStatusFailed {
pkg.Status.BuildStatus = fv1.BuildStatusPending
}
newmeta, err := fclient.FissionClientSet.CoreV1().Packages(pkg.ObjectMeta.Namespace).Update(ctx, pkg, metav1.UpdateOptions{})
if err != nil {
return nil, nil, err
// TODO check for resourceVersion conflict errors and retry
}
ras.Updated = append(ras.Updated, &newmeta.ObjectMeta)
// keep track of metadata in case we need to create a reference to it
metadataMap[mapKey(&o.ObjectMeta)] = newmeta.ObjectMeta
}
} else {
// create
newmeta, err := fclient.FissionClientSet.CoreV1().Packages(o.ObjectMeta.Namespace).Create(ctx, &o, metav1.CreateOptions{})
if err != nil {
return nil, nil, err
}
ras.Created = append(ras.Created, &newmeta.ObjectMeta)
metadataMap[mapKey(&o.ObjectMeta)] = newmeta.ObjectMeta
}
}
// deletes
if delete {
// objs is already filtered with our UID
for _, o := range objs {
_, wanted := desired[mapKey(&o.ObjectMeta)]
if !wanted {
err := fclient.FissionClientSet.CoreV1().Packages(o.ObjectMeta.Namespace).Delete(ctx, o.ObjectMeta.Name, metav1.DeleteOptions{})
if err != nil {
return nil, nil, err
}
ras.Deleted = append(ras.Deleted, &o.ObjectMeta)
fmt.Printf("Deleted %v %v/%v\n", o.TypeMeta.Kind, o.ObjectMeta.Namespace, o.ObjectMeta.Name)
}
}
}
return metadataMap, &ras, nil
}
func applyFunctions(ctx context.Context, fclient cmd.Client, fr *FissionResources, delete bool, specAllowConflicts bool) (map[string]metav1.ObjectMeta, *ResourceApplyStatus, error) {
// get list
allObjs, err := fclient.FissionClientSet.CoreV1().Functions(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, nil, err
}
// filter
objs := make([]fv1.Function, 0)
if specAllowConflicts {
objs = allObjs.Items
} else {
for _, o := range allObjs.Items {
if hasDeploymentConfig(&o.ObjectMeta, fr) {
objs = append(objs, o)
}
}
}
// index
existent := make(map[string]fv1.Function)
for _, obj := range objs {
existent[mapKey(&obj.ObjectMeta)] = obj
}
metadataMap := make(map[string]metav1.ObjectMeta)
// desired set. used to compute the set to delete.
desired := make(map[string]bool)
var ras ResourceApplyStatus
// create or update desired state
for _, o := range fr.Functions {
// apply deploymentConfig so we can find our objects on future apply invocations
applyDeploymentConfig(&o.ObjectMeta, fr)
// index desired state
desired[mapKey(&o.ObjectMeta)] = true
// exists?
existingObj, ok := existent[mapKey(&o.ObjectMeta)]
if ok {
// ok, a resource with the same name exists, is it the same?
if isObjectMetaEqual(existingObj.ObjectMeta, o.ObjectMeta) && reflect.DeepEqual(existingObj.Spec, o.Spec) {
// nothing to do on the server
metadataMap[mapKey(&o.ObjectMeta)] = existingObj.ObjectMeta
} else {
// update
o.ObjectMeta.ResourceVersion = existingObj.ObjectMeta.ResourceVersion
newmeta, err := fclient.FissionClientSet.CoreV1().Functions(o.ObjectMeta.Namespace).Update(ctx, &o, metav1.UpdateOptions{})
if err != nil {
return nil, nil, err
}
ras.Updated = append(ras.Updated, &newmeta.ObjectMeta)
// keep track of metadata in case we need to create a reference to it
metadataMap[mapKey(&o.ObjectMeta)] = newmeta.ObjectMeta
}
} else {
// create
newmeta, err := fclient.FissionClientSet.CoreV1().Functions(o.ObjectMeta.Namespace).Create(ctx, &o, metav1.CreateOptions{})
if err != nil {
return nil, nil, err
}
ras.Created = append(ras.Created, &newmeta.ObjectMeta)
metadataMap[mapKey(&o.ObjectMeta)] = newmeta.ObjectMeta
}
}
// deletes
if delete {
// objs is already filtered with our UID
for _, o := range objs {
_, wanted := desired[mapKey(&o.ObjectMeta)]
if !wanted {
err := fclient.FissionClientSet.CoreV1().Functions(o.ObjectMeta.Namespace).Delete(ctx, o.ObjectMeta.Name, metav1.DeleteOptions{})
if err != nil {
return nil, nil, err
}
ras.Deleted = append(ras.Deleted, &o.ObjectMeta)
fmt.Printf("Deleted %v %v/%v\n", o.TypeMeta.Kind, o.ObjectMeta.Namespace, o.ObjectMeta.Name)
}
}
}
return metadataMap, &ras, nil
}
func applyEnvironments(ctx context.Context, fclient cmd.Client, fr *FissionResources, delete bool, specAllowConflicts bool) (map[string]metav1.ObjectMeta, *ResourceApplyStatus, error) {
// get list
allObjs, err := fclient.FissionClientSet.CoreV1().Environments(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, nil, err
}
// filter
objs := make([]fv1.Environment, 0)
if specAllowConflicts {
objs = allObjs.Items
} else {
for _, o := range allObjs.Items {
if hasDeploymentConfig(&o.ObjectMeta, fr) {
objs = append(objs, o)
}
}
}
// index
existent := make(map[string]fv1.Environment)
for _, obj := range objs {
existent[mapKey(&obj.ObjectMeta)] = obj
}
metadataMap := make(map[string]metav1.ObjectMeta)
// desired set. used to compute the set to delete.
desired := make(map[string]bool)
var ras ResourceApplyStatus
// create or update desired state
for _, o := range fr.Environments {
// apply deploymentConfig so we can find our objects on future apply invocations
applyDeploymentConfig(&o.ObjectMeta, fr)
// index desired state
desired[mapKey(&o.ObjectMeta)] = true
// exists?
existingObj, ok := existent[mapKey(&o.ObjectMeta)]
if ok {
// ok, a resource with the same name exists, is it the same?
if isObjectMetaEqual(existingObj.ObjectMeta, o.ObjectMeta) && reflect.DeepEqual(existingObj.Spec, o.Spec) {
// nothing to do on the server
metadataMap[mapKey(&o.ObjectMeta)] = existingObj.ObjectMeta
} else {
// update
o.ObjectMeta.ResourceVersion = existingObj.ObjectMeta.ResourceVersion
newmeta, err := fclient.FissionClientSet.CoreV1().Environments(o.ObjectMeta.Namespace).Update(ctx, &o, metav1.UpdateOptions{})
if err != nil {
return nil, nil, err
}
ras.Updated = append(ras.Updated, &newmeta.ObjectMeta)
// keep track of metadata in case we need to create a reference to it
metadataMap[mapKey(&o.ObjectMeta)] = newmeta.ObjectMeta
}
} else {
// create
newmeta, err := fclient.FissionClientSet.CoreV1().Environments(o.ObjectMeta.Namespace).Create(ctx, &o, metav1.CreateOptions{})
if err != nil {
return nil, nil, err
}
ras.Created = append(ras.Created, &newmeta.ObjectMeta)
metadataMap[mapKey(&o.ObjectMeta)] = newmeta.ObjectMeta
}
}
// deletes
if delete {
// objs is already filtered with our UID
for _, o := range objs {
_, wanted := desired[mapKey(&o.ObjectMeta)]
if !wanted {
err := fclient.FissionClientSet.CoreV1().Environments(o.ObjectMeta.Namespace).Delete(ctx, o.ObjectMeta.Namespace, metav1.DeleteOptions{})
if err != nil {
return nil, nil, err
}
ras.Deleted = append(ras.Deleted, &o.ObjectMeta)
fmt.Printf("Deleted %v %v/%v\n", o.TypeMeta.Kind, o.ObjectMeta.Namespace, o.ObjectMeta.Name)
}
}
}
return metadataMap, &ras, nil
}
func applyHTTPTriggers(ctx context.Context, fclient cmd.Client, fr *FissionResources, delete bool, specAllowConflicts bool) (map[string]metav1.ObjectMeta, *ResourceApplyStatus, error) {
// get list
allObjs, err := fclient.FissionClientSet.CoreV1().HTTPTriggers(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, nil, err
}
// filter
objs := make([]fv1.HTTPTrigger, 0)
if specAllowConflicts {
objs = allObjs.Items
} else {
for _, o := range allObjs.Items {
if hasDeploymentConfig(&o.ObjectMeta, fr) {
objs = append(objs, o)
}
}
}
// index
existent := make(map[string]fv1.HTTPTrigger)
for _, obj := range objs {
existent[mapKey(&obj.ObjectMeta)] = obj
}
metadataMap := make(map[string]metav1.ObjectMeta)
// desired set. used to compute the set to delete.
desired := make(map[string]bool)
var ras ResourceApplyStatus
// create or update desired state
for _, o := range fr.HttpTriggers {
// apply deploymentConfig so we can find our objects on future apply invocations
applyDeploymentConfig(&o.ObjectMeta, fr)
// index desired state
desired[mapKey(&o.ObjectMeta)] = true
// exists?
existingObj, ok := existent[mapKey(&o.ObjectMeta)]
if ok {
// ok, a resource with the same name exists, is it the same?
if isObjectMetaEqual(existingObj.ObjectMeta, o.ObjectMeta) && reflect.DeepEqual(existingObj.Spec, o.Spec) {
// nothing to do on the server
metadataMap[mapKey(&o.ObjectMeta)] = existingObj.ObjectMeta
} else {
err := util.CheckHTTPTriggerDuplicates(ctx, fclient, &o)
if err != nil {
return nil, nil, err
}
// update
o.ObjectMeta.ResourceVersion = existingObj.ObjectMeta.ResourceVersion
newmeta, err := fclient.FissionClientSet.CoreV1().HTTPTriggers(o.ObjectMeta.Namespace).Update(ctx, &o, metav1.UpdateOptions{})
if err != nil {
return nil, nil, err
}
ras.Updated = append(ras.Updated, &newmeta.ObjectMeta)
// keep track of metadata in case we need to create a reference to it
metadataMap[mapKey(&o.ObjectMeta)] = newmeta.ObjectMeta
}