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

Allow segregate applications among controllers using label #9

Merged
merged 1 commit into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions cmd/argocd-application-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@ func newCommand() *cobra.Command {
appClient := appclientset.NewForConfigOrDie(kubeConfig)

// TODO (amatyushentsev): Use config map to store controller configuration
namespace := "default"
config := controller.ApplicationControllerConfig{
Namespace: "default",
InstanceID: "",
}
appResyncPeriod := time.Minute * 10

appManager := application.NewAppManager(
nativeGitClient,
repository.NewServer(namespace, kubeClient, appClient),
repository.NewServer(config.Namespace, kubeClient, appClient),
application.NewKsonnetAppComparator(),
appResyncPeriod)

appController := controller.NewApplicationController(kubeClient, appClient, appManager, appResyncPeriod)
appController := controller.NewApplicationController(kubeClient, appClient, appManager, appResyncPeriod, &config)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
4 changes: 4 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package common

import "github.com/argoproj/argo-cd/pkg/apis/application"

const (
// MetadataPrefix is the prefix used for our labels and annotations
MetadataPrefix = "argocd.argoproj.io"
Expand All @@ -23,4 +25,6 @@ var (

// LabelKeySecretType contains the type of argocd secret (either 'cluster' or 'repo')
LabelKeySecretType = MetadataPrefix + "/secret-type"
// LabelKeyApplicationControllerInstanceID is the label which allows to separate application among multiple running application controllers.
LabelKeyApplicationControllerInstanceID = application.ApplicationFullName + "/controller-instanceid"
)
39 changes: 35 additions & 4 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"

"github.com/argoproj/argo-cd/common"
appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
appclientset "github.com/argoproj/argo-cd/pkg/client/clientset/versioned"
appinformers "github.com/argoproj/argo-cd/pkg/client/informers/externalversions"
Expand All @@ -11,6 +12,10 @@ import (
"time"

"github.com/argoproj/argo-cd/application"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
Expand All @@ -26,21 +31,28 @@ type ApplicationController struct {
applicationClientset appclientset.Interface
appQueue workqueue.RateLimitingInterface
appInformer cache.SharedIndexInformer
config *ApplicationControllerConfig
}

type ApplicationControllerConfig struct {
InstanceID string
Namespace string
}

// NewApplicationController creates new instance of ApplicationController.
func NewApplicationController(
kubeClientset kubernetes.Interface,
applicationClientset appclientset.Interface,
appManager *application.Manager,
appResyncPeriod time.Duration) *ApplicationController {
appResyncPeriod time.Duration,
config *ApplicationControllerConfig) *ApplicationController {
appQueue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
return &ApplicationController{
appManager: appManager,
kubeClientset: kubeClientset,
applicationClientset: applicationClientset,
appQueue: appQueue,
appInformer: newApplicationInformer(applicationClientset, appQueue, appResyncPeriod),
appInformer: newApplicationInformer(applicationClientset, appQueue, appResyncPeriod, config),
}
}

Expand Down Expand Up @@ -109,10 +121,29 @@ func (ctrl *ApplicationController) persistApp(app *appv1.Application) {
log.Info("Application update successful")
}

func newApplicationInformer(appClientset appclientset.Interface, appQueue workqueue.RateLimitingInterface, appResyncPeriod time.Duration) cache.SharedIndexInformer {
appInformerFactory := appinformers.NewSharedInformerFactory(
func newApplicationInformer(
appClientset appclientset.Interface, appQueue workqueue.RateLimitingInterface, appResyncPeriod time.Duration, config *ApplicationControllerConfig) cache.SharedIndexInformer {

appInformerFactory := appinformers.NewFilteredSharedInformerFactory(
appClientset,
appResyncPeriod,
config.Namespace,
func(options *metav1.ListOptions) {
var instanceIDReq *labels.Requirement
var err error
if config.InstanceID != "" {
instanceIDReq, err = labels.NewRequirement(common.LabelKeyApplicationControllerInstanceID, selection.Equals, []string{config.InstanceID})
} else {
instanceIDReq, err = labels.NewRequirement(common.LabelKeyApplicationControllerInstanceID, selection.DoesNotExist, nil)
}
if err != nil {
panic(err)
}

options.FieldSelector = fields.Everything().String()
labelSelector := labels.NewSelector().Add(*instanceIDReq)
options.LabelSelector = labelSelector.String()
},
)
informer := appInformerFactory.Argoproj().V1alpha1().Applications().Informer()
informer.AddEventHandler(
Expand Down