From 7c9314cae0d3c1c3cff5a6db7d63ce242615e6a4 Mon Sep 17 00:00:00 2001 From: Alice Lilith Date: Fri, 6 Dec 2024 15:08:29 -0800 Subject: [PATCH] remove boilerplate type field from trafficPolicy field (#548) Signed-off-by: Alice-Lilith --- api/ngrok/v1alpha1/agentendpoint_types.go | 29 +++++++------------ .../ngrok.k8s.ngrok.com_agentendpoints.yaml | 29 ++++++------------- .../agent/agent_endpoint_controller.go | 2 +- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/api/ngrok/v1alpha1/agentendpoint_types.go b/api/ngrok/v1alpha1/agentendpoint_types.go index 04d808cf..fb561592 100644 --- a/api/ngrok/v1alpha1/agentendpoint_types.go +++ b/api/ngrok/v1alpha1/agentendpoint_types.go @@ -118,6 +118,7 @@ type AgentEndpointSpec struct { Upstream EndpointUpstream `json:"upstream"` // Allows configuring a TrafficPolicy to be used with this AgentEndpoint + // When configured, the traffic policy is provided inline or as a reference to an NgrokTrafficPolicy resource // // +kubebuilder:validation:Optional TrafficPolicy *TrafficPolicyCfg `json:"trafficPolicy,omitempty"` @@ -147,26 +148,11 @@ const ( TrafficPolicyCfgType_Inline TrafficPolicyCfgType = "inline" ) -func (t TrafficPolicyCfgType) IsKnown() bool { - switch t { - case TrafficPolicyCfgType_K8sRef, TrafficPolicyCfgType_Inline: - return true - default: - return false - } -} - -// +kubebuilder:validation:XValidation:rule="self.type == 'inline' ? has(self.inline) && !has(self.targetRef) : true",message="When type is inline, inline must be set, and targetRef must not be set." -// +kubebuilder:validation:XValidation:rule="self.type == 'targetRef' ? has(self.targetRef) && !has(self.inline) : true",message="When type is targetRef, targetRef must be set, and inline must not be set." +// +kubebuilder:validation:XValidation:rule="has(self.inline) || has(self.targetRef)", message="targetRef or inline must be provided to trafficPolicy" +// +kubebuilder:validation:XValidation:rule="has(self.inline) != has(self.targetRef)",message="Only one of inline and targetRef can be configured for trafficPolicy" type TrafficPolicyCfg struct { - // Controls the way that the TrafficPolicy configuration will be provided to the Agent Endpoint - // - // +kubebuilder:validation:Required - // +kubebuilder:validation:Enum=targetRef;inline - Type TrafficPolicyCfgType `json:"type"` - // Inline definition of a TrafficPolicy to attach to the agent Endpoint - // The raw json encoded policy that was applied to the ngrok API + // The raw JSON-encoded policy that was applied to the ngrok API // // +kubebuilder:validation:Optional // +kubebuilder:validation:Schemaless @@ -180,6 +166,13 @@ type TrafficPolicyCfg struct { Reference *K8sObjectRef `json:"targetRef,omitempty"` } +func (t *TrafficPolicyCfg) Type() TrafficPolicyCfgType { + if t.Reference != nil { + return TrafficPolicyCfgType_K8sRef + } + return TrafficPolicyCfgType_Inline +} + // AgentEndpointStatus defines the observed state of an AgentEndpoint type AgentEndpointStatus struct { // The unique identifier for this endpoint diff --git a/helm/ngrok-operator/templates/crds/ngrok.k8s.ngrok.com_agentendpoints.yaml b/helm/ngrok-operator/templates/crds/ngrok.k8s.ngrok.com_agentendpoints.yaml index b601fb14..a5976dbb 100644 --- a/helm/ngrok-operator/templates/crds/ngrok.k8s.ngrok.com_agentendpoints.yaml +++ b/helm/ngrok-operator/templates/crds/ngrok.k8s.ngrok.com_agentendpoints.yaml @@ -74,13 +74,14 @@ spec: the ngrok API/Dashboard type: string trafficPolicy: - description: Allows configuring a TrafficPolicy to be used with this - AgentEndpoint + description: |- + Allows configuring a TrafficPolicy to be used with this AgentEndpoint + When configured, the traffic policy is provided inline or as a reference to an NgrokTrafficPolicy resource properties: inline: description: |- Inline definition of a TrafficPolicy to attach to the agent Endpoint - The raw json encoded policy that was applied to the ngrok API + The raw JSON-encoded policy that was applied to the ngrok API type: object x-kubernetes-preserve-unknown-fields: true targetRef: @@ -93,25 +94,13 @@ spec: required: - name type: object - type: - description: Controls the way that the TrafficPolicy configuration - will be provided to the Agent Endpoint - enum: - - targetRef - - inline - type: string - required: - - type type: object x-kubernetes-validations: - - message: When type is inline, inline must be set, and targetRef - must not be set. - rule: 'self.type == ''inline'' ? has(self.inline) && !has(self.targetRef) - : true' - - message: When type is targetRef, targetRef must be set, and inline - must not be set. - rule: 'self.type == ''targetRef'' ? has(self.targetRef) && !has(self.inline) - : true' + - message: targetRef or inline must be provided to trafficPolicy + rule: has(self.inline) || has(self.targetRef) + - message: Only one of inline and targetRef can be configured for + trafficPolicy + rule: has(self.inline) != has(self.targetRef) upstream: description: Defines the destination for traffic to this AgentEndpoint properties: diff --git a/internal/controller/agent/agent_endpoint_controller.go b/internal/controller/agent/agent_endpoint_controller.go index b5399482..dde98183 100644 --- a/internal/controller/agent/agent_endpoint_controller.go +++ b/internal/controller/agent/agent_endpoint_controller.go @@ -208,7 +208,7 @@ func (r *AgentEndpointReconciler) getTrafficPolicy(ctx context.Context, aep *ngr var policy string var err error - switch aep.Spec.TrafficPolicy.Type { + switch aep.Spec.TrafficPolicy.Type() { case ngrokv1alpha1.TrafficPolicyCfgType_Inline: policyBytes, err := aep.Spec.TrafficPolicy.Inline.MarshalJSON() if err != nil {