Skip to content

Commit 6b81bdb

Browse files
authored
refactor: Move file from interfaces to authtoken package directories and add comments (#938)
* move interfaces.go from interfaces to authtoken/providers directory and add commenting * Edit comments * Rename structs, add periods to comments and update AuthToken comment * remove pkg/interfaces to be copied from go source * Ran goimports -w on files in pkg/authtoken/providers/ directory * Elaborate commenting for documentation * Reorder imports by moving fleet pkgs to the bottom
1 parent a174370 commit 6b81bdb

File tree

10 files changed

+57
-60
lines changed

10 files changed

+57
-60
lines changed

cmd/authtoken/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ import (
1616
"go.goms.io/fleet/pkg/authtoken"
1717
"go.goms.io/fleet/pkg/authtoken/providers/azure"
1818
"go.goms.io/fleet/pkg/authtoken/providers/secret"
19-
"go.goms.io/fleet/pkg/interfaces"
2019
)
2120

2221
var (
2322
configPath string
2423
)
2524

26-
func parseArgs() (interfaces.AuthTokenProvider, error) {
27-
var tokenProvider interfaces.AuthTokenProvider
25+
func parseArgs() (authtoken.Provider, error) {
26+
var tokenProvider authtoken.Provider
2827
rootCmd := &cobra.Command{Use: "refreshtoken", Args: cobra.NoArgs}
2928
rootCmd.PersistentFlags().StringVar(&configPath, "file-path", "/config/token", "token file path")
3029

docker/refresh-token.Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ RUN go mod download
1212
# Copy the go source
1313
COPY cmd/authtoken/main.go main.go
1414
COPY pkg/authtoken pkg/authtoken
15-
COPY pkg/interfaces pkg/interfaces
1615

1716
ARG TARGETARCH
1817

pkg/authtoken/interfaces.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (c) Microsoft Corporation.
3+
Licensed under the MIT license.
4+
*/
5+
package authtoken
6+
7+
import (
8+
"context"
9+
"time"
10+
)
11+
12+
// An AuthToken is an authentication token used to communicate with the hub API server.
13+
type AuthToken struct {
14+
Token string // The authentication token string.
15+
ExpiresOn time.Time // The expiration time of the token.
16+
}
17+
18+
// Provider defines a method for fetching an authentication token.
19+
type Provider interface {
20+
// FetchToken fetches an authentication token to make requests to its associated fleet's hub cluster.
21+
// It returns the token for a given input context, or an error if the retrieval fails.
22+
FetchToken(ctx context.Context) (AuthToken, error)
23+
}
24+
25+
// Writer defines a method for writing an authentication token to a specified location.
26+
type Writer interface {
27+
// WriteToken writes the provided authentication token to a filepath location specified in a TokenWriter.
28+
// It returns an error if the writing process fails.
29+
WriteToken(token AuthToken) error
30+
}

pkg/authtoken/providers/azure/azure_msi.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"k8s.io/client-go/util/retry"
1515
"k8s.io/klog/v2"
1616

17-
"go.goms.io/fleet/pkg/interfaces"
17+
"go.goms.io/fleet/pkg/authtoken"
1818
)
1919

2020
const (
@@ -26,7 +26,7 @@ type AuthTokenProvider struct {
2626
Scope string
2727
}
2828

29-
func New(clientID, scope string) interfaces.AuthTokenProvider {
29+
func New(clientID, scope string) authtoken.Provider {
3030
if scope == "" {
3131
scope = aksScope
3232
}
@@ -37,8 +37,8 @@ func New(clientID, scope string) interfaces.AuthTokenProvider {
3737
}
3838

3939
// FetchToken gets a new token to make request to the associated fleet' hub cluster.
40-
func (a *AuthTokenProvider) FetchToken(ctx context.Context) (interfaces.AuthToken, error) {
41-
token := interfaces.AuthToken{}
40+
func (a *AuthTokenProvider) FetchToken(ctx context.Context) (authtoken.AuthToken, error) {
41+
token := authtoken.AuthToken{}
4242
opts := &azidentity.ManagedIdentityCredentialOptions{ID: azidentity.ClientID(a.ClientID)}
4343

4444
klog.V(2).InfoS("FetchToken", "client ID", a.ClientID)

pkg/authtoken/providers/secret/k8s_secret.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
ctrl "sigs.k8s.io/controller-runtime"
1717
"sigs.k8s.io/controller-runtime/pkg/client"
1818

19-
"go.goms.io/fleet/pkg/interfaces"
19+
"go.goms.io/fleet/pkg/authtoken"
2020
)
2121

2222
var (
@@ -29,7 +29,7 @@ type secretAuthTokenProvider struct {
2929
secretNamespace string
3030
}
3131

32-
func New(secretName, namespace string) (interfaces.AuthTokenProvider, error) {
32+
func New(secretName, namespace string) (authtoken.Provider, error) {
3333
client, err := getClient()
3434
if err != nil {
3535
return nil, fmt.Errorf("an error occurred will creating client: %w", err)
@@ -41,9 +41,9 @@ func New(secretName, namespace string) (interfaces.AuthTokenProvider, error) {
4141
}, nil
4242
}
4343

44-
func (s *secretAuthTokenProvider) FetchToken(ctx context.Context) (interfaces.AuthToken, error) {
44+
func (s *secretAuthTokenProvider) FetchToken(ctx context.Context) (authtoken.AuthToken, error) {
4545
klog.V(2).InfoS("fetching token from secret", "secret", klog.KRef(s.secretName, s.secretNamespace))
46-
token := interfaces.AuthToken{}
46+
token := authtoken.AuthToken{}
4747
secret, err := s.fetchSecret(ctx)
4848
if err != nil {
4949
return token, fmt.Errorf("cannot get the secret: %w", err)

pkg/authtoken/token_refresher.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ import (
1010
"time"
1111

1212
"k8s.io/klog/v2"
13-
14-
"go.goms.io/fleet/pkg/interfaces"
1513
)
1614

17-
type RefreshDurationFuncType func(token interfaces.AuthToken) time.Duration
15+
type RefreshDurationFuncType func(token AuthToken) time.Duration
1816
type CreateTickerFuncType func(time.Duration) <-chan time.Time
1917

2018
type Refresher struct {
21-
provider interfaces.AuthTokenProvider
22-
writer interfaces.AuthTokenWriter
19+
provider Provider
20+
writer Writer
2321
refreshCalculate RefreshDurationFuncType
2422
createTicker CreateTickerFuncType
2523
}
2624

27-
func NewAuthTokenRefresher(tokenProvider interfaces.AuthTokenProvider,
28-
writer interfaces.AuthTokenWriter,
25+
func NewAuthTokenRefresher(tokenProvider Provider,
26+
writer Writer,
2927
refreshCalculate RefreshDurationFuncType,
3028
createTicker CreateTickerFuncType) *Refresher {
3129
return &Refresher{
@@ -37,14 +35,14 @@ func NewAuthTokenRefresher(tokenProvider interfaces.AuthTokenProvider,
3735
}
3836

3937
var (
40-
DefaultRefreshDurationFunc = func(token interfaces.AuthToken) time.Duration {
38+
DefaultRefreshDurationFunc = func(token AuthToken) time.Duration {
4139
return time.Until(token.ExpiresOn) / 2
4240
}
4341
DefaultCreateTicker = time.Tick
4442
DefaultRefreshDuration = time.Second * 30
4543
)
4644

47-
func (at *Refresher) callFetchToken(ctx context.Context) (interfaces.AuthToken, error) {
45+
func (at *Refresher) callFetchToken(ctx context.Context) (AuthToken, error) {
4846
klog.V(2).InfoS("FetchToken start")
4947
deadline := time.Now().Add(DefaultRefreshDuration)
5048
fetchTokenContext, cancel := context.WithDeadline(ctx, deadline)

pkg/authtoken/token_refresher_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@ import (
1212
"time"
1313

1414
"github.com/stretchr/testify/assert"
15-
16-
"go.goms.io/fleet/pkg/interfaces"
1715
)
1816

1917
type MockAuthTokenProvider struct {
20-
Token interfaces.AuthToken
18+
Token AuthToken
2119
}
2220

23-
func (m MockAuthTokenProvider) FetchToken(_ context.Context) (interfaces.AuthToken, error) {
21+
func (m MockAuthTokenProvider) FetchToken(_ context.Context) (AuthToken, error) {
2422
return m.Token, nil
2523
}
2624

2725
// TestRefreshTokenOnce test to refresh/rewrite token for one time
2826
func TestRefreshTokenOnce(t *testing.T) {
2927
provider := MockAuthTokenProvider{
30-
Token: interfaces.AuthToken{
28+
Token: AuthToken{
3129
Token: "test token",
3230
ExpiresOn: time.Now(),
3331
},
@@ -60,7 +58,7 @@ func TestRefreshTokenOnce(t *testing.T) {
6058
// TestRefreshToken test to refresh/rewrite token multiple times
6159
func TestRefreshToken(t *testing.T) {
6260
provider := MockAuthTokenProvider{
63-
Token: interfaces.AuthToken{
61+
Token: AuthToken{
6462
Token: "test token",
6563
ExpiresOn: time.Now(),
6664
},
@@ -99,7 +97,7 @@ func TestRefreshToken(t *testing.T) {
9997
// TestRefresherCancelContext test if the func will be canceled/returned once the ctx is canceled
10098
func TestRefresherCancelContext(t *testing.T) {
10199
provider := MockAuthTokenProvider{
102-
Token: interfaces.AuthToken{
100+
Token: AuthToken{
103101
Token: "test token",
104102
ExpiresOn: time.Now().Add(100 * time.Millisecond),
105103
},

pkg/authtoken/token_writer.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"os"
1111

1212
"k8s.io/klog/v2"
13-
14-
"go.goms.io/fleet/pkg/interfaces"
1513
)
1614

1715
type Factory struct {
@@ -30,17 +28,17 @@ func (w Factory) Create() (io.WriteCloser, error) {
3028
return wc, nil
3129
}
3230

33-
type Writer struct {
31+
type TokenWriter struct {
3432
writerFactory func() (io.WriteCloser, error)
3533
}
3634

37-
func NewWriter(factory func() (io.WriteCloser, error)) interfaces.AuthTokenWriter {
38-
return &Writer{
35+
func NewWriter(factory func() (io.WriteCloser, error)) Writer {
36+
return &TokenWriter{
3937
writerFactory: factory,
4038
}
4139
}
4240

43-
func (w *Writer) WriteToken(token interfaces.AuthToken) error {
41+
func (w *TokenWriter) WriteToken(token AuthToken) error {
4442
writer, err := w.writerFactory()
4543
if err != nil {
4644
return err

pkg/authtoken/token_writer_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/stretchr/testify/assert"
14-
15-
"go.goms.io/fleet/pkg/interfaces"
1614
)
1715

1816
type BufferWriterFactory struct {
@@ -43,7 +41,7 @@ func (c BufferWriter) Close() error {
4341
}
4442

4543
func TestWriteToken(t *testing.T) {
46-
token := interfaces.AuthToken{
44+
token := AuthToken{
4745
Token: "test token",
4846
ExpiresOn: time.Now(),
4947
}

pkg/interfaces/interfaces.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)