github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/argocdexport/export.go (about)

     1  // Copyright 2019 ArgoCD Operator Developers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // 	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package argocdexport
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/sethvargo/go-password/password"
    21  	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
    22  
    23  	argoprojv1alpha1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
    24  	argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
    25  	"github.com/argoproj-labs/argocd-operator/common"
    26  	"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
    27  )
    28  
    29  // generateBackupKey will generate and return the backup key for the export process.
    30  func generateBackupKey() ([]byte, error) {
    31  	pass, err := password.Generate(
    32  		common.ArgoCDDefaultBackupKeyLength,
    33  		common.ArgoCDDefaultBackupKeyNumDigits,
    34  		common.ArgoCDDefaultBackupKeyNumSymbols,
    35  		false, false)
    36  
    37  	return []byte(pass), err
    38  }
    39  
    40  // reconcileExport will ensure that the resources for the export process are present for the ArgoCDExport.
    41  func (r *ReconcileArgoCDExport) reconcileExport(cr *argoprojv1alpha1.ArgoCDExport) error {
    42  	log.Info("reconciling export secret")
    43  	if err := r.reconcileExportSecret(cr); err != nil {
    44  		return err
    45  	}
    46  
    47  	if cr.Spec.Schedule != nil && len(*cr.Spec.Schedule) > 0 {
    48  		log.Info("reconciling export cronjob")
    49  		if err := r.reconcileCronJob(cr); err != nil {
    50  			return err
    51  		}
    52  	} else {
    53  		log.Info("reconciling export job")
    54  		if err := r.reconcileJob(cr); err != nil {
    55  			return err
    56  		}
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  // reconcileExportSecret will ensure that the Secret used for the export process is present.
    63  func (r *ReconcileArgoCDExport) reconcileExportSecret(cr *argoprojv1alpha1.ArgoCDExport) error {
    64  	name := argoutil.FetchStorageSecretName(cr)
    65  	// Dummy CR to retrieve secret
    66  	a := &argoproj.ArgoCD{}
    67  	a.ObjectMeta = cr.ObjectMeta
    68  	secret := argoutil.NewSecretWithName(a, name)
    69  	if argoutil.IsObjectFound(r.Client, cr.Namespace, name, secret) {
    70  		backupKey := secret.Data[common.ArgoCDKeyBackupKey]
    71  		if len(backupKey) <= 0 {
    72  			backupKey, err := generateBackupKey()
    73  			if err != nil {
    74  				return err
    75  			}
    76  			secret.Data[common.ArgoCDKeyBackupKey] = backupKey
    77  			return r.Client.Update(context.TODO(), secret)
    78  		}
    79  
    80  		return nil // TODO: Handle case where backup key changes, should trigger a new export?
    81  	}
    82  
    83  	backupKey, err := generateBackupKey()
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	secret.Data = map[string][]byte{
    89  		common.ArgoCDKeyBackupKey: backupKey,
    90  	}
    91  
    92  	if err := controllerutil.SetControllerReference(cr, secret, r.Scheme); err != nil {
    93  		return err
    94  	}
    95  	return r.Client.Create(context.TODO(), secret)
    96  }
    97  
    98  // validateExport will ensure that the given ArgoCDExport is valid.
    99  func (r *ReconcileArgoCDExport) validateExport(cr *argoprojv1alpha1.ArgoCDExport) error {
   100  	if len(cr.Status.Phase) <= 0 {
   101  		cr.Status.Phase = "Pending"
   102  		return r.Client.Status().Update(context.TODO(), cr)
   103  	}
   104  	return nil
   105  }