Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manifest generation: remove metadata.namespace from manifests #3813

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions changelog/fragments/gen-bundle-namespaced.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# entries is a list of entries to include in
# release notes and/or the migration guide
entries:
- description: >
When generating bundles and packagemanifests, remove `metadata.namespace` from
namespaced resources when writing them into the `manifests` directory to avoid
validation errors.

# kind is one of:
# - addition
# - change
# - deprecation
# - removal
# - bugfix
kind: bugfix

# Is this a breaking change?
breaking: false
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())
}
})
})