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

     1  package deprovisioning
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/process/input"
     9  
    10  	"github.com/kyma-project/kyma-environment-broker/internal/process/steps"
    11  
    12  	"k8s.io/apimachinery/pkg/api/errors"
    13  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    14  	"sigs.k8s.io/controller-runtime/pkg/client"
    15  
    16  	"github.com/sirupsen/logrus"
    17  
    18  	"github.com/kyma-project/kyma-environment-broker/internal"
    19  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    20  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    21  )
    22  
    23  const (
    24  	backoffForK8SOperation = time.Second
    25  	timeoutForK8sOperation = 10 * time.Second
    26  )
    27  
    28  type DeleteKymaResourceStep struct {
    29  	operationManager   *process.OperationManager
    30  	kcpClient          client.Client
    31  	configProvider     input.ConfigurationProvider
    32  	defaultKymaVersion string
    33  }
    34  
    35  func NewDeleteKymaResourceStep(operations storage.Operations, kcpClient client.Client, configProvider input.ConfigurationProvider, defaultKymaVersion string) *DeleteKymaResourceStep {
    36  	return &DeleteKymaResourceStep{
    37  		operationManager:   process.NewOperationManager(operations),
    38  		kcpClient:          kcpClient,
    39  		configProvider:     configProvider,
    40  		defaultKymaVersion: defaultKymaVersion,
    41  	}
    42  }
    43  
    44  func (step *DeleteKymaResourceStep) Name() string {
    45  	return "Delete_Kyma_Resource"
    46  }
    47  
    48  func (step *DeleteKymaResourceStep) Run(operation internal.Operation, logger logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    49  	// read the KymaTemplate from the config if needed
    50  	if operation.KymaTemplate == "" {
    51  		cfg, err := step.configProvider.ProvideForGivenVersionAndPlan(step.defaultKymaVersion, broker.PlanNamesMapping[operation.Plan])
    52  		if err != nil {
    53  			return step.operationManager.RetryOperationWithoutFail(operation, step.Name(), "unable to get config for given version and plan", 5*time.Second, 30*time.Second, logger)
    54  		}
    55  		modifiedOperation, backoff, err := step.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    56  			op.KymaTemplate = cfg.KymaTemplate
    57  		}, logger)
    58  		if backoff > 0 {
    59  			return operation, backoff, err
    60  		}
    61  		operation = modifiedOperation
    62  	}
    63  	obj, err := steps.DecodeKymaTemplate(operation.KymaTemplate)
    64  	if err != nil {
    65  		return step.operationManager.RetryOperationWithoutFail(operation, step.Name(), "unable to decode kyma template", 5*time.Second, 30*time.Second, logger)
    66  	}
    67  
    68  	if operation.KymaResourceNamespace == "" {
    69  		logger.Warnf("namespace for Kyma resource not specified")
    70  		return operation, 0, nil
    71  	}
    72  	kymaResourceName := steps.KymaName(operation)
    73  	if kymaResourceName == "" {
    74  		logger.Infof("Kyma resource name is empty, skipping")
    75  		return operation, 0, nil
    76  	}
    77  
    78  	logger.Infof("Deleting Kyma resource: %s in namespace:%s", kymaResourceName, operation.KymaResourceNamespace)
    79  
    80  	kymaUnstructured := &unstructured.Unstructured{}
    81  	kymaUnstructured.SetName(kymaResourceName)
    82  	kymaUnstructured.SetNamespace(operation.KymaResourceNamespace)
    83  	kymaUnstructured.SetGroupVersionKind(obj.GroupVersionKind())
    84  
    85  	err = step.kcpClient.Delete(context.Background(), kymaUnstructured)
    86  	if err != nil {
    87  		if errors.IsNotFound(err) {
    88  			logger.Info("no Kyma resource to delete - ignoring")
    89  		} else {
    90  			logger.Errorf("unable to delete the Kyma resource: %s", err)
    91  			return step.operationManager.RetryOperationWithoutFail(operation, step.Name(), "unable to delete the Kyma resource", backoffForK8SOperation, timeoutForK8sOperation, logger)
    92  		}
    93  	}
    94  
    95  	return operation, 0, nil
    96  }