Skip to content

Commit

Permalink
FS-1101; Fixing things brought up in review
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeschuurmans committed Jan 26, 2024
1 parent 27a6bfb commit fafca77
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 166 deletions.
1 change: 0 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ builds:
- CGO_ENABLED=0
goos:
- linux
- darwin
ldflags:
- -X "github.com/metal-toolbox/fleet-scheduler/internal/version.AppVersion={{ .Version }}"
-X "github.com/metal-toolbox/fleet-scheduler/internal/version.GoVersion={{ .Env.GOVERSION }}"
Expand Down
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ GIT_SUMMARY := $(shell git describe --tags --dirty --always)
VERSION := $(shell git describe --tags 2> /dev/null)
BUILD_DATE := $(shell date +%s)
GIT_COMMIT_FULL := $(shell git rev-parse HEAD)
GO_VERSION := $(shell expr `go version |cut -d ' ' -f3 |cut -d. -f2` \>= 16)
DOCKER_IMAGE_INBAND := "ghcr.io/metal-toolbox/fleet-scheduler"
GO_VERSION := $(shell expr `go version |cut -d ' ' -f3 |cut -d. -f2` \>= 20)
DOCKER_IMAGE := "ghcr.io/metal-toolbox/fleet-scheduler"
REPO := "https://github.com/metal-toolbox/fleet-scheduler.git"

Expand All @@ -25,12 +24,12 @@ lint:

## Go mod
go-mod:
go mod tidy -compat=1.19 && go mod vendor
go mod tidy -compat=1.20 && go mod vendor

## build osx bin
build-osx:
ifeq ($(GO_VERSION), 0)
$(error build requies go version 1.17.n or higher)
$(error build requies go version 1.20 or higher)
endif
GOOS=darwin GOARCH=amd64 go build -o $(PROJECT_NAME) \
-ldflags \
Expand All @@ -43,7 +42,7 @@ endif
## Build linux bin
build-linux:
ifeq ($(GO_VERSION), 0)
$(error build requies go version 1.16.n or higher)
$(error build requies go version 1.20 or higher)
endif
GOOS=linux GOARCH=amd64 go build -o $(PROJECT_NAME) \
-ldflags \
Expand All @@ -69,7 +68,7 @@ push-image-devel: build-image

## push docker image
push-image:
docker push ${DOCKER_IMAGE_INBAND}:latest
docker push ${DOCKER_IMAGE}:latest

# https://gist.github.com/prwhite/8168133
# COLORS
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ Fleet Scheduler jobs can be created within the value.yaml file of the [sandbox](

Each job requires a few values in order to function.

## Creating new tasks

- Tasks need to be implemented in code within [Fleet-Scheduler](https://github.com/metal-toolbox/fleet-scheduler)
- Tasks are implemented as [cobra](https://github.com/spf13/cobra) command line commands within /cmd
- Take a look at /cmd/inventory.go for a good example.

## Creating new jobs

You just need to add the job to the [value.yaml](https://github.com/metal-toolbox/sandbox) file.
Each job requires a few values in order to function.

### Values for creating new jobs

Job Values required and location to explain them with `kubectl explain <field>`
Expand Down
10 changes: 3 additions & 7 deletions cmd/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/equinix-labs/otel-init-go/otelinit"
"github.com/metal-toolbox/fleet-scheduler/internal/app"
"github.com/metal-toolbox/fleet-scheduler/internal/client"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
Expand All @@ -33,15 +32,12 @@ func inventory(ctx context.Context) error {
otelCtxWithCancel, cancelFunc := context.WithCancel(otelCtx)
defer cancelFunc()

newApp, err := app.New(otelCtxWithCancel, cfgFile)
newApp, err := app.New(cfgFile)
if err != nil {
return err
}

loggerEntry := newApp.Logger.WithFields(logrus.Fields{"component": "store.serverservice"})
loggerEntry.Level = newApp.Logger.Level

newClient, err := client.New(newApp.Ctx, newApp.Cfg, loggerEntry)
newClient, err := client.New(otelCtxWithCancel, newApp)
if err != nil {
return err
}
Expand All @@ -51,7 +47,7 @@ func inventory(ctx context.Context) error {
return err
}

newApp.Logger.Info("Task: 'CreateConditionInventoryForAllServers' complete")
newApp.Log.Info("Task: 'CreateConditionInventoryForAllServers' complete")

return nil
}
10 changes: 3 additions & 7 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/equinix-labs/otel-init-go/otelinit"
"github.com/metal-toolbox/fleet-scheduler/internal/app"
"github.com/metal-toolbox/fleet-scheduler/internal/client"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -36,16 +35,13 @@ func test(ctx context.Context) error {
otelCtxWithCancel, cancelFunc := context.WithCancel(otelCtx)
defer cancelFunc()

newApp, err := app.New(otelCtxWithCancel, cfgFile)
newApp, err := app.New(cfgFile)
if err != nil {
return err
}

loggerEntry := newApp.Logger.WithFields(logrus.Fields{"component": "store.serverservice"})
loggerEntry.Level = newApp.Logger.Level

// Just used to verify fleet-scheduler can authenticate
_, err = client.New(newApp.Ctx, newApp.Cfg, loggerEntry)
_, err = client.New(otelCtxWithCancel, newApp)
if err != nil {
return err
}
Expand All @@ -65,7 +61,7 @@ func test(ctx context.Context) error {
return err
}

newApp.Logger.Info("Config: ", prettyJSON.String())
newApp.Log.Info("Config: ", prettyJSON.String())

return nil
}
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ store_kind: fleetscheduler
facility_code: sandbox
fleetdb_api:
endpoint: http://localhost:8000
disable_oauth: false
disable_oauth: true
conditionorc_api:
endpoint: http://localhost:9001
disable_oauth: false
disable_oauth: true
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/metal-toolbox/fleet-scheduler

go 1.21
go 1.20

require (
github.com/adrg/xdg v0.4.0
Expand Down
22 changes: 7 additions & 15 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
package app

import (
"context"

"github.com/sirupsen/logrus"
)

type App struct {
Ctx context.Context
Cfg *Configuration

Logger *logrus.Logger
Log *logrus.Logger
}

func New(ctx context.Context, cfgFilePath string) (*App, error) {
func New(cfgFilePath string) (*App, error) {
cfgFileBytes, err := loadConfig(cfgFilePath)
if err != nil {
return nil, err
}

app := App{
Cfg: cfgFileBytes,
Logger: logrus.New(),
Ctx: ctx,
Cfg: cfgFileBytes,
Log: logrus.New(),
}

switch app.Cfg.LogLevel {
case LogLevelDebug:
app.Logger.Level = logrus.DebugLevel
case LogLevelTrace:
app.Logger.Level = logrus.TraceLevel
default:
app.Logger.Level = logrus.InfoLevel
app.Log.Level, err = logrus.ParseLevel(app.Cfg.LogLevel)
if err != nil {
return nil, err
}

return &app, nil
Expand Down
28 changes: 14 additions & 14 deletions internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func loadConfig(path string) (*Configuration, error) {
}

func loadEnvOverrides(cfg *Configuration, v *viper.Viper) error {
cfg.FdbCfg.ClientSecret = v.GetString("serverservice.oidc.client.secret")
cfg.FdbCfg.ClientSecret = v.GetString("fleetdb.oidc.client.secret")
if cfg.FdbCfg.ClientSecret == "" {
return errors.New("FLEET_SCHEDULER_SERVERSERVICE_OIDC_CLIENT_SECRET was empty")
return errors.New("FLEET_SCHEDULER_FLEETDB_OIDC_CLIENT_SECRET was empty")
}

cfg.CoCfg.ClientSecret = v.GetString("conditionorc.oidc.client.secret")
Expand All @@ -100,48 +100,48 @@ func loadEnvOverrides(cfg *Configuration, v *viper.Viper) error {
}

func validateClientParams(cfg *Configuration) error {
errCfgInvalid := errors.New("Configuration is invalid")

if cfg.LogLevel == "" {
cfg.LogLevel = LogLevelInfo
} else if cfg.LogLevel != LogLevelInfo &&
cfg.LogLevel != LogLevelDebug &&
cfg.LogLevel != LogLevelTrace {
return errors.Wrap(errCfgInvalid, "LogLevel")
return errors.Wrap(ErrInvalidConfig, "LogLevel")
}

// FleetDB (serverservice) Configuration
if cfg.FdbCfg == nil {
return errors.Wrap(errCfgInvalid, "fleetdb_api entry doesnt exist")
return errors.Wrap(ErrInvalidConfig, "fleetdb_api entry doesnt exist")
}
if cfg.CoCfg == nil {
return errors.Wrap(errCfgInvalid, "conditionorc_api entry doesnt exist")
return errors.Wrap(ErrInvalidConfig, "conditionorc_api entry doesnt exist")
}
if cfg.FacilityCode == "" {
return errors.Wrap(errCfgInvalid, "Facility Code")
return errors.Wrap(ErrInvalidConfig, "Facility Code")
}

err := validateOIDCConfig(cfg.FdbCfg, model.DefaultFleetDBClientID, errors.Wrap(errCfgInvalid, "fleetdb_api is invalid"))
err := validateOIDCConfig(cfg.FdbCfg, model.DefaultFleetDBClientID)
if err != nil {
return err
}
err = validateOIDCConfig(cfg.CoCfg, model.DefaultConditionOrcClientID, errors.Wrap(errCfgInvalid, "conditionorc_api is invalid"))
err = validateOIDCConfig(cfg.CoCfg, model.DefaultConditionOrcClientID)
if err != nil {
return err
}

return nil
}

func validateOIDCConfig(cfg *ConfigOIDC, defaultClientID string, err error) error {
func validateOIDCConfig(cfg *ConfigOIDC, defaultClientID string) error {
if cfg.ClientID == "" {
cfg.ClientID = defaultClientID
}
err := errors.Wrap(ErrInvalidConfig, cfg.ClientID)

if cfg.Endpoint == "" {
return errors.Wrap(err, "endpoint")
}

if !cfg.DisableOAuth {
if cfg.ClientID == "" {
cfg.ClientID = defaultClientID
}
if cfg.IssuerEndpoint == "" {
return errors.Wrap(err, "oidc_issuer_endpoint")
}
Expand Down
12 changes: 12 additions & 0 deletions internal/app/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app

import "github.com/pkg/errors"

var (
ErrFdbClientIsNil = errors.New("FleetDB client wasnt initialized")
ErrCoClientIsNil = errors.New("Conditionorc client wasnt initialized")
ErrNoTokenInRing = errors.New("secret not found in keyring")
ErrAuth = errors.New("authentication error")
ErrNilConfig = errors.New("configuration was nil")
ErrInvalidConfig = errors.New("configuration is invalid")
)
Loading

0 comments on commit fafca77

Please sign in to comment.