/
configuration_strat_merge_patch.go
127 lines (105 loc) · 3.05 KB
/
configuration_strat_merge_patch.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package controllers
import (
"bytes"
"compress/gzip"
"fmt"
"os"
"path/filepath"
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/fluxcd/pkg/http/fetch"
generator "github.com/fluxcd/pkg/kustomize"
"github.com/fluxcd/pkg/tar"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
"gopkg.in/yaml.v2"
"sigs.k8s.io/controller-runtime/pkg/client"
kustypes "sigs.k8s.io/kustomize/api/types"
"github.com/open-component-model/ocm-controller/api/v1alpha1"
ocmmetav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
)
// the following is influenced by https://github.com/fluxcd/kustomize-controller
func (m *MutationReconcileLooper) strategicMergePatch(
source sourcev1.Source,
resource []byte,
tmpDir, sourcePath, targetPath string,
) (string, ocmmetav1.Identity, error) {
workDir, err := securejoin.SecureJoin(tmpDir, "work")
if err != nil {
return "", nil, err
}
gzipSnapshot := &bytes.Buffer{}
gz := gzip.NewWriter(gzipSnapshot)
if _, err := gz.Write(resource); err != nil {
return "", nil, err
}
if err := gz.Close(); err != nil {
return "", nil, err
}
if err := tar.Untar(gzipSnapshot, workDir); err != nil {
return "", nil, err
}
tarSize := tar.UnlimitedUntarSize
const retries = 10
fetcher := fetch.NewArchiveFetcher(retries, tarSize, tarSize, "")
err = fetcher.Fetch(source.GetArtifact().URL, source.GetArtifact().Digest, workDir)
if err != nil {
return "", nil, err
}
kus := kustypes.Kustomization{
TypeMeta: kustypes.TypeMeta{
APIVersion: kustypes.KustomizationVersion,
Kind: kustypes.KustomizationKind,
},
Resources: []string{
sourcePath,
},
PatchesStrategicMerge: []kustypes.PatchStrategicMerge{
kustypes.PatchStrategicMerge(targetPath),
},
}
manifest, err := yaml.Marshal(kus)
if err != nil {
return "", nil, err
}
err = os.WriteFile(filepath.Join(workDir, "kustomization.yaml"), manifest, os.ModePerm)
if err != nil {
return "", nil, err
}
result, err := generator.SecureBuild(tmpDir, workDir, false)
if err != nil {
return "", nil, err
}
contents, err := result.AsYaml()
if err != nil {
return "", nil, err
}
outputPath, err := securejoin.SecureJoin(workDir, targetPath)
if err != nil {
return "", nil, err
}
if err := os.Remove(outputPath); err != nil {
return "", nil, err
}
patched, err := os.Create(outputPath)
if err != nil {
return "", nil, err
}
if _, err := patched.Write(contents); err != nil {
return "", nil, err
}
if err := patched.Close(); err != nil {
return "", nil, err
}
clientObject, ok := source.(client.Object)
if !ok {
return "", nil, fmt.Errorf("source object was not a client object")
}
identity := ocmmetav1.Identity{
v1alpha1.SourceNameKey: clientObject.GetName(),
v1alpha1.SourceNamespaceKey: clientObject.GetNamespace(),
v1alpha1.SourceArtifactChecksumKey: source.GetArtifact().Digest,
}
return filepath.Dir(outputPath), identity, nil
}