Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code-refactoring: Gitops 3127 implement applicationset controller package #1011

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions common/cmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package common

const (
EntryPointSh = "entrypoint.sh"
LogLevel = "--loglevel"
ArgoCDRepoServer = "--argocd-repo-server"
jaideepr97 marked this conversation as resolved.
Show resolved Hide resolved
)
2 changes: 1 addition & 1 deletion common/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const (
ArgoCDDefaultArgoImage = "quay.io/argoproj/argocd"

// ArgoCDDefaultArgoVersion is the Argo CD container image digest to use when version not specified.
ArgoCDDefaultArgoVersion = "sha256:7daba5f38b23f4f091951b727db6f87dc04ad396fd21044401502438d633836e" // v2.7.6
ArgoCDDefaultArgoVersion = "sha256:d40da8f5747415eb7f9b5c2d9b645aecd423888cad9b36e4f986bff8ecf0a786" // v2.8.3

// ArgoCDDefaultBackupKeyLength is the length of the generated default backup key.
ArgoCDDefaultBackupKeyLength = 32
Expand Down
15 changes: 13 additions & 2 deletions common/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ const (
// ArgoCDRepoServerTLS is the argocd repo server tls value.
ArgoCDRepoServerTLS = "argocd-repo-server-tls"

// K8sOSLinux is the value for kubernetes.io/os key for linux pods
K8sOSLinux = "linux"
// ArgoCDAppSetGitlabSCMTLSCertsConfigMapName is the hard-coded ApplicationSet Gitlab SCM TLS certificate data ConfigMap name.
ArgoCDAppSetGitlabSCMTLSCertsConfigMapName = "argocd-appset-gitlab-scm-tls-certs-cm"

// K8sOSLinux is the value for kubernetes.io/os key for linux pods
K8sOSLinux = "linux"

// ArgoCDRedisServerTLSSecretName is the name of the TLS secret for the redis-server
ArgoCDRedisServerTLSSecretName = "argocd-operator-redis-tls"

Expand All @@ -62,6 +63,15 @@ const (
VolumeMountPathTLS = "/app/config/tls"
VolumeMountPathRepoServerTLS = "/app/config/reposerver/tls"
WorkingDirApp = "/app"
PortWebhook = "webhook"
SSHKnownHosts = "ssh-known-hosts"
VolumeMountPathSSH = "/app/config/ssh"
GPGKeys = "gpg-keys"
VolumeMountPathGPG = "/app/config/gpg/source"
GPGKeyRing = "gpg-keyring"
VolumeMountPathGPGKeyring = "/app/config/gpg/keys"
VolumeTmp = "tmp"
VolumeMountPathTmp = "/tmp"
)

// API group versions and resource kinds
Expand All @@ -75,5 +85,6 @@ const (
RoleBindingKind = "RoleBinding"
ConfigMapKind = "ConfigMap"
SecretKind = "Secret"
ServiceKind = "Service"
ServiceAccountKind = "ServiceAccount"
)
73 changes: 71 additions & 2 deletions controllers/argocd/applicationset/applicationset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,89 @@ package applicationset

import (
argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/pkg/util"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type ApplicationSetReconciler struct {
Client *client.Client
Client client.Client
Scheme *runtime.Scheme
Instance *argoproj.ArgoCD
Logger logr.Logger
}

var (
resourceName string
resourceLabels map[string]string
)

func (asr *ApplicationSetReconciler) Reconcile() error {

// controller logic goes here
asr.Logger = ctrl.Log.WithName(ArgoCDApplicationSetControllerComponent).WithValues("instance", asr.Instance.Name, "instance-namespace", asr.Instance.Namespace)

resourceName = util.GenerateUniqueResourceName(asr.Instance.Name, asr.Instance.Namespace, ArgoCDApplicationSetControllerComponent)
resourceLabels = common.DefaultLabels(resourceName, asr.Instance.Name, ArgoCDApplicationSetControllerComponent)

if err := asr.reconcileServiceAccount(); err != nil {
asr.Logger.Info("reconciling applicationSet serviceaccount")
return err
}

if err := asr.reconcileRole(); err != nil {
asr.Logger.Info("reconciling applicationSet role")
return err
}

if err := asr.reconcileRoleBinding(); err != nil {
asr.Logger.Info("reconciling applicationSet roleBinding")
return err
}

if err := asr.reconcileDeployment(); err != nil {
asr.Logger.Info("reconciling applicationSet deployment")
return err
}

if err := asr.reconcileService(); err != nil {
asr.Logger.Info("reconciling applicationSet deployment")
return err
}

return nil
}

func (asr *ApplicationSetReconciler) DeleteResources() error {

var deletionError error = nil

if err := asr.DeleteService(resourceName, asr.Instance.Namespace); err != nil {
asr.Logger.Error(err, "DeleteResources: failed to delete service")
deletionError = err
}

if err := asr.DeleteDeployment(resourceName, asr.Instance.Namespace); err != nil {
asr.Logger.Error(err, "DeleteResources: failed to delete deployment")
deletionError = err
}

if err := asr.DeleteRoleBinding(resourceName, asr.Instance.Namespace); err != nil {
asr.Logger.Error(err, "DeleteResources: failed to delete roleBinding")
deletionError = err
}

if err := asr.DeleteRole(resourceName, asr.Instance.Namespace); err != nil {
asr.Logger.Error(err, "DeleteResources: failed to delete role")
deletionError = err
}

if err := asr.DeleteServiceAccount(resourceName, asr.Instance.Namespace); err != nil {
asr.Logger.Error(err, "DeleteResources: failed to delete serviceaccount")
deletionError = err
}

return deletionError
}
34 changes: 34 additions & 0 deletions controllers/argocd/applicationset/applicationset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package applicationset

import (
"testing"

"github.com/argoproj-labs/argocd-operator/api/v1beta1"
argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argocd/argocdcommon"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var testExpectedLabels = common.DefaultLabels(argocdcommon.TestArgoCDName, argocdcommon.TestNamespace, ArgoCDApplicationSetControllerComponent)

func makeTestApplicationSetReconciler(t *testing.T, objs ...runtime.Object) *ApplicationSetReconciler {
s := scheme.Scheme
assert.NoError(t, v1beta1.AddToScheme(s))

cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...).Build()
logger := ctrl.Log.WithName(ArgoCDApplicationSetControllerComponent)

return &ApplicationSetReconciler{
Client: cl,
Scheme: s,
Instance: argocdcommon.MakeTestArgoCD(func(a *argoproj.ArgoCD) {
a.Spec.ApplicationSet = &argoproj.ArgoCDApplicationSet{}
}),
Logger: logger,
}
}
9 changes: 9 additions & 0 deletions controllers/argocd/applicationset/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package applicationset

// Values
const (
ArgoCDApplicationSetControllerComponent = "applicationset-controller"
ArgoCDApplicationSetController = "argocd-applicationset-controller"
ApplicationSetGitlabSCMTlsCert = "appset-gitlab-scm-tls-cert"
ApplicationSetGitlabSCMTlsCertPath = "/app/tls/scm/cert"
jaideepr97 marked this conversation as resolved.
Show resolved Hide resolved
)
Loading
Loading