Skip to content

Commit

Permalink
fix: remove unneeded DOCKER_API_VERSION env var (#193)
Browse files Browse the repository at this point in the history
* fix: remove unneeded DOCKER_API_VERSION env var

* fix: remove unneeded DOCKER_API_VERSION env var

* docs: update wiki
  • Loading branch information
kimdre authored Nov 6, 2024
1 parent e71339d commit 333c378
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/doco-cd/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (h *handlerData) WebhookHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *handlerData) HealthCheckHandler(w http.ResponseWriter, _ *http.Request) {
err := docker.VerifySocketConnection(h.appConfig.DockerAPIVersion)
err := docker.VerifySocketConnection()
if err != nil {
h.log.Error(docker.ErrDockerSocketConnectionFailed.Error(), logger.ErrAttr(err))
JSONError(w, "unhealthy", err.Error(), "", http.StatusServiceUnavailable)
Expand Down
2 changes: 1 addition & 1 deletion cmd/doco-cd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
log.Info("starting application", slog.String("version", Version), slog.String("log_level", c.LogLevel))

// Test/verify the connection to the docker socket
err = docker.VerifySocketConnection(c.DockerAPIVersion)
err = docker.VerifySocketConnection()
if err != nil {
log.Critical(docker.ErrDockerSocketConnectionFailed.Error(), logger.ErrAttr(err))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/doco-cd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestHandleEvent(t *testing.T) {
}
})

err = docker.VerifySocketConnection(appConfig.DockerAPIVersion)
err = docker.VerifySocketConnection()
if err != nil {
t.Fatalf("Failed to verify docker socket connection: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs
Submodule docs updated from a41ebb to 592180
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/caarlos0/env/v11 v11.2.2
github.com/compose-spec/compose-go/v2 v2.4.3
github.com/creasty/defaults v1.8.0
github.com/docker/cli 8c22315e3123
github.com/docker/cli v27.3.2-0.20241008150905-cb3048fbebb1+incompatible
github.com/docker/compose/v2 v2.30.1
github.com/docker/docker v27.3.1+incompatible
github.com/go-git/go-git/v5 v5.12.0
Expand Down
24 changes: 8 additions & 16 deletions internal/config/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ import (

// AppConfig is used to configure this application
type AppConfig struct {
LogLevel string `env:"LOG_LEVEL,required" envDefault:"info"` // LogLevel is the log level for the application
HttpPort uint16 `env:"HTTP_PORT,required" envDefault:"80" validate:"min=1,max=65535"` // HttpPort is the port the HTTP server will listen on
WebhookSecret string `env:"WEBHOOK_SECRET,required"` // WebhookSecret is the secret used to authenticate the webhook
GitAccessToken string `env:"GIT_ACCESS_TOKEN"` // GitAccessToken is the access token used to authenticate with the Git server (e.g. GitHub) for private repositories
AuthType string `env:"AUTH_TYPE" envDefault:"oauth2"` // AuthType is the type of authentication to use when cloning repositories
SkipTLSVerification bool `env:"SKIP_TLS_VERIFICATION" envDefault:"false"` // SkipTLSVerification skips the TLS verification when cloning repositories.
DockerAPIVersion string `env:"DOCKER_API_VERSION" envDefault:"v1.40" validate:"regexp=^v[0-9]+\\.[0-9]+$"` // DockerAPIVersion is the version of the Docker API to use
DockerQuietDeploy bool `env:"DOCKER_QUIET_DEPLOY" envDefault:"true"` // DockerQuietDeploy suppresses the status output of dockerCli in deployments (e.g. pull, create, start)
LogLevel string `env:"LOG_LEVEL,required" envDefault:"info"` // LogLevel is the log level for the application
HttpPort uint16 `env:"HTTP_PORT,required" envDefault:"80" validate:"min=1,max=65535"` // HttpPort is the port the HTTP server will listen on
WebhookSecret string `env:"WEBHOOK_SECRET,required"` // WebhookSecret is the secret used to authenticate the webhook
GitAccessToken string `env:"GIT_ACCESS_TOKEN"` // GitAccessToken is the access token used to authenticate with the Git server (e.g. GitHub) for private repositories
AuthType string `env:"AUTH_TYPE" envDefault:"oauth2"` // AuthType is the type of authentication to use when cloning repositories
SkipTLSVerification bool `env:"SKIP_TLS_VERIFICATION" envDefault:"false"` // SkipTLSVerification skips the TLS verification when cloning repositories.
DockerQuietDeploy bool `env:"DOCKER_QUIET_DEPLOY" envDefault:"true"` // DockerQuietDeploy suppresses the status output of dockerCli in deployments (e.g. pull, create, start)
}

var (
ErrInvalidLogLevel = validator.TextErr{Err: errors.New("invalid log level, must be one of debug, info, warn, error")}
ErrInvalidDockerAPIVersion = validator.TextErr{Err: errors.New("invalid Docker API version format, must be e.g. v1.40")}
)
var ErrInvalidLogLevel = validator.TextErr{Err: errors.New("invalid log level, must be one of debug, info, warn, error")}

// GetAppConfig returns the configuration
func GetAppConfig() (*AppConfig, error) {
Expand All @@ -38,10 +34,6 @@ func GetAppConfig() (*AppConfig, error) {
}

if err := validator.Validate(cfg); err != nil {
if strings.Contains(err.Error(), "DockerAPIVersion") {
return nil, ErrInvalidDockerAPIVersion
}

return nil, err
}

Expand Down
10 changes: 0 additions & 10 deletions internal/config/app_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func TestGetAppConfig(t *testing.T) {
"HTTP_PORT": "8080",
"WEBHOOK_SECRET": "secret",
"AUTH_TYPE": "oauth2",
"DOCKER_API_VERSION": "v1.40",
"GIT_ACCESS_TOKEN": "token",
"SKIP_TLS_VERIFICATION": "false",
},
Expand All @@ -34,15 +33,6 @@ func TestGetAppConfig(t *testing.T) {
},
expectedErr: ErrInvalidLogLevel,
},
{
name: "invalid API version",
envVars: map[string]string{
"LOG_LEVEL": "info",
"WEBHOOK_SECRET": "secret",
"DOCKER_API_VERSION": "1.40",
},
expectedErr: ErrInvalidDockerAPIVersion,
},
}

for _, tt := range tests {
Expand Down
8 changes: 4 additions & 4 deletions internal/docker/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ func NewHttpClient() *http.Client {
}

// VerifySocketRead verifies whether the application can read from the docker socket
func VerifySocketRead(httpClient *http.Client, apiVersion string) error {
func VerifySocketRead(httpClient *http.Client) error {
reqBody, err := json.Marshal("")
if err != nil {
return err
}

req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost/%s/info", apiVersion), bytes.NewReader(reqBody))
req, err := http.NewRequest("GET", "http://localhost/info", bytes.NewReader(reqBody))
if err != nil {
return err
}
Expand All @@ -97,7 +97,7 @@ func VerifySocketRead(httpClient *http.Client, apiVersion string) error {
}

// VerifySocketConnection verifies whether the application can connect to the docker socket
func VerifySocketConnection(apiVersion string) error {
func VerifySocketConnection() error {
// Check if the docker socket file exists
if _, err := os.Stat("/var/run/docker.sock"); errors.Is(err, os.ErrNotExist) {
return err
Expand All @@ -120,7 +120,7 @@ func VerifySocketConnection(apiVersion string) error {
httpClient := NewHttpClient()
defer httpClient.CloseIdleConnections()

err = VerifySocketRead(httpClient, apiVersion)
err = VerifySocketRead(httpClient)
if err != nil {
return err
}
Expand Down
9 changes: 2 additions & 7 deletions internal/docker/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ var (
)

func TestVerifySocketConnection(t *testing.T) {
c, err := config.GetAppConfig()
if err != nil {
t.Fatal(err)
}

err = VerifySocketConnection(c.DockerAPIVersion)
err := VerifySocketConnection()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -110,7 +105,7 @@ func TestDeployCompose(t *testing.T) {

t.Log("Verify socket connection")

err = VerifySocketConnection(c.DockerAPIVersion)
err = VerifySocketConnection()
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 333c378

Please sign in to comment.