forked from projectcontour/contour
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/k8s: add check to verify resources exist in cluster
Add a discovery client to look up server groups and resources which allows for a check to be made that a resource type exists before starting any informers against. Fixes projectcontour#2219 Signed-off-by: Steve Sloka <[email protected]>
- Loading branch information
1 parent
eb45c16
commit a4ca720
Showing
4 changed files
with
128 additions
and
11 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
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
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,71 @@ | ||
// Copyright Project Contour 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 k8s | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
"github.com/projectcontour/contour/internal/assert" | ||
) | ||
|
||
func TestResourceKindExists(t *testing.T) { | ||
type testcase struct { | ||
gvr schema.GroupVersionResource | ||
apiResourceList map[schema.GroupVersionResource]struct{} | ||
want bool | ||
} | ||
|
||
run := func(t *testing.T, name string, tc testcase) { | ||
t.Helper() | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Helper() | ||
|
||
clients := &Clients{} | ||
got := clients.ResourceExists(tc.gvr, tc.apiResourceList) | ||
assert.Equal(t, tc.want, got) | ||
}) | ||
} | ||
|
||
valid := schema.GroupVersionResource{ | ||
Group: "networking.k8s.io", | ||
Version: "v1beta1", | ||
Resource: "ingress", | ||
} | ||
|
||
invalid := schema.GroupVersionResource{ | ||
Group: "networking.k8s.io", | ||
Version: "v1beta1", | ||
Resource: "ingressclass", | ||
} | ||
|
||
run(t, "ingress exist", testcase{ | ||
gvr: valid, | ||
want: true, | ||
apiResourceList: map[schema.GroupVersionResource]struct{}{ | ||
valid: struct{}{}, | ||
}, | ||
}) | ||
|
||
run(t, "ingressclass does not exist", testcase{ | ||
gvr: invalid, | ||
want: false, | ||
apiResourceList: map[schema.GroupVersionResource]struct{}{ | ||
valid: struct{}{}, | ||
}, | ||
}) | ||
|
||
} |
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