Skip to content

Commit

Permalink
code-refactoring: streamline resource packages (argoproj-labs#1160)
Browse files Browse the repository at this point in the history
* streamline resource pkgs

Signed-off-by: Jaideep Rao <[email protected]>
  • Loading branch information
jaideepr97 authored and Julia Teslia committed Apr 24, 2024
1 parent 2132994 commit 118b4d5
Show file tree
Hide file tree
Showing 52 changed files with 1,039 additions and 963 deletions.
7 changes: 5 additions & 2 deletions controllers/argocd/applicationset/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -47,7 +47,7 @@ func (asr *ApplicationSetReconciler) reconcileDeployment() error {

existingDeployment, err := workloads.GetDeployment(desiredDeployment.Name, desiredDeployment.Namespace, asr.Client)
if err != nil {
if !errors.IsNotFound(err) {
if !apierrors.IsNotFound(err) {
asr.Logger.Error(err, "reconcileDeployment: failed to retrieve deployment", "name", desiredDeployment.Name, "namespace", desiredDeployment.Namespace)
return err
}
Expand Down Expand Up @@ -105,6 +105,9 @@ func (asr *ApplicationSetReconciler) reconcileDeployment() error {

func (asr *ApplicationSetReconciler) deleteDeployment(name, namespace string) error {
if err := workloads.DeleteDeployment(name, namespace, asr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
asr.Logger.Error(err, "DeleteDeployment: failed to delete deployment", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/applicationset/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -80,6 +81,9 @@ func (asr *ApplicationSetReconciler) reconcileRole() error {

func (asr *ApplicationSetReconciler) deleteRole(name, namespace string) error {
if err := permissions.DeleteRole(name, namespace, asr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
asr.Logger.Error(err, "DeleteRole: failed to delete role", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/applicationset/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -109,6 +110,9 @@ func (asr *ApplicationSetReconciler) reconcileRoleBinding() error {

func (asr *ApplicationSetReconciler) deleteRoleBinding(name, namespace string) error {
if err := permissions.DeleteRoleBinding(name, namespace, asr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
asr.Logger.Error(err, "DeleteRole: failed to delete roleBinding", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/applicationset/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -72,6 +73,9 @@ func (asr *ApplicationSetReconciler) reconcileService() error {

func (asr *ApplicationSetReconciler) deleteService(name, namespace string) error {
if err := networking.DeleteService(name, namespace, asr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
asr.Logger.Error(err, "DeleteService: failed to delete service", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/applicationset/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/argoproj-labs/argocd-operator/pkg/permissions"

"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -60,6 +61,9 @@ func (asr *ApplicationSetReconciler) reconcileServiceAccount() error {

func (nr *ApplicationSetReconciler) deleteServiceAccount(name, namespace string) error {
if err := permissions.DeleteServiceAccount(name, namespace, nr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
nr.Logger.Error(err, "DeleteServiceAccount: failed to delete serviceAccount", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/applicationset/webhookroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

routev1 "github.com/openshift/api/route/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -92,6 +93,9 @@ func (asr *ApplicationSetReconciler) reconcileWebhookRoute() error {

func (asr *ApplicationSetReconciler) deleteWebhookRoute(name, namespace string) error {
if err := openshift.DeleteRoute(name, namespace, asr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
asr.Logger.Error(err, "DeleteRoute: failed to delete route", "name", name, "namespace", namespace)
return err
}
Expand Down
7 changes: 5 additions & 2 deletions controllers/argocd/notifications/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/argoproj-labs/argocd-operator/pkg/cluster"
"github.com/argoproj-labs/argocd-operator/pkg/workloads"

"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func (nr *NotificationsReconciler) reconcileConfigMap() error {

_, err = workloads.GetConfigMap(desiredConfigMap.Name, desiredConfigMap.Namespace, nr.Client)
if err != nil {
if !errors.IsNotFound(err) {
if !apierrors.IsNotFound(err) {
nr.Logger.Error(err, "reconcileConfigMap: failed to retrieve configMap", "name", desiredConfigMap.Name, "namespace", desiredConfigMap.Namespace)
return err
}
Expand All @@ -68,6 +68,9 @@ func (nr *NotificationsReconciler) reconcileConfigMap() error {

func (nr *NotificationsReconciler) deleteConfigMap(namespace string) error {
if err := workloads.DeleteConfigMap(common.NotificationsConfigMapName, namespace, nr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
nr.Logger.Error(err, "DeleteConfigMap: failed to delete configMap", "name", common.NotificationsConfigMapName, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/notifications/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -104,6 +105,9 @@ func (nr *NotificationsReconciler) reconcileDeployment() error {

func (nr *NotificationsReconciler) deleteDeployment(name, namespace string) error {
if err := workloads.DeleteDeployment(name, namespace, nr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
nr.Logger.Error(err, "DeleteDeployment: failed to delete deployment", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/notifications/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -80,6 +81,9 @@ func (nr *NotificationsReconciler) reconcileRole() error {

func (nr *NotificationsReconciler) deleteRole(name, namespace string) error {
if err := permissions.DeleteRole(name, namespace, nr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
nr.Logger.Error(err, "DeleteRole: failed to delete role", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/notifications/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -109,6 +110,9 @@ func (nr *NotificationsReconciler) reconcileRoleBinding() error {

func (nr *NotificationsReconciler) deleteRoleBinding(name, namespace string) error {
if err := permissions.DeleteRoleBinding(name, namespace, nr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
nr.Logger.Error(err, "DeleteRole: failed to delete roleBinding", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/notifications/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/argoproj-labs/argocd-operator/pkg/workloads"

"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -69,6 +70,9 @@ func (nr *NotificationsReconciler) reconcileSecret() error {

func (nr *NotificationsReconciler) deleteSecret(namespace string) error {
if err := workloads.DeleteSecret(common.NotificationsSecretName, namespace, nr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
nr.Logger.Error(err, "DeleteSecret: failed to delete secret", "name", common.NotificationsSecretName, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions controllers/argocd/notifications/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/argoproj-labs/argocd-operator/pkg/permissions"

"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -60,6 +61,9 @@ func (nr *NotificationsReconciler) reconcileServiceAccount() error {

func (nr *NotificationsReconciler) deleteServiceAccount(name, namespace string) error {
if err := permissions.DeleteServiceAccount(name, namespace, nr.Client); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
nr.Logger.Error(err, "DeleteServiceAccount: failed to delete serviceAccount", "name", name, "namespace", namespace)
return err
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/json-iterator/go v1.1.12
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
github.com/onsi/ginkgo v1.16.5
=======
Expand All @@ -29,6 +30,9 @@ require (
=======
github.com/onsi/ginkgo v1.16.5
>>>>>>> db87a01 (rebase: final changes (8/8) (#1144))
=======
github.com/onsi/ginkgo v1.16.4
>>>>>>> f9485c0 (code-refactoring: streamline resource packages (#1160))
github.com/onsi/gomega v1.27.10
github.com/openshift/api v3.9.1-0.20190916204813-cdbe64fb0c91+incompatible
github.com/openshift/client-go v0.0.0-20200325131901-f7baeb993edb
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,7 @@ github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
Expand All @@ -1590,6 +1591,10 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
>>>>>>> db87a01 (rebase: final changes (8/8) (#1144))
=======
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
>>>>>>> f9485c0 (code-refactoring: streamline resource packages (#1160))
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk=
Expand Down
19 changes: 19 additions & 0 deletions pkg/argoutil/TOBEREMOVED.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import (
tlsutil "github.com/operator-framework/operator-sdk/pkg/tls"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

// NewPrivateKey returns randomly generated RSA private key.
Expand Down Expand Up @@ -262,3 +265,19 @@ func AnnotationsForCluster(cr *argoproj.ArgoCD) map[string]string {
}
return annotations
}

// IsObjectFound will perform a basic check that the given object exists via the Kubernetes API.
// If an error occurs as part of the check, the function will return false.
func IsObjectFound(client client.Client, namespace string, name string, obj client.Object) bool {
return !apierrors.IsNotFound(FetchObject(client, namespace, name, obj))
}

func FilterObjectsBySelector(c client.Client, objectList client.ObjectList, selector labels.Selector) error {
return c.List(context.TODO(), objectList, client.MatchingLabelsSelector{Selector: selector})
}

// FetchObject will retrieve the object with the given namespace and name using the Kubernetes API.
// The result will be stored in the given object.
func FetchObject(client client.Client, namespace string, name string, obj client.Object) error {
return client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, obj)
}
16 changes: 3 additions & 13 deletions pkg/argoutil/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package argoutil

import (
"context"
"fmt"
<<<<<<< HEAD
"strings"
Expand Down Expand Up @@ -78,12 +77,6 @@ func CreateEvent(client client.Client, eventType, action, message, reason string
return client.Create(context.TODO(), event)
=======

"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

apierrors "k8s.io/apimachinery/pkg/api/errors"

argoprojv1alpha1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
)

Expand Down Expand Up @@ -113,12 +106,6 @@ func GenerateUniqueResourceName(instanceName, instanceNamespace, component strin
return fmt.Sprintf("%s-%s-%s", instanceName, instanceNamespace, component)
}

// FetchObject will retrieve the object with the given namespace and name using the Kubernetes API.
// The result will be stored in the given object.
func FetchObject(client client.Client, namespace string, name string, obj client.Object) error {
return client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, obj)
}

// FetchStorageSecretName will return the name of the Secret to use for the export process.
func FetchStorageSecretName(export *argoprojv1alpha1.ArgoCDExport) string {
<<<<<<< HEAD
Expand All @@ -131,6 +118,7 @@ func FetchStorageSecretName(export *argoprojv1alpha1.ArgoCDExport) string {
}
return name
}
<<<<<<< HEAD

// IsObjectFound will perform a basic check that the given object exists via the Kubernetes API.
// If an error occurs as part of the check, the function will return false.
Expand Down Expand Up @@ -193,3 +181,5 @@ func FilterObjectsBySelector(c client.Client, objectList client.ObjectList, sele
return c.List(context.TODO(), objectList, client.MatchingLabelsSelector{Selector: selector})
}
>>>>>>> ae37d0a (retain old and new constants. .github folder changes, fix errors (#1133))
=======
>>>>>>> f9485c0 (code-refactoring: streamline resource packages (#1160))
Loading

0 comments on commit 118b4d5

Please sign in to comment.