Skip to content

Commit

Permalink
fix: apply a dummy default controller config
Browse files Browse the repository at this point in the history
to make garm work after the initialization, we apply a default config
for webhookURL, metadataURL and callbackURL

this config will be overwritten once a garmserverconfig CR got applied

Signed-off-by: Mario Constanti <[email protected]>
  • Loading branch information
bavarianbidi committed Nov 19, 2024
1 parent f0044a2 commit 5117e5b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -189,15 +190,23 @@ func IsUnauthenticatedError(err interface{}) bool {
if !ok {
return false
}
return apiErr.IsCode(401)
return apiErr.IsCode(http.StatusUnauthorized)
}

func IsNotFoundError(err interface{}) bool {
apiErr, ok := err.(runtime.ClientResponseStatus)
if !ok {
return false
}
return apiErr.IsCode(404)
return apiErr.IsCode(http.StatusNotFound)
}

func IsConflictError(err interface{}) bool {
apiErr, ok := err.(runtime.ClientResponseStatus)
if !ok {
return false
}
return apiErr.IsCode(http.StatusConflict)
}

type Func[T interface{}] func() (T, error)
Expand Down
24 changes: 24 additions & 0 deletions pkg/client/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package client

import (
"github.com/cloudbase/garm/client/controller"
garmcontroller "github.com/cloudbase/garm/client/controller"
"github.com/cloudbase/garm/client/controller_info"
"github.com/cloudbase/garm/params"

"github.com/mercedes-benz/garm-operator/pkg/metrics"
"github.com/mercedes-benz/garm-operator/pkg/util"
)

type ControllerClient interface {
Expand All @@ -23,12 +26,33 @@ func NewControllerClient() ControllerClient {
}
}

const (
initialMetadataURL = "https://initial.metadata.garm.cloud"
initialCallbackURL = "https://initial.callback.garm.cloud"
initialWebhookURL = "https://initial.webhook.garm.cloud"
)

func (s *controllerClient) GetControllerInfo() (*controller_info.ControllerInfoOK, error) {
return EnsureAuth(func() (*controller_info.ControllerInfoOK, error) {
metrics.TotalGarmCalls.WithLabelValues("controller.Info").Inc()
controllerInfo, err := s.GarmAPI().ControllerInfo.ControllerInfo(&controller_info.ControllerInfoParams{}, s.Token())
if err != nil {
metrics.GarmCallErrors.WithLabelValues("controller.Info").Inc()

// after the first run, garm needs a configuration for webhook, metadata and callback
// to make garm work after the first run, we set some defaults
if IsConflictError(err) {
updateParams := garmcontroller.NewUpdateControllerParams().WithBody(params.UpdateControllerParams{
MetadataURL: util.StringPtr(initialMetadataURL),
CallbackURL: util.StringPtr(initialCallbackURL),
WebhookURL: util.StringPtr(initialWebhookURL),
})
// let's initiate the new controller with some defaults
_, err := s.UpdateController(updateParams)
if err != nil {
return nil, err
}
}
return nil, err
}
return controllerInfo, nil
Expand Down

0 comments on commit 5117e5b

Please sign in to comment.