Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove boilerplate type field from trafficPolicy field #548

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading