Skip to content

Commit 4f37d53

Browse files
committed
Adds a client that enables strict field validation by default
1 parent 2219c68 commit 4f37d53

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

pkg/client/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ type Options struct {
5656

5757
// DryRun instructs the client to only perform dry run requests.
5858
DryRun *bool
59+
60+
// Strict instructs the client to enable strict field validation
61+
// for all requests.
62+
Strict *bool
5963
}
6064

6165
// WarningHandlerOptions are options for configuring a
@@ -111,6 +115,9 @@ func New(config *rest.Config, options Options) (c Client, err error) {
111115
if err == nil && options.DryRun != nil && *options.DryRun {
112116
c = NewDryRunClient(c)
113117
}
118+
if err == nil && options.Strict != nil && *options.Strict {
119+
c = NewStrictClient(c)
120+
}
114121
return c, err
115122
}
116123

pkg/client/strict.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
Copyright 2024 The Kubernetes 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 client
18+
19+
import (
20+
"context"
21+
22+
"k8s.io/apimachinery/pkg/api/meta"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/runtime/schema"
25+
)
26+
27+
// NewStrictClient wraps an existing client and enforces strict field validation
28+
// on all mutating api calls.
29+
func NewStrictClient(c Client) Client {
30+
return &StrictClient{client: c}
31+
}
32+
33+
var _ Client = &StrictClient{}
34+
35+
// StrictClient is a Client that wraps another Client in order to enforce strict field validation.
36+
type StrictClient struct {
37+
client Client
38+
}
39+
40+
// Scheme returns the scheme this client is using.
41+
func (c *StrictClient) Scheme() *runtime.Scheme {
42+
return c.client.Scheme()
43+
}
44+
45+
// RESTMapper returns the rest mapper this client is using.
46+
func (c *StrictClient) RESTMapper() meta.RESTMapper {
47+
return c.client.RESTMapper()
48+
}
49+
50+
// GroupVersionKindFor returns the GroupVersionKind for the given object.
51+
func (c *StrictClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
52+
return c.client.GroupVersionKindFor(obj)
53+
}
54+
55+
// IsObjectNamespaced returns true if the GroupVersionKind of the object is namespaced.
56+
func (c *StrictClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
57+
return c.client.IsObjectNamespaced(obj)
58+
}
59+
60+
// Create implements client.Client.
61+
func (c *StrictClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
62+
return c.client.Create(ctx, obj, append(opts, FieldValidationStrict)...)
63+
}
64+
65+
// Update implements client.Client.
66+
func (c *StrictClient) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
67+
return c.client.Update(ctx, obj, append(opts, FieldValidationStrict)...)
68+
}
69+
70+
// Delete implements client.Client.
71+
func (c *StrictClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
72+
return c.client.Delete(ctx, obj, opts...)
73+
}
74+
75+
// DeleteAllOf implements client.Client.
76+
func (c *StrictClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
77+
return c.client.DeleteAllOf(ctx, obj, opts...)
78+
}
79+
80+
// Patch implements client.Client.
81+
func (c *StrictClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
82+
return c.client.Patch(ctx, obj, patch, append(opts, FieldValidationStrict)...)
83+
}
84+
85+
// Get implements client.Client.
86+
func (c *StrictClient) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
87+
return c.client.Get(ctx, key, obj, opts...)
88+
}
89+
90+
// List implements client.Client.
91+
func (c *StrictClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
92+
return c.client.List(ctx, obj, opts...)
93+
}
94+
95+
// Status implements client.StatusClient.
96+
func (c *StrictClient) Status() SubResourceWriter {
97+
return c.SubResource("status")
98+
}
99+
100+
// SubResource implements client.SubResourceClient.
101+
func (c *StrictClient) SubResource(subResource string) SubResourceClient {
102+
return &StrictSubResourceClient{client: c.client.SubResource(subResource)}
103+
}
104+
105+
// ensure StrictSubResourceWriter implements client.SubResourceWriter.
106+
var _ SubResourceWriter = &StrictSubResourceClient{}
107+
108+
// StrictSubResourceClient is client.SubResourceWriter that writes status subresource with strict field validation
109+
// enforced.
110+
type StrictSubResourceClient struct {
111+
client SubResourceClient
112+
}
113+
114+
func (sw *StrictSubResourceClient) Get(ctx context.Context, obj, subResource Object, opts ...SubResourceGetOption) error {
115+
return sw.client.Get(ctx, obj, subResource, opts...)
116+
}
117+
118+
func (sw *StrictSubResourceClient) Create(ctx context.Context, obj, subResource Object, opts ...SubResourceCreateOption) error {
119+
return sw.client.Create(ctx, obj, subResource, append(opts, FieldValidationStrict)...)
120+
}
121+
122+
// Update implements client.SubResourceWriter.
123+
func (sw *StrictSubResourceClient) Update(ctx context.Context, obj Object, opts ...SubResourceUpdateOption) error {
124+
return sw.client.Update(ctx, obj, append(opts, FieldValidationStrict)...)
125+
}
126+
127+
// Patch implements client.SubResourceWriter.
128+
func (sw *StrictSubResourceClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...SubResourcePatchOption) error {
129+
return sw.client.Patch(ctx, obj, patch, append(opts, FieldValidationStrict)...)
130+
}

0 commit comments

Comments
 (0)