-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix: sort secrets before filter Signed-off-by: jzli <[email protected]>
- Loading branch information
Kinso
authored
Mar 15, 2024
1 parent
7d3bd3b
commit ad0aff2
Showing
5 changed files
with
390 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright 2023 The Katanomi Authors. | ||
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 options | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
//go:generate mockgen -destination=../../testing/mock/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go -package=genericclioptions k8s.io/cli-runtime/pkg/genericclioptions RESTClientGetter | ||
//go:generate mockgen -destination=../../testing/mock/k8s.io/client-go/tools/clientcmd/client_config.go -package=clientcmd k8s.io/client-go/tools/clientcmd ClientConfig | ||
|
||
// RESTClientGetterOption is the generic client option for k8s client | ||
type RESTClientGetterOption struct { | ||
// ConfigFlags for interface of genericclioptions.ConfigFlags | ||
ConfigFlag genericclioptions.RESTClientGetter | ||
} | ||
|
||
// Setup is the setup function for RESTClientGetterOption | ||
func (m *RESTClientGetterOption) Setup(ctx context.Context, cmd *cobra.Command, args []string) (err error) { | ||
m.ConfigFlag = &genericclioptions.ConfigFlags{} | ||
return nil | ||
} | ||
|
||
// GetClusterToken get token for kubeconfig. | ||
func (m *RESTClientGetterOption) GetClusterToken(ctx context.Context) (string, error) { | ||
config, err := m.ConfigFlag.ToRESTConfig() | ||
if err != nil { | ||
return "", fmt.Errorf("get kubeconfig token failed, error is: %v", err) | ||
} | ||
if config.BearerToken != "" { | ||
return config.BearerToken, nil | ||
} | ||
config, err = rest.InClusterConfig() | ||
if err != nil { | ||
return "", fmt.Errorf("get kubeconfig token from cluster failed, error is: %v", err) | ||
} | ||
return config.BearerToken, nil | ||
} | ||
|
||
// GetNamespace get namespace from environment or incluster kubeconfig | ||
func (m *RESTClientGetterOption) GetNamespace() (string, error) { | ||
cfgLoader := m.ConfigFlag.ToRawKubeConfigLoader() | ||
ns, _, err := cfgLoader.Namespace() | ||
return ns, err | ||
} |
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,128 @@ | ||
/* | ||
Copyright 2023 The Katanomi Authors. | ||
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 options | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/golang/mock/gomock" | ||
mockopt "github.com/katanomi/pkg/testing/mock/k8s.io/cli-runtime/pkg/genericclioptions" | ||
mockclientcmd "github.com/katanomi/pkg/testing/mock/k8s.io/client-go/tools/clientcmd" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/spf13/cobra" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
var _ = Describe("Test.RESTClientGetterOption.Setup", func() { | ||
|
||
var ( | ||
ctx context.Context | ||
opt *RESTClientGetterOption | ||
) | ||
|
||
BeforeEach(func() { | ||
ctx = context.Background() | ||
opt = &RESTClientGetterOption{} | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
opt.Setup(ctx, &cobra.Command{}, []string{}) | ||
}) | ||
It("init configFlag", func() { | ||
Expect(opt.ConfigFlag).NotTo(BeNil()) | ||
}) | ||
|
||
}) | ||
|
||
var _ = Describe("Test.RESTClientGetterOption.Funcs", func() { | ||
|
||
var ( | ||
ctx context.Context | ||
opt *RESTClientGetterOption | ||
mockCfgFlag *mockopt.MockRESTClientGetter | ||
mockCliCfg *mockclientcmd.MockClientConfig | ||
err error | ||
ret string | ||
) | ||
|
||
BeforeEach(func() { | ||
ctx = context.Background() | ||
opt = &RESTClientGetterOption{} | ||
mockCtl := gomock.NewController(GinkgoT()) | ||
mockCfgFlag = mockopt.NewMockRESTClientGetter(mockCtl) | ||
mockCliCfg = mockclientcmd.NewMockClientConfig(mockCtl) | ||
opt.ConfigFlag = mockCfgFlag | ||
|
||
err = nil | ||
ret = "" | ||
}) | ||
|
||
When("GetClusterToken", func() { | ||
|
||
JustBeforeEach(func() { | ||
ret, err = opt.GetClusterToken(ctx) | ||
}) | ||
|
||
Context("get kubeconfig failed", func() { | ||
expErr := fmt.Errorf("foo err") | ||
BeforeEach(func() { | ||
mockCfgFlag.EXPECT().ToRESTConfig().Return(nil, expErr) | ||
}) | ||
|
||
It("returns empty token and err", func() { | ||
Expect(ret).To(BeEmpty()) | ||
Expect(err).Should(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring(expErr.Error())) | ||
}) | ||
}) | ||
|
||
Context("kubeconfig contains bearer token", func() { | ||
BeforeEach(func() { | ||
mockCfgFlag.EXPECT().ToRESTConfig().Return(&rest.Config{ | ||
BearerToken: "xxx", | ||
}, nil) | ||
}) | ||
|
||
It("return token", func() { | ||
Expect(err).To(BeNil()) | ||
Expect(ret).To(Equal("xxx")) | ||
}) | ||
}) | ||
}) | ||
|
||
When("GetNamespace", func() { | ||
BeforeEach(func() { | ||
|
||
mockCfgFlag.EXPECT().ToRawKubeConfigLoader().Return(mockCliCfg) | ||
mockCliCfg.EXPECT().Namespace().Return("yyy", false, nil) | ||
|
||
}) | ||
JustBeforeEach(func() { | ||
ret, err = opt.GetNamespace() | ||
}) | ||
|
||
It("returns namespace result", func() { | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(ret).To(Equal("yyy")) | ||
}) | ||
|
||
}) | ||
|
||
}) |
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
97 changes: 97 additions & 0 deletions
97
testing/mock/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.