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 8 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
15 changes: 14 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 @@ -595,6 +595,19 @@ 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.ReconcileCSIDriver(ctx, dynakube)

require.NoError(t, err)

expectedName = istio.BuildNameForFQDNServiceEntry(dynakube.GetName(), istio.CSIDiverComponent)
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)
})
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/controllers/dynakube/injection/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func (r *reconciler) Reconcile(ctx context.Context) error {
return err
}

err = r.reconcileIstioForCSIDriver(ctx)
if err != nil {
return err
}

err = r.connectionInfoReconciler.Reconcile(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -120,6 +125,17 @@ func (r *reconciler) Reconcile(ctx context.Context) error {
return nil
}

func (r *reconciler) reconcileIstioForCSIDriver(ctx context.Context) error {
if r.istioReconciler != nil {
err := r.istioReconciler.ReconcileCSIDriver(ctx, r.dynakube)
if err != nil {
return errors.WithMessage(err, "failed to reconcile istio objects for CSI Driver")
}
}

return nil
}
0sewa0 marked this conversation as resolved.
Show resolved Hide resolved

func (r *reconciler) removeAppInjection(ctx context.Context) (err error) {
namespaces, err := mapper.GetNamespacesForDynakube(ctx, r.apiReader, r.dynakube.Name)
if err != nil {
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 (
CSIDiverComponent = "csi-driver"
waodim marked this conversation as resolved.
Show resolved Hide resolved
OperatorComponent = "operator"
OneAgentComponent = "oneagent"
CodeModuleComponent = "CodeModule"
Expand Down
56 changes: 55 additions & 1 deletion pkg/controllers/dynakube/istio/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
goerrors "errors"
"net"
"net/url"
"strings"

dynatracev1beta2 "github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta2/dynakube"
dtclient "github.com/Dynatrace/dynatrace-operator/pkg/clients/dynatrace"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/connectioninfo/activegate"
"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/util/conditions"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/labels"
"github.com/Dynatrace/dynatrace-operator/pkg/util/timeprovider"
Expand All @@ -22,6 +23,7 @@ type Reconciler interface {
ReconcileAPIUrl(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
ReconcileCodeModuleCommunicationHosts(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
ReconcileActiveGateCommunicationHosts(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
ReconcileCSIDriver(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error
0sewa0 marked this conversation as resolved.
Show resolved Hide resolved
}

type reconciler struct {
Expand All @@ -38,6 +40,58 @@ func NewReconciler(istio *Client) Reconciler {
}
}

func (r *reconciler) ReconcileCSIDriver(ctx context.Context, dynakube *dynatracev1beta2.DynaKube) error {
log.Info("reconciling istio components for the 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
}

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

err = r.reconcileCommunicationHosts(ctx, []dtclient.CommunicationHost{codeModulesHost}, CSIDiverComponent)
if err != nil {
return errors.WithMessage(err, "error reconciling config for codeModulesImage")
}

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

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")
}

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

if !strings.HasPrefix(rawUrl, "//") {
// if no scheme at all is set we want to add this prefix
rawUrl = "//" + rawUrl
}
0sewa0 marked this conversation as resolved.
Show resolved Hide resolved

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

return parsedURL.String(), nil
}

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

Expand Down
58 changes: 58 additions & 0 deletions pkg/controllers/dynakube/istio/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/Dynatrace/dynatrace-operator/pkg/api/status"
dynatracev1beta2 "github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta2/dynakube"
dtclient "github.com/Dynatrace/dynatrace-operator/pkg/clients/dynatrace"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -212,6 +213,62 @@ func TestReconcileAPIUrl(t *testing.T) {
})
}

func TestReconcileCSIDriver(t *testing.T) {
ctx := context.Background()
dynakube := createTestDynaKube()

t.Run("nil => error", func(t *testing.T) {
istioClient := newTestingClient(nil, dynakube.GetNamespace())
reconciler := NewReconciler(istioClient)

err := reconciler.ReconcileCSIDriver(ctx, nil)
require.Error(t, err)
})
t.Run("malformed image uri (no protocol prefix) => still no error", func(t *testing.T) {
dynakube := createTestDynaKube()
dynakube.Status = dynatracev1beta2.DynaKubeStatus{
CodeModules: dynatracev1beta2.CodeModulesStatus{
VersionStatus: status.VersionStatus{
ImageID: "something-random",
},
},
}
fakeClient := fakeistio.NewSimpleClientset()
istioClient := newTestingClient(fakeClient, dynakube.GetNamespace())
reconciler := NewReconciler(istioClient)

err := reconciler.ReconcileCSIDriver(ctx, dynakube)
require.NoError(t, err)
})
t.Run("success", func(t *testing.T) {
fakeClient := fakeistio.NewSimpleClientset()
istioClient := newTestingClient(fakeClient, dynakube.GetNamespace())
reconciler := NewReconciler(istioClient)

err := reconciler.ReconcileCSIDriver(ctx, dynakube)
require.NoError(t, err)

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

virtualService, err := fakeClient.NetworkingV1beta1().VirtualServices(dynakube.GetNamespace()).Get(ctx, expectedName, metav1.GetOptions{})
require.NoError(t, err)
assert.NotNil(t, virtualService)
})
t.Run("unknown k8s client error => error", func(t *testing.T) {
fakeClient := fakeistio.NewSimpleClientset()
fakeClient.PrependReactor("*", "*", boomReaction)

istioClient := newTestingClient(fakeClient, dynakube.GetNamespace())
reconciler := NewReconciler(istioClient)

err := reconciler.ReconcileCSIDriver(ctx, dynakube)
require.Error(t, err)
})
}

func TestReconcileOneAgentCommunicationHosts(t *testing.T) {
ctx := context.Background()

Expand Down Expand Up @@ -301,6 +358,7 @@ func TestReconcileOneAgentCommunicationHosts(t *testing.T) {

err = r.ReconcileCodeModuleCommunicationHosts(ctx, dynakube)
require.NoError(t, err)

statusCondition = meta.FindStatusCondition(*dynakube.Conditions(), "IstioForCodeModule")
require.Nil(t, statusCondition)

Expand Down
47 changes: 47 additions & 0 deletions test/mocks/pkg/controllers/dynakube/istio/reconciler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading