-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch makes all Antrea APIs respect label selector to fix the problem that some K8s clients get unexpected resources when labelSelector is specified.
- Loading branch information
Showing
14 changed files
with
532 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
pkg/apiserver/registry/networkpolicy/addressgroup/rest_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2020 Antrea 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 addressgroup | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vmware-tanzu/antrea/pkg/apis/controlplane" | ||
"k8s.io/apimachinery/pkg/apis/meta/internalversion" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
"github.com/vmware-tanzu/antrea/pkg/controller/networkpolicy/store" | ||
"github.com/vmware-tanzu/antrea/pkg/controller/types" | ||
) | ||
|
||
func TestRESTList(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
addressGroups []*types.AddressGroup | ||
labelSelector labels.Selector | ||
expectedObj runtime.Object | ||
}{ | ||
{ | ||
name: "label selector selecting nothing", | ||
addressGroups: []*types.AddressGroup{ | ||
{ | ||
Name: "foo", | ||
}, | ||
}, | ||
labelSelector: labels.Nothing(), | ||
expectedObj: &controlplane.AddressGroupList{}, | ||
}, | ||
{ | ||
name: "label selector selecting everything", | ||
addressGroups: []*types.AddressGroup{ | ||
{ | ||
Name: "foo", | ||
}, | ||
}, | ||
labelSelector: labels.Everything(), | ||
expectedObj: &controlplane.AddressGroupList{ | ||
Items: []controlplane.AddressGroup{ | ||
{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
storage := store.NewAddressGroupStore() | ||
for _, obj := range tt.addressGroups { | ||
storage.Create(obj) | ||
} | ||
r := NewREST(storage) | ||
actualObj, err := r.List(context.TODO(), &internalversion.ListOptions{LabelSelector: tt.labelSelector}) | ||
assert.NoError(t, err) | ||
assert.ElementsMatch(t, tt.expectedObj.(*controlplane.AddressGroupList).Items, actualObj.(*controlplane.AddressGroupList).Items) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
pkg/apiserver/registry/networkpolicy/appliedtogroup/rest_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2020 Antrea 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 appliedtogroup | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vmware-tanzu/antrea/pkg/apis/controlplane" | ||
"k8s.io/apimachinery/pkg/apis/meta/internalversion" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
"github.com/vmware-tanzu/antrea/pkg/controller/networkpolicy/store" | ||
"github.com/vmware-tanzu/antrea/pkg/controller/types" | ||
) | ||
|
||
func TestRESTList(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
appliedToGroups []*types.AppliedToGroup | ||
labelSelector labels.Selector | ||
expectedObj runtime.Object | ||
}{ | ||
{ | ||
name: "label selector selecting nothing", | ||
appliedToGroups: []*types.AppliedToGroup{ | ||
{ | ||
Name: "foo", | ||
}, | ||
}, | ||
labelSelector: labels.Nothing(), | ||
expectedObj: &controlplane.AppliedToGroupList{}, | ||
}, | ||
{ | ||
name: "label selector selecting everything", | ||
appliedToGroups: []*types.AppliedToGroup{ | ||
{ | ||
Name: "foo", | ||
}, | ||
}, | ||
labelSelector: labels.Everything(), | ||
expectedObj: &controlplane.AppliedToGroupList{ | ||
Items: []controlplane.AppliedToGroup{ | ||
{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
storage := store.NewAppliedToGroupStore() | ||
for _, obj := range tt.appliedToGroups { | ||
storage.Create(obj) | ||
} | ||
r := NewREST(storage) | ||
actualObj, err := r.List(context.TODO(), &internalversion.ListOptions{LabelSelector: tt.labelSelector}) | ||
assert.NoError(t, err) | ||
assert.ElementsMatch(t, tt.expectedObj.(*controlplane.AppliedToGroupList).Items, actualObj.(*controlplane.AppliedToGroupList).Items) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
pkg/apiserver/registry/networkpolicy/networkpolicy/rest_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2020 Antrea 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 networkpolicy | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vmware-tanzu/antrea/pkg/apis/controlplane" | ||
"k8s.io/apimachinery/pkg/apis/meta/internalversion" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
"github.com/vmware-tanzu/antrea/pkg/controller/networkpolicy/store" | ||
"github.com/vmware-tanzu/antrea/pkg/controller/types" | ||
) | ||
|
||
func TestRESTList(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
networkPolicies []*types.NetworkPolicy | ||
labelSelector labels.Selector | ||
expectedObj runtime.Object | ||
}{ | ||
{ | ||
name: "label selector selecting nothing", | ||
networkPolicies: []*types.NetworkPolicy{ | ||
{ | ||
Name: "foo", | ||
}, | ||
}, | ||
labelSelector: labels.Nothing(), | ||
expectedObj: &controlplane.NetworkPolicyList{}, | ||
}, | ||
{ | ||
name: "label selector selecting everything", | ||
networkPolicies: []*types.NetworkPolicy{ | ||
{ | ||
Name: "foo", | ||
}, | ||
}, | ||
labelSelector: labels.Everything(), | ||
expectedObj: &controlplane.NetworkPolicyList{ | ||
Items: []controlplane.NetworkPolicy{ | ||
{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
storage := store.NewNetworkPolicyStore() | ||
for _, obj := range tt.networkPolicies { | ||
storage.Create(obj) | ||
} | ||
r := NewREST(storage) | ||
actualObj, err := r.List(context.TODO(), &internalversion.ListOptions{LabelSelector: tt.labelSelector}) | ||
assert.NoError(t, err) | ||
assert.ElementsMatch(t, tt.expectedObj.(*controlplane.NetworkPolicyList).Items, actualObj.(*controlplane.NetworkPolicyList).Items) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.