Skip to content

Commit b0741cf

Browse files
Renamed MergeRoleForResource func and added CHANGELOG
1 parent 16e0cff commit b0741cf

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- The `--overwrite` flag instructs [`operator-sdk bundle create`](./doc/cli/operator-sdk_bundle_create.md) to overwrite metadata, manifests, and `bundle.Dockerfile`. ([#2715](https://github.com/operator-framework/operator-sdk/pull/2715))
1313
- [`operator-sdk bundle validate`](./doc/cli/operator-sdk_bundle_validate.md) now accepts either an image tag or a directory arg. If the arg is a directory, its children must contain a `manifests/` and a `metadata/` directory. ([#2737](https://github.com/operator-framework/operator-sdk/pull/2737))
1414
- Add support to release SDK arm64 binaries and images. ([#2742](https://github.com/operator-framework/operator-sdk/pull/2715))
15+
- Add support for additional API creation for Anisble/Helm based operators. ([#2703](https://github.com/operator-framework/operator-sdk/pull/2703))
1516

1617
### Changed
1718

cmd/operator-sdk/add/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func doHelmAPIScaffold() error {
271271
roleScaffold = helm.GenerateRoleScaffold(dc, chart)
272272
}
273273

274-
if err = scaffold.MergeHelmRoleForResource(r, absProjectPath, roleScaffold); err != nil {
274+
if err = scaffold.MergeRoleForResource(r, absProjectPath, roleScaffold); err != nil {
275275
return fmt.Errorf("failed to merge rules in the RBAC manifest for resource (%v, %v): %v",
276276
r.APIVersion, r.Kind, err)
277277
}

internal/scaffold/role.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"io/ioutil"
2222
"path/filepath"
2323
"sort"
24-
"strings"
2524

2625
log "github.com/sirupsen/logrus"
2726
yaml "gopkg.in/yaml.v2"
@@ -146,8 +145,8 @@ func UpdateRoleForResource(r *Resource, absProjectPath string) error {
146145
}
147146
}
148147

149-
// MergeHelmRoleForResource merges incoming new API resource rules with existing deploy/role.yaml
150-
func MergeHelmRoleForResource(r *Resource, absProjectPath string, roleScaffold Role) error {
148+
// MergeRoleForResource merges incoming new API resource rules with existing deploy/role.yaml
149+
func MergeRoleForResource(r *Resource, absProjectPath string, roleScaffold Role) error {
151150
roleFilePath := filepath.Join(absProjectPath, DeployDir, RoleYamlFile)
152151
roleYAML, err := ioutil.ReadFile(roleFilePath)
153152
if err != nil {
@@ -161,21 +160,20 @@ func MergeHelmRoleForResource(r *Resource, absProjectPath string, roleScaffold R
161160
if len(roleScaffold.CustomRules) == 0 {
162161
return fmt.Errorf("customRules cannot be empty for new Role at: %v", r.APIVersion)
163162
}
164-
if roleScaffold.IsClusterScoped {
165-
// TODO: Add logic to merge Cluster scoped rules into existing Kind: Role scoped rules
166-
// Check if existing deploy/role.yaml is ClusterScoped already.
167-
if !strings.Contains(string(roleYAML), "kind: ClusterRole") {
168-
return fmt.Errorf("cannot Merge Cluster scoped rules with existing deploy/role.yaml" +
169-
"please modify existing deploy/role.yaml and deploy/role_binding.yaml " +
170-
"to reflect Cluster scope and try again")
171-
}
172-
}
163+
173164
obj, _, err := cgoscheme.Codecs.UniversalDeserializer().Decode(roleYAML, nil, nil)
174165
if err != nil {
175166
return fmt.Errorf("failed to decode role manifest %v: %v", roleFilePath, err)
176167
}
177168
switch role := obj.(type) {
178169
case *rbacv1.Role:
170+
// TODO: Add logic to merge Cluster scoped rules into existing Kind: Role scoped rules
171+
// Error out for ClusterRole merging with existing Kind: Role
172+
if roleScaffold.IsClusterScoped {
173+
return fmt.Errorf("cannot Merge Cluster scoped rules with existing deploy/role.yaml. " +
174+
"please modify existing deploy/role.yaml and deploy/role_binding.yaml " +
175+
"to reflect Cluster scope and try again")
176+
}
179177
mergedRoleRules := mergeRules(role.Rules, roleScaffold)
180178
role.Rules = mergedRoleRules
181179

internal/scaffold/role_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestRoleCustomRules(t *testing.T) {
8888
}
8989
}
9090

91-
func TestMergeHelmRoleForResource(t *testing.T) {
91+
func TestMergeRoleForResource(t *testing.T) {
9292
clusterRoleFilePath1 := "./testdata/testroles/valid_clusterrole"
9393
clusterRoleFile1 := clusterRoleFilePath1 + "/deploy/role.yaml"
9494
if err := ioutil.WriteFile(clusterRoleFile1, []byte(clusterRole), fileutil.DefaultFileMode); err != nil {
@@ -471,7 +471,7 @@ func TestMergeHelmRoleForResource(t *testing.T) {
471471
},
472472
},
473473
{
474-
name: "Invalid ClusterRole",
474+
name: "Invalid ClusterRole",
475475
absProjectPath: roleFilePath1,
476476
r: &Resource{
477477
APIVersion: "charts.helm.k8s.io/v1alpha1",
@@ -536,7 +536,7 @@ func TestMergeHelmRoleForResource(t *testing.T) {
536536
}
537537
for _, tc := range testCases {
538538
t.Run(tc.name, func(t *testing.T) {
539-
actualErr := MergeHelmRoleForResource(tc.r, tc.absProjectPath, *tc.roleScaffold)
539+
actualErr := MergeRoleForResource(tc.r, tc.absProjectPath, *tc.roleScaffold)
540540
absFilePath := tc.absProjectPath + "/deploy/role.yaml"
541541
actualMergedRoleYAML, err := ioutil.ReadFile(absFilePath)
542542
if err != nil {

0 commit comments

Comments
 (0)