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

     1  /*
     2  Copyright 2019, 2021.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package argocdexport
    18  
    19  import (
    20  	"context"
    21  
    22  	ctrl "sigs.k8s.io/controller-runtime"
    23  
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  	logr "sigs.k8s.io/controller-runtime/pkg/log"
    28  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    29  
    30  	argoproj "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
    31  )
    32  
    33  var log = logr.Log.WithName("controller_argocdexport")
    34  
    35  // blank assignment to verify that ReconcileArgoCDExport implements reconcile.Reconciler
    36  var _ reconcile.Reconciler = &ReconcileArgoCDExport{}
    37  
    38  // ReconcileArgoCDExport reconciles a ArgoCDExport object
    39  // TODO(update) rename to ArgoCDExportReconciler
    40  type ReconcileArgoCDExport struct {
    41  	// This client, initialized using mgr.Client() above, is a split client
    42  	// that reads objects from the cache and writes to the apiserver
    43  	Client client.Client
    44  	Scheme *runtime.Scheme
    45  }
    46  
    47  //+kubebuilder:rbac:groups=argoproj.io,resources=argocdexports;argocdexports/finalizers;argocdexports/status,verbs=*
    48  
    49  // Reconcile is part of the main kubernetes reconciliation loop which aims to
    50  // move the current state of the cluster closer to the desired state.
    51  //
    52  // For more details, check Reconcile and its Result here:
    53  // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.9.2/pkg/reconcile
    54  func (r *ReconcileArgoCDExport) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
    55  	reqLogger := logr.FromContext(ctx, "Request.Namespace", request.Namespace, "Request.Name", request.Name)
    56  	reqLogger.Info("Reconciling ArgoCDExport")
    57  
    58  	// Fetch the ArgoCDExport instance
    59  	export := &argoproj.ArgoCDExport{}
    60  	err := r.Client.Get(ctx, request.NamespacedName, export)
    61  	if err != nil {
    62  		if errors.IsNotFound(err) {
    63  			// Request object not found, could have been deleted after reconcile request.
    64  			// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
    65  			// Return and don't requeue
    66  			return reconcile.Result{}, nil
    67  		}
    68  		// Error reading the object - requeue the request.
    69  		return reconcile.Result{}, err
    70  	}
    71  
    72  	if err := r.reconcileArgoCDExportResources(export); err != nil {
    73  		// Error reconciling ArgoCDExport sub-resources - requeue the request.
    74  		return reconcile.Result{}, err
    75  	}
    76  
    77  	return reconcile.Result{}, nil
    78  }
    79  
    80  // SetupWithManager sets up the controller with the Manager.
    81  func (r *ReconcileArgoCDExport) SetupWithManager(mgr ctrl.Manager) error {
    82  	bld := ctrl.NewControllerManagedBy(mgr)
    83  	setResourceWatches(bld)
    84  	return bld.Complete(r)
    85  }