Skip to content

Commit 7d5ccec

Browse files
authored
internal/.../manifest.go: manifest generators take directory arguments (#1059)
commands/operator-sdk/cmd/*,test/e2e/memcached_test.go: pass args to manifest generators, use yaml scanner
1 parent c2f43a4 commit 7d5ccec

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

commands/operator-sdk/cmd/scorecard/resource_handler.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"time"
2525

26+
"github.com/operator-framework/operator-sdk/internal/util/yamlutil"
2627
proxyConf "github.com/operator-framework/operator-sdk/pkg/ansible/proxy/kubeconfig"
2728
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
2829
"github.com/spf13/viper"
@@ -64,18 +65,14 @@ func yamlToUnstructured(yamlPath string) (*unstructured.Unstructured, error) {
6465
// createFromYAMLFile will take a path to a YAML file and create the resource. If it finds a
6566
// deployment, it will add the scorecard proxy as a container in the deployments podspec.
6667
func createFromYAMLFile(yamlPath string) error {
67-
yamlFile, err := ioutil.ReadFile(yamlPath)
68+
yamlSpecs, err := ioutil.ReadFile(yamlPath)
6869
if err != nil {
6970
return fmt.Errorf("failed to read file %s: %v", yamlPath, err)
7071
}
71-
yamlSplit := bytes.Split(yamlFile, []byte("\n---\n"))
72-
for _, yamlSpec := range yamlSplit {
73-
// some autogenerated files may include an extra `---` at the end of the file
74-
if string(yamlSpec) == "" {
75-
continue
76-
}
72+
scanner := yamlutil.NewYAMLScanner(yamlSpecs)
73+
for scanner.Scan() {
7774
obj := &unstructured.Unstructured{}
78-
jsonSpec, err := yaml.YAMLToJSON(yamlSpec)
75+
jsonSpec, err := yaml.YAMLToJSON(scanner.Bytes())
7976
if err != nil {
8077
return fmt.Errorf("could not convert yaml file to json: %v", err)
8178
}
@@ -130,6 +127,10 @@ func createFromYAMLFile(yamlPath string) error {
130127
}
131128
addResourceCleanup(obj, types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()})
132129
}
130+
if err := scanner.Err(); err != nil {
131+
return fmt.Errorf("failed to scan %s: (%v)", yamlPath, err)
132+
}
133+
133134
return nil
134135
}
135136

commands/operator-sdk/cmd/scorecard/scorecard.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222

2323
"github.com/operator-framework/operator-sdk/internal/util/projutil"
24+
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2425

2526
k8sInternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
2627
"github.com/operator-framework/operator-sdk/internal/util/yamlutil"
@@ -115,7 +116,7 @@ func ScorecardTests(cmd *cobra.Command, args []string) error {
115116
}
116117
// if no namespaced manifest path is given, combine deploy/service_account.yaml, deploy/role.yaml, deploy/role_binding.yaml and deploy/operator.yaml
117118
if viper.GetString(NamespacedManifestOpt) == "" {
118-
file, err := yamlutil.GenerateCombinedNamespacedManifest()
119+
file, err := yamlutil.GenerateCombinedNamespacedManifest(scaffold.DeployDir)
119120
if err != nil {
120121
return err
121122
}
@@ -128,7 +129,7 @@ func ScorecardTests(cmd *cobra.Command, args []string) error {
128129
}()
129130
}
130131
if viper.GetString(GlobalManifestOpt) == "" {
131-
file, err := yamlutil.GenerateCombinedGlobalManifest()
132+
file, err := yamlutil.GenerateCombinedGlobalManifest(scaffold.CRDsDir)
132133
if err != nil {
133134
return err
134135
}

commands/operator-sdk/cmd/test/local.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func testLocalGoFunc(cmd *cobra.Command, args []string) error {
122122

123123
// if no namespaced manifest path is given, combine deploy/service_account.yaml, deploy/role.yaml, deploy/role_binding.yaml and deploy/operator.yaml
124124
if tlConfig.namespacedManPath == "" && !tlConfig.noSetup {
125-
file, err := yamlutil.GenerateCombinedNamespacedManifest()
125+
file, err := yamlutil.GenerateCombinedNamespacedManifest(scaffold.DeployDir)
126126
if err != nil {
127127
return err
128128
}
@@ -135,7 +135,7 @@ func testLocalGoFunc(cmd *cobra.Command, args []string) error {
135135
}()
136136
}
137137
if tlConfig.globalManPath == "" && !tlConfig.noSetup {
138-
file, err := yamlutil.GenerateCombinedGlobalManifest()
138+
file, err := yamlutil.GenerateCombinedGlobalManifest(scaffold.CRDsDir)
139139
if err != nil {
140140
return err
141141
}

internal/util/yamlutil/manifest.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func CombineManifests(base []byte, manifests ...[]byte) []byte {
5454
}
5555

5656
// GenerateCombinedNamespacedManifest creates a temporary manifest yaml
57-
// containing all standard namespaced resource manifests combined into 1 file
58-
func GenerateCombinedNamespacedManifest() (*os.File, error) {
57+
// by combining all standard namespaced resource manifests in deployDir.
58+
func GenerateCombinedNamespacedManifest(deployDir string) (*os.File, error) {
5959
file, err := ioutil.TempFile("", "namespaced-manifest.yaml")
6060
if err != nil {
6161
return nil, err
@@ -66,19 +66,19 @@ func GenerateCombinedNamespacedManifest() (*os.File, error) {
6666
}
6767
}()
6868

69-
sa, err := ioutil.ReadFile(filepath.Join(scaffold.DeployDir, scaffold.ServiceAccountYamlFile))
69+
sa, err := ioutil.ReadFile(filepath.Join(deployDir, scaffold.ServiceAccountYamlFile))
7070
if err != nil {
7171
log.Warnf("Could not find the serviceaccount manifest: (%v)", err)
7272
}
73-
role, err := ioutil.ReadFile(filepath.Join(scaffold.DeployDir, scaffold.RoleYamlFile))
73+
role, err := ioutil.ReadFile(filepath.Join(deployDir, scaffold.RoleYamlFile))
7474
if err != nil {
7575
log.Warnf("Could not find role manifest: (%v)", err)
7676
}
77-
roleBinding, err := ioutil.ReadFile(filepath.Join(scaffold.DeployDir, scaffold.RoleBindingYamlFile))
77+
roleBinding, err := ioutil.ReadFile(filepath.Join(deployDir, scaffold.RoleBindingYamlFile))
7878
if err != nil {
7979
log.Warnf("Could not find role_binding manifest: (%v)", err)
8080
}
81-
operator, err := ioutil.ReadFile(filepath.Join(scaffold.DeployDir, scaffold.OperatorYamlFile))
81+
operator, err := ioutil.ReadFile(filepath.Join(deployDir, scaffold.OperatorYamlFile))
8282
if err != nil {
8383
return nil, fmt.Errorf("could not find operator manifest: (%v)", err)
8484
}
@@ -98,8 +98,8 @@ func GenerateCombinedNamespacedManifest() (*os.File, error) {
9898
}
9999

100100
// GenerateCombinedGlobalManifest creates a temporary manifest yaml
101-
// containing all standard global resource manifests combined into 1 file
102-
func GenerateCombinedGlobalManifest() (*os.File, error) {
101+
// by combining all standard global resource manifests in crdsDir.
102+
func GenerateCombinedGlobalManifest(crdsDir string) (*os.File, error) {
103103
file, err := ioutil.TempFile("", "global-manifest.yaml")
104104
if err != nil {
105105
return nil, err
@@ -110,16 +110,16 @@ func GenerateCombinedGlobalManifest() (*os.File, error) {
110110
}
111111
}()
112112

113-
files, err := ioutil.ReadDir(scaffold.CRDsDir)
113+
files, err := ioutil.ReadDir(crdsDir)
114114
if err != nil {
115115
return nil, fmt.Errorf("could not read deploy directory: (%v)", err)
116116
}
117117
combined := []byte{}
118118
for _, file := range files {
119119
if strings.HasSuffix(file.Name(), "crd.yaml") {
120-
fileBytes, err := ioutil.ReadFile(filepath.Join(scaffold.CRDsDir, file.Name()))
120+
fileBytes, err := ioutil.ReadFile(filepath.Join(crdsDir, file.Name()))
121121
if err != nil {
122-
return nil, fmt.Errorf("could not read file %s: (%v)", filepath.Join(scaffold.CRDsDir, file.Name()), err)
122+
return nil, fmt.Errorf("could not read file %s: (%v)", filepath.Join(crdsDir, file.Name()), err)
123123
}
124124
combined = CombineManifests(combined, fileBytes)
125125
}

test/e2e/memcached_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
3333
"github.com/operator-framework/operator-sdk/internal/util/projutil"
3434
"github.com/operator-framework/operator-sdk/internal/util/yamlutil"
35+
"github.com/operator-framework/operator-sdk/pkg/scaffold"
3536
framework "github.com/operator-framework/operator-sdk/pkg/test"
3637
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
3738

@@ -251,7 +252,7 @@ func TestMemcached(t *testing.T) {
251252
}
252253
}
253254

254-
file, err := yamlutil.GenerateCombinedGlobalManifest()
255+
file, err := yamlutil.GenerateCombinedGlobalManifest(scaffold.CRDsDir)
255256
if err != nil {
256257
t.Fatal(err)
257258
}
@@ -496,7 +497,7 @@ func MemcachedCluster(t *testing.T) {
496497
}
497498
}
498499

499-
file, err := yamlutil.GenerateCombinedNamespacedManifest()
500+
file, err := yamlutil.GenerateCombinedNamespacedManifest(scaffold.DeployDir)
500501
if err != nil {
501502
t.Fatal(err)
502503
}

0 commit comments

Comments
 (0)