Skip to content

Commit 3a8489f

Browse files
finalize refactoring
1 parent 48c9acc commit 3a8489f

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

shield/pkg/common/requestcontext.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ type RequestContext struct {
4848
}
4949

5050
type VRequestObject struct {
51-
RawObject []byte `json:"-"`
52-
RawOldObject []byte `json:"-"`
53-
OrgMetadata *VObjectMetadata `json:"orgMetadata"`
54-
ClaimedMetadata *VObjectMetadata `json:"claimedMetadata"`
55-
ObjLabels string `json:"objLabels"`
56-
ObjMetaName string `json:"objMetaName"`
51+
RawObject []byte `json:"-"`
52+
RawOldObject []byte `json:"-"`
53+
OrgMetadata *ObjectMetadata `json:"orgMetadata"`
54+
ClaimedMetadata *ObjectMetadata `json:"claimedMetadata"`
55+
ObjLabels string `json:"objLabels"`
56+
ObjMetaName string `json:"objMetaName"`
5757
}
5858

59-
type VObjectMetadata struct {
59+
type ObjectMetadata struct {
6060
Annotations *ResourceAnnotation `json:"annotations"`
6161
Labels *ResourceLabel `json:"labels"`
6262
}
@@ -143,13 +143,13 @@ func AdmissionRequestToResourceContext(request *admv1.AdmissionRequest) *Resourc
143143
return NewResourceContext(obj)
144144
}
145145

146-
type VParsedRequest struct {
146+
type ParsedRequest struct {
147147
UID string
148148
JsonStr string
149149
}
150150

151-
func NewVParsedRequest(request *admv1.AdmissionRequest) *VParsedRequest {
152-
var pr = &VParsedRequest{
151+
func NewParsedRequest(request *admv1.AdmissionRequest) *ParsedRequest {
152+
var pr = &ParsedRequest{
153153
UID: string(request.UID),
154154
}
155155
if reqBytes, err := json.Marshal(request); err != nil {
@@ -163,15 +163,15 @@ func NewVParsedRequest(request *admv1.AdmissionRequest) *VParsedRequest {
163163
return pr
164164
}
165165

166-
func (pr *VParsedRequest) getValue(path string) string {
166+
func (pr *ParsedRequest) getValue(path string) string {
167167
var v string
168168
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
169169
v = w.String()
170170
}
171171
return v
172172
}
173173

174-
func (pr *VParsedRequest) getArrayValue(path string) []string {
174+
func (pr *ParsedRequest) getArrayValue(path string) []string {
175175
var v []string
176176
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
177177
x := w.Array()
@@ -182,7 +182,7 @@ func (pr *VParsedRequest) getArrayValue(path string) []string {
182182
return v
183183
}
184184

185-
func (pr *VParsedRequest) getAnnotations(path string) *ResourceAnnotation {
185+
func (pr *ParsedRequest) getAnnotations(path string) *ResourceAnnotation {
186186
var r map[string]string = map[string]string{}
187187
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
188188
m := w.Map()
@@ -196,7 +196,7 @@ func (pr *VParsedRequest) getAnnotations(path string) *ResourceAnnotation {
196196
}
197197
}
198198

199-
func (pr *VParsedRequest) getLabels(path string) *ResourceLabel {
199+
func (pr *ParsedRequest) getLabels(path string) *ResourceLabel {
200200
var r map[string]string = map[string]string{}
201201
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
202202
m := w.Map()
@@ -210,7 +210,7 @@ func (pr *VParsedRequest) getLabels(path string) *ResourceLabel {
210210
}
211211
}
212212

213-
func (pr *VParsedRequest) getBool(path string, defaultValue bool) bool {
213+
func (pr *ParsedRequest) getBool(path string, defaultValue bool) bool {
214214
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
215215
v := w.String()
216216
if b, err := strconv.ParseBool(v); err != nil {
@@ -224,7 +224,7 @@ func (pr *VParsedRequest) getBool(path string, defaultValue bool) bool {
224224

225225
func NewRequestContext(req *admv1.AdmissionRequest) (*RequestContext, *VRequestObject) {
226226

227-
pr := NewVParsedRequest(req)
227+
pr := NewParsedRequest(req)
228228

229229
name := pr.getValue("name")
230230
if name == "" {
@@ -239,12 +239,12 @@ func NewRequestContext(req *admv1.AdmissionRequest) (*RequestContext, *VRequestO
239239
namespace = pr.getValue("object.metdata.namespace")
240240
}
241241

242-
orgMetadata := &VObjectMetadata{
242+
orgMetadata := &ObjectMetadata{
243243
Annotations: pr.getAnnotations("oldObject.metadata.annotations"),
244244
Labels: pr.getLabels("oldObject.metadata.labels"),
245245
}
246246

247-
claimedMetadata := &VObjectMetadata{
247+
claimedMetadata := &ObjectMetadata{
248248
Annotations: pr.getAnnotations("object.metadata.annotations"),
249249
Labels: pr.getLabels("object.metadata.labels"),
250250
}

shield/pkg/common/resourcecontext.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,16 @@ import (
3030
)
3131

3232
type ResourceContext struct {
33-
ResourceScope string `json:"resourceScope,omitempty"`
34-
RawObject []byte `json:"-"`
35-
Namespace string `json:"namespace"`
36-
Name string `json:"name"`
37-
ApiGroup string `json:"apiGroup"`
38-
ApiVersion string `json:"apiVersion"`
39-
Kind string `json:"kind"`
40-
ClaimedMetadata *V2ObjectMetadata `json:"claimedMetadata"`
41-
ObjLabels string `json:"objLabels"`
42-
ObjMetaName string `json:"objMetaName"`
43-
}
44-
45-
type V2ObjectMetadata struct {
46-
Annotations *ResourceAnnotation `json:"annotations"`
47-
Labels *ResourceLabel `json:"labels"`
33+
ResourceScope string `json:"resourceScope,omitempty"`
34+
RawObject []byte `json:"-"`
35+
Namespace string `json:"namespace"`
36+
Name string `json:"name"`
37+
ApiGroup string `json:"apiGroup"`
38+
ApiVersion string `json:"apiVersion"`
39+
Kind string `json:"kind"`
40+
ClaimedMetadata *ObjectMetadata `json:"claimedMetadata"`
41+
ObjLabels string `json:"objLabels"`
42+
ObjMetaName string `json:"objMetaName"`
4843
}
4944

5045
func (resc *ResourceContext) ResourceRef() *ResourceRef {
@@ -108,12 +103,12 @@ func (rc *ResourceContext) ExcludeDiffValue() bool {
108103
return false
109104
}
110105

111-
type V2ParsedRequest struct {
106+
type ParsedResource struct {
112107
JsonStr string
113108
}
114109

115-
func NewV2ParsedRequest(resource *unstructured.Unstructured) *V2ParsedRequest {
116-
var pr = &V2ParsedRequest{}
110+
func NewParsedResource(resource *unstructured.Unstructured) *ParsedResource {
111+
var pr = &ParsedResource{}
117112
if resBytes, err := json.Marshal(resource); err != nil {
118113
logger.WithFields(log.Fields{
119114
"err": err,
@@ -125,15 +120,15 @@ func NewV2ParsedRequest(resource *unstructured.Unstructured) *V2ParsedRequest {
125120
return pr
126121
}
127122

128-
func (pr *V2ParsedRequest) getValue(path string) string {
123+
func (pr *ParsedResource) getValue(path string) string {
129124
var v string
130125
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
131126
v = w.String()
132127
}
133128
return v
134129
}
135130

136-
func (pr *V2ParsedRequest) getArrayValue(path string) []string {
131+
func (pr *ParsedResource) getArrayValue(path string) []string {
137132
var v []string
138133
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
139134
x := w.Array()
@@ -144,7 +139,7 @@ func (pr *V2ParsedRequest) getArrayValue(path string) []string {
144139
return v
145140
}
146141

147-
func (pr *V2ParsedRequest) getAnnotations(path string) *ResourceAnnotation {
142+
func (pr *ParsedResource) getAnnotations(path string) *ResourceAnnotation {
148143
var r map[string]string = map[string]string{}
149144
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
150145
m := w.Map()
@@ -158,7 +153,7 @@ func (pr *V2ParsedRequest) getAnnotations(path string) *ResourceAnnotation {
158153
}
159154
}
160155

161-
func (pr *V2ParsedRequest) getLabels(path string) *ResourceLabel {
156+
func (pr *ParsedResource) getLabels(path string) *ResourceLabel {
162157
var r map[string]string = map[string]string{}
163158
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
164159
m := w.Map()
@@ -172,7 +167,7 @@ func (pr *V2ParsedRequest) getLabels(path string) *ResourceLabel {
172167
}
173168
}
174169

175-
func (pr *V2ParsedRequest) getBool(path string, defaultValue bool) bool {
170+
func (pr *ParsedResource) getBool(path string, defaultValue bool) bool {
176171
if w := gjson.Get(pr.JsonStr, path); w.Exists() {
177172
v := w.String()
178173
if b, err := strconv.ParseBool(v); err != nil {
@@ -186,7 +181,7 @@ func (pr *V2ParsedRequest) getBool(path string, defaultValue bool) bool {
186181

187182
func NewResourceContext(res *unstructured.Unstructured) *ResourceContext {
188183

189-
pr := NewV2ParsedRequest(res)
184+
pr := NewParsedResource(res)
190185

191186
name := pr.getValue("name")
192187
if name == "" {
@@ -198,7 +193,7 @@ func NewResourceContext(res *unstructured.Unstructured) *ResourceContext {
198193
namespace = pr.getValue("metadata.namespace")
199194
}
200195

201-
claimedMetadata := &V2ObjectMetadata{
196+
claimedMetadata := &ObjectMetadata{
202197
Annotations: pr.getAnnotations("metadata.annotations"),
203198
Labels: pr.getLabels("metadata.labels"),
204199
}

0 commit comments

Comments
 (0)