This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
rawfiles.go
97 lines (88 loc) · 2.79 KB
/
rawfiles.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
package manifests
import (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"github.com/fluxcd/flux/pkg/image"
"github.com/fluxcd/flux/pkg/resource"
)
type rawFiles struct {
baseDir string
paths []string
manifests Manifests
}
// NewRawFiles constructs a `Store` that assumes the provided
// directories contain plain YAML files
func NewRawFiles(baseDir string, paths []string, manifests Manifests) *rawFiles {
return &rawFiles{
baseDir: baseDir,
paths: paths,
manifests: manifests,
}
}
// Set the container image of a resource in the store
func (f *rawFiles) SetWorkloadContainerImage(ctx context.Context, id resource.ID, container string, newImageID image.Ref) error {
resourcesByID, err := f.GetAllResourcesByID(ctx)
if err != nil {
return err
}
r, ok := resourcesByID[id.String()]
if !ok {
return ErrResourceNotFound(id.String())
}
return f.setManifestWorkloadContainerImage(r, container, newImageID)
}
func (f *rawFiles) setManifestWorkloadContainerImage(r resource.Resource, container string, newImageID image.Ref) error {
fullFilePath := filepath.Join(f.baseDir, r.Source())
def, err := ioutil.ReadFile(fullFilePath)
if err != nil {
return err
}
newDef, err := f.manifests.SetWorkloadContainerImage(def, r.ResourceID(), container, newImageID)
if err != nil {
return err
}
fi, err := os.Stat(fullFilePath)
if err != nil {
return err
}
return ioutil.WriteFile(fullFilePath, newDef, fi.Mode())
}
// UpdateWorkloadPolicies modifies a resource in the store to apply the policy-update specified.
// It returns whether a change in the resource was actually made as a result of the change
func (f *rawFiles) UpdateWorkloadPolicies(ctx context.Context, id resource.ID, update resource.PolicyUpdate) (bool, error) {
resourcesByID, err := f.GetAllResourcesByID(ctx)
if err != nil {
return false, err
}
r, ok := resourcesByID[id.String()]
if !ok {
return false, ErrResourceNotFound(id.String())
}
return f.updateManifestWorkloadPolicies(r, update)
}
func (f *rawFiles) updateManifestWorkloadPolicies(r resource.Resource, update resource.PolicyUpdate) (bool, error) {
fullFilePath := filepath.Join(f.baseDir, r.Source())
def, err := ioutil.ReadFile(fullFilePath)
if err != nil {
return false, err
}
newDef, err := f.manifests.UpdateWorkloadPolicies(def, r.ResourceID(), update)
if err != nil {
return false, err
}
fi, err := os.Stat(fullFilePath)
if err != nil {
return false, err
}
if err := ioutil.WriteFile(fullFilePath, newDef, fi.Mode()); err != nil {
return false, err
}
return bytes.Compare(def, newDef) != 0, nil
}
// Load all the resources in the store. The returned map is indexed by the resource IDs
func (f *rawFiles) GetAllResourcesByID(_ context.Context) (map[string]resource.Resource, error) {
return f.manifests.LoadManifests(f.baseDir, f.paths)
}