This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
apply.go
89 lines (74 loc) · 2.67 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
package bundle
import (
"path/filepath"
"github.com/caos/orbos/internal/operator/boom/application"
"github.com/caos/orbos/internal/operator/boom/desired"
"github.com/caos/orbos/internal/utils/clientgo"
"github.com/caos/orbos/mntr"
)
func applyWithCurrentState(monitor mntr.Monitor, currentResourceList []*clientgo.Resource, app application.Application, force bool) func(resultFilePath, namespace string) error {
logFields := map[string]interface{}{
"command": "apply",
}
applyMonitor := monitor.WithFields(logFields)
resultFunc := func(resultFilePath, namespace string) error {
applyFunc := apply(monitor, app, force)
desiredResources, err := desired.Get(monitor, resultFilePath, namespace, app.GetName())
if err != nil {
return err
}
if err := applyFunc(resultFilePath, namespace); err != nil {
return err
}
deleteResources := make([]*clientgo.Resource, 0)
for _, currentResource := range currentResourceList {
found := false
for _, desiredResource := range desiredResources {
apiVersion := filepath.Join(currentResource.Group, currentResource.Version)
if desiredResource.ApiVersion == apiVersion &&
desiredResource.Kind == currentResource.Kind &&
desiredResource.Metadata.Name == currentResource.Name &&
(currentResource.Namespace == "" || desiredResource.Metadata.Namespace == currentResource.Namespace) {
found = true
break
}
}
if found == false {
otherAPI := false
for _, desiredResource := range desiredResources {
apiVersion := filepath.Join(currentResource.Group, currentResource.Version)
if apiVersion != desiredResource.ApiVersion &&
currentResource.Kind == desiredResource.Kind &&
currentResource.Name == desiredResource.Metadata.Name &&
(currentResource.Namespace == "" || desiredResource.Metadata.Namespace == currentResource.Namespace) {
otherAPI = true
break
}
}
if !otherAPI {
deleteResources = append(deleteResources, currentResource)
}
}
}
applyMonitor.Debug("Resources to delete calculated")
if deleteResources != nil && len(deleteResources) > 0 {
for _, deleteResource := range deleteResources {
if err := clientgo.DeleteResource(deleteResource); err != nil {
return err
}
}
}
return nil
}
return resultFunc
}
func apply(monitor mntr.Monitor, app application.Application, force bool) func(resultFilePath, namespace string) error {
logFields := map[string]interface{}{
"command": "apply",
}
applyMonitor := monitor.WithFields(logFields)
resultFunc := func(resultFilePath, namespace string) error {
return desired.Apply(applyMonitor, resultFilePath, namespace, app.GetName(), force)
}
return resultFunc
}