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

fix: apply a dummy default controller config #215

Merged
merged 1 commit into from
Nov 20, 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
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.local"
initialCallbackURL = "https://initial.callback.garm.local"
initialWebhookURL = "https://initial.webhook.garm.local"
)

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