Skip to content

Commit

Permalink
feat: support URLs and just hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakknutsen committed Jul 22, 2024
1 parent bda9800 commit 62b98d5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 15 deletions.
78 changes: 78 additions & 0 deletions pkg/condition/condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package condition

import (
"github.com/go-logr/logr"
"github.com/opendatahub-io/odh-platform/controllers/authorization"
"github.com/opendatahub-io/odh-platform/pkg/spi"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

/*
# Initial
* Load Config
* Register Conditional pr GAV pr Capability
* Start
# Update
* Load Config
* Register Conditional pr GAV pr Capability
* If existing but changed; kill to reload everything
* If removed?; kill to reload everything
* If exisitng but not changed; all ready running?

Check failure on line 25 in pkg/condition/condition.go

View workflow job for this annotation

GitHub Actions / golangci-lint

`exisitng` is a misspelling of `existing` (misspell)
* If new, just add to start
*/

type When func() bool
type Do func() error
type Conditional func() (When, Do)

type ConditionalEngine struct {
conditions []Conditional
}

func (c *ConditionalEngine) Add(condition Conditional) {
c.conditions = append(c.conditions, condition)
}

func (c *ConditionalEngine) Start() {

/*

Check failure on line 45 in pkg/condition/condition.go

View workflow job for this annotation

GitHub Actions / test

commentedOutCode: may want to remove commented-out code (gocritic)

Check failure on line 45 in pkg/condition/condition.go

View workflow job for this annotation

GitHub Actions / test

commentedOutCode: may want to remove commented-out code (gocritic)

Check failure on line 45 in pkg/condition/condition.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentedOutCode: may want to remove commented-out code (gocritic)
for i, con := range c.conditions {
if con.When() {
con.Do()
// remove con
}
}
if len(c.conditions) > 0 {
Sleep(500)
c.Start()
}
*/

}

func Authorization(mgr manager.Manager, log logr.Logger, component spi.AuthorizationComponent) Conditional {
return func() (When, Do) {
return WhenGAVExists(mgr.GetClient(), component),
DoSetupAuthorization(mgr, log, component)
}
}

func WhenGAVExists(cli client.Client, component spi.AuthorizationComponent) When {
return func() bool {
return false // TODO: check CRD
}
}

func DoSetupAuthorization(mgr manager.Manager, log logr.Logger, component spi.AuthorizationComponent) Do {
return func() error {
return authorization.NewPlatformAuthorizationReconciler(mgr.GetClient(), log, component).
SetupWithManager(mgr)
}
}
33 changes: 23 additions & 10 deletions pkg/resource/authconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ func NewConfigMapTemplateLoader(cli client.Client, fallback spi.AuthConfigTempla
func (c *configMapTemplateLoader) Load(ctx context.Context, authType spi.AuthType, key types.NamespacedName) (authorinov1beta2.AuthConfig, error) {
// else
ac, err := c.fallback.Load(ctx, authType, key)

return ac, fmt.Errorf("could not load from fallback: %w", err)
if err != nil {
return authorinov1beta2.AuthConfig{}, fmt.Errorf("could not load from fallback: %w", err)
}
return ac, nil

Check failure on line 111 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

return with no blank line before (nlreturn)

Check failure on line 111 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

return with no blank line before (nlreturn)

Check failure on line 111 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

type annotationAuthTypeDetector struct {
Expand Down Expand Up @@ -141,21 +143,32 @@ func NewExpressionHostExtractor(paths []string) spi.HostExtractor {
func (k *expressionHostExtractor) Extract(target *unstructured.Unstructured) []string {
hosts := []string{}

isURL := func(url string) bool {
if strings.HasPrefix("http://", url) || strings.HasPrefix("https://", url) {

Check failure on line 147 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

argOrder: "http://" and url arguments order looks reversed (gocritic)

Check failure on line 147 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

argOrder: "http://" and url arguments order looks reversed (gocritic)

Check failure on line 147 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

argOrder: "http://" and url arguments order looks reversed (gocritic)
return true
}
return false

Check failure on line 150 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

return with no blank line before (nlreturn)

Check failure on line 150 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

return with no blank line before (nlreturn)

Check failure on line 150 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

appendHost := func(h []string, urlStr string) []string {

Check failure on line 153 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

parameter name 'h' is too short for the scope of its usage (varnamelen)

Check failure on line 153 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

parameter name 'h' is too short for the scope of its usage (varnamelen)

Check failure on line 153 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

parameter name 'h' is too short for the scope of its usage (varnamelen)
if isURL(urlStr) {
parsedURL, err := url.Parse(urlStr)
if err == nil {
return append(h, parsedURL.Host)
}
}
return append(h, urlStr)

Check failure on line 160 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

return with no blank line before (nlreturn)

Check failure on line 160 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

return with no blank line before (nlreturn)

Check failure on line 160 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

for _, path := range k.paths {
foundHost, found, err := unstructured.NestedString(target.Object, strings.Split(path, ".")...)
if err == nil && found {
parsedURL, err := url.Parse(foundHost)
if err == nil {
hosts = append(hosts, parsedURL.Host)
}
hosts = appendHost(hosts, foundHost)
}
foundHosts, found, err := unstructured.NestedStringSlice(target.Object, strings.Split(path, ".")...)

Check failure on line 168 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

assignments should only be cuddled with other assignments (wsl)

Check failure on line 168 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

assignments should only be cuddled with other assignments (wsl)

Check failure on line 168 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

assignments should only be cuddled with other assignments (wsl)
if err == nil && found {

Check failure on line 169 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

only one cuddle assignment allowed before if statement (wsl)

Check failure on line 169 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

only one cuddle assignment allowed before if statement (wsl)

Check failure on line 169 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

only one cuddle assignment allowed before if statement (wsl)
for _, foundHost := range foundHosts {
parsedURL, err := url.Parse(foundHost)
if err == nil {
hosts = append(hosts, parsedURL.Host)
}
hosts = appendHost(hosts, foundHost)
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/resource/authconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ var _ = Describe("AuthConfig functions", Label(labels.Unit), func() {
// given
extractor := resource.NewExpressionHostExtractor([]string{"status.url"})
target := unstructured.Unstructured{
Object: map[string]interface{}{
"status": map[string]interface{}{
"url": []interface{}{"http://test.com", "http://test2.com"},
},
},
Object: map[string]interface{}{},
}
unstructured.SetNestedStringSlice(target.Object, []string{"test.com", "test2.com"}, "status", "url")

// when
hosts := extractor.Extract(&target)
Expand Down

0 comments on commit 62b98d5

Please sign in to comment.