Skip to content

Commit

Permalink
Refactor 'operatorcontext' (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinothamar committed Jun 20, 2024
1 parent ae1af2b commit f239cb6
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 48 deletions.
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func main() {
}

if err = (controller.NewMaskinportenClientReconciler(
ctx,
mgr.GetClient(),
mgr.GetScheme(),
)).SetupWithManager(mgr); err != nil {
Expand Down
7 changes: 2 additions & 5 deletions internal/config/azure_keyvault.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package config

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"
operatorcontext "github.com/altinn/altinn-k8s-operator/internal/operator_context"
"github.com/altinn/altinn-k8s-operator/internal/operatorcontext"
)

func loadFromAzureKeyVault(operatorContext *operatorcontext.Context) (*Config, error) {
context := context.Background()

var cred azcore.TokenCredential
var err error

Expand All @@ -36,7 +33,7 @@ func loadFromAzureKeyVault(operatorContext *operatorcontext.Context) (*Config, e

config := &Config{}
for _, secretKey := range secretKeys {
secret, err := client.GetSecret(context, secretKey, "", nil)
secret, err := client.GetSecret(operatorContext, secretKey, "", nil)
if err != nil {
return nil, fmt.Errorf("error getting secret: %s, %w", secretKey, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
operatorcontext "github.com/altinn/altinn-k8s-operator/internal/operator_context"
"github.com/altinn/altinn-k8s-operator/internal/operatorcontext"
"github.com/go-playground/validator/v10"
)

Expand Down
7 changes: 4 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package config

import (
"context"
"os"
"reflect"
"testing"

operatorcontext "github.com/altinn/altinn-k8s-operator/internal/operator_context"
"github.com/altinn/altinn-k8s-operator/internal/operatorcontext"
"github.com/go-playground/validator/v10"
. "github.com/onsi/gomega"
)
Expand All @@ -27,7 +28,7 @@ func TestConfigMissingValuesFail(t *testing.T) {
_, err = file.WriteString("maskinporten_api.url=https://example.com")
Expect(err).NotTo(HaveOccurred())

operatorContext := operatorcontext.DiscoverOrDie()
operatorContext := operatorcontext.DiscoverOrDie(context.Background())
cfg, err := GetConfig(operatorContext, file.Name())
Expect(cfg).To(BeNil())
Expect(err).To(HaveOccurred())
Expand All @@ -40,7 +41,7 @@ func TestConfigMissingValuesFail(t *testing.T) {
func TestConfigTestEnvLoadsOk(t *testing.T) {
RegisterTestingT(t)

operatorContext := operatorcontext.DiscoverOrDie()
operatorContext := operatorcontext.DiscoverOrDie(context.Background())
cfg, err := GetConfig(operatorContext, "")
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
Expand Down
2 changes: 1 addition & 1 deletion internal/config/koanf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path"

operatorcontext "github.com/altinn/altinn-k8s-operator/internal/operator_context"
"github.com/altinn/altinn-k8s-operator/internal/operatorcontext"
"github.com/knadh/koanf/parsers/dotenv"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
Expand Down
8 changes: 6 additions & 2 deletions internal/controller/maskinportenclient_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ type MaskinportenClientReconciler struct {
tracer trace.Tracer
}

func NewMaskinportenClientReconciler(client client.Client, scheme *runtime.Scheme) *MaskinportenClientReconciler {
rt, err := internal.NewRuntime()
func NewMaskinportenClientReconciler(
ctx context.Context,
client client.Client,
scheme *runtime.Scheme,
) *MaskinportenClientReconciler {
rt, err := internal.NewRuntime(ctx)
if err != nil {
panic(err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/controller/maskinportenclient_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var _ = Describe("MaskinportenClient Controller", func() {
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
controllerReconciler := NewMaskinportenClientReconciler(
context.Background(),
k8sClient,
k8sClient.Scheme(),
)
Expand Down
8 changes: 5 additions & 3 deletions internal/internal.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package internal

import (
"context"

"github.com/altinn/altinn-k8s-operator/internal/config"
"github.com/altinn/altinn-k8s-operator/internal/maskinporten"
operatorcontext "github.com/altinn/altinn-k8s-operator/internal/operator_context"
"github.com/altinn/altinn-k8s-operator/internal/operatorcontext"
rt "github.com/altinn/altinn-k8s-operator/internal/runtime"
"github.com/jonboulle/clockwork"
)
Expand All @@ -16,8 +18,8 @@ type runtime struct {

var _ rt.Runtime = (*runtime)(nil)

func NewRuntime() (rt.Runtime, error) {
operatorContext, err := operatorcontext.Discover()
func NewRuntime(ctx context.Context) (rt.Runtime, error) {
operatorContext, err := operatorcontext.Discover(ctx)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/maskinporten/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/altinn/altinn-k8s-operator/internal/caching"
"github.com/altinn/altinn-k8s-operator/internal/config"
operatorcontext "github.com/altinn/altinn-k8s-operator/internal/operator_context"
"github.com/altinn/altinn-k8s-operator/internal/operatorcontext"
"github.com/google/uuid"
"github.com/jonboulle/clockwork"
"github.com/onsi/gomega"
Expand All @@ -28,7 +28,7 @@ func getMaskinportenApiFixture(
g *gomega.WithT,
generateApis func(cfg *config.Config) (apis []testApi),
) (*httptest.Server, *config.Config) {
operatorContext := operatorcontext.DiscoverOrDie()
operatorContext := operatorcontext.DiscoverOrDie(context.Background())
cfg := config.GetConfigOrDie(operatorContext, "")

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -85,7 +85,7 @@ func getMaskinportenApiWellKnownFixture(g *gomega.WithT, statusCode int) (*httpt
func TestFixtureIsNotRemote(t *testing.T) {
g := NewWithT(t)

operatorContext := operatorcontext.DiscoverOrDie()
operatorContext := operatorcontext.DiscoverOrDie(context.Background())
configBefore := config.GetConfigOrDie(operatorContext, "")

server, configAfter := getMaskinportenApiWellKnownFixture(g, http.StatusOK)
Expand Down
29 changes: 0 additions & 29 deletions internal/operator_context/operator_context.go

This file was deleted.

58 changes: 58 additions & 0 deletions internal/operatorcontext/operatorcontext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package operatorcontext

import (
"context"
"time"
)

const EnvLocal = "local"

var _ context.Context = (*Context)(nil)

type Context struct {
Te string
Env string
inner context.Context
}

func (c *Context) IsLocal() bool {
return c.Env == EnvLocal
}

func Discover(ctx context.Context) (*Context, error) {
err := ctx.Err()
if err != nil {
return nil, err
}

// This should come from the environment/context somewhere
// there should be 1:1 mapping between TE/env:cluster
te := "local"
env := EnvLocal

return &Context{Te: te, Env: env, inner: ctx}, nil
}

func DiscoverOrDie(ctx context.Context) *Context {
context, err := Discover(ctx)
if err != nil {
panic(err)
}
return context
}

func (c *Context) Deadline() (deadline time.Time, ok bool) {
return c.inner.Deadline()
}

func (c *Context) Done() <-chan struct{} {
return c.inner.Done()
}

func (c *Context) Err() error {
return c.inner.Err()
}

func (c *Context) Value(key any) any {
return c.inner.Value(key)
}
41 changes: 41 additions & 0 deletions internal/operatorcontext/operatorcontext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package operatorcontext

import (
"context"
"testing"

. "github.com/onsi/gomega"
)

func TestDiscoversOk(t *testing.T) {
RegisterTestingT(t)

operatorContext, err := Discover(context.Background())
Expect(err).NotTo(HaveOccurred())
Expect(operatorContext).NotTo(BeNil())
}

func TestCancellationBefore(t *testing.T) {
RegisterTestingT(t)

ctx, cancel := context.WithCancel(context.Background())
cancel()
operatorContext, err := Discover(ctx)
Expect(operatorContext).To(BeNil())
Expect(ctx.Err()).To(MatchError("context canceled"))
Expect(err).To(MatchError("context canceled"))

}

func TestCancellationAfter(t *testing.T) {
RegisterTestingT(t)

ctx, cancel := context.WithCancel(context.Background())
operatorContext, err := Discover(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(operatorContext).NotTo(BeNil())
Expect(ctx.Err()).To(Succeed())

cancel()
Expect(ctx.Err()).To(MatchError("context canceled"))
}
2 changes: 1 addition & 1 deletion internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package runtime
import (
"github.com/altinn/altinn-k8s-operator/internal/config"
"github.com/altinn/altinn-k8s-operator/internal/maskinporten"
operatorcontext "github.com/altinn/altinn-k8s-operator/internal/operator_context"
"github.com/altinn/altinn-k8s-operator/internal/operatorcontext"
)

type Runtime interface {
Expand Down

0 comments on commit f239cb6

Please sign in to comment.