Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

FIX: namespaced condition for api resource #1199

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
2 changes: 1 addition & 1 deletion pkg/kubefedctl/federate/federate.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func FederateResources(resources []*unstructured.Unstructured) ([]*unstructured.
Version: gvk.Version,
Kind: gvk.Kind,
}
apiResource.Namespaced = targetResource.GetNamespace() == ""
apiResource.Namespaced = targetResource.GetNamespace() != ""

qualifiedName := ctlutil.NewQualifiedName(targetResource)
typeConfig := enable.GenerateTypeConfigForTarget(apiResource, enable.NewEnableTypeDirective())
Expand Down
81 changes: 81 additions & 0 deletions pkg/kubefedctl/federate/federate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2018 The Kubernetes 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 federate_test

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kubefed/pkg/kubefedctl/federate"
)

func TestFederateResources(t *testing.T) {
var resource = &unstructured.Unstructured{}
resource.Object = map[string]interface{}{
"name": "name",
"spec": map[string]interface{}{
"replicas": "2",
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"foo": "bar",
},
},
"template": map[string]interface{}{
"labels": map[string]interface{}{
"foo": "bar",
},
},
},
}
resource.SetGroupVersionKind(schema.GroupVersionKind{
Group: "apps",
Kind: "Deployment",
Version: "v1",
})

t.Run("TestNonNameSpacedDeployment", func(t *testing.T) {
federatedResources, err := federate.FederateResources([]*unstructured.Unstructured{resource})
assert.NoError(t, err, "Should not expect any errorr")
assert.Len(t, federatedResources, 1, "Should return a federated resource")

federatedResource := federatedResources[0]

assert.Empty(t, federatedResource.GetNamespace(), "Should not return a namespaces if not set")
assert.Equal(t, "FederatedDeployment", federatedResource.GetKind(), "Federated Resources should return a federated crd")
assert.Equal(t, "types.kubefed.io/v1beta1", federatedResource.GetAPIVersion(), "federated resourece should return correct api-version")
federatedSpec := federatedResource.Object["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"]
assert.Equal(t, resource.Object["spec"], federatedSpec)
})

t.Run("TestNameSpacedDeployment", func(t *testing.T) {
testNS := "testNS"
resource.SetNamespace(testNS)
federatedResources, err := federate.FederateResources([]*unstructured.Unstructured{resource})
assert.NoError(t, err, "Should not expect any errorr")
assert.Len(t, federatedResources, 1, "Should return a federated resource")

federatedResource := federatedResources[0]
assert.Equal(t, testNS, federatedResource.GetNamespace(), "A namespaces should be set and match orignal resource")
assert.Equal(t, "FederatedDeployment", federatedResource.GetKind(), "Federated Resources should return a federated crd")
assert.Equal(t, "types.kubefed.io/v1beta1", federatedResource.GetAPIVersion(), "federated resourece should return correct api-version")
federatedSpec := federatedResource.Object["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"]
assert.Equal(t, resource.Object["spec"], federatedSpec)
})

}