Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Add ownership to Istio objects #217

Merged
merged 2 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Operation cannot be fulfilled on oneagents.dynatrace.com \"oneagent\": the object has been modified; please apply your changes to the latest version and try again
```
* Proxy environment variables (e.g., `http_proxy`, etc.) can be ignored on Operator container when `skipCertCheck` is true ([#204](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/204))
* Istio objects don't have an owner object, so wouldn't get removed if the OneAgent object is deleted ([#217](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/217))

### Other changes
* As part of the support for ARM ([#201](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/201), [#203](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/203))
Expand Down
40 changes: 22 additions & 18 deletions pkg/controller/istio/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

Expand All @@ -33,15 +35,17 @@ const (
// Controller - manager istioclientset and config
type Controller struct {
istioClient istioclientset.Interface
scheme *runtime.Scheme

logger logr.Logger
config *rest.Config
}

// NewController - creates new instance of istio controller
func NewController(config *rest.Config) *Controller {
func NewController(config *rest.Config, scheme *runtime.Scheme) *Controller {
c := &Controller{
config: config,
scheme: scheme,
logger: log.Log.WithName("istio.controller"),
}
istioClient, err := c.initialiseIstioClient(config)
Expand All @@ -65,48 +69,42 @@ func (c *Controller) initialiseIstioClient(config *rest.Config) (istioclientset.
// ReconcileIstio - runs the istio's reconcile workflow,
// creating/deleting VS & SE for external communications
func (c *Controller) ReconcileIstio(oneagent *dynatracev1alpha1.OneAgent,
dtc dtclient.Client) (updated bool, ok bool, err error) {
dtc dtclient.Client) (updated bool, err error) {

enabled, err := CheckIstioEnabled(c.config)
if err != nil {
c.logger.Error(err, "istio: failed to verify Istio availability")
return false, false, err
return false, fmt.Errorf("istio: failed to verify Istio availability: %w", err)
}
c.logger.Info("istio: status", "enabled", enabled)

if !enabled {
return false, true, nil
return false, nil
}

apiHost, err := dtc.GetCommunicationHostForClient()
if err != nil {
c.logger.Error(err, "istio: failed to get host for Dynatrace API URL")
return false, false, err
return false, fmt.Errorf("istio: failed to get host for Dynatrace API URL: %w", err)
}

upd, err := c.reconcileIstioConfigurations(oneagent, []dtclient.CommunicationHost{apiHost}, "api-url")
if err != nil {
c.logger.Error(err, "istio: error reconciling config for Dynatrace API URL")
return false, false, err
if upd, err := c.reconcileIstioConfigurations(oneagent, []dtclient.CommunicationHost{apiHost}, "api-url"); err != nil {
return false, fmt.Errorf("istio: error reconciling config for Dynatrace API URL: %w", err)
} else if upd {
return true, true, nil
return true, nil
}

// Fetch endpoints via Dynatrace client
comHosts, err := dtc.GetCommunicationHosts()
if err != nil {
c.logger.Error(err, "istio: failed to get Dynatrace communication endpoints")
return false, false, err
return false, fmt.Errorf("istio: failed to get Dynatrace communication endpoints: %w", err)
}

if upd, err := c.reconcileIstioConfigurations(oneagent, comHosts, "communication-endpoint"); err != nil {
c.logger.Error(err, "istio: error reconciling config for Dynatrace communication endpoints")
return false, false, err
return false, fmt.Errorf("istio: error reconciling config for Dynatrace communication endpoints: %w", err)
} else if upd {
return true, true, nil
return true, nil
}

return false, true, nil
return false, nil
}

func (c *Controller) reconcileIstioConfigurations(instance *dynatracev1alpha1.OneAgent,
Expand Down Expand Up @@ -301,6 +299,9 @@ func (c *Controller) createIstioConfigurationForServiceEntry(oneagent *dynatrace
serviceEntry *istiov1alpha3.ServiceEntry, role string) error {

serviceEntry.Labels = buildIstioLabels(oneagent.Name, role)
if err := controllerutil.SetControllerReference(oneagent, serviceEntry, c.scheme); err != nil {
return err
}
sve, err := c.istioClient.NetworkingV1alpha3().ServiceEntries(oneagent.Namespace).Create(serviceEntry)
if err != nil {
return err
Expand All @@ -316,6 +317,9 @@ func (c *Controller) createIstioConfigurationForVirtualService(oneagent *dynatra
virtualService *istiov1alpha3.VirtualService, role string) error {

virtualService.Labels = buildIstioLabels(oneagent.Name, role)
if err := controllerutil.SetControllerReference(oneagent, virtualService, c.scheme); err != nil {
return err
}
vs, err := c.istioClient.NetworkingV1alpha3().VirtualServices(oneagent.Namespace).Create(virtualService)
if err != nil {
return err
Expand Down
10 changes: 6 additions & 4 deletions pkg/controller/oneagent/oneagent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewOneAgentReconciler(client client.Client, apiReader client.Reader, scheme
config: config,
logger: logger,
dynatraceClientFunc: dynatraceClientFunc,
istioController: istio.NewController(config),
istioController: istio.NewController(config, scheme),
}
}

Expand Down Expand Up @@ -158,9 +158,11 @@ func (r *ReconcileOneAgent) Reconcile(request reconcile.Request) (reconcile.Resu
}

if instance.Spec.EnableIstio {
upd, ok, err := r.istioController.ReconcileIstio(instance, dtc)
if ok && upd && err != nil {
return reconcile.Result{RequeueAfter: 1 * time.Minute}, nil
if upd, err := r.istioController.ReconcileIstio(instance, dtc); err != nil {
// If there are errors log them, but move on.
logger.Info("istio: failed to reconcile objects", "error", err)
} else if upd {
return reconcile.Result{RequeueAfter: 30 * time.Second}, nil
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/integrationtests/envutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Dynatrace/dynatrace-oneagent-operator/pkg/controller/oneagent"
"github.com/Dynatrace/dynatrace-oneagent-operator/pkg/controller/utils"
"github.com/Dynatrace/dynatrace-oneagent-operator/pkg/dtclient"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
corev1 "k8s.io/api/core/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -90,6 +91,7 @@ var testEnvironmentCRDs = []*apiextensionsv1beta1.CustomResourceDefinition{
func init() {
// Register OneAgent and Istio object schemas.
apis.AddToScheme(scheme.Scheme)
os.Setenv(k8sutil.WatchNamespaceEnvVar, DefaultTestNamespace)
}

type ControllerTestEnvironment struct {
Expand Down