github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/argocdexport/local.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  	"fmt"
    20  	"strings"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
    24  
    25  	argoproj "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
    26  	"github.com/argoproj-labs/argocd-operator/common"
    27  	"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
    28  )
    29  
    30  // reconcileLocalStorage will ensure the PersistentVolumeClaim is present for the ArgoCDExport.
    31  func (r *ReconcileArgoCDExport) reconcileLocalStorage(cr *argoproj.ArgoCDExport) error {
    32  	if cr.Spec.Storage == nil || strings.ToLower(cr.Spec.Storage.Backend) != common.ArgoCDExportStorageBackendLocal {
    33  		return nil // Do nothing if storage or local options not set
    34  	}
    35  
    36  	log.Info("reconciling local pvc")
    37  	if err := r.reconcilePVC(cr); err != nil {
    38  		return err
    39  	}
    40  	return nil
    41  }
    42  
    43  // reconcilePVC will ensure that the PVC for the ArgoCDExport is present.
    44  func (r *ReconcileArgoCDExport) reconcilePVC(cr *argoproj.ArgoCDExport) error {
    45  	if cr.Status.Phase == common.ArgoCDStatusCompleted {
    46  		return nil // Nothing to see here, move along...
    47  	}
    48  
    49  	pvc := argoutil.NewPersistentVolumeClaim(cr.ObjectMeta)
    50  	if argoutil.IsObjectFound(r.Client, cr.Namespace, pvc.Name, pvc) {
    51  		return nil // PVC exists, move along...
    52  	}
    53  
    54  	// Allow override of PVC spec
    55  	if cr.Spec.Storage.PVC != nil {
    56  		pvc.Spec = *cr.Spec.Storage.PVC
    57  	} else {
    58  		pvc.Spec.AccessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}
    59  		pvc.Spec.Resources = argoutil.DefaultPVCResources()
    60  	}
    61  
    62  	if err := controllerutil.SetControllerReference(cr, pvc, r.Scheme); err != nil {
    63  		return err
    64  	}
    65  
    66  	// Create PVC
    67  	log.Info(fmt.Sprintf("creating new pvc: %s", pvc.Name))
    68  	if err := r.Client.Create(context.TODO(), pvc); err != nil {
    69  		return err
    70  	}
    71  
    72  	// Create event
    73  	log.Info("creating new event")
    74  	return argoutil.CreateEvent(r.Client, "Normal", "Exporting", "Created claim for export process.", "PersistentVolumeClaimCreated", cr.ObjectMeta, cr.TypeMeta)
    75  }