-
Notifications
You must be signed in to change notification settings - Fork 2
feat: skip setup gateway api related controllers when cluster not support #215
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,72 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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 utils | ||
|
|
||
| import ( | ||
| "github.com/go-logr/logr" | ||
| "k8s.io/client-go/discovery" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/apache/apisix-ingress-controller/internal/types" | ||
| ) | ||
|
|
||
| // HasAPIResource checks if a specific API resource is available in the current cluster. | ||
| // It uses the Discovery API to query the cluster's available resources and returns true | ||
| // if the resource is found, false otherwise. | ||
| func HasAPIResource(mgr ctrl.Manager, obj client.Object) bool { | ||
| return HasAPIResourceWithLogger(mgr, obj, ctrl.Log.WithName("api-detection")) | ||
| } | ||
|
|
||
| // HasAPIResourceWithLogger is the same as HasAPIResource but accepts a custom logger | ||
| // for more detailed debugging information. | ||
| func HasAPIResourceWithLogger(mgr ctrl.Manager, obj client.Object, logger logr.Logger) bool { | ||
| gvk := types.GvkOf(obj) | ||
| groupVersion := gvk.GroupVersion().String() | ||
|
|
||
| logger = logger.WithValues( | ||
| "kind", gvk.Kind, | ||
| "group", gvk.Group, | ||
| "version", gvk.Version, | ||
| "groupVersion", groupVersion, | ||
| ) | ||
|
|
||
| // Create discovery client | ||
| discoveryClient, err := discovery.NewDiscoveryClientForConfig(mgr.GetConfig()) | ||
| if err != nil { | ||
| logger.Info("failed to create discovery client", "error", err) | ||
ronething marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false | ||
| } | ||
|
|
||
| // Query server resources for the specific group/version | ||
| apiResources, err := discoveryClient.ServerResourcesForGroupVersion(groupVersion) | ||
| if err != nil { | ||
| logger.Info("group/version not available in cluster", "error", err) | ||
ronething marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false | ||
| } | ||
|
|
||
| // Check if the specific kind exists in the resource list | ||
| for _, res := range apiResources.APIResources { | ||
| if res.Kind == gvk.Kind { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| logger.Info("API resource kind not found in group/version") | ||
| return false | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic here is basically similar, we can write it like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.