From 6861dadeaa96d396d753f38f51bc14b8df18a4fd Mon Sep 17 00:00:00 2001 From: Alessandro Olivero Date: Mon, 16 Oct 2023 11:54:51 +0200 Subject: [PATCH] add secret ref to top-level resources --- .../v1alpha1/gatewayclient_types.go | 2 + .../v1alpha1/gatewayserver_types.go | 2 + .../v1alpha1/zz_generated.deepcopy.go | 12 +++++- .../networking.liqo.io_gatewayclients.yaml | 37 +++++++++++++++++++ .../networking.liqo.io_gatewayservers.yaml | 37 +++++++++++++++++++ go.mod | 2 +- .../client-operator/client_controller.go | 10 +++++ .../server-operator/server_controller.go | 12 +++--- .../external-network/utils/getters.go | 32 ++++++++++++++++ 9 files changed, 139 insertions(+), 7 deletions(-) diff --git a/apis/networking/v1alpha1/gatewayclient_types.go b/apis/networking/v1alpha1/gatewayclient_types.go index ef81430e94..34eb292cc6 100644 --- a/apis/networking/v1alpha1/gatewayclient_types.go +++ b/apis/networking/v1alpha1/gatewayclient_types.go @@ -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 diff --git a/apis/networking/v1alpha1/gatewayserver_types.go b/apis/networking/v1alpha1/gatewayserver_types.go index cd88bf6686..4749e5e6d1 100644 --- a/apis/networking/v1alpha1/gatewayserver_types.go +++ b/apis/networking/v1alpha1/gatewayserver_types.go @@ -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 diff --git a/apis/networking/v1alpha1/zz_generated.deepcopy.go b/apis/networking/v1alpha1/zz_generated.deepcopy.go index 079a477055..59d8bbd327 100644 --- a/apis/networking/v1alpha1/zz_generated.deepcopy.go +++ b/apis/networking/v1alpha1/zz_generated.deepcopy.go @@ -462,7 +462,7 @@ func (in *GatewayClient) DeepCopyInto(out *GatewayClient) { out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.Spec.DeepCopyInto(&out.Spec) - out.Status = in.Status + in.Status.DeepCopyInto(&out.Status) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GatewayClient. @@ -536,6 +536,11 @@ func (in *GatewayClientSpec) DeepCopy() *GatewayClientSpec { func (in *GatewayClientStatus) DeepCopyInto(out *GatewayClientStatus) { *out = *in out.ClientRef = in.ClientRef + if in.SecretRef != nil { + in, out := &in.SecretRef, &out.SecretRef + *out = new(v1.ObjectReference) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GatewayClientStatus. @@ -633,6 +638,11 @@ func (in *GatewayServerStatus) DeepCopyInto(out *GatewayServerStatus) { *out = new(EndpointStatus) (*in).DeepCopyInto(*out) } + if in.SecretRef != nil { + in, out := &in.SecretRef, &out.SecretRef + *out = new(v1.ObjectReference) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GatewayServerStatus. diff --git a/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayclients.yaml b/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayclients.yaml index d77a2846b9..55dd0d4c1e 100644 --- a/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayclients.yaml +++ b/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayclients.yaml @@ -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 diff --git a/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayservers.yaml b/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayservers.yaml index 0b73a9b025..7a46389107 100644 --- a/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayservers.yaml +++ b/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayservers.yaml @@ -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: diff --git a/go.mod b/go.mod index e44722df60..3cfc896e44 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/liqo-controller-manager/external-network/client-operator/client_controller.go b/pkg/liqo-controller-manager/external-network/client-operator/client_controller.go index 0e34b3e102..dc143847ea 100644 --- a/pkg/liqo-controller-manager/external-network/client-operator/client_controller.go +++ b/pkg/liqo-controller-manager/external-network/client-operator/client_controller.go @@ -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 } diff --git a/pkg/liqo-controller-manager/external-network/server-operator/server_controller.go b/pkg/liqo-controller-manager/external-network/server-operator/server_controller.go index cea196bb20..778000be7f 100644 --- a/pkg/liqo-controller-manager/external-network/server-operator/server_controller.go +++ b/pkg/liqo-controller-manager/external-network/server-operator/server_controller.go @@ -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 } diff --git a/pkg/liqo-controller-manager/external-network/utils/getters.go b/pkg/liqo-controller-manager/external-network/utils/getters.go index 1b3a152a1c..96b6bfefcb 100644 --- a/pkg/liqo-controller-manager/external-network/utils/getters.go +++ b/pkg/liqo-controller-manager/external-network/utils/getters.go @@ -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" ) @@ -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 {