forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
138 lines (126 loc) · 4.59 KB
/
manifest.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
// Copyright 2018 The Operator-SDK 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 yamlutil
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
log "github.com/sirupsen/logrus"
)
var yamlSep = []byte("\n\n---\n\n")
// CombineManifests combines given manifests with a base manifest and adds yaml
// style separation. Nothing is appended if the manifest is empty or base
// already contains a trailing separator.
func CombineManifests(base []byte, manifests ...[]byte) []byte {
// Base already has manifests we're appending to.
base = bytes.Trim(base, " \n")
if len(base) > 0 && len(manifests) > 0 {
if i := bytes.LastIndex(base, bytes.Trim(yamlSep, "\n")); i != len(base)-3 {
base = append(base, yamlSep...)
}
}
for j, manifest := range manifests {
if len(manifest) > 0 {
base = append(base, bytes.Trim(manifest, " \n")...)
// Don't append sep if manifest is the last element in manifests.
if j < len(manifests)-1 {
base = append(base, yamlSep...)
}
}
}
return append(base, '\n')
}
// GenerateCombinedNamespacedManifest creates a temporary manifest yaml
// by combining all standard namespaced resource manifests in deployDir.
func GenerateCombinedNamespacedManifest(deployDir string) (*os.File, error) {
file, err := ioutil.TempFile("", "namespaced-manifest.yaml")
if err != nil {
return nil, err
}
defer func() {
if err := file.Close(); err != nil && !fileutil.IsClosedError(err) {
log.Errorf("Failed to close file %s: (%v)", file.Name(), err)
}
}()
sa, err := ioutil.ReadFile(filepath.Join(deployDir, scaffold.ServiceAccountYamlFile))
if err != nil {
log.Warnf("Could not find the serviceaccount manifest: (%v)", err)
}
role, err := ioutil.ReadFile(filepath.Join(deployDir, scaffold.RoleYamlFile))
if err != nil {
log.Warnf("Could not find role manifest: (%v)", err)
}
roleBinding, err := ioutil.ReadFile(filepath.Join(deployDir, scaffold.RoleBindingYamlFile))
if err != nil {
log.Warnf("Could not find role_binding manifest: (%v)", err)
}
operator, err := ioutil.ReadFile(filepath.Join(deployDir, scaffold.OperatorYamlFile))
if err != nil {
return nil, fmt.Errorf("could not find operator manifest: (%v)", err)
}
combined := []byte{}
combined = CombineManifests(combined, sa, role, roleBinding, operator)
if err := file.Chmod(os.FileMode(fileutil.DefaultFileMode)); err != nil {
return nil, fmt.Errorf("could not chown temporary namespaced manifest file: (%v)", err)
}
if _, err := file.Write(combined); err != nil {
return nil, fmt.Errorf("could not create temporary namespaced manifest file: (%v)", err)
}
if err := file.Close(); err != nil {
return nil, err
}
return file, nil
}
// GenerateCombinedGlobalManifest creates a temporary manifest yaml
// by combining all standard global resource manifests in crdsDir.
func GenerateCombinedGlobalManifest(crdsDir string) (*os.File, error) {
file, err := ioutil.TempFile("", "global-manifest.yaml")
if err != nil {
return nil, err
}
defer func() {
if err := file.Close(); err != nil && !fileutil.IsClosedError(err) {
log.Errorf("Failed to close file %s: (%v)", file.Name(), err)
}
}()
files, err := ioutil.ReadDir(crdsDir)
if err != nil {
return nil, fmt.Errorf("could not read deploy directory: (%v)", err)
}
combined := []byte{}
for _, file := range files {
if strings.HasSuffix(file.Name(), "crd.yaml") {
fileBytes, err := ioutil.ReadFile(filepath.Join(crdsDir, file.Name()))
if err != nil {
return nil, fmt.Errorf("could not read file %s: (%v)", filepath.Join(crdsDir, file.Name()), err)
}
combined = CombineManifests(combined, fileBytes)
}
}
if err := file.Chmod(os.FileMode(fileutil.DefaultFileMode)); err != nil {
return nil, fmt.Errorf("could not chown temporary global manifest file: (%v)", err)
}
if _, err := file.Write(combined); err != nil {
return nil, fmt.Errorf("could not create temporary global manifest file: (%v)", err)
}
if err := file.Close(); err != nil {
return nil, err
}
return file, nil
}