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

Major refactoring & implement standalone mode #12

Merged
merged 19 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 1 addition & 8 deletions internal/executors/http/csrf_token_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"github.com/SAP/remote-work-processor/internal/utils"
"net/http"

"github.com/SAP/remote-work-processor/internal/functional"
)

const CsrfVerb = "fetch"
Expand Down Expand Up @@ -57,10 +55,5 @@ func createCsrfHeaders(authHeader string) HttpHeaders {
}

func (f *csrfTokenFetcher) createRequestParameters() (*HttpRequestParameters, error) {
opts := []functional.OptionWithError[HttpRequestParameters]{
WithUrl(f.csrfUrl),
WithMethod(http.MethodGet),
WithHeaders(f.headers),
}
return NewHttpRequestParameters(opts...)
return NewHttpRequestParameters(http.MethodGet, f.csrfUrl, WithHeaders(f.headers))
}
63 changes: 16 additions & 47 deletions internal/executors/http/http_executor_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ type HttpRequestParameters struct {
}

func NewHttpRequestParametersFromContext(ctx executors.Context) (*HttpRequestParameters, error) {
method, err := ctx.GetRequiredString(METHOD)
if err != nil {
return nil, nonRetryableError(err)
}

url, err := ctx.GetRequiredString(URL)
if err != nil {
return nil, nonRetryableError(err)
}

opts := []functional.OptionWithError[HttpRequestParameters]{
withMethodFromContext(ctx),
withUrlFromContext(ctx),
withTokenUrlFromContext(ctx),
withCsrfUrlFromContext(ctx),
withClientIdFromContext(ctx),
Expand All @@ -73,14 +81,15 @@ func NewHttpRequestParametersFromContext(ctx executors.Context) (*HttpRequestPar
withAuthorizationHeaderFromContext(ctx),
withStoreFromContext(ctx),
}
return applyBuildOptions(&HttpRequestParameters{}, opts...)
return NewHttpRequestParameters(method, url, opts...)
}

func NewHttpRequestParameters(opts ...functional.OptionWithError[HttpRequestParameters]) (*HttpRequestParameters, error) {
return applyBuildOptions(&HttpRequestParameters{}, opts...)
}
func NewHttpRequestParameters(method, url string, opts ...functional.OptionWithError[HttpRequestParameters]) (*HttpRequestParameters, error) {
p := &HttpRequestParameters{
method: method,
url: url,
}

func applyBuildOptions(p *HttpRequestParameters, opts ...functional.OptionWithError[HttpRequestParameters]) (*HttpRequestParameters, error) {
for _, opt := range opts {
if err := opt(p); err != nil {
return nil, err
Expand Down Expand Up @@ -125,22 +134,6 @@ func (p HttpRequestParameters) GetCertificateAuthentication() *tls.CertificateAu
return p.certAuthentication
}

func WithMethod(m string) functional.OptionWithError[HttpRequestParameters] {
return func(params *HttpRequestParameters) error {
params.method = m

return nil
}
}

func WithUrl(u string) functional.OptionWithError[HttpRequestParameters] {
return func(params *HttpRequestParameters) error {
params.url = u

return nil
}
}

func WithTokenUrl(u string) functional.OptionWithError[HttpRequestParameters] {
return func(params *HttpRequestParameters) error {
params.tokenUrl = u
Expand Down Expand Up @@ -253,30 +246,6 @@ func WithAuthorizationHeader(h string) functional.OptionWithError[HttpRequestPar
}
}

func withMethodFromContext(ctx executors.Context) functional.OptionWithError[HttpRequestParameters] {
return func(params *HttpRequestParameters) error {
m, err := ctx.GetRequiredString(METHOD)
if err != nil {
return nonRetryableError(err)
}

params.method = m
return nil
}
}

func withUrlFromContext(ctx executors.Context) functional.OptionWithError[HttpRequestParameters] {
return func(params *HttpRequestParameters) error {
u, err := ctx.GetRequiredString(URL)
if err != nil {
return nonRetryableError(err)
}

params.url = u
return nil
}
}

func withTokenUrlFromContext(ctx executors.Context) functional.OptionWithError[HttpRequestParameters] {
return func(params *HttpRequestParameters) error {
u := ctx.GetString(TOKEN_URL)
Expand Down
14 changes: 4 additions & 10 deletions internal/executors/http/ias_token_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/SAP/remote-work-processor/internal/executors/http/tls"
"github.com/SAP/remote-work-processor/internal/functional"
)

type iasTokenFetcher struct {
Expand Down Expand Up @@ -35,14 +34,9 @@ func (f *iasTokenFetcher) Fetch() (string, error) {
}

func (f *iasTokenFetcher) createRequestParameters() (*HttpRequestParameters, error) {
opts := []functional.OptionWithError[HttpRequestParameters]{
WithUrl(f.tokenUrl),
WithMethod(http.MethodGet),
WithCertificateAuthentication(
tls.NewCertAuthentication(
tls.WithClientCertificate(f.clientCert),
),
return NewHttpRequestParameters(http.MethodGet, f.tokenUrl, WithCertificateAuthentication(
tls.NewCertAuthentication(
tls.WithClientCertificate(f.clientCert),
),
}
return NewHttpRequestParameters(opts...)
))
}
4 changes: 1 addition & 3 deletions internal/executors/http/oauth_token_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ func (f *oAuthTokenFetcher) Fetch() (string, error) {

func (f *oAuthTokenFetcher) createRequestParameters() (*HttpRequestParameters, error) {
opts := []functional.OptionWithError[HttpRequestParameters]{
WithUrl(f.tokenUrl),
WithMethod(http.MethodPost),
WithHeaders(ContentTypeUrlFormEncoded()),
WithBody(f.body),
WithAuthorizationHeader(f.authHeader),
Expand All @@ -80,7 +78,7 @@ func (f *oAuthTokenFetcher) createRequestParameters() (*HttpRequestParameters, e
opts = append(opts, WithCertificateAuthentication(f.certAuthentication))
}

return NewHttpRequestParameters(opts...)
return NewHttpRequestParameters(http.MethodPost, f.tokenUrl, opts...)
}

func ContentTypeUrlFormEncoded() map[string]string {
Expand Down
4 changes: 4 additions & 0 deletions internal/kubernetes/controller/controller_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (c *ControllerBuilder) ManagedBy(manager *Manager) *ControllerBuilder {

func (c *ControllerBuilder) Create(reconciler string, grpcClient *grpc.RemoteWorkProcessorGrpcClient,
isEnabled func() bool) error {
if c.manager == nil || c.selector == nil || c.reconciliationPeriodInMinutes == 0 || c.resource == nil {
return fmt.Errorf("controller is missing required parameters")
}

gvk := schema.FromAPIVersionAndKind(c.resource.ApiVersion, c.resource.Kind)
mapping, err := c.manager.dynamicClient.GetGVR(&gvk)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/kubernetes/controller/manager_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func (b *ManagerBuilder) BuildInternalManager(config *rest.Config, scheme *runti
}

func (b *ManagerBuilder) Build() Manager {
if b.delegate == nil || b.dynamicClient == nil || b.grpcClient == nil {
panic("Manager is missing required parameters")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can have the build return a (*Manager, error) so that error is handled by the upper function WatchResources which already has a flow handling exiting the app in case of error?

}
return Manager{
delegate: b.delegate,
dynamicClient: b.dynamicClient,
Expand Down
Loading