-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(kube-api-rewriter): add rewriter for Event resources (#496)
* refactor(kube-api-rewriter): add rewriter for Event resources - Stop confusing API consumers with non-renamed kind VirtualMachineInstance in Events. --------- Signed-off-by: Ivan Mikheykin <[email protected]>
- Loading branch information
Showing
8 changed files
with
252 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rewriter | ||
|
||
const ( | ||
EventKind = "Event" | ||
EventListKind = "EventList" | ||
) | ||
|
||
// RewriteEventOrList rewrites a single Event resource or a list of Events in EventList. | ||
// The only field need to rewrite is involvedObject: | ||
// | ||
// { | ||
// "metadata": { "name": "...", "namespace": "...", "managedFields": [...] }, | ||
// "involvedObject": { | ||
// "kind": "SomeResource", | ||
// "namespace": "name", | ||
// "name": "ns", | ||
// "uid": "a260fe4f-103a-41c6-996c-d29edb01fbbd", | ||
// "apiVersion": "group.io/v1" | ||
// }, | ||
// "type": "...", | ||
// "reason": "...", | ||
// "message": "...", | ||
// "source": { | ||
// "component": "...", | ||
// "host": "..." | ||
// }, | ||
// "reportingComponent": "...", | ||
// "reportingInstance": "..." | ||
// }, | ||
func RewriteEventOrList(rules *RewriteRules, obj []byte, action Action) ([]byte, error) { | ||
return RewriteResourceOrList(obj, EventListKind, func(singleObj []byte) ([]byte, error) { | ||
return TransformObject(singleObj, "involvedObject", func(involvedObj []byte) ([]byte, error) { | ||
return RewriteAPIVersionAndKind(rules, involvedObj, action) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rewriter | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
func TestRewriteEvent(t *testing.T) { | ||
eventReq := `POST /api/v1/namespaces/vm/events HTTP/1.1 | ||
Host: 127.0.0.1 | ||
` | ||
eventPayload := `{ | ||
"kind": "Event", | ||
"apiVersion": "v1", | ||
"metadata": { | ||
"name": "some-event-name", | ||
"namespace": "vm", | ||
}, | ||
"involvedObject": { | ||
"kind": "SomeResource", | ||
"namespace": "vm", | ||
"name": "some-vm-name", | ||
"uid": "ad9f7357-f6b0-4679-8571-042c75ec53fb", | ||
"apiVersion": "original.group.io/v1" | ||
}, | ||
"reason": "EventReason", | ||
"message": "Event message for some-vm-name", | ||
"source": { | ||
"component": "some-component", | ||
"host": "some-node" | ||
}, | ||
"count": 1000, | ||
"type": "Warning", | ||
"eventTime": null, | ||
"reportingComponent": "some-component", | ||
"reportingInstance": "some-node" | ||
}` | ||
|
||
req, err := http.ReadRequest(bufio.NewReader(bytes.NewBufferString(eventReq + eventPayload))) | ||
require.NoError(t, err, "should parse hardcoded http request") | ||
require.NotNil(t, req.URL, "should parse url in hardcoded http request") | ||
|
||
rwr := createTestRewriterForCore() | ||
targetReq := NewTargetRequest(rwr, req) | ||
require.NotNil(t, targetReq, "should get TargetRequest") | ||
require.True(t, targetReq.ShouldRewriteRequest(), "should rewrite request") | ||
require.True(t, targetReq.ShouldRewriteResponse(), "should rewrite response") | ||
// require.Equal(t, origGroup, targetReq.OrigGroup(), "should set proper orig group") | ||
|
||
resultBytes, err := rwr.RewriteJSONPayload(targetReq, []byte(eventPayload), Rename) | ||
if err != nil { | ||
t.Fatalf("should rename Error without error: %v", err) | ||
} | ||
if resultBytes == nil { | ||
t.Fatalf("should rename Error: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
path string | ||
expected string | ||
}{ | ||
{`involvedObject.kind`, "PrefixedSomeResource"}, | ||
{`involvedObject.apiVersion`, "prefixed.resources.group.io/v1"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.path, func(t *testing.T) { | ||
actual := gjson.GetBytes(resultBytes, tt.path).String() | ||
if actual != tt.expected { | ||
t.Fatalf("%s value should be %s, got %s", tt.path, tt.expected, actual) | ||
} | ||
}) | ||
} | ||
|
||
// Restore. | ||
resultBytes, err = rwr.RewriteJSONPayload(targetReq, []byte(eventPayload), Restore) | ||
if err != nil { | ||
t.Fatalf("should restore PVC without error: %v", err) | ||
} | ||
if resultBytes == nil { | ||
t.Fatalf("should restore PVC: %v", err) | ||
} | ||
|
||
tests = []struct { | ||
path string | ||
expected string | ||
}{ | ||
{`involvedObject.kind`, "SomeResource"}, | ||
{`involvedObject.apiVersion`, "original.group.io/v1"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.path, func(t *testing.T) { | ||
actual := gjson.GetBytes(resultBytes, tt.path).String() | ||
if actual != tt.expected { | ||
t.Fatalf("%s value should be %s, got %s", tt.path, tt.expected, actual) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rewriter | ||
|
||
import ( | ||
"github.com/tidwall/gjson" | ||
"github.com/tidwall/sjson" | ||
) | ||
|
||
func RewriteAPIGroupAndKind(rules *RewriteRules, obj []byte, action Action) ([]byte, error) { | ||
return RewriteGVK(rules, obj, action, "apiGroup") | ||
} | ||
|
||
func RewriteAPIVersionAndKind(rules *RewriteRules, obj []byte, action Action) ([]byte, error) { | ||
return RewriteGVK(rules, obj, action, "apiVersion") | ||
} | ||
|
||
// RewriteGVK rewrites a "kind" field and a field with the group | ||
// if there is the rule for these particular kind and group. | ||
func RewriteGVK(rules *RewriteRules, obj []byte, action Action, gvFieldName string) ([]byte, error) { | ||
kind := gjson.GetBytes(obj, "kind").String() | ||
apiGroupVersion := gjson.GetBytes(obj, gvFieldName).String() | ||
|
||
rwrApiVersion := "" | ||
rwrKind := "" | ||
if action == Rename { | ||
// Rename if there is a rule for kind and group | ||
_, resourceRule := rules.KindRules(apiGroupVersion, kind) | ||
if resourceRule == nil { | ||
return obj, nil | ||
} | ||
rwrApiVersion = rules.RenameApiVersion(apiGroupVersion) | ||
rwrKind = rules.RenameKind(kind) | ||
} | ||
if action == Restore { | ||
// Restore if group is renamed and a rule can be found | ||
// for restored kind and group. | ||
if !rules.IsRenamedGroup(apiGroupVersion) { | ||
return obj, nil | ||
} | ||
rwrApiVersion = rules.RestoreApiVersion(apiGroupVersion) | ||
rwrKind = rules.RestoreKind(kind) | ||
_, resourceRule := rules.KindRules(rwrApiVersion, rwrKind) | ||
if resourceRule == nil { | ||
return obj, nil | ||
} | ||
} | ||
|
||
obj, err := sjson.SetBytes(obj, "kind", rwrKind) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return sjson.SetBytes(obj, gvFieldName, rwrApiVersion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters