chore: bump k8s.io/client-go from v0.22.1 to v0.26.3
This commit is contained in:
parent
ac9d88e5a2
commit
f2eda3aa6d
13 changed files with 538 additions and 167 deletions
70
pkg/provider/kubernetes/ingress/convert.go
Normal file
70
pkg/provider/kubernetes/ingress/convert.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package ingress
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
networkingv1 "k8s.io/api/networking/v1"
|
||||
networkingv1beta1 "k8s.io/api/networking/v1beta1"
|
||||
)
|
||||
|
||||
type marshaler interface {
|
||||
Marshal() ([]byte, error)
|
||||
}
|
||||
|
||||
type unmarshaler interface {
|
||||
Unmarshal([]byte) error
|
||||
}
|
||||
|
||||
type LoadBalancerIngress interface {
|
||||
corev1.LoadBalancerIngress | networkingv1beta1.IngressLoadBalancerIngress | networkingv1.IngressLoadBalancerIngress
|
||||
}
|
||||
|
||||
// convertSlice converts slice of LoadBalancerIngress to slice of LoadBalancerIngress.
|
||||
// O (Bar), I (Foo) => []Bar.
|
||||
func convertSlice[O LoadBalancerIngress, I LoadBalancerIngress](loadBalancerIngresses []I) ([]O, error) {
|
||||
var results []O
|
||||
|
||||
for _, loadBalancerIngress := range loadBalancerIngresses {
|
||||
mar, ok := any(&loadBalancerIngress).(marshaler)
|
||||
if !ok {
|
||||
// All the pointer of types related to the interface LoadBalancerIngress are compatible with the interface marshaler.
|
||||
continue
|
||||
}
|
||||
|
||||
um, err := convert[O](mar)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, ok := any(*um).(O)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
results = append(results, v)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// convert must only be used with unmarshaler and marshaler compatible types.
|
||||
func convert[T any](input marshaler) (*T, error) {
|
||||
data, err := input.Marshal()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var output T
|
||||
um, ok := any(&output).(unmarshaler)
|
||||
if !ok {
|
||||
return nil, errors.New("the output type doesn't implement unmarshaler interface")
|
||||
}
|
||||
|
||||
err = um.Unmarshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &output, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue