Skip to content

Commit

Permalink
add secret ref to top-level resources
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli committed Oct 16, 2023
1 parent 1c515cb commit 3a2e167
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 7 deletions.
2 changes: 2 additions & 0 deletions apis/networking/v1alpha1/gatewayclient_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type GatewayClientSpec struct {
type GatewayClientStatus struct {
// ClientRef specifies the reference to the client.
ClientRef corev1.ObjectReference `json:"clientRef,omitempty"`
// SecretRef specifies the reference to the secret.
SecretRef *corev1.ObjectReference `json:"secretRef,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
2 changes: 2 additions & 0 deletions apis/networking/v1alpha1/gatewayserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type GatewayServerStatus struct {
ServerRef corev1.ObjectReference `json:"serverRef,omitempty"`
// Endpoint specifies the endpoint of the tunnel.
Endpoint *EndpointStatus `json:"endpoint,omitempty"`
// SecretRef specifies the reference to the secret.
SecretRef *corev1.ObjectReference `json:"secretRef,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
12 changes: 11 additions & 1 deletion apis/networking/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,43 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
secretRef:
description: SecretRef specifies the reference to the secret.
properties:
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: 'If referring to a piece of an object instead of
an entire object, this string should contain a valid JSON/Go
field access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container within
a pod, this would take on a value like: "spec.containers{name}"
(where "name" refers to the name of the container that triggered
the event) or if no container name is specified "spec.containers[2]"
(container with index 2 in this pod). This syntax is chosen
only to have some well-defined way of referencing a part of
an object. TODO: this design is not final and this field is
subject to change in the future.'
type: string
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
namespace:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
resourceVersion:
description: 'Specific resourceVersion to which this reference
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
type: string
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
type: string
type: object
x-kubernetes-map-type: atomic
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,43 @@ spec:
- UDP
type: string
type: object
secretRef:
description: SecretRef specifies the reference to the secret.
properties:
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: 'If referring to a piece of an object instead of
an entire object, this string should contain a valid JSON/Go
field access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container within
a pod, this would take on a value like: "spec.containers{name}"
(where "name" refers to the name of the container that triggered
the event) or if no container name is specified "spec.containers[2]"
(container with index 2 in this pod). This syntax is chosen
only to have some well-defined way of referencing a part of
an object. TODO: this design is not final and this field is
subject to change in the future.'
type: string
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
namespace:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
resourceVersion:
description: 'Specific resourceVersion to which this reference
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
type: string
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
type: string
type: object
x-kubernetes-map-type: atomic
serverRef:
description: ServerRef specifies the reference to the server.
properties:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ require (
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/term v0.13.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.zx2c4.com/wireguard v0.0.0-20220904105730-b51010ba13f0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ func (r *ClientReconciler) EnsureGatewayClient(ctx context.Context, gwClient *ne
UID: unstructuredObject.GetUID(),
}

status, ok := unstructuredObject.Object["status"].(map[string]interface{})
if !ok {
// the object does not have a status
return nil
}
secretRef, ok := enutils.GetIfExists[map[string]interface{}](status, "secretRef")
if ok && secretRef != nil {
gwClient.Status.SecretRef = enutils.ParseRef(*secretRef)
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,14 @@ func (r *ServerReconciler) EnsureGatewayServer(ctx context.Context, server *netw
// the object does not have a status
return nil
}
endpoint, ok := status["endpoint"].(map[string]interface{})
if !ok {
// the object does not have an endpoint
return nil
endpoint, ok := enutils.GetIfExists[map[string]interface{}](status, "endpoint")
if ok && endpoint != nil {
server.Status.Endpoint = enutils.ParseEndpoint(*endpoint)
}
secretRef, ok := enutils.GetIfExists[map[string]interface{}](status, "secretRef")
if ok && secretRef != nil {
server.Status.SecretRef = enutils.ParseRef(*secretRef)
}
server.Status.Endpoint = enutils.ParseEndpoint(endpoint)

return nil
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/liqo-controller-manager/external-network/utils/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"

networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
)
Expand All @@ -40,6 +41,37 @@ func ParseEndpoint(endpoint map[string]interface{}) *networkingv1alpha1.Endpoint
return res
}

// ParseRef parses an ObjectReference from a map.
func ParseRef(ref map[string]interface{}) *corev1.ObjectReference {
res := &corev1.ObjectReference{}
if value, ok := ref["apiVersion"]; ok {
res.APIVersion = value.(string)
}
if value, ok := ref["kind"]; ok {
res.Kind = value.(string)
}
if value, ok := ref["name"]; ok {
res.Name = value.(string)
}
if value, ok := ref["namespace"]; ok {
res.Namespace = value.(string)
}
if value, ok := ref["uid"]; ok {
res.UID = value.(types.UID)
}
return res
}

// GetIfExists returns the value of a key in a map casting its type, or nil if the key is not present
// or the type is wrong.
func GetIfExists[T any](m map[string]interface{}, key string) (*T, bool) {
if value, ok := m[key]; ok {
v, ok := value.(T)
return &v, ok
}
return nil, false
}

func interfaceListToList[T any](list []interface{}) []T {
res := make([]T, len(list))
for i, v := range list {
Expand Down

0 comments on commit 3a2e167

Please sign in to comment.