NGINX Ingress Provider
Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com>
This commit is contained in:
parent
b39ee8ede5
commit
9bd5c61782
32 changed files with 3894 additions and 18 deletions
69
pkg/provider/kubernetes/ingress-nginx/convert.go
Normal file
69
pkg/provider/kubernetes/ingress-nginx/convert.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package ingressnginx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
netv1 "k8s.io/api/networking/v1"
|
||||
)
|
||||
|
||||
type marshaler interface {
|
||||
Marshal() ([]byte, error)
|
||||
}
|
||||
|
||||
type unmarshaler interface {
|
||||
Unmarshal(data []byte) error
|
||||
}
|
||||
|
||||
type LoadBalancerIngress interface {
|
||||
corev1.LoadBalancerIngress | netv1.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