Skip to content

Commit

Permalink
remove boilerplate type field from trafficPolicy field (#548)
Browse files Browse the repository at this point in the history
Signed-off-by: Alice-Lilith <a.wasko@ngrok.com>
  • Loading branch information
Alice-Lilith authored Dec 6, 2024
1 parent 5d73f12 commit 7c9314c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 39 deletions.
29 changes: 11 additions & 18 deletions api/ngrok/v1alpha1/agentendpoint_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

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

2 changes: 1 addition & 1 deletion internal/controller/agent/agent_endpoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7c9314c

Please sign in to comment.