Skip to content

Commit

Permalink
Merge pull request #90 from PrasadG193/init-from-incluster
Browse files Browse the repository at this point in the history
Correct kubeclient init and parameters in snapshot-metadata-lister
  • Loading branch information
k8s-ci-robot authored Dec 19, 2024
2 parents c81d89e + 1995910 commit 91ffa4a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
12 changes: 11 additions & 1 deletion examples/snapshot-metadata-lister/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os/signal"
"path/filepath"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"

Expand Down Expand Up @@ -132,7 +133,7 @@ func main() {
parseFlags()

// get the K8s config from either kubeConfig, in-cluster or default
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
config, err := buildConfig(kubeConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading kubeconfig %s: %v\n", kubeConfig, err)
os.Exit(1)
Expand All @@ -156,3 +157,12 @@ func main() {

os.Exit(0)
}

func buildConfig(kubeconfigPath string) (*rest.Config, error) {
// If kubeconfig exists, try from kubeconfig file
if _, err := os.Stat(kubeconfigPath); err == nil {
return clientcmd.BuildConfigFromFlags("", kubeconfigPath)
}
// try in-cluster config
return rest.InClusterConfig()
}
4 changes: 2 additions & 2 deletions pkg/iterator/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (th *testHarness) SnapshotMetadataIteratorDone(numberRecords int) {
// fake helpers
func (th *testHarness) getDefaultServiceAccount(ctx context.Context) (string, string, error) {
th.CalledGetDefaultServiceAccount = true
return th.RetGetDefaultSAName, th.RetGetDefaultSANamespace, th.RetGetDefaultServiceAccountErr
return th.RetGetDefaultSANamespace, th.RetGetDefaultSAName, th.RetGetDefaultServiceAccountErr
}

func (th *testHarness) getCSIDriverFromPrimarySnapshot(ctx context.Context) (string, error) {
Expand All @@ -271,7 +271,7 @@ func (th *testHarness) getSnapshotMetadataServiceCR(ctx context.Context, csiDriv
return th.RetGetSnapshotMetadataServiceCRService, th.RetGetSnapshotMetadataServiceCRErr
}

func (th *testHarness) createSecurityToken(ctx context.Context, saName, saNamespace, audience string) (string, error) {
func (th *testHarness) createSecurityToken(ctx context.Context, saNamespace, saName, audience string) (string, error) {
th.InCreateSecurityTokenSAName = saName
th.InCreateSecurityTokenSANamespace = saNamespace
th.InCreateSecurityTokenAudience = audience
Expand Down
4 changes: 2 additions & 2 deletions pkg/iterator/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type iterator struct {

type iteratorHelpers interface {
getCSIDriverFromPrimarySnapshot(ctx context.Context) (string, error)
getDefaultServiceAccount(ctx context.Context) (string, string, error)
getDefaultServiceAccount(ctx context.Context) (saNamespace string, saName string, err error)
getSnapshotMetadataServiceCR(ctx context.Context, csiDriver string) (*smsCRv1alpha1.SnapshotMetadataService, error)
createSecurityToken(ctx context.Context, saNamespace, saName, audience string) (string, error)
getGRPCClient(caCert []byte, URL string) (api.SnapshotMetadataClient, error)
Expand Down Expand Up @@ -296,7 +296,7 @@ func (iter *iterator) getSnapshotMetadataServiceCR(ctx context.Context, csiDrive

// createSecurityToken will create a security token for the specified storage
// account using the audience string from the SnapshotMetadataService CR.
func (iter *iterator) createSecurityToken(ctx context.Context, sa, saNamespace, audience string) (string, error) {
func (iter *iterator) createSecurityToken(ctx context.Context, saNamespace, sa, audience string) (string, error) {
tokenRequest := authv1.TokenRequest{
Spec: authv1.TokenRequestSpec{
Audiences: []string{audience},
Expand Down
4 changes: 2 additions & 2 deletions pkg/iterator/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func TestCreateSecurityToken(t *testing.T) {
th := newTestHarness()
iter := th.NewTestIterator()

securityToken, err := iter.createSecurityToken(context.Background(), th.SAName, th.SANamespace, th.Audience)
securityToken, err := iter.createSecurityToken(context.Background(), th.SANamespace, th.SAName, th.Audience)
assert.Error(t, err)
assert.ErrorContains(t, err, "ServiceAccounts.CreateToken")
assert.Empty(t, securityToken)
Expand All @@ -503,7 +503,7 @@ func TestCreateSecurityToken(t *testing.T) {
return true, th.FakeTokenRequest(), nil
})

securityToken, err := iter.createSecurityToken(context.Background(), th.SAName, th.SANamespace, th.Audience)
securityToken, err := iter.createSecurityToken(context.Background(), th.SANamespace, th.SAName, th.Audience)
assert.NoError(t, err)
assert.Equal(t, th.SecurityToken, securityToken)
})
Expand Down

0 comments on commit 91ffa4a

Please sign in to comment.