Skip to content

Commit

Permalink
Read the cluster domain from the Eventing CR or from the ConfigMap (#152
Browse files Browse the repository at this point in the history
)

* Read the cluster domain from the Eventing CR or from the ConfigMap

* Format imports

* Fix lint issues

* Fix lint issues

* Fix tests after rebase

* Simplify code

* Fix tests
  • Loading branch information
marcobebway authored Oct 20, 2023
1 parent 7142bc1 commit 08b9ec6
Show file tree
Hide file tree
Showing 32 changed files with 743 additions and 127 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/eventing_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ type BackendConfig struct {
// +kubebuilder:default:="sap.kyma.custom"
// +kubebuilder:validation:XValidation:rule="self!=''", message="eventTypePrefix cannot be empty"
EventTypePrefix string `json:"eventTypePrefix,omitempty"`

// Domain defines the cluster public domain used to configure the EventMesh Subscriptions
// and their corresponding ApiRules.
// +kubebuilder:validation:Pattern:="^(?:([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*)?$"
Domain string `json:"domain,omitempty"`
}

// Publisher defines the configurations for eventing-publisher-proxy.
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/operator.kyma-project.io_eventings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ spec:
natsStreamStorageType: File
description: Config defines configuration for eventing backend.
properties:
domain:
description: Domain defines the cluster public domain used
to configure the EventMesh Subscriptions and their corresponding
ApiRules.
pattern: ^(?:([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*)?$
type: string
eventMeshSecret:
description: EventMeshSecret defines the namespaced name of
K8s Secret containing EventMesh credentials. The format
Expand Down
4 changes: 0 additions & 4 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ spec:
value: "kyma-system"
- name: NATS_URL
value: eventing-nats.kyma-system.svc.cluster.local
- name: DOMAIN
value: kyma.example.com
- name: WEBHOOK_TOKEN_ENDPOINT
value: https://oauth2.kyma.example.com/oauth2/token
- name: PUBLISHER_REQUESTS_CPU
value: 10m
- name: PUBLISHER_REQUESTS_MEMORY
Expand Down
37 changes: 37 additions & 0 deletions internal/controller/eventing/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package eventing

import (
"context"
"fmt"
)

const (
shootInfoConfigMapName = "shoot-info"
shootInfoConfigMapNamespace = "kube-system"
shootInfoConfigMapKeyDomain = "domain"
domainMissingMessageFormat = `domain configuration is missing. domain must be configured in either the Eventing` +
` CustomResource under "Spec.Backend.Config.Domain" or in the ConfigMap "%s/%s" under "data.%s"`
domainMissingMessageFormatWithError = domainMissingMessageFormat + `: %v`
)

func (r *Reconciler) readDomainFromConfigMap(ctx context.Context) (string, error) {
cm, err := r.kubeClient.GetConfigMap(ctx, shootInfoConfigMapName, shootInfoConfigMapNamespace)
if err != nil {
return "", err
}
return cm.Data[shootInfoConfigMapKeyDomain], nil
}

func domainMissingError(err error) error {
if err != nil {
return fmt.Errorf(
domainMissingMessageFormatWithError,
shootInfoConfigMapNamespace, shootInfoConfigMapName, shootInfoConfigMapKeyDomain, err,
)
}

return fmt.Errorf(
domainMissingMessageFormat,
shootInfoConfigMapNamespace, shootInfoConfigMapName, shootInfoConfigMapKeyDomain,
)
}
59 changes: 59 additions & 0 deletions internal/controller/eventing/domain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package eventing

import (
"context"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"

k8smocks "github.com/kyma-project/eventing-manager/pkg/k8s/mocks"
"github.com/kyma-project/eventing-manager/test/utils"
)

func Test_readDomainFromConfigMap(t *testing.T) {
// given
ctx := context.TODO()

cm := &corev1.ConfigMap{
Data: map[string]string{
shootInfoConfigMapKeyDomain: utils.Domain,
},
}

kubeClient := func() *k8smocks.Client {
kubeClient := new(k8smocks.Client)
kubeClient.On("GetConfigMap", ctx, shootInfoConfigMapName, shootInfoConfigMapNamespace).
Return(cm, nil).Once()
return kubeClient
}

wantError := error(nil)
wantDomain := utils.Domain

// when
r := &Reconciler{kubeClient: kubeClient()}
gotDomain, gotError := r.readDomainFromConfigMap(ctx)

// then
assert.Equal(t, wantError, gotError)
assert.Equal(t, wantDomain, gotDomain)
}

func Test_domainMissingError(t *testing.T) {
// given
const errorMessage = "some error"
err := fmt.Errorf(errorMessage)

// when
err0 := domainMissingError(nil)
err1 := domainMissingError(err)

// then
assert.NotNil(t, err0)
assert.NotNil(t, err1)
assert.False(t, strings.Contains(strings.ToLower(err0.Error()), "nil"))
assert.True(t, strings.Contains(err1.Error(), errorMessage))
}
32 changes: 24 additions & 8 deletions internal/controller/eventing/eventmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/kyma-project/eventing-manager/pkg/env"
"os"

"github.com/kyma-project/eventing-manager/api/v1alpha1"
"github.com/kyma-project/eventing-manager/pkg/env"
"github.com/kyma-project/eventing-manager/pkg/eventing"

subscriptionmanager "github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/manager"

"github.com/kyma-project/eventing-manager/api/v1alpha1"
"github.com/kyma-project/eventing-manager/pkg/utils"
"github.com/pkg/errors"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/types"
)

Expand Down Expand Up @@ -59,13 +57,31 @@ func (r *Reconciler) reconcileEventMeshSubManager(ctx context.Context, eventing
return fmt.Errorf("failed to setup environment variables for EventMesh controller: %v", err)
}

// Read the cluster domain from the Eventing CR, or
// read it from the configmap managed by gardener
domain := eventing.Spec.Backend.Config.Domain
if utils.IsEmpty(domain) {
r.namedLogger().Infof(
`Domain is not configured in the Eventing CR, reading it from the ConfigMap %s/%s`,
shootInfoConfigMapNamespace, shootInfoConfigMapName,
)
domain, err = r.readDomainFromConfigMap(ctx)
if err != nil || utils.IsEmpty(domain) {
return domainMissingError(err)
}
}
r.namedLogger().Infof(`Domain is %s`, domain)

// get the subscription config
defaultSubsConfig := r.getDefaultSubscriptionConfig()
// get the subManager parameters
eventMeshSubMgrParams := r.getEventMeshSubManagerParams()
// get the hash of current config
specHash, err := r.getEventMeshBackendConfigHash(eventing.Spec.Backend.Config.EventMeshSecret,
eventing.Spec.Backend.Config.EventTypePrefix)
specHash, err := getEventMeshBackendConfigHash(
eventing.Spec.Backend.Config.EventMeshSecret,
eventing.Spec.Backend.Config.EventTypePrefix,
domain,
)
if err != nil {
return err
}
Expand All @@ -80,7 +96,7 @@ func (r *Reconciler) reconcileEventMeshSubManager(ctx context.Context, eventing

if r.eventMeshSubManager == nil {
// create instance of EventMesh subscription manager
eventMeshSubManager, err := r.subManagerFactory.NewEventMeshManager()
eventMeshSubManager, err := r.subManagerFactory.NewEventMeshManager(domain)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 08b9ec6

Please sign in to comment.