Skip to content

Commit

Permalink
chore(client): add support for additional configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
shini4i committed May 25, 2024
1 parent d833f36 commit c0205fb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 23 deletions.
17 changes: 14 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

var (
clientConfig *ClientConfig
clientConfig *Config
)

type Watcher struct {
Expand Down Expand Up @@ -124,6 +124,8 @@ func (watcher *Watcher) getWatcherConfig() (*config.ServerConfig, error) {
}

func (watcher *Watcher) waitForDeployment(id, appName, version string) error {
retryCount := 0

for {
taskInfo, err := watcher.getTaskStatus(id)
if err != nil {
Expand All @@ -134,8 +136,13 @@ func (watcher *Watcher) waitForDeployment(id, appName, version string) error {
case models.StatusFailedMessage:
return fmt.Errorf("The deployment has failed, please check logs.\n%s", taskInfo.StatusReason)
case models.StatusInProgressMessage:
log.Println("Application deployment is in progress...")
time.Sleep(15 * time.Second)
if !isDeploymentOverTime(retryCount, clientConfig.RetryInterval, clientConfig.ExpectedDeploymentTime) {
log.Println("Application deployment is in progress...")
} else {
log.Println("Application deployment is taking longer than expected, it might be worth checking ArgoCD UI...")
}
retryCount++
time.Sleep(clientConfig.RetryInterval)

Check warning on line 145 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L139-L145

Added lines #L139 - L145 were not covered by tests
case models.StatusAppNotFoundMessage:
return fmt.Errorf("Application %s does not exist.\n%s", appName, taskInfo.StatusReason)
case models.StatusArgoCDUnavailableMessage:
Expand Down Expand Up @@ -163,6 +170,10 @@ func handleFatalError(err error, message string) {
log.Fatalf("%s Got the following error: %s", message, err)
}

func isDeploymentOverTime(retryCount int, retryInterval time.Duration, expectedDeploymentTime time.Duration) bool {
return time.Duration(retryCount)*retryInterval > expectedDeploymentTime
}

func Run() {
var err error

Expand Down
24 changes: 24 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,27 @@ func TestWaitForDeployment(t *testing.T) {
})
}
}

func TestIsDeploymentOverTime(t *testing.T) {
var tests = []struct {
retryCount int
retryInterval time.Duration
expectedDuration time.Duration
expected bool
}{
{10, 5 * time.Second, 1 * time.Minute, false},
{13, 5 * time.Second, 1 * time.Minute, true},
{7, 10 * time.Second, 1 * time.Minute, true},
{7, 15 * time.Second, 1 * time.Minute, true},
{0, 2 * time.Second, 1 * time.Minute, false},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
result := isDeploymentOverTime(tt.retryCount, tt.retryInterval, tt.expectedDuration)
if result != tt.expected {
t.Errorf("for %d retries with %s interval, expected %t but got %t", tt.retryCount, tt.retryInterval, tt.expected, result)
}
})
}
}
30 changes: 16 additions & 14 deletions pkg/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ import (
envConfig "github.com/caarlos0/env/v10"
)

type ClientConfig struct {
Url string `env:"ARGO_WATCHER_URL,required"`
Images []string `env:"IMAGES,required"`
Tag string `env:"IMAGE_TAG,required"`
App string `env:"ARGO_APP,required"`
Author string `env:"COMMIT_AUTHOR,required"`
Project string `env:"PROJECT_NAME,required"`
Token string `env:"ARGO_WATCHER_DEPLOY_TOKEN"`
JsonWebToken string `env:"BEARER_TOKEN"`
Timeout time.Duration `env:"TIMEOUT" envDefault:"60s"`
TaskTimeout int `env:"TASK_TIMEOUT"`
Debug bool `env:"DEBUG"`
type Config struct {
Url string `env:"ARGO_WATCHER_URL,required"`
Images []string `env:"IMAGES,required"`
Tag string `env:"IMAGE_TAG,required"`
App string `env:"ARGO_APP,required"`
Author string `env:"COMMIT_AUTHOR,required"`
Project string `env:"PROJECT_NAME,required"`
Token string `env:"ARGO_WATCHER_DEPLOY_TOKEN"`
JsonWebToken string `env:"BEARER_TOKEN"`
Timeout time.Duration `env:"TIMEOUT" envDefault:"60s"`
TaskTimeout int `env:"TASK_TIMEOUT"`
RetryInterval time.Duration `env:"RETRY_INTERVAL" envDefault:"15s"`
ExpectedDeploymentTime time.Duration `env:"EXPECTED_DEPLOY_TIME" envDefault:"15m"`
Debug bool `env:"DEBUG"`
}

func NewClientConfig() (*ClientConfig, error) {
var config ClientConfig
func NewClientConfig() (*Config, error) {
var config Config

if err := envConfig.Parse(&config); err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getImagesList(list []string, tag string) []models.Image {
return images
}

func createTask(config *ClientConfig) models.Task {
func createTask(config *Config) models.Task {
images := getImagesList(config.Images, config.Tag)
return models.Task{
App: config.App,
Expand Down Expand Up @@ -86,7 +86,7 @@ func generateAppUrl(watcher *Watcher, task models.Task) (string, error) {
return fmt.Sprintf("%s://%s/applications/%s", cfg.ArgoUrl.Scheme, cfg.ArgoUrl.Host, task.App), nil
}

func setupWatcher(config *ClientConfig) *Watcher {
func setupWatcher(config *Config) *Watcher {
return NewWatcher(
strings.TrimSuffix(config.Url, "/"),
config.Debug,
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestGetImagesList(t *testing.T) {

func TestCreateTask(t *testing.T) {
t.Run("TimeoutProvided", func(t *testing.T) {
config := &ClientConfig{
config := &Config{
App: "test-app",
Author: "test-author",
Project: "test-project",
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestCreateTask(t *testing.T) {
})

t.Run("TimeoutNotProvided", func(t *testing.T) {
config := &ClientConfig{
config := &Config{
App: "test-app",
Author: "test-author",
Project: "test-project",
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestCreateTask(t *testing.T) {

func TestPrintClientConfiguration(t *testing.T) {
// Initialize clientConfig
clientConfig = &ClientConfig{
clientConfig = &Config{
Url: "http://localhost:8080",
Images: []string{"image1", "image2"},
Tag: "test-tag",
Expand Down Expand Up @@ -334,7 +334,7 @@ func TestGenerateAppUrl(t *testing.T) {

func TestSetupWatcher(t *testing.T) {
// Define the input
config := &ClientConfig{
config := &Config{
Url: "http://localhost:8080",
Debug: true,
}
Expand Down

0 comments on commit c0205fb

Please sign in to comment.