-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a08d1a1
commit efa8afc
Showing
8 changed files
with
266 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ golang 1.20.2 | |
golangci-lint 1.51.2 | ||
pre-commit 3.1.1 | ||
goreleaser 1.16.1 | ||
mockery 2.27.1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package rewriter | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
type conditionalRewriter struct { | ||
rewriter ResourceRewriter | ||
condition Condition | ||
} | ||
|
||
var _ ResourceRewriter = (*conditionalRewriter)(nil) | ||
|
||
// When executes provided rewriter only if condition matches | ||
func When(condition Condition, rewriter ResourceRewriter) ResourceRewriter { | ||
return &conditionalRewriter{ | ||
rewriter: rewriter, | ||
condition: condition, | ||
} | ||
} | ||
|
||
func (r *conditionalRewriter) BeforeImport(u *unstructured.Unstructured) error { | ||
if r.condition(u) { | ||
return r.rewriter.BeforeImport(u) | ||
} | ||
return nil | ||
} | ||
|
||
func (r *conditionalRewriter) BeforeServing(u *unstructured.Unstructured) error { | ||
if r.condition(u) { | ||
return r.rewriter.BeforeServing(u) | ||
} | ||
return nil | ||
} | ||
|
||
// Condition is function that returns bool with condition match result. | ||
type Condition func(*unstructured.Unstructured) bool | ||
|
||
// MatchGVK checks if resource matches given GroupVersionKind | ||
func MatchGVK(gvk schema.GroupVersionKind) Condition { | ||
return func(u *unstructured.Unstructured) bool { | ||
apiVersion, kind := gvk.ToAPIVersionAndKind() | ||
|
||
if u.GetAPIVersion() != apiVersion { | ||
return false | ||
} | ||
|
||
if u.GetKind() != kind { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
} |
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,80 @@ | ||
package rewriter_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
"github.com/mhrabovcin/troubleshoot-live/pkg/rewriter" | ||
"github.com/mhrabovcin/troubleshoot-live/pkg/rewriter/mocks" | ||
) | ||
|
||
var ( | ||
anyUnstructured = mock.MatchedBy(func(_ *unstructured.Unstructured) bool { return true }) | ||
matchAny = func(u *unstructured.Unstructured) bool { return true } | ||
matchNone = func(u *unstructured.Unstructured) bool { return false } | ||
) | ||
|
||
func TestWhen_Matching(t *testing.T) { | ||
rrMock := mocks.NewResourceRewriter(t) | ||
rrMock.EXPECT().BeforeImport(anyUnstructured).Return(nil).Once() | ||
rrMock.EXPECT().BeforeServing(anyUnstructured).Return(nil).Once() | ||
assert.NoError(t, rewriter.When(matchAny, rrMock).BeforeImport(&unstructured.Unstructured{})) | ||
assert.NoError(t, rewriter.When(matchAny, rrMock).BeforeServing(&unstructured.Unstructured{})) | ||
} | ||
|
||
func TestWhen_Matching_Error(t *testing.T) { | ||
rrMock := mocks.NewResourceRewriter(t) | ||
rrMock.EXPECT().BeforeImport(anyUnstructured).Return(fmt.Errorf("test")).Once() | ||
rrMock.EXPECT().BeforeServing(anyUnstructured).Return(fmt.Errorf("serving")).Once() | ||
assert.ErrorContains(t, rewriter.When(matchAny, rrMock).BeforeImport(&unstructured.Unstructured{}), "test") | ||
assert.ErrorContains(t, rewriter.When(matchAny, rrMock).BeforeServing(&unstructured.Unstructured{}), "serving") | ||
} | ||
|
||
func TestWhen_NoMatch(t *testing.T) { | ||
rrMock := mocks.NewResourceRewriter(t) | ||
assert.NoError(t, rewriter.When(matchNone, rrMock).BeforeImport(&unstructured.Unstructured{})) | ||
assert.NoError(t, rewriter.When(matchNone, rrMock).BeforeServing(&unstructured.Unstructured{})) | ||
} | ||
|
||
func TestCondition_MatchGVK(t *testing.T) { | ||
testCases := []struct { | ||
config schema.GroupVersionKind | ||
apiVersion string | ||
kind string | ||
expected bool | ||
}{ | ||
{ | ||
config: schema.FromAPIVersionAndKind("v1", "Pod"), | ||
apiVersion: "v1", | ||
kind: "Pod", | ||
expected: true, | ||
}, | ||
{ | ||
config: schema.FromAPIVersionAndKind("scheduling.k8s.io/v1beta1", "PriorityClass"), | ||
apiVersion: "scheduling.k8s.io/v1", | ||
kind: "PriorityClass", | ||
expected: false, | ||
}, | ||
{ | ||
config: schema.FromAPIVersionAndKind("scheduling.k8s.io/v1", "PriorityClass"), | ||
apiVersion: "scheduling.k8s.io/v1", | ||
kind: "PriorityClassDifferent", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("%s:%s/%s", tc.config, tc.apiVersion, tc.kind), func(tt *testing.T) { | ||
condition := rewriter.MatchGVK(tc.config) | ||
u := &unstructured.Unstructured{} | ||
u.SetAPIVersion(tc.apiVersion) | ||
u.SetKind(tc.kind) | ||
assert.Equal(tt, tc.expected, condition(u)) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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