Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: eliminate go-mpatch dependency (#9045)
* chore: eliminate go-mpatch dependency Signed-off-by: Michael Crenshaw <michael@crenshaw.dev> * chore: abstract out resource list function Signed-off-by: Michael Crenshaw <michael@crenshaw.dev> * chore: don't exit the program in anything but the main function Signed-off-by: Michael Crenshaw <michael@crenshaw.dev> * chore: better error messages Signed-off-by: Michael Crenshaw <michael@crenshaw.dev> * chore: better error messages Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
- Loading branch information
1 parent
92f53ae
commit 2f4afe8
Showing
4 changed files
with
38 additions
and
65 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 |
---|---|---|
@@ -1,63 +1,20 @@ | ||
package admin | ||
|
||
import ( | ||
"reflect" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/undefinedlabs/go-mpatch" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/discovery" | ||
restclient "k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func TestProjectAllowListGen(t *testing.T) { | ||
// go-mpatch only works on platforms with amd64 architecture | ||
if runtime.GOARCH != "amd64" { | ||
t.Skip() | ||
res := metav1.APIResource{ | ||
Name: "services", | ||
Kind: "Service", | ||
} | ||
resourceList := []*metav1.APIResourceList{{APIResources: []metav1.APIResource{res}}} | ||
|
||
useMock := true | ||
rules := clientcmd.NewDefaultClientConfigLoadingRules() | ||
overrides := &clientcmd.ConfigOverrides{} | ||
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides) | ||
|
||
if useMock { | ||
var patchClientConfig *mpatch.Patch | ||
patchClientConfig, err := mpatch.PatchInstanceMethodByName(reflect.TypeOf(clientConfig), "ClientConfig", func(*clientcmd.DeferredLoadingClientConfig) (*restclient.Config, error) { | ||
return nil, nil | ||
}) | ||
assert.NoError(t, err) | ||
|
||
patch, err := mpatch.PatchMethod(discovery.NewDiscoveryClientForConfig, func(c *restclient.Config) (*discovery.DiscoveryClient, error) { | ||
return &discovery.DiscoveryClient{LegacyPrefix: "/api"}, nil | ||
}) | ||
assert.NoError(t, err) | ||
|
||
var patchSeverPreferredResources *mpatch.Patch | ||
discoClient := &discovery.DiscoveryClient{} | ||
patchSeverPreferredResources, err = mpatch.PatchInstanceMethodByName(reflect.TypeOf(discoClient), "ServerPreferredResources", func(*discovery.DiscoveryClient) ([]*metav1.APIResourceList, error) { | ||
res := metav1.APIResource{ | ||
Name: "services", | ||
Kind: "Service", | ||
} | ||
resourceList := []*metav1.APIResourceList{{APIResources: []metav1.APIResource{res}}} | ||
return resourceList, nil | ||
}) | ||
assert.NoError(t, err) | ||
|
||
defer func() { | ||
err = patchClientConfig.Unpatch() | ||
assert.NoError(t, err) | ||
err = patch.Unpatch() | ||
assert.NoError(t, err) | ||
err = patchSeverPreferredResources.Unpatch() | ||
err = patch.Unpatch() | ||
}() | ||
} | ||
|
||
globalProj := generateProjectAllowList(clientConfig, "testdata/test_clusterrole.yaml", "testproj") | ||
globalProj, err := generateProjectAllowList(resourceList, "testdata/test_clusterrole.yaml", "testproj") | ||
assert.NoError(t, err) | ||
assert.True(t, len(globalProj.Spec.NamespaceResourceWhitelist) > 0) | ||
} |
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