Skip to content

Commit

Permalink
move namespace remove logic to internal/cmd/operator-sdk/generate/int…
Browse files Browse the repository at this point in the history
…ernal/manifests.go
  • Loading branch information
joelanford committed Aug 31, 2020
1 parent f42f055 commit e548936
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 102 deletions.
27 changes: 27 additions & 0 deletions internal/cmd/operator-sdk/generate/internal/genutil_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020 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 genutil

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestGenutil(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Collector Suite")
}
14 changes: 14 additions & 0 deletions internal/cmd/operator-sdk/generate/internal/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,19 @@ func GetManifestObjects(c *collector.Manifests) (objs []controllerutil.Object) {
_, clusterRoleObjs := c.SplitCSVClusterPermissionsObjects()
objs = append(objs, clusterRoleObjs...)

removeNamespace(objs)
return objs
}

// removeNamespace removes the namespace field of resources intended to be inserted into
// an OLM manifests directory.
//
// This is required to pass OLM validations which require that namespaced resources do
// not include explicit namespace settings. OLM automatically installs namespaced
// resources in the same namespace that the operator is installed in, which is determined
// at runtime, not bundle/packagemanifests creation time.
func removeNamespace(objs []controllerutil.Object) {
for _, obj := range objs {
obj.SetNamespace("")
}
}
56 changes: 56 additions & 0 deletions internal/cmd/operator-sdk/generate/internal/manifests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020 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 genutil

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/operator-framework/operator-sdk/internal/generate/collector"
)

var _ = Describe("GetManifestObjects", func() {
It("should unset the namespace", func() {
m := collector.Manifests{
Roles: []rbacv1.Role{
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "foo"}},
{ObjectMeta: metav1.ObjectMeta{Namespace: "bar"}},
},
ClusterRoles: []rbacv1.ClusterRole{
{ObjectMeta: metav1.ObjectMeta{Namespace: "bar"}},
},
ServiceAccounts: []corev1.ServiceAccount{
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "foo"}},
{ObjectMeta: metav1.ObjectMeta{Namespace: "bar"}},
},
V1beta1CustomResourceDefinitions: []apiextensionsv1beta1.CustomResourceDefinition{
{ObjectMeta: metav1.ObjectMeta{Namespace: "bar"}},
},
V1CustomResourceDefinitions: []apiextensionsv1.CustomResourceDefinition{
{ObjectMeta: metav1.ObjectMeta{Namespace: "bar"}},
},
}
objs := GetManifestObjects(&m)
Expect(objs).To(HaveLen(len(m.Roles) + len(m.ClusterRoles) + len(m.ServiceAccounts) + len(m.V1CustomResourceDefinitions) + len(m.V1beta1CustomResourceDefinitions)))
for _, obj := range objs {
Expect(obj.GetNamespace()).To(BeEmpty())
}
})
})
5 changes: 0 additions & 5 deletions internal/generate/collector/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func (c *Manifests) addRoles(rawManifests ...[]byte) error {
if err := yaml.Unmarshal(rawManifest, &role); err != nil {
return err
}
role.SetNamespace("")
c.Roles = append(c.Roles, role)
}
return nil
Expand All @@ -238,7 +237,6 @@ func (c *Manifests) addRoleBindings(rawManifests ...[]byte) error {
if err := yaml.Unmarshal(rawManifest, &binding); err != nil {
return err
}
binding.SetNamespace("")
c.RoleBindings = append(c.RoleBindings, binding)
}
return nil
Expand All @@ -263,7 +261,6 @@ func (c *Manifests) addServiceAccounts(rawManifests ...[]byte) error {
if err := yaml.Unmarshal(rawManifest, &sa); err != nil {
return err
}
sa.SetNamespace("")
c.ServiceAccounts = append(c.ServiceAccounts, sa)
}
return nil
Expand All @@ -277,7 +274,6 @@ func (c *Manifests) addDeployments(rawManifests ...[]byte) error {
if err := yaml.Unmarshal(rawManifest, &dep); err != nil {
return err
}
dep.SetNamespace("")
c.Deployments = append(c.Deployments, dep)
}
return nil
Expand Down Expand Up @@ -356,7 +352,6 @@ func (c *Manifests) addOthers(rawManifests ...[]byte) error {
if err := yaml.Unmarshal(rawManifest, &u); err != nil {
return err
}
u.SetNamespace("")
c.Others = append(c.Others, u)
}
return nil
Expand Down
97 changes: 0 additions & 97 deletions internal/generate/collector/manifests_test.go

This file was deleted.

0 comments on commit e548936

Please sign in to comment.