Skip to content

Commit a0dcec4

Browse files
committed
Add SnippetsPolicy resource and related methods
1 parent 89aee48 commit a0dcec4

File tree

6 files changed

+601
-0
lines changed

6 files changed

+601
-0
lines changed

apis/v1alpha1/policy_methods.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ func (p *UpstreamSettingsPolicy) GetPolicyStatus() gatewayv1.PolicyStatus {
3131
func (p *UpstreamSettingsPolicy) SetPolicyStatus(status gatewayv1.PolicyStatus) {
3232
p.Status = status
3333
}
34+
35+
func (p *SnippetsPolicy) GetTargetRefs() []gatewayv1.LocalPolicyTargetReference {
36+
return []gatewayv1.LocalPolicyTargetReference{p.Spec.TargetRef.LocalPolicyTargetReference}
37+
}
38+
39+
func (p *SnippetsPolicy) GetPolicyStatus() gatewayv1.PolicyStatus {
40+
return p.Status
41+
}
42+
43+
func (p *SnippetsPolicy) SetPolicyStatus(status gatewayv1.PolicyStatus) {
44+
p.Status = status
45+
}

apis/v1alpha1/register.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
4040
&SnippetsFilterList{},
4141
&UpstreamSettingsPolicy{},
4242
&UpstreamSettingsPolicyList{},
43+
&SnippetsPolicy{},
44+
&SnippetsPolicyList{},
4345
)
4446
// AddToGroupVersion allows the serialization of client types like ListOptions.
4547
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2025 The NGINX Gateway Fabric Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
22+
)
23+
24+
// +genclient
25+
// +kubebuilder:object:root=true
26+
// +kubebuilder:storageversion
27+
// +kubebuilder:subresource:status
28+
// +kubebuilder:resource:shortName=snipol
29+
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
30+
// +kubebuilder:printcolumn:name="Accepted",type=string,JSONPath=`.status.conditions[?(@.type=="Accepted")].status`
31+
// +kubebuilder:printcolumn:name="Reason",type=string,JSONPath=`.status.conditions[?(@.type=="Accepted")].reason`
32+
33+
// SnippetsPolicy provides a way to inject NGINX snippets into the configuration.
34+
type SnippetsPolicy struct {
35+
metav1.TypeMeta `json:",inline"`
36+
metav1.ObjectMeta `json:"metadata,omitempty"`
37+
38+
// Spec defines the desired state of the SnippetsPolicy.
39+
Spec SnippetsPolicySpec `json:"spec"`
40+
41+
// Status defines the current state of the SnippetsPolicy.
42+
// +optional
43+
Status gatewayv1.PolicyStatus `json:"status,omitempty"`
44+
}
45+
46+
// SnippetsPolicySpec defines the desired state of the SnippetsPolicy.
47+
type SnippetsPolicySpec struct {
48+
// TargetRef is the reference to the Gateway that this policy should be applied to.
49+
TargetRef SnippetsPolicyTargetRef `json:"targetRef"`
50+
51+
// Snippets is a list of snippets to be injected into the NGINX configuration.
52+
// +kubebuilder:validation:MaxItems=3
53+
// +kubebuilder:validation:XValidation:message="Only one snippet allowed per context",rule="self.all(s1, self.exists_one(s2, s1.context == s2.context))"
54+
// +kubebuilder:validation:XValidation:message="http.server.location context is not supported in SnippetsPolicy",rule="!self.exists(s, s.context == 'http.server.location')"
55+
Snippets []Snippet `json:"snippets"`
56+
}
57+
58+
// SnippetsPolicyTargetRef identifies an API object to apply the policy to.
59+
type SnippetsPolicyTargetRef struct {
60+
gatewayv1.LocalPolicyTargetReference `json:",inline"`
61+
}
62+
63+
// +kubebuilder:object:root=true
64+
65+
// SnippetsPolicyList contains a list of SnippetsPolicies.
66+
type SnippetsPolicyList struct {
67+
metav1.TypeMeta `json:",inline"`
68+
metav1.ListMeta `json:"metadata,omitempty"`
69+
Items []SnippetsPolicy `json:"items"`
70+
}

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)