-
Notifications
You must be signed in to change notification settings - Fork 551
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
Add K8s version query function through Tongyi Lingma analysis #1135
Open
owenwang2020
wants to merge
5
commits into
alibaba:main
Choose a base branch
from
owenwang2020:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all 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 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 |
---|---|---|
|
@@ -17,9 +17,11 @@ package common | |
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"net" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
networking "istio.io/api/networking/v1alpha3" | ||
"istio.io/istio/pilot/pkg/model" | ||
|
@@ -32,6 +34,30 @@ import ( | |
. "github.com/alibaba/higress/pkg/ingress/log" | ||
) | ||
|
||
const ( | ||
defaultInterval = 3 * time.Second | ||
defaultTimeout = 1 * time.Minute | ||
) | ||
|
||
type retry struct { | ||
interval time.Duration | ||
timeout time.Duration | ||
} | ||
|
||
type RetryOption func(o *retry) | ||
|
||
func WithInterval(interval time.Duration) RetryOption { | ||
return func(r *retry) { | ||
r.interval = interval | ||
} | ||
} | ||
|
||
func WithTimeout(timeout time.Duration) RetryOption { | ||
return func(r *retry) { | ||
r.timeout = timeout | ||
} | ||
} | ||
|
||
func ValidateBackendResource(resource *v1.TypedLocalObjectReference) bool { | ||
if resource == nil || resource.APIGroup == nil || | ||
*resource.APIGroup != netv1.SchemeGroupVersion.Group || | ||
|
@@ -62,6 +88,62 @@ func V1Available(client kube.Client) bool { | |
return runningVersion.AtLeast(version119) | ||
} | ||
|
||
func V1Available(client kube.Client, retryOptions ...RetryOption) bool { | ||
retry := &retry{ | ||
interval: defaultInterval, | ||
timeout: defaultTimeout, | ||
} | ||
|
||
for _, option := range retryOptions { | ||
option(retry) | ||
} | ||
|
||
// most case is greater than 1.18 | ||
supportV1 := true | ||
err := wait.PollImmediate(retry.interval, retry.timeout, func() (done bool, err error) { | ||
available, err := v1Available(client) | ||
if err != nil { | ||
IngressLog.Errorf("check v1 available error: %v", err) | ||
// retry | ||
return false, nil | ||
} | ||
supportV1 = available | ||
// we have done. | ||
return true, nil | ||
}) | ||
|
||
if err != nil { | ||
IngressLog.Errorf("check v1 available finally error: %v", err) | ||
} | ||
|
||
return supportV1 | ||
} | ||
|
||
func IsRunningVersionAtLeast(atLeastVersionStr string, client kube.Client) (bool, error) { | ||
atLeastVersion, _ := version.ParseGeneric(atLeastVersionStr) | ||
|
||
serverVersion, err := client.GetKubernetesVersion() | ||
if err != nil { | ||
// Consider the new ingress package is available as default | ||
return true | ||
queryK8sVersionFail.Increment() | ||
return false, err | ||
} | ||
|
||
runningVersion, err := version.ParseGeneric(serverVersion.String()) | ||
if err != nil { | ||
// Consider the new ingress package is available as default | ||
IngressLog.Errorf("unexpected error parsing running Kubernetes version: %v", err) | ||
return true | ||
queryK8sVersionFail.Increment() | ||
return false, err | ||
} | ||
|
||
return runningVersion.AtLeast(version119) | ||
return runningVersion.AtLeast(atLeastVersion), nil | ||
} | ||
|
||
|
||
// NetworkingIngressAvailable check if the "networking" group Ingress is available. | ||
func NetworkingIngressAvailable(client kube.Client) bool { | ||
// check kubernetes version to use new ingress package or not | ||
|
@@ -81,6 +163,45 @@ func NetworkingIngressAvailable(client kube.Client) bool { | |
return runningVersion.AtLeast(version118) | ||
} | ||
|
||
func NetworkingIngressAvailable(client kube.Client, retryOptions ...RetryOption) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这么做的好处在哪里?原本机制有什么问题? |
||
retry := &retry{ | ||
interval: defaultInterval, | ||
timeout: defaultTimeout, | ||
} | ||
|
||
serverVersion, err := client.GetKubernetesVersion() | ||
if err != nil { | ||
return false | ||
for _, option := range retryOptions { | ||
option(retry) | ||
} | ||
|
||
runningVersion, err := version.ParseGeneric(serverVersion.String()) | ||
// most case is greater than or equal 1.18. | ||
supportNetworking := true | ||
|
||
err := wait.PollImmediate(retry.interval, retry.timeout, func() (done bool, err error) { | ||
available, err := networkingIngressAvailable(client) | ||
if err != nil { | ||
IngressLog.Errorf("check networking available error: %v", err) | ||
// retry | ||
return false, nil | ||
} | ||
supportNetworking = available | ||
// we have done. | ||
return true, nil | ||
}) | ||
|
||
if err != nil { | ||
IngressLog.Errorf("unexpected error parsing running Kubernetes version: %v", err) | ||
return false | ||
IngressLog.Errorf("check networking available finally error: %v", err) | ||
} | ||
|
||
return runningVersion.AtLeast(version118) | ||
return supportNetworking | ||
} | ||
|
||
// SortIngressByCreationTime sorts the list of config objects in ascending order by their creation time (if available). | ||
func SortIngressByCreationTime(configs []config.Config) { | ||
sort.Slice(configs, func(i, j int) bool { | ||
|
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 |
---|---|---|
|
@@ -158,32 +158,68 @@ inline std::string subspan(absl::string_view source, size_t start, size_t end) { | |
return {source.data() + start, end - start}; | ||
} | ||
|
||
QueryParams parseParameters(absl::string_view data, size_t start, | ||
bool decode_params) { | ||
QueryParams params; | ||
class QueryParams { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修改这里是要干什么? |
||
public: | ||
using ParamMap = std::unordered_map<std::string, std::string>; | ||
ParamMap params; | ||
|
||
while (start < data.size()) { | ||
size_t end = data.find('&', start); | ||
if (end == std::string::npos) { | ||
end = data.size(); | ||
void emplace(const std::string& name, const std::string& value) { | ||
params.emplace(name, value); | ||
} | ||
absl::string_view param(data.data() + start, end - start); | ||
|
||
const size_t equal = param.find('='); | ||
if (equal != std::string::npos) { | ||
const auto param_name = subspan(data, start, start + equal); | ||
const auto param_value = subspan(data, start + equal + 1, end); | ||
params.emplace( | ||
decode_params ? PercentEncoding::decode(param_name) : param_name, | ||
decode_params ? PercentEncoding::decode(param_value) : param_value); | ||
} else { | ||
params.emplace(subspan(data, start, end), ""); | ||
}; | ||
|
||
QueryParams parseParameters(absl::string_view data, size_t start, | ||
bool decode_params) { | ||
if (start > data.size()) { | ||
throw std::out_of_range("Start index is out of range."); | ||
} | ||
|
||
start = end + 1; | ||
} | ||
QueryParams params; | ||
|
||
return params; | ||
while (start < data.size()) { | ||
size_t end = data.find('&', start); | ||
if (end == std::string::npos) { | ||
end = data.size(); | ||
} | ||
|
||
absl::string_view param(data.data() + start, end - start); | ||
|
||
const size_t equal = param.find('='); | ||
if (equal != std::string::npos) { | ||
auto param_name = subspan(data, start, start + equal); | ||
auto param_value = subspan(data, start + equal + 1, end); | ||
|
||
try { | ||
if (decode_params) { | ||
param_name = PercentEncoding::decode(param_name); | ||
param_value = PercentEncoding::decode(param_value); | ||
} | ||
} catch (const std::exception& e) { | ||
// Handle decoding exceptions if necessary, or rethrow. | ||
throw; | ||
} | ||
|
||
params.emplace(param_name, param_value); | ||
} else { | ||
// Handle parameters without '=' (i.e., no value). | ||
// Optionally decode according to `decode_params`. | ||
try { | ||
if (decode_params) { | ||
auto decoded_param = PercentEncoding::decode(param); | ||
params.emplace(decoded_param, ""); | ||
} else { | ||
params.emplace(param, ""); | ||
} | ||
} catch (const std::exception& e) { | ||
// Handle decoding exceptions if necessary, or rethrow. | ||
throw; | ||
} | ||
} | ||
|
||
start = end + 1; | ||
} | ||
|
||
return params; | ||
} | ||
|
||
std::vector<std::string> getAllOfHeader(std::string_view key) { | ||
|
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.
这个指标没有意义