github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/steps/lifecycle_manager_kubeconfig.go (about) 1 package steps 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/kyma-project/kyma-environment-broker/internal" 9 "github.com/kyma-project/kyma-environment-broker/internal/process" 10 "github.com/kyma-project/kyma-environment-broker/internal/storage" 11 "github.com/sirupsen/logrus" 12 corev1 "k8s.io/api/core/v1" 13 "k8s.io/apimachinery/pkg/api/errors" 14 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 15 "sigs.k8s.io/controller-runtime/pkg/client" 16 ) 17 18 // syncKubeconfig step ensures desired state of kubeconfig secret for lifecycle manager 19 type syncKubeconfig struct { 20 k8sClient client.Client 21 operationManager *process.OperationManager 22 } 23 24 // deleteKubeconfig step ensures kubeconfig secret for lifecycle manager is removed during deprovisioning 25 type deleteKubeconfig struct { 26 k8sClient client.Client 27 operationManager *process.OperationManager 28 } 29 30 func SyncKubeconfig(os storage.Operations, k8sClient client.Client) syncKubeconfig { 31 return syncKubeconfig{k8sClient: k8sClient, operationManager: process.NewOperationManager(os)} 32 } 33 34 func DeleteKubeconfig(os storage.Operations, k8sClient client.Client) deleteKubeconfig { 35 return deleteKubeconfig{k8sClient: k8sClient, operationManager: process.NewOperationManager(os)} 36 } 37 38 func (_ syncKubeconfig) Name() string { 39 return "Sync_Kubeconfig" 40 } 41 42 func (_ deleteKubeconfig) Name() string { 43 return "Delete_Kubeconfig" 44 } 45 46 func (s syncKubeconfig) Run(o internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) { 47 secret := initSecret(o) 48 if err := s.k8sClient.Create(context.Background(), secret); errors.IsAlreadyExists(err) { 49 if err := s.k8sClient.Update(context.Background(), secret); err != nil { 50 msg := fmt.Sprintf("failed to update kubeconfig secret %v/%v for lifecycle manager: %v", secret.Namespace, secret.Name, err) 51 log.Error(msg) 52 return s.operationManager.RetryOperation(o, msg, err, time.Minute, time.Minute*5, log) 53 } 54 } else if err != nil { 55 msg := fmt.Sprintf("failed to create kubeconfig secret %v/%v for lifecycle manager: %v", secret.Namespace, secret.Name, err) 56 log.Error(msg) 57 return s.operationManager.RetryOperation(o, msg, err, time.Minute, time.Minute*5, log) 58 } 59 return o, 0, nil 60 } 61 62 func (s deleteKubeconfig) Run(o internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) { 63 if o.KymaResourceNamespace == "" || o.KymaResourceName == "" { 64 log.Info("kubeconfig Secret should not exist, skipping") 65 return o, 0, nil 66 } 67 secret := initSecret(o) 68 if err := s.k8sClient.Delete(context.Background(), secret); err != nil && !errors.IsNotFound(err) { 69 msg := fmt.Sprintf("failed to delete kubeconfig Secret %v/%v for lifecycle manager: %v", secret.Namespace, secret.Name, err) 70 log.Error(msg) 71 return s.operationManager.RetryOperationWithoutFail(o, s.Name(), msg, time.Minute, time.Minute*5, log) 72 } 73 return o, 0, nil 74 } 75 76 func initSecret(o internal.Operation) *corev1.Secret { 77 secret := &corev1.Secret{ 78 ObjectMeta: metav1.ObjectMeta{ 79 Namespace: o.InstanceDetails.KymaResourceNamespace, 80 Name: KymaKubeconfigName(o), 81 }, 82 StringData: map[string]string{ 83 "config": o.Kubeconfig, 84 }, 85 } 86 ApplyLabelsAndAnnotationsForLM(secret, o) 87 return secret 88 } 89 90 // NOTE: adapter for upgrade_kyma which is currently not using shared staged_manager 91 type syncKubeconfigUpgradeKyma struct { 92 syncKubeconfig 93 } 94 95 func SyncKubeconfigUpgradeKyma(os storage.Operations, k8sClient client.Client) syncKubeconfigUpgradeKyma { 96 return syncKubeconfigUpgradeKyma{SyncKubeconfig(os, k8sClient)} 97 } 98 99 func (s syncKubeconfigUpgradeKyma) Run(o internal.UpgradeKymaOperation, logger logrus.FieldLogger) (internal.UpgradeKymaOperation, time.Duration, error) { 100 o2, w, err := s.syncKubeconfig.Run(o.Operation, logger) 101 return internal.UpgradeKymaOperation{o2}, w, err 102 }