Skip to content

Commit

Permalink
Move connection-info and version reconcile to oneagent reconciler (#2781
Browse files Browse the repository at this point in the history
)
  • Loading branch information
0sewa0 authored Feb 22, 2024
1 parent 47ed00a commit 235a37b
Show file tree
Hide file tree
Showing 37 changed files with 1,254 additions and 1,361 deletions.
3 changes: 0 additions & 3 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ packages:
github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/istio:
interfaces:
Reconciler:
github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/connectioninfo:
interfaces:
Reconciler:
github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/deploymentmetadata:
interfaces:
Reconciler:
Expand Down
17 changes: 17 additions & 0 deletions pkg/api/v1beta1/dynakube/dynakube_status.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package dynakube

import (
"context"
"fmt"
"time"

"github.com/Dynatrace/dynatrace-operator/pkg/api/status"
containerv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/pkg/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// DynaKubeStatus defines the observed state of DynaKube
Expand Down Expand Up @@ -142,3 +146,16 @@ func (dk *DynaKubeStatus) SetPhase(phase status.DeploymentPhase) bool {

return upd
}

func (dk *DynaKube) UpdateStatus(ctx context.Context, client client.Client) error {
dk.Status.UpdatedTimestamp = metav1.Now()
err := client.Status().Update(ctx, dk)

if err != nil && k8serrors.IsConflict(err) {
log.Info("could not update dynakube due to conflict", "name", dk.Name)

return nil
}

return errors.WithStack(err)
}
11 changes: 10 additions & 1 deletion pkg/controllers/csi/provisioner/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func TestOneAgentProvisioner_Reconcile(t *testing.T) {
Name: dynakube.OneagentTenantSecret(),
},
Data: map[string][]byte{
connectioninfo.TenantTokenName: []byte("tenant-token"),
connectioninfo.TenantTokenKey: []byte("tenant-token"),
},
},
),
Expand Down Expand Up @@ -613,12 +613,15 @@ func TestHandleMetadata(t *testing.T) {

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

t.Run("updateAgentInstallation with codeModules enabled", func(t *testing.T) {
dynakube := getDynakube()
enableCodeModules(dynakube)

mockDtcBuilder := dtClientMock.NewBuilder(t)

var dtc dtclient.Client

mockDtcBuilder.On("Build").Return(dtc, nil)
dtc, err := mockDtcBuilder.Build()
require.NoError(t, err)
Expand Down Expand Up @@ -660,7 +663,9 @@ func TestUpdateAgentInstallation(t *testing.T) {
enableCodeModules(dynakube)

mockDtcBuilder := dtClientMock.NewBuilder(t)

var dtc dtclient.Client

mockDtcBuilder.On("Build").Return(dtc, nil)
dtc, err := mockDtcBuilder.Build()
require.NoError(t, err)
Expand Down Expand Up @@ -696,7 +701,9 @@ func TestUpdateAgentInstallation(t *testing.T) {
dynakube := getDynakube()

mockDtcBuilder := dtClientMock.NewBuilder(t)

var dtc dtclient.Client

mockDtcBuilder.On("Build").Return(dtc, nil)
dtc, err := mockDtcBuilder.Build()
require.NoError(t, err)
Expand Down Expand Up @@ -736,7 +743,9 @@ func TestUpdateAgentInstallation(t *testing.T) {
dynakube := getDynakube()

mockDtcBuilder := dtClientMock.NewBuilder(t)

var dtc dtclient.Client

mockDtcBuilder.On("Build").Return(dtc, nil)
dtc, err := mockDtcBuilder.Build()
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (mod RawImageModifier) getVolumeMounts() []corev1.VolumeMount {
Name: connectioninfo.TenantSecretVolumeName,
ReadOnly: true,
MountPath: connectioninfo.TenantTokenMountPoint,
SubPath: connectioninfo.TenantTokenName,
SubPath: connectioninfo.TenantTokenKey,
},
}
}
Expand All @@ -81,7 +81,7 @@ func (mod RawImageModifier) tenantUUIDEnvVar() corev1.EnvVar {
LocalObjectReference: corev1.LocalObjectReference{
Name: mod.dynakube.ActiveGateConnectionInfoConfigMapName(),
},
Key: connectioninfo.TenantUUIDName,
Key: connectioninfo.TenantUUIDKey,
Optional: address.Of(false),
}}}
}
Expand All @@ -93,7 +93,7 @@ func (mod RawImageModifier) communicationEndpointEnvVar() corev1.EnvVar {
LocalObjectReference: corev1.LocalObjectReference{
Name: mod.dynakube.ActiveGateConnectionInfoConfigMapName(),
},
Key: connectioninfo.CommunicationEndpointsName,
Key: connectioninfo.CommunicationEndpointsKey,
Optional: address.Of(false),
}},
}
Expand Down
17 changes: 7 additions & 10 deletions pkg/controllers/dynakube/activegate/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/activegate/internal/customproperties"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/activegate/internal/statefulset"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/connectioninfo"
agconnectioninfo "github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/connectioninfo/activegate"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/istio"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/version"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/configmap"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/object"
"github.com/Dynatrace/dynatrace-operator/pkg/util/timeprovider"
"github.com/pkg/errors"
"github.com/spf13/afero"
"golang.org/x/net/context"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -32,7 +32,7 @@ type Reconciler struct {
scheme *runtime.Scheme
authTokenReconciler controllers.Reconciler
istioReconciler istio.Reconciler
connectionReconciler connectioninfo.Reconciler
connectionReconciler controllers.Reconciler
versionReconciler version.Reconciler
newStatefulsetReconcilerFunc statefulset.NewReconcilerFunc
newCapabilityReconcilerFunc capabilityInternal.NewReconcilerFunc
Expand All @@ -44,17 +44,14 @@ var _ controllers.Reconciler = (*Reconciler)(nil)
type ReconcilerBuilder func(clt client.Client, apiReader client.Reader, scheme *runtime.Scheme, dynakube *dynatracev1beta1.DynaKube, dtc dtclient.Client, istioClient *istio.Client) controllers.Reconciler

func NewReconciler(clt client.Client, apiReader client.Reader, scheme *runtime.Scheme, dynakube *dynatracev1beta1.DynaKube, dtc dtclient.Client, istioClient *istio.Client) controllers.Reconciler { //nolint: revive
time := timeprovider.New().Freeze()
fs := afero.Afero{Fs: afero.NewOsFs()}

var istioReconciler istio.Reconciler
if istioClient != nil {
istioReconciler = istio.NewReconciler(istioClient)
}

authTokenReconciler := authtoken.NewReconciler(clt, apiReader, scheme, dynakube, dtc)
versionReconciler := version.NewReconciler(apiReader, dtc, fs, time)
connectionInfoReconciler := connectioninfo.NewReconciler(clt, apiReader, scheme, dtc)
versionReconciler := version.NewReconciler(apiReader, dtc, timeprovider.New().Freeze())
connectionInfoReconciler := agconnectioninfo.NewReconciler(clt, apiReader, scheme, dtc, dynakube)

newCustomPropertiesReconcilerFunc := func(customPropertiesOwnerName string, customPropertiesSource *dynatracev1beta1.DynaKubeValueSource) controllers.Reconciler {
return customproperties.NewReconciler(clt, dynakube, customPropertiesOwnerName, scheme, customPropertiesSource)
Expand Down Expand Up @@ -82,7 +79,7 @@ func (r *Reconciler) Reconcile(ctx context.Context) error {
}

if r.dynakube.NeedsActiveGate() {
err = r.connectionReconciler.ReconcileActiveGate(ctx, r.dynakube)
err = r.connectionReconciler.Reconcile(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -152,11 +149,11 @@ func extractPublicData(dynakube *dynatracev1beta1.DynaKube) map[string]string {
data := map[string]string{}

if dynakube.Status.ActiveGate.ConnectionInfoStatus.TenantUUID != "" {
data[connectioninfo.TenantUUIDName] = dynakube.Status.ActiveGate.ConnectionInfoStatus.TenantUUID
data[connectioninfo.TenantUUIDKey] = dynakube.Status.ActiveGate.ConnectionInfoStatus.TenantUUID
}

if dynakube.Status.ActiveGate.ConnectionInfoStatus.Endpoints != "" {
data[connectioninfo.CommunicationEndpointsName] = dynakube.Status.ActiveGate.ConnectionInfoStatus.Endpoints
data[connectioninfo.CommunicationEndpointsKey] = dynakube.Status.ActiveGate.ConnectionInfoStatus.Endpoints
}

return data
Expand Down
14 changes: 6 additions & 8 deletions pkg/controllers/dynakube/activegate/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/version"
mocks "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/clients/dynatrace"
controllerMocks "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/controllers"
connectioninfoMocks "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/controllers/dynakube/connectioninfo"
istioMocks "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/controllers/dynakube/istio"
versionMocks "github.com/Dynatrace/dynatrace-operator/test/mocks/pkg/controllers/dynakube/version"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -354,16 +353,15 @@ func TestReconcile_ActivegateConfigMap(t *testing.T) {
var actual corev1.ConfigMap
err = fakeClient.Get(context.TODO(), client.ObjectKey{Name: dynakube.ActiveGateConnectionInfoConfigMapName(), Namespace: testNamespace}, &actual)
require.NoError(t, err)
assert.Equal(t, testTenantUUID, actual.Data[connectioninfo.TenantUUIDName])
assert.Equal(t, testTenantEndpoints, actual.Data[connectioninfo.CommunicationEndpointsName])
assert.Equal(t, testTenantUUID, actual.Data[connectioninfo.TenantUUIDKey])
assert.Equal(t, testTenantEndpoints, actual.Data[connectioninfo.CommunicationEndpointsKey])
})
}

func createConnectionInfoReconcilerMock(t *testing.T) connectioninfo.Reconciler {
connectionInfoReconciler := connectioninfoMocks.NewReconciler(t)
connectionInfoReconciler.On("ReconcileActiveGate",
mock.AnythingOfType("context.backgroundCtx"),
mock.AnythingOfType("*dynakube.DynaKube")).Return(nil).Once()
func createConnectionInfoReconcilerMock(t *testing.T) controllers.Reconciler {
connectionInfoReconciler := controllerMocks.NewReconciler(t)
connectionInfoReconciler.On("Reconcile",
mock.AnythingOfType("context.backgroundCtx")).Return(nil).Once()

return connectionInfoReconciler
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package connectioninfo
package activegate

import (
"net/url"
"strconv"
"strings"

dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta1/dynakube"
"github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta1/dynakube"
dtclient "github.com/Dynatrace/dynatrace-operator/pkg/clients/dynatrace"
"github.com/pkg/errors"
)
Expand All @@ -14,20 +14,7 @@ const (
DefaultHttpPort = uint32(80)
)

func GetOneAgentCommunicationHosts(dynakube *dynatracev1beta1.DynaKube) []dtclient.CommunicationHost {
communicationHosts := make([]dtclient.CommunicationHost, 0, len(dynakube.Status.OneAgent.ConnectionInfoStatus.CommunicationHosts))
for _, host := range dynakube.Status.OneAgent.ConnectionInfoStatus.CommunicationHosts {
communicationHosts = append(communicationHosts, dtclient.CommunicationHost{
Protocol: host.Protocol,
Host: host.Host,
Port: host.Port,
})
}

return communicationHosts
}

func GetActiveGateEndpointsAsCommunicationHosts(dynakube *dynatracev1beta1.DynaKube) []dtclient.CommunicationHost {
func GetEndpointsAsCommunicationHosts(dynakube *dynakube.DynaKube) []dtclient.CommunicationHost {
activegateEndpointsString := dynakube.Status.ActiveGate.ConnectionInfoStatus.Endpoints
if activegateEndpointsString == "" {
return []dtclient.CommunicationHost{}
Expand Down Expand Up @@ -64,7 +51,7 @@ func parseEndpointToCommunicationHost(endpointString string) (dtclient.Communica
return dtclient.CommunicationHost{}, err
}

port, err := GetPortOrDefault(parsedEndpoint, DefaultHttpPort)
port, err := getPortOrDefault(parsedEndpoint, DefaultHttpPort)
if err != nil {
return dtclient.CommunicationHost{}, err
}
Expand All @@ -76,7 +63,7 @@ func parseEndpointToCommunicationHost(endpointString string) (dtclient.Communica
}, nil
}

func GetPortOrDefault(u *url.URL, defaultPort uint32) (uint32, error) {
func getPortOrDefault(u *url.URL, defaultPort uint32) (uint32, error) {
portString := u.Port()

if portString == "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,18 @@
package connectioninfo
package activegate

import (
"testing"

dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta1/dynakube"
dtclient "github.com/Dynatrace/dynatrace-operator/pkg/clients/dynatrace"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetCommunicationHosts(t *testing.T) {
dynakube := &dynatracev1beta1.DynaKube{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: testName,
},
Status: dynatracev1beta1.DynaKubeStatus{
OneAgent: dynatracev1beta1.OneAgentStatus{
ConnectionInfoStatus: dynatracev1beta1.OneAgentConnectionInfoStatus{
ConnectionInfoStatus: dynatracev1beta1.ConnectionInfoStatus{},
},
},
},
}

expectedCommunicationHosts := []dtclient.CommunicationHost{
{
Protocol: "protocol",
Host: "host",
Port: 12345,
},
}

t.Run(`communications host empty`, func(t *testing.T) {
hosts := GetOneAgentCommunicationHosts(dynakube)
assert.Empty(t, hosts)
})

t.Run(`communication-hosts field found`, func(t *testing.T) {
dynakube.Status.OneAgent.ConnectionInfoStatus.CommunicationHosts = []dynatracev1beta1.CommunicationHostStatus{
{
Protocol: "protocol",
Host: "host",
Port: 12345,
},
}

hosts := GetOneAgentCommunicationHosts(dynakube)
assert.NotNil(t, hosts)
assert.Equal(t, expectedCommunicationHosts[0].Host, hosts[0].Host)
assert.Equal(t, expectedCommunicationHosts[0].Protocol, hosts[0].Protocol)
assert.Equal(t, expectedCommunicationHosts[0].Port, hosts[0].Port)
})
}

func TestParseCommunicationHostsFromActiveGateEndpoints(t *testing.T) {
dynakube := &dynatracev1beta1.DynaKube{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: testName,
Namespace: "test-namespace",
Name: "test-name",
},
Status: dynatracev1beta1.DynaKubeStatus{
OneAgent: dynatracev1beta1.OneAgentStatus{
Expand All @@ -77,7 +31,7 @@ func TestParseCommunicationHostsFromActiveGateEndpoints(t *testing.T) {
t.Run(`activegate endpoint set`, func(t *testing.T) {
dynakube.Status.ActiveGate.ConnectionInfoStatus.Endpoints = "https://abcd123.some.activegate.endpointurl.com:443"

hosts := GetActiveGateEndpointsAsCommunicationHosts(dynakube)
hosts := GetEndpointsAsCommunicationHosts(dynakube)
assert.Len(t, hosts, 1)
assert.Equal(t, "abcd123.some.activegate.endpointurl.com", hosts[0].Host)
assert.Equal(t, "https", hosts[0].Protocol)
Expand All @@ -86,7 +40,7 @@ func TestParseCommunicationHostsFromActiveGateEndpoints(t *testing.T) {
t.Run(`activegate multiple endpoints set`, func(t *testing.T) {
dynakube.Status.ActiveGate.ConnectionInfoStatus.Endpoints = "https://abcd123.some.activegate.endpointurl.com:443,https://efg5678.some.other.activegate.endpointurl.com"

hosts := GetActiveGateEndpointsAsCommunicationHosts(dynakube)
hosts := GetEndpointsAsCommunicationHosts(dynakube)
assert.Len(t, hosts, 2)
hostNames := []string{hosts[0].Host, hosts[1].Host}
assert.Contains(t, hostNames, "abcd123.some.activegate.endpointurl.com")
Expand All @@ -95,7 +49,7 @@ func TestParseCommunicationHostsFromActiveGateEndpoints(t *testing.T) {
t.Run(`activegate duplicate endpoints set`, func(t *testing.T) {
dynakube.Status.ActiveGate.ConnectionInfoStatus.Endpoints = "https://abcd123.some.activegate.endpointurl.com:443,https://abcd123.some.activegate.endpointurl.com:443,https://abcd123.some.activegate.endpointurl.com:443"

hosts := GetActiveGateEndpointsAsCommunicationHosts(dynakube)
hosts := GetEndpointsAsCommunicationHosts(dynakube)
assert.Len(t, hosts, 1)
assert.Equal(t, "abcd123.some.activegate.endpointurl.com", hosts[0].Host)
assert.Equal(t, "https", hosts[0].Protocol)
Expand Down
7 changes: 7 additions & 0 deletions pkg/controllers/dynakube/connectioninfo/activegate/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package activegate

import "github.com/Dynatrace/dynatrace-operator/pkg/util/logger"

var (
log = logger.Get().WithName("activegate-connectioninfo")
)
Loading

0 comments on commit 235a37b

Please sign in to comment.