Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create istio exclusion for CSI Driver in case of codeModules or public registry #3343

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f9d2522
Create istio exclusion for CSI Driver in case of codeModules or publi…
waodim Jun 20, 2024
a4ea79f
Merge branch 'main' into feature/csi-istio-exclusion
waodim Jun 20, 2024
c5f59e5
Remove unnecessary check and complete mock interface
waodim Jun 20, 2024
9a2ba7b
Merge branch 'feature/csi-istio-exclusion' of github.com:Dynatrace/dy…
waodim Jun 20, 2024
8bd069e
Fix linting
waodim Jun 20, 2024
b5566bd
Move istio reconciliation and use CodeModulesImage()
waodim Jun 20, 2024
8d971bd
Fix linting by removing cyclomatic complexity
waodim Jun 20, 2024
c92b4c3
Use url library to properly parse url
waodim Jun 20, 2024
8075a65
Add unit tests for parseCodeModuleUrl
waodim Jun 20, 2024
eeb60eb
Move csi istio reconciliation to already present istio check
waodim Jun 20, 2024
1564bbe
Fix linting
waodim Jun 20, 2024
81d0714
Update pkg/controllers/dynakube/istio/config.go
waodim Jun 24, 2024
0105021
Fix linting
waodim Jun 24, 2024
fdc1c92
Move CSI Driver reconciliation into comm hosts reconciliation
waodim Jun 25, 2024
a7c72c4
Update pkg/controllers/dynakube/istio/reconciler_test.go
waodim Jul 1, 2024
619e50a
Extend tests and apply feedback from review
waodim Jul 1, 2024
ea80b58
Set docker.io as default host for image url
waodim Jul 1, 2024
379c22d
Fix docker host
waodim Jul 3, 2024
ad97487
Merge branch 'main' into feature/csi-istio-exclusion
waodim Jul 4, 2024
d23af95
Fix handling of docker case
waodim Jul 5, 2024
9cd9a5d
Fix linting
waodim Jul 5, 2024
6bfb710
Further linting fix
waodim Jul 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion pkg/controllers/dynakube/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/Dynatrace/dynatrace-operator/pkg/controllers"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/activegate"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/connectioninfo"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/connectioninfo/oneagent"
oaconnectioninfo "github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/connectioninfo/oneagent"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/injection"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/istio"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/oneagent"
Expand Down Expand Up @@ -573,6 +573,9 @@ func TestSetupIstio(t *testing.T) {
})
t.Run("success", func(t *testing.T) {
dynakube := dynakubeBase.DeepCopy()
activateAppInjection(dynakube)
createCommunciationHosts(dynakube)

fakeIstio := fakeistio.NewSimpleClientset()
isIstioInstalled := true
controller := &Controller{
Expand All @@ -597,9 +600,69 @@ func TestSetupIstio(t *testing.T) {
virtualService, err := fakeIstio.NetworkingV1beta1().VirtualServices(dynakube.GetNamespace()).Get(ctx, expectedName, metav1.GetOptions{})
require.NoError(t, err)
assert.NotNil(t, virtualService)

err = istioReconciler.ReconcileCodeModulesInjectionEndpoints(ctx, dynakube)

require.NoError(t, err)

expectedName = istio.BuildNameForFQDNServiceEntry(dynakube.GetName(), istio.CSIDriverComponent)
serviceEntry, err = fakeIstio.NetworkingV1beta1().ServiceEntries(dynakube.GetNamespace()).Get(ctx, expectedName, metav1.GetOptions{})
require.NoError(t, err)
assert.NotNil(t, serviceEntry)

virtualService, err = fakeIstio.NetworkingV1beta1().VirtualServices(dynakube.GetNamespace()).Get(ctx, expectedName, metav1.GetOptions{})
require.NoError(t, err)
assert.NotNil(t, virtualService)
})
}

func createTestIPCommunicationHost() dtclient.CommunicationHost {
return dtclient.CommunicationHost{
Protocol: "http",
Host: "42.42.42.42",
Port: 620,
}
}

func createTestFQDNCommunicationHost() dtclient.CommunicationHost {
return dtclient.CommunicationHost{
Protocol: "http",
Host: "something.test.io",
Port: 620,
}
}

func createCommunciationHosts(dynakube *dynatracev1beta2.DynaKube) *dynatracev1beta2.DynaKube {
fqdnHost := createTestFQDNCommunicationHost()
ipHost := createTestIPCommunicationHost()
dynakube.Status = dynatracev1beta2.DynaKubeStatus{
OneAgent: dynatracev1beta2.OneAgentStatus{
ConnectionInfoStatus: dynatracev1beta2.OneAgentConnectionInfoStatus{
CommunicationHosts: []dynatracev1beta2.CommunicationHostStatus{
{
Protocol: fqdnHost.Protocol,
Host: fqdnHost.Host,
Port: fqdnHost.Port,
},
{
Protocol: ipHost.Protocol,
Host: ipHost.Host,
Port: ipHost.Port,
},
},
},
},
}

return dynakube
}

func activateAppInjection(dynakube *dynatracev1beta2.DynaKube) *dynatracev1beta2.DynaKube {
dynakube.Spec.OneAgent.CloudNativeFullStack = &dynatracev1beta2.CloudNativeFullStackSpec{}

return dynakube
}

func fakeIstioClientBuilder(t *testing.T, fakeIstio *fakeistio.Clientset, isIstioInstalled bool) istio.ClientBuilder {
return func(_ *rest.Config, owner metav1.Object) (*istio.Client, error) {
if isIstioInstalled == true {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/dynakube/injection/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (r *reconciler) Reconcile(ctx context.Context) error {

// do istio reconciliation for CodeModules here to enable cleanup of conditions
if r.istioReconciler != nil {
err = r.istioReconciler.ReconcileCodeModuleCommunicationHosts(ctx, r.dynakube)
err = r.istioReconciler.ReconcileCodeModulesInjectionEndpoints(ctx, r.dynakube)

if err != nil {
log.Error(err, "error reconciling istio configuration for codemodules")
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/dynakube/istio/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
)

const (
CSIDriverComponent = "csi-driver"
OperatorComponent = "operator"
OneAgentComponent = "oneagent"
CodeModuleComponent = "CodeModule"
Expand Down
144 changes: 141 additions & 3 deletions pkg/controllers/dynakube/istio/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
goerrors "errors"
"net"
"net/url"
"strings"

dynatracev1beta2 "github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta2/dynakube"
Expand All @@ -18,9 +19,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

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

type Reconciler interface {
ReconcileAPIUrl(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
ReconcileCodeModuleCommunicationHosts(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
ReconcileCodeModulesInjectionEndpoints(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
ReconcileActiveGateCommunicationHosts(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
}

Expand All @@ -38,6 +45,132 @@ func NewReconciler(istio *Client) Reconciler {
}
}

func (r *reconciler) reconcileCSIDriver(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error {
log.Info("reconciling istio components for csi driver")

if dynakube == nil {
return errors.New("can't reconcile csi driver of nil dynakube")
}

codeModulesURL := dynakube.CodeModulesImage()

parsedCodeModulesURL, err := parseCodeModulesImageURL(codeModulesURL)
if err != nil {
return err
}

commHosts, err := storeCommunicationHosts(parsedCodeModulesURL)
if err != nil {
return err
}

err = r.reconcileCommunicationHosts(ctx, commHosts, CSIDriverComponent)
if err != nil {
return errors.WithMessage(err, "error reconciling config for codeModulesImage")
}

log.Info("reconciled istio objects for csi driver")

return nil
}

func storeCommunicationHosts(parsedURLList []string) ([]dtclient.CommunicationHost, error) {
commHosts := []dtclient.CommunicationHost{}

if len(parsedURLList) == 1 {
codeModulesHost, err := dtclient.ParseEndpoint(parsedURLList[0])
if err != nil {
return nil, err
}

commHosts = append(commHosts, codeModulesHost)

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

return commHosts, err
}
}

func addNecessaryDockerHosts(urlList []string) ([]dtclient.CommunicationHost, error) {
commHosts := []dtclient.CommunicationHost{}

for _, host := range urlList {
dockerHost, err := dtclient.ParseEndpoint(host)
if err != nil {
return nil, err
}

commHosts = append(commHosts, dockerHost)
}

return commHosts, nil
}

func parseCodeModulesImageURL(rawUrl string) ([]string, error) {
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 {
parsedURLList, err := storeParsedURLs(urlList)
if err != nil {
return nil, err
}

return parsedURLList, nil
}
}

func storeParsedURLs(urlList []string) ([]string, error) {
parsedList := make([]string, 0, len(urlList))

for _, currURL := range urlList {
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")
}
}

parsedList = append(parsedList, currURL.String())
}

return parsedList, nil
}

func (r *reconciler) ReconcileAPIUrl(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error {
log.Info("reconciling istio components for the Dynatrace API url")

Expand All @@ -60,8 +193,8 @@ func (r *reconciler) ReconcileAPIUrl(ctx context.Context, dynakube *dynatracev1b
return nil
}

func (r *reconciler) ReconcileCodeModuleCommunicationHosts(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error {
log.Info("reconciling istio components for oneagent-code-modules communication hosts")
func (r *reconciler) ReconcileCodeModulesInjectionEndpoints(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error {
log.Info("reconciling istio components for oneagent-code-modules injections endpoints")

if dynakube == nil {
return errors.New("can't reconcile oneagent communication hosts of nil dynakube")
Expand Down Expand Up @@ -94,6 +227,11 @@ func (r *reconciler) ReconcileCodeModuleCommunicationHosts(ctx context.Context,

setServiceEntryUpdatedConditionForComponent(dynakube.Conditions(), CodeModuleComponent)

err = r.reconcileCSIDriver(ctx, dynakube)
if err != nil {
return err
}

return nil
}

Expand Down
Loading
Loading