Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move file from interfaces to authtoken package directories and add comments #938

Merged
merged 7 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/authtoken/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import (
"go.goms.io/fleet/pkg/authtoken"
"go.goms.io/fleet/pkg/authtoken/providers/azure"
"go.goms.io/fleet/pkg/authtoken/providers/secret"
"go.goms.io/fleet/pkg/interfaces"
)

var (
configPath string
)

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

Expand Down
1 change: 0 additions & 1 deletion docker/refresh-token.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ RUN go mod download
# Copy the go source
COPY cmd/authtoken/main.go main.go
COPY pkg/authtoken pkg/authtoken
COPY pkg/interfaces pkg/interfaces

ARG TARGETARCH

Expand Down
30 changes: 30 additions & 0 deletions pkg/authtoken/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/
package authtoken

import (
"context"
"time"
)

// An AuthToken is an authentication token used to communicate with the hub API server.
type AuthToken struct {
Token string // The authentication token string.
ExpiresOn time.Time // The expiration time of the token.
}

// Provider defines a method for fetching an authentication token.
type Provider interface {
// FetchToken fetches an authentication token to make requests to its associated fleet's hub cluster.
// It returns the token for a given input context, or an error if the retrieval fails.
FetchToken(ctx context.Context) (AuthToken, error)
}

// Writer defines a method for writing an authentication token to a specified location.
type Writer interface {
// WriteToken writes the provided authentication token to a filepath location specified in a TokenWriter.
// It returns an error if the writing process fails.
WriteToken(token AuthToken) error
}
8 changes: 4 additions & 4 deletions pkg/authtoken/providers/azure/azure_msi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"

"go.goms.io/fleet/pkg/interfaces"
"go.goms.io/fleet/pkg/authtoken"
)

const (
Expand All @@ -26,7 +26,7 @@ type AuthTokenProvider struct {
Scope string
}

func New(clientID, scope string) interfaces.AuthTokenProvider {
func New(clientID, scope string) authtoken.Provider {
if scope == "" {
scope = aksScope
}
Expand All @@ -37,8 +37,8 @@ func New(clientID, scope string) interfaces.AuthTokenProvider {
}

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

klog.V(2).InfoS("FetchToken", "client ID", a.ClientID)
Expand Down
8 changes: 4 additions & 4 deletions pkg/authtoken/providers/secret/k8s_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"go.goms.io/fleet/pkg/interfaces"
"go.goms.io/fleet/pkg/authtoken"
)

var (
Expand All @@ -29,7 +29,7 @@ type secretAuthTokenProvider struct {
secretNamespace string
}

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

func (s *secretAuthTokenProvider) FetchToken(ctx context.Context) (interfaces.AuthToken, error) {
func (s *secretAuthTokenProvider) FetchToken(ctx context.Context) (authtoken.AuthToken, error) {
klog.V(2).InfoS("fetching token from secret", "secret", klog.KRef(s.secretName, s.secretNamespace))
token := interfaces.AuthToken{}
token := authtoken.AuthToken{}
secret, err := s.fetchSecret(ctx)
if err != nil {
return token, fmt.Errorf("cannot get the secret: %w", err)
Expand Down
16 changes: 7 additions & 9 deletions pkg/authtoken/token_refresher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ import (
"time"

"k8s.io/klog/v2"

"go.goms.io/fleet/pkg/interfaces"
)

type RefreshDurationFuncType func(token interfaces.AuthToken) time.Duration
type RefreshDurationFuncType func(token AuthToken) time.Duration
type CreateTickerFuncType func(time.Duration) <-chan time.Time

type Refresher struct {
provider interfaces.AuthTokenProvider
writer interfaces.AuthTokenWriter
provider Provider
writer Writer
refreshCalculate RefreshDurationFuncType
createTicker CreateTickerFuncType
}

func NewAuthTokenRefresher(tokenProvider interfaces.AuthTokenProvider,
writer interfaces.AuthTokenWriter,
func NewAuthTokenRefresher(tokenProvider Provider,
writer Writer,
refreshCalculate RefreshDurationFuncType,
createTicker CreateTickerFuncType) *Refresher {
return &Refresher{
Expand All @@ -37,14 +35,14 @@ func NewAuthTokenRefresher(tokenProvider interfaces.AuthTokenProvider,
}

var (
DefaultRefreshDurationFunc = func(token interfaces.AuthToken) time.Duration {
DefaultRefreshDurationFunc = func(token AuthToken) time.Duration {
return time.Until(token.ExpiresOn) / 2
}
DefaultCreateTicker = time.Tick
DefaultRefreshDuration = time.Second * 30
)

func (at *Refresher) callFetchToken(ctx context.Context) (interfaces.AuthToken, error) {
func (at *Refresher) callFetchToken(ctx context.Context) (AuthToken, error) {
klog.V(2).InfoS("FetchToken start")
deadline := time.Now().Add(DefaultRefreshDuration)
fetchTokenContext, cancel := context.WithDeadline(ctx, deadline)
Expand Down
12 changes: 5 additions & 7 deletions pkg/authtoken/token_refresher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ import (
"time"

"github.com/stretchr/testify/assert"

"go.goms.io/fleet/pkg/interfaces"
)

type MockAuthTokenProvider struct {
Token interfaces.AuthToken
Token AuthToken
}

func (m MockAuthTokenProvider) FetchToken(_ context.Context) (interfaces.AuthToken, error) {
func (m MockAuthTokenProvider) FetchToken(_ context.Context) (AuthToken, error) {
return m.Token, nil
}

// TestRefreshTokenOnce test to refresh/rewrite token for one time
func TestRefreshTokenOnce(t *testing.T) {
provider := MockAuthTokenProvider{
Token: interfaces.AuthToken{
Token: AuthToken{
Token: "test token",
ExpiresOn: time.Now(),
},
Expand Down Expand Up @@ -60,7 +58,7 @@ func TestRefreshTokenOnce(t *testing.T) {
// TestRefreshToken test to refresh/rewrite token multiple times
func TestRefreshToken(t *testing.T) {
provider := MockAuthTokenProvider{
Token: interfaces.AuthToken{
Token: AuthToken{
Token: "test token",
ExpiresOn: time.Now(),
},
Expand Down Expand Up @@ -99,7 +97,7 @@ func TestRefreshToken(t *testing.T) {
// TestRefresherCancelContext test if the func will be canceled/returned once the ctx is canceled
func TestRefresherCancelContext(t *testing.T) {
provider := MockAuthTokenProvider{
Token: interfaces.AuthToken{
Token: AuthToken{
Token: "test token",
ExpiresOn: time.Now().Add(100 * time.Millisecond),
},
Expand Down
10 changes: 4 additions & 6 deletions pkg/authtoken/token_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"os"

"k8s.io/klog/v2"

"go.goms.io/fleet/pkg/interfaces"
)

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

type Writer struct {
type TokenWriter struct {
writerFactory func() (io.WriteCloser, error)
}

func NewWriter(factory func() (io.WriteCloser, error)) interfaces.AuthTokenWriter {
return &Writer{
func NewWriter(factory func() (io.WriteCloser, error)) Writer {
return &TokenWriter{
writerFactory: factory,
}
}

func (w *Writer) WriteToken(token interfaces.AuthToken) error {
func (w *TokenWriter) WriteToken(token AuthToken) error {
writer, err := w.writerFactory()
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions pkg/authtoken/token_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"time"

"github.com/stretchr/testify/assert"

"go.goms.io/fleet/pkg/interfaces"
)

type BufferWriterFactory struct {
Expand Down Expand Up @@ -43,7 +41,7 @@ func (c BufferWriter) Close() error {
}

func TestWriteToken(t *testing.T) {
token := interfaces.AuthToken{
token := AuthToken{
Token: "test token",
ExpiresOn: time.Now(),
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/interfaces/interfaces.go

This file was deleted.

Loading