Skip to content

Commit

Permalink
fix: sort secrets before filter (#498) (#558)
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
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 0 deletions.
64 changes: 64 additions & 0 deletions command/options/rest_client_getter.go
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
}
128 changes: 128 additions & 0 deletions command/options/rest_client_getter_test.go
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"))
})

})

})
4 changes: 4 additions & 0 deletions secret/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func selectToolSecret(logger *zap.SugaredLogger, secretList []corev1.Secret, glo
if len(usableSecrets) == 0 {
return nil, nil
}

// sort secrets to reduce the chance of occasional bugs causing by random order
sort.Sort(SortedSecretList(usableSecrets))

find, secretIndex := findPreferredSecret(usableSecrets, option.PerferredSecret.Namespace, option.PerferredSecret.Name)
if find {
return &usableSecrets[secretIndex], nil
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ad0aff2

Please sign in to comment.