Skip to content

Commit

Permalink
feat(controller): pass configuration in as part of activation (maistr…
Browse files Browse the repository at this point in the history
…a#75)

When running in embedded mode the controller is only created once but it
rely on configuration from updatable sources.

When e.g. the DSCI is updated, this does not get reflected to the
runnning controller.

Now the Operator can pass the new Configuration per reconcile to the
Controller

The Activable interface has expanded to take a generic any type as an
argument in the Activate(T) method. The Controller should store and use
this configuration for it's next reconcile.

---------

Co-authored-by: bartoszmajsak <[email protected]>
  • Loading branch information
aslakknutsen and bartoszmajsak authored Sep 4, 2024
1 parent a6fe071 commit d84c4c6
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 24 deletions.
7 changes: 5 additions & 2 deletions controllers/authzctrl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func New(cli client.Client, log logr.Logger,
hostExtractor: spi.UnifiedHostExtractor(
spi.NewPathExpressionExtractor(protectedResource.HostPaths),
spi.NewAnnotationHostExtractor(";", metadata.Keys(annotations.RoutingAddressesExternal(""), annotations.RoutingAddressesPublic(""))...)),
templateLoader: authorization.NewConfigMapTemplateLoader(cli, authorization.NewStaticTemplateLoader(config.Audiences)),
templateLoader: authorization.NewConfigMapTemplateLoader(cli, authorization.NewStaticTemplateLoader()),
}
}

Expand Down Expand Up @@ -120,8 +120,11 @@ func (r *Controller) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *Controller) Activate() {
var _ platformctrl.Activable[authorization.ProviderConfig] = &Controller{}

func (r *Controller) Activate(config authorization.ProviderConfig) {
r.active = true
r.config = config
}

func (r *Controller) Deactivate() {
Expand Down
7 changes: 6 additions & 1 deletion controllers/authzctrl/reconcile_authconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ func (r *Controller) createAuthConfigTemplate(ctx context.Context, target *unstr
return authorinov1beta2.AuthConfig{}, fmt.Errorf("could not detect authtype: %w", err)
}

templ, err := r.templateLoader.Load(ctx, authType, types.NamespacedName{Namespace: target.GetNamespace(), Name: target.GetName()})
templateData := map[string]any{
"Namespace": target.GetNamespace(),
"Audiences": r.config.Audiences,
}

templ, err := r.templateLoader.Load(ctx, authType, types.NamespacedName{Namespace: target.GetNamespace(), Name: target.GetName()}, templateData)
if err != nil {
return authorinov1beta2.AuthConfig{}, fmt.Errorf("could not load template %s: %w", authType, err)
}
Expand Down
5 changes: 4 additions & 1 deletion controllers/routingctrl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ func (r *Controller) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *Controller) Activate() {
var _ platformctrl.Activable[routing.IngressConfig] = &Controller{}

func (r *Controller) Activate(config routing.IngressConfig) {
r.active = true
r.config = config
}

func (r *Controller) Deactivate() {
Expand Down
4 changes: 2 additions & 2 deletions controllers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
)

type Activable interface {
Activate()
type Activable[T any] interface {
Activate(config T)
Deactivate()
}

Expand Down
18 changes: 6 additions & 12 deletions pkg/authorization/authconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,17 @@ var authConfigTemplateAnonymous []byte
var authConfigTemplateUserDefined []byte

type staticTemplateLoader struct {
audience []string
}

var _ AuthConfigTemplateLoader = (*staticTemplateLoader)(nil)

func NewStaticTemplateLoader(audience []string) *staticTemplateLoader {
return &staticTemplateLoader{audience: audience}
func NewStaticTemplateLoader() *staticTemplateLoader {
return &staticTemplateLoader{}
}

func (s *staticTemplateLoader) Load(_ context.Context, authType AuthType, key types.NamespacedName) (authorinov1beta2.AuthConfig, error) {
func (s *staticTemplateLoader) Load(_ context.Context, authType AuthType, key types.NamespacedName, templateData map[string]any) (authorinov1beta2.AuthConfig, error) {
authConfig := authorinov1beta2.AuthConfig{}

templateData := map[string]interface{}{
"Namespace": key.Namespace,
"Audiences": s.audience,
}

templateContent := authConfigTemplateAnonymous
if authType == UserDefined {
templateContent = authConfigTemplateUserDefined
Expand All @@ -71,7 +65,7 @@ func (s *staticTemplateLoader) Load(_ context.Context, authType AuthType, key ty
return authConfig, nil
}

func (s *staticTemplateLoader) resolveTemplate(tmpl []byte, data map[string]interface{}) ([]byte, error) {
func (s *staticTemplateLoader) resolveTemplate(tmpl []byte, data map[string]any) ([]byte, error) {
engine, err := template.New("authconfig").Parse(string(tmpl))
if err != nil {
return []byte{}, fmt.Errorf("could not create template engine: %w", err)
Expand Down Expand Up @@ -103,9 +97,9 @@ func NewConfigMapTemplateLoader(cli client.Client, fallback AuthConfigTemplateLo

// TODO: check "authconfig-template" CM in key.Namespace to see if there is a "spec" to use, construct a AuthConfig object
// https://issues.redhat.com/browse/RHOAIENG-847
func (c *configMapTemplateLoader) Load(ctx context.Context, authType AuthType, key types.NamespacedName) (authorinov1beta2.AuthConfig, error) {
func (c *configMapTemplateLoader) Load(ctx context.Context, authType AuthType, key types.NamespacedName, templateData map[string]any) (authorinov1beta2.AuthConfig, error) {
// else
ac, err := c.fallback.Load(ctx, authType, key)
ac, err := c.fallback.Load(ctx, authType, key, templateData)
if err != nil {
return authorinov1beta2.AuthConfig{}, fmt.Errorf("could not load from fallback: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/authorization/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ type AuthTypeDetector interface {
// - Namespace / Resource name
// - Loader source
type AuthConfigTemplateLoader interface {
Load(ctx context.Context, authType AuthType, key types.NamespacedName) (v1beta2.AuthConfig, error)
Load(ctx context.Context, authType AuthType, key types.NamespacedName, templateData map[string]any) (v1beta2.AuthConfig, error)
}
2 changes: 1 addition & 1 deletion pkg/routing/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var _ = Describe("Resource functions", test.Unit(), func() {
// given
extractor := spi.NewAnnotationHostExtractor(";", "A", "B")
target := unstructured.Unstructured{
Object: map[string]interface{}{},
Object: map[string]any{},
}
target.SetAnnotations(map[string]string{
"A": "a.com;a2.com",
Expand Down
8 changes: 4 additions & 4 deletions pkg/spi/host_extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ var _ = Describe("Host extraction", test.Unit(), func() {
// given
extractor := spi.NewPathExpressionExtractor([]string{"status.url"})
target := unstructured.Unstructured{
Object: map[string]interface{}{
"status": map[string]interface{}{
Object: map[string]any{
"status": map[string]any{
"url": "http://test.com",
},
},
Expand All @@ -33,7 +33,7 @@ var _ = Describe("Host extraction", test.Unit(), func() {
// given
extractor := spi.NewPathExpressionExtractor([]string{"status.url"})
target := unstructured.Unstructured{
Object: map[string]interface{}{},
Object: map[string]any{},
}
Expect(unstructured.SetNestedStringSlice(target.Object, []string{"test.com", "test2.com"}, "status", "url")).To(Succeed())

Expand All @@ -50,7 +50,7 @@ var _ = Describe("Host extraction", test.Unit(), func() {
// given
extractor := spi.NewPathExpressionExtractor([]string{"status.url"})
target := unstructured.Unstructured{
Object: map[string]interface{}{},
Object: map[string]any{},
}
Expect(unstructured.SetNestedStringSlice(target.Object, []string{"test.com", "http://test.com", "https://test.com"}, "status", "url")).To(Succeed())

Expand Down

0 comments on commit d84c4c6

Please sign in to comment.