Skip to content

Commit

Permalink
Fix handling of docker case
Browse files Browse the repository at this point in the history
  • Loading branch information
waodim committed Jul 5, 2024
1 parent ad97487 commit d23af95
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 24 deletions.
97 changes: 81 additions & 16 deletions pkg/controllers/dynakube/istio/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const defaultRepositoryHost = "index.docker.io/"
const (
defaultDockerRepositoryHost = "index.docker.io/"
authDockerHost = "auth.docker.io/"
prodCloudFlareHost = "production.cloudflare.docker.com/"
)

var necessarAdditionalDockerHosts = []string{authDockerHost, prodCloudFlareHost}

Check failure on line 28 in pkg/controllers/dynakube/istio/reconciler.go

View workflow job for this annotation

GitHub Actions / Run linting

var `necessarAdditionalDockerHosts` is unused (unused)

type Reconciler interface {
ReconcileAPIUrl(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
Expand Down Expand Up @@ -55,12 +61,12 @@ func (r *reconciler) reconcileCSIDriver(ctx context.Context, dynakube *dynatrace
return err
}

codeModulesHost, err := dtclient.ParseEndpoint(parsedCodeModulesURL)
commHosts, err := storeCommunicationHosts(parsedCodeModulesURL)
if err != nil {
return err
}

err = r.reconcileCommunicationHosts(ctx, []dtclient.CommunicationHost{codeModulesHost}, CSIDriverComponent)
err = r.reconcileCommunicationHosts(ctx, commHosts, CSIDriverComponent)
if err != nil {
return errors.WithMessage(err, "error reconciling config for codeModulesImage")
}
Expand All @@ -70,27 +76,86 @@ func (r *reconciler) reconcileCSIDriver(ctx context.Context, dynakube *dynatrace
return nil
}

func parseCodeModulesImageURL(rawUrl string) (string, error) {
parsedURL, err := url.Parse(rawUrl)
if err != nil {
return "", errors.New("can't parse the codeModules image URL")
}
func storeCommunicationHosts(parsedURLList []string) ([]dtclient.CommunicationHost, error) {
commHosts := []dtclient.CommunicationHost{}

tmpURL := strings.Split(rawUrl, "/")
if !strings.Contains(tmpURL[0], ".") {
rawUrl = defaultRepositoryHost + rawUrl
if len(parsedURLList) == 1 {
codeModulesHost, err := dtclient.ParseEndpoint(parsedURLList[0])
if err != nil {
return nil, err
}
commHosts = append(commHosts, codeModulesHost)

Check failure on line 87 in pkg/controllers/dynakube/istio/reconciler.go

View workflow job for this annotation

GitHub Actions / Run linting

append only allowed to cuddle with appended value (wsl)

return commHosts, nil
} else {
commHosts, err := addNecessaryDockerHosts(parsedURLList)
if err != nil {
return nil, err
}

return commHosts, err
}
}

if parsedURL.Scheme == "" {
parsedURL.Scheme = "https"
func addNecessaryDockerHosts(urlList []string) ([]dtclient.CommunicationHost, error) {
commHosts := []dtclient.CommunicationHost{}

parsedURL, err = url.Parse(parsedURL.Scheme + "://" + rawUrl)
for _, host := range urlList {
dockerHost, err := dtclient.ParseEndpoint(host)
if err != nil {
return "", errors.New("can't parse the codeModules image URL")
return nil, err
}
commHosts = append(commHosts, dockerHost)

Check failure on line 108 in pkg/controllers/dynakube/istio/reconciler.go

View workflow job for this annotation

GitHub Actions / Run linting

append only allowed to cuddle with appended value (wsl)
}
return commHosts, nil

Check failure on line 110 in pkg/controllers/dynakube/istio/reconciler.go

View workflow job for this annotation

GitHub Actions / Run linting

return with no blank line before (nlreturn)
}

return parsedURL.String(), nil
func parseCodeModulesImageURL(rawUrl string) ([]string, error) {

Check failure on line 113 in pkg/controllers/dynakube/istio/reconciler.go

View workflow job for this annotation

GitHub Actions / Run linting

cognitive-complexity: function parseCodeModulesImageURL has cognitive complexity 22 (> max enabled 20) (revive)
var urlList []string

tmpURL := strings.Split(rawUrl, "/")
if !strings.Contains(tmpURL[0], ".") && !strings.Contains(tmpURL[0], "http") {
urlList = append(urlList, defaultDockerRepositoryHost+rawUrl)
urlList = append(urlList, authDockerHost+rawUrl)
urlList = append(urlList, prodCloudFlareHost+rawUrl)
}

if len(urlList) == 0 {
parsedURL, err := url.Parse(rawUrl)
if err != nil {
return nil, errors.New("can't parse the codeModules image URL")
}

if parsedURL.Scheme == "" {
parsedURL.Scheme = "https"

parsedURL, err = url.Parse(parsedURL.Scheme + "://" + rawUrl)
if err != nil {
return nil, errors.New("can't parse the codeModules image URL")
}
}

return []string{parsedURL.String()}, nil
} else {
var parsedURLList []string
for _, currURL := range urlList {

Check failure on line 141 in pkg/controllers/dynakube/istio/reconciler.go

View workflow job for this annotation

GitHub Actions / Run linting

ranges should only be cuddled with assignments used in the iteration (wsl)
currURL, err := url.Parse(currURL)
if err != nil {
return nil, errors.New("can't parse the codeModules image URL")
}

if currURL.Scheme == "" {
currURL.Scheme = "https"

currURL, err = url.Parse(currURL.String())
if err != nil {
return nil, errors.New("can't parse the codeModules image URL")
}
}
parsedURLList = append(parsedURLList, currURL.String())

Check failure on line 155 in pkg/controllers/dynakube/istio/reconciler.go

View workflow job for this annotation

GitHub Actions / Run linting

append only allowed to cuddle with appended value (wsl)
}
return parsedURLList, nil

Check failure on line 157 in pkg/controllers/dynakube/istio/reconciler.go

View workflow job for this annotation

GitHub Actions / Run linting

return with no blank line before (nlreturn)
}
}

func (r *reconciler) ReconcileAPIUrl(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error {
Expand Down
16 changes: 8 additions & 8 deletions pkg/controllers/dynakube/istio/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,42 +511,42 @@ func TestReconcileActiveGateCommunicationHosts(t *testing.T) {
func TestParseCodeModulesImageURL(t *testing.T) {
tests := []struct {
input string
output string
output []string
parsedCorreclty bool
}{
{
input: "some.url.com/test",
output: "https://some.url.com/test",
output: []string{"https://some.url.com/test"},
parsedCorreclty: true,
},
{
input: "http://some.url.com/test",
output: "http://some.url.com/test",
output: []string{"http://some.url.com/test"},
parsedCorreclty: true,
},
{
input: "https://some.url.com/test",
output: "https://some.url.com/test",
output: []string{"https://some.url.com/test"},
parsedCorreclty: true,
},
{
input: ":example.com/test",
output: "",
output: []string{""},
parsedCorreclty: false,
},
{
input: "some.url.com/test:some-tag",
output: "https://some.url.com/test:some-tag",
output: []string{"https://some.url.com/test:some-tag"},
parsedCorreclty: true,
},
{
input: "some/url/test:some-tag",
output: "https://index.docker.io/some/url/test:some-tag",
output: []string{"https://index.docker.io/some/url/test:some-tag", "https://auth.docker.io/some/url/test:some-tag", "https://production.cloudflare.docker.com/some/url/test:some-tag"},
parsedCorreclty: true,
},
{
input: ":example.com/test:some-tag",
output: "",
output: []string{""},
parsedCorreclty: false,
},
}
Expand Down

0 comments on commit d23af95

Please sign in to comment.