github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/deprovisioning/check_kyma_resource_deleted_step.go (about)

     1  package deprovisioning
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/process/steps"
     8  
     9  	"k8s.io/apimachinery/pkg/api/errors"
    10  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    11  	"sigs.k8s.io/controller-runtime/pkg/client"
    12  
    13  	"github.com/sirupsen/logrus"
    14  
    15  	"github.com/kyma-project/kyma-environment-broker/internal"
    16  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    17  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    18  )
    19  
    20  type CheckKymaResourceDeletedStep struct {
    21  	operationManager *process.OperationManager
    22  	kcpClient        client.Client
    23  }
    24  
    25  func NewCheckKymaResourceDeletedStep(operations storage.Operations, kcpClient client.Client) *CheckKymaResourceDeletedStep {
    26  	return &CheckKymaResourceDeletedStep{
    27  		operationManager: process.NewOperationManager(operations),
    28  		kcpClient:        kcpClient,
    29  	}
    30  }
    31  
    32  func (step *CheckKymaResourceDeletedStep) Name() string {
    33  	return "Check_Kyma_Resource_Deleted"
    34  }
    35  
    36  func (step *CheckKymaResourceDeletedStep) Run(operation internal.Operation, logger logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    37  	if operation.KymaResourceNamespace == "" {
    38  		logger.Warnf("namespace for Kyma resource not specified")
    39  		return operation, 0, nil
    40  	}
    41  	kymaResourceName := steps.KymaName(operation)
    42  	if kymaResourceName == "" {
    43  		logger.Infof("Kyma resource name is empty, skipping")
    44  		return operation, 0, nil
    45  	}
    46  
    47  	obj, err := steps.DecodeKymaTemplate(operation.KymaTemplate)
    48  	if err != nil {
    49  		return step.operationManager.RetryOperationWithoutFail(operation, step.Name(), "unable to decode kyma template", 5*time.Second, 30*time.Second, logger)
    50  	}
    51  
    52  	logger.Infof("Checking existence of Kyma resource: %s in namespace:%s", kymaResourceName, operation.KymaResourceNamespace)
    53  
    54  	kymaUnstructured := &unstructured.Unstructured{}
    55  	kymaUnstructured.SetGroupVersionKind(obj.GroupVersionKind())
    56  	err = step.kcpClient.Get(context.Background(), client.ObjectKey{
    57  		Namespace: operation.KymaResourceNamespace,
    58  		Name:      kymaResourceName,
    59  	}, kymaUnstructured)
    60  
    61  	if err == nil {
    62  		logger.Infof("Kyma resource still exists")
    63  		return step.operationManager.RetryOperationWithoutFail(operation, step.Name(), "Kyma resource still exists", 15*time.Second, 30*time.Minute, logger)
    64  	}
    65  
    66  	if !errors.IsNotFound(err) {
    67  		logger.Errorf("unable to check Kyma resource existence: %s", err)
    68  		return step.operationManager.RetryOperationWithoutFail(operation, step.Name(), "unable to check Kyma resource existence", backoffForK8SOperation, timeoutForK8sOperation, logger)
    69  	}
    70  
    71  	return operation, 0, nil
    72  }