-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Application controller boilerplate (#2)
- Loading branch information
Showing
5 changed files
with
190 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
|
||
appclientset "github.com/argoproj/argo-cd/pkg/client/clientset/versioned" | ||
appinformers "github.com/argoproj/argo-cd/pkg/client/informers/externalversions" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/util/workqueue" | ||
) | ||
|
||
const ( | ||
appResyncPeriod = 10 * time.Minute | ||
) | ||
|
||
// ApplicationController is the controller for application resources. | ||
type ApplicationController struct { | ||
kubeclientset kubernetes.Interface | ||
applicationclientset appclientset.Interface | ||
appQueue workqueue.RateLimitingInterface | ||
|
||
appInformer cache.SharedIndexInformer | ||
} | ||
|
||
// NewApplicationController creates new instance of ApplicationController. | ||
func NewApplicationController(kubeclientset kubernetes.Interface, applicationclientset appclientset.Interface) *ApplicationController { | ||
appQueue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()) | ||
return &ApplicationController{ | ||
kubeclientset: kubeclientset, | ||
applicationclientset: applicationclientset, | ||
appQueue: appQueue, | ||
appInformer: newApplicationInformer(applicationclientset, appQueue), | ||
} | ||
} | ||
|
||
// Run starts the Application CRD controller. | ||
func (ctrl *ApplicationController) Run(ctx context.Context, appWorkers int) { | ||
defer runtime.HandleCrash() | ||
defer ctrl.appQueue.ShutDown() | ||
|
||
go ctrl.appInformer.Run(ctx.Done()) | ||
|
||
if !cache.WaitForCacheSync(ctx.Done(), ctrl.appInformer.HasSynced) { | ||
log.Error("Timed out waiting for caches to sync") | ||
return | ||
} | ||
|
||
for i := 0; i < appWorkers; i++ { | ||
go wait.Until(ctrl.runWorker, time.Second, ctx.Done()) | ||
} | ||
|
||
<-ctx.Done() | ||
} | ||
|
||
func (ctrl *ApplicationController) processNextItem() bool { | ||
appKey, shutdown := ctrl.appQueue.Get() | ||
defer ctrl.appQueue.Done(appKey) | ||
if shutdown { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (ctrl *ApplicationController) runWorker() { | ||
for ctrl.processNextItem() { | ||
} | ||
} | ||
|
||
func newApplicationInformer(appclientset appclientset.Interface, appQueue workqueue.RateLimitingInterface) cache.SharedIndexInformer { | ||
appInformerFactory := appinformers.NewSharedInformerFactory( | ||
appclientset, | ||
appResyncPeriod, | ||
) | ||
informer := appInformerFactory.Argoproj().V1alpha1().Applications().Informer() | ||
informer.AddEventHandler( | ||
cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) { | ||
key, err := cache.MetaNamespaceKeyFunc(obj) | ||
if err == nil { | ||
appQueue.Add(key) | ||
} | ||
}, | ||
UpdateFunc: func(old, new interface{}) { | ||
key, err := cache.MetaNamespaceKeyFunc(new) | ||
if err == nil { | ||
appQueue.Add(key) | ||
} | ||
}, | ||
DeleteFunc: func(obj interface{}) { | ||
// IndexerInformer uses a delta queue, therefore for deletes we have to use this | ||
// key function. | ||
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) | ||
if err == nil { | ||
appQueue.Add(key) | ||
} | ||
}, | ||
}, | ||
) | ||
return informer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/argoproj/argo-cd/application/controller" | ||
"github.com/argoproj/argo-cd/cmd/argocd/commands" | ||
appclientset "github.com/argoproj/argo-cd/pkg/client/clientset/versioned" | ||
"github.com/spf13/cobra" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"os" | ||
) | ||
|
||
const ( | ||
// CLIName is the name of the CLI | ||
cliName = "application-controller" | ||
) | ||
|
||
func newCommand() *cobra.Command { | ||
var ( | ||
kubeConfigOverrides clientcmd.ConfigOverrides | ||
kubeConfigPath string | ||
) | ||
var command = cobra.Command{ | ||
Use: cliName, | ||
Short: "application-controller is a controller to operate on applications CRD", | ||
Run: func(c *cobra.Command, args []string) { | ||
kubeConfig := commands.GetKubeConfig(kubeConfigPath, kubeConfigOverrides) | ||
|
||
kubeClient := kubernetes.NewForConfigOrDie(kubeConfig) | ||
appClient := appclientset.NewForConfigOrDie(kubeConfig) | ||
|
||
appController := controller.NewApplicationController(kubeClient, appClient) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
go appController.Run(ctx, 1) | ||
// Wait forever | ||
select {} | ||
}, | ||
} | ||
|
||
command.Flags().StringVar(&kubeConfigPath, "kubeconfig", "", "Path to the config file to use for CLI requests.") | ||
kubeConfigOverrides = clientcmd.ConfigOverrides{} | ||
clientcmd.BindOverrideFlags(&kubeConfigOverrides, command.Flags(), clientcmd.RecommendedConfigOverrideFlags("")) | ||
|
||
return &command | ||
} | ||
|
||
func main() { | ||
if err := newCommand().Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters