Skip to content

Commit

Permalink
Revert "Refactor SKR watcher installation to use native Kubernetes cl…
Browse files Browse the repository at this point in the history
…ient (kyma-project#313)"

This reverts commit 4643b27.
  • Loading branch information
jakobmoellerdev committed Jan 24, 2023
1 parent 1f44757 commit 2cf70ce
Show file tree
Hide file tree
Showing 27 changed files with 510 additions and 667 deletions.
90 changes: 42 additions & 48 deletions controllers/kyma_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ import (
"fmt"
"time"

"github.com/kyma-project/lifecycle-manager/pkg/deploy"
"k8s.io/client-go/rest"

manifestv1alpha1 "github.com/kyma-project/module-manager/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"

"github.com/kyma-project/lifecycle-manager/api/v1alpha1"
"github.com/kyma-project/lifecycle-manager/pkg/adapter"
"github.com/kyma-project/lifecycle-manager/pkg/catalog"
"github.com/kyma-project/lifecycle-manager/pkg/channel"
"github.com/kyma-project/lifecycle-manager/pkg/deploy"
"github.com/kyma-project/lifecycle-manager/pkg/module/common"
"github.com/kyma-project/lifecycle-manager/pkg/module/parse"
"github.com/kyma-project/lifecycle-manager/pkg/module/sync"
"github.com/kyma-project/lifecycle-manager/pkg/remote"
"github.com/kyma-project/lifecycle-manager/pkg/signature"
"github.com/kyma-project/lifecycle-manager/pkg/status"
manifestV1alpha1 "github.com/kyma-project/module-manager/api/v1alpha1"

"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -63,9 +62,9 @@ type KymaReconciler struct {
record.EventRecorder
RequeueIntervals
signature.VerificationSettings
RemoteClientCache *remote.ClientCache
SKRWebhookChartManager deploy.SKRWebhookChartManager
KcpRestConfig *rest.Config
RemoteClientCache *remote.ClientCache
deploy.SKRWebhookChartManager
KcpRestConfig *rest.Config
}

//nolint:lll
Expand Down Expand Up @@ -233,21 +232,48 @@ func (r *KymaReconciler) HandleInitialState(ctx context.Context, kyma *v1alpha1.
func (r *KymaReconciler) HandleProcessingState(ctx context.Context, kyma *v1alpha1.Kyma) error {
logger := log.FromContext(ctx)

statusUpdateRequiredFromModulesSync, err := r.syncModules(ctx, kyma)
statusUpdateRequiredFromCatalog, err := r.syncModuleCatalog(ctx, kyma)
if err != nil {
return r.UpdateStatusWithEventFromErr(ctx, kyma, v1alpha1.StateError,
fmt.Errorf("could not synchronize remote module catalog: %w", err))
}

var modules common.Modules
// these are the actual modules
modules, err = r.GenerateModulesFromTemplate(ctx, kyma)
if err != nil {
return r.UpdateStatusWithEventFromErr(ctx, kyma, v1alpha1.StateError, err)
return r.UpdateStatusWithEventFromErr(ctx, kyma, v1alpha1.StateError,
fmt.Errorf("error while fetching modules during processing: %w", err))
}

runner := sync.New(r)

statusUpdateRequiredFromModuleSync, err := runner.Sync(ctx, kyma, modules)
if err != nil {
return r.UpdateStatusWithEventFromErr(ctx, kyma, v1alpha1.StateError,
fmt.Errorf("sync failed: %w", err))
}

statusUpdateRequiredFromModuleStatusSync := runner.SyncModuleStatus(ctx, kyma, modules)

// If module get removed from kyma, the module deletion happens here.
statusUpdateRequiredFromDeletion, err := r.DeleteNoLongerExistingModules(ctx, kyma)
if err != nil {
return r.UpdateStatusWithEventFromErr(ctx, kyma, v1alpha1.StateError,
fmt.Errorf("error while syncing conditions during deleting non exists modules: %w", err))
}
statusUpdateRequiredFromSKRWebhookSync := false
if kyma.Spec.Sync.Enabled && r.SKRWebhookChartManager != nil {
if statusUpdateRequiredFromSKRWebhookSync, err = r.SKRWebhookChartManager.Install(ctx, kyma); err != nil {
if kyma.Spec.Sync.Enabled {
if statusUpdateRequiredFromSKRWebhookSync, err = r.InstallWebhookChart(ctx, kyma,
r.RemoteClientCache, remote.NewClientWithConfig(r.Client, r.KcpRestConfig)); err != nil {
kyma.UpdateCondition(v1alpha1.ConditionReasonSKRWebhookIsReady, metav1.ConditionFalse)
return err
}
}

kyma.SyncConditionsWithModuleStates()

isStatusUpdateRequired := statusUpdateRequiredFromModulesSync || statusUpdateRequiredFromSKRWebhookSync
isStatusUpdateRequired := statusUpdateRequiredFromModuleSync || statusUpdateRequiredFromModuleStatusSync ||
statusUpdateRequiredFromDeletion || statusUpdateRequiredFromSKRWebhookSync || statusUpdateRequiredFromCatalog
// if the ready condition is not applicable, but we changed the conditions, we still need to issue an update
if isStatusUpdateRequired {
if err := r.UpdateStatusWithEvent(ctx, kyma, v1alpha1.StateProcessing, "updating component conditions"); err != nil {
Expand All @@ -266,48 +292,16 @@ func (r *KymaReconciler) HandleProcessingState(ctx context.Context, kyma *v1alph
return nil
}

func (r *KymaReconciler) syncModules(ctx context.Context, kyma *v1alpha1.Kyma) (bool, error) {
statusUpdateRequiredFromCatalog, err := r.syncModuleCatalog(ctx, kyma)
if err != nil {
return false, fmt.Errorf("could not synchronize remote module catalog: %w", err)
}

var modules common.Modules
// these are the actual modules
modules, err = r.GenerateModulesFromTemplate(ctx, kyma)
if err != nil {
return false, fmt.Errorf("error while fetching modules during processing: %w", err)
}

runner := sync.New(r)

statusUpdateRequiredFromModuleSync, err := runner.Sync(ctx, kyma, modules)
if err != nil {
return false, fmt.Errorf("sync failed: %w", err)
}

statusUpdateRequiredFromModuleStatusSync := runner.SyncModuleStatus(ctx, kyma, modules)
// If module get removed from kyma, the module deletion happens here.
statusUpdateRequiredFromDeletion, err := r.DeleteNoLongerExistingModules(ctx, kyma)
if err != nil {
return false, fmt.Errorf("error while syncing conditions during deleting non exists modules: %w", err)
}
return statusUpdateRequiredFromCatalog || statusUpdateRequiredFromModuleSync ||
statusUpdateRequiredFromModuleStatusSync || statusUpdateRequiredFromDeletion, nil
}

func (r *KymaReconciler) HandleDeletingState(ctx context.Context, kyma *v1alpha1.Kyma) (bool, error) {
logger := log.FromContext(ctx)

if kyma.Spec.Sync.Enabled && r.SKRWebhookChartManager != nil {
if err := r.SKRWebhookChartManager.Remove(ctx, kyma); err != nil {
if kyma.Spec.Sync.Enabled {
if err := r.RemoveWebhookChart(ctx, kyma); err != nil {
// here we expect that an error is normal and means we have to try again if it didn't work
r.Event(kyma, "Normal", "WebhookChartRemoval", err.Error())
return true, nil
}
}

if kyma.Spec.Sync.Enabled {
if err := catalog.NewRemoteCatalogFromKyma(kyma).Delete(ctx); err != nil {
err := fmt.Errorf("could not delete remote module catalog: %w", err)
r.Event(kyma, "Warning", string(DeletionError), err.Error())
Expand Down Expand Up @@ -407,7 +401,7 @@ func (r *KymaReconciler) DeleteNoLongerExistingModules(ctx context.Context, kyma
}

func (r *KymaReconciler) deleteModule(ctx context.Context, moduleStatus *v1alpha1.ModuleStatus) error {
manifest := manifestv1alpha1.Manifest{}
manifest := manifestV1alpha1.Manifest{}
manifest.SetNamespace(moduleStatus.Namespace)
manifest.SetName(moduleStatus.Name)
return r.Delete(ctx, &manifest, &client.DeleteOptions{})
Expand Down
7 changes: 3 additions & 4 deletions controllers/kyma_controller_remote_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

ocm "github.com/gardener/component-spec/bindings-go/apis/v2"

"github.com/kyma-project/lifecycle-manager/api/v1alpha1"
. "github.com/kyma-project/lifecycle-manager/pkg/testutils"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/kyma-project/lifecycle-manager/api/v1alpha1"
. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
)

var _ = Describe("Kyma with multiple module CRs in remote sync mode", Ordered, func() {
Expand Down
5 changes: 3 additions & 2 deletions controllers/kyma_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyma-project/lifecycle-manager/api/v1alpha1"
. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/kyma-project/lifecycle-manager/api/v1alpha1"
. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
)

var _ = Describe("Kyma with no ModuleTemplate", Ordered, func() {
Expand Down
5 changes: 3 additions & 2 deletions controllers/kyma_moduleinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"errors"
"fmt"

"github.com/kyma-project/lifecycle-manager/api/v1alpha1"
. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/kyma-project/lifecycle-manager/api/v1alpha1"
. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
)

var (
Expand Down
9 changes: 6 additions & 3 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"testing"
"time"

"github.com/kyma-project/lifecycle-manager/pkg/deploy"

moduleManagerV1alpha1 "github.com/kyma-project/module-manager/api/v1alpha1"
//nolint:gci
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -136,9 +138,10 @@ var _ = BeforeSuite(func() {

remoteClientCache := remote.NewClientCache()
err = (&controllers.KymaReconciler{
Client: k8sManager.GetClient(),
EventRecorder: k8sManager.GetEventRecorderFor(operatorv1alpha1.OperatorName),
RequeueIntervals: intervals,
Client: k8sManager.GetClient(),
EventRecorder: k8sManager.GetEventRecorderFor(operatorv1alpha1.OperatorName),
RequeueIntervals: intervals,
SKRWebhookChartManager: &deploy.DisabledSKRWebhookChartManager{},
VerificationSettings: signature.VerificationSettings{
EnableVerification: false,
},
Expand Down
4 changes: 1 addition & 3 deletions controllers/watcher_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"errors"
"fmt"

"go.uber.org/zap"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"k8s.io/client-go/rest"
Expand Down Expand Up @@ -72,7 +70,7 @@ func (r *WatcherReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

watcherObj := &v1alpha1.Watcher{}
if err := r.Get(ctx, client.ObjectKey{Name: req.Name, Namespace: req.Namespace}, watcherObj); err != nil {
logger.V(int(zap.DebugLevel)).Info("Failed to get reconciliation object")
logger.Error(err, "Failed to get reconciliation object")
return ctrl.Result{}, client.IgnoreNotFound(err)
}

Expand Down
Loading

0 comments on commit 2cf70ce

Please sign in to comment.