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

Adding ComponentConfig example #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/crd/bases/github.go.hein.dev_repositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.2.4
controller-gen.kubebuilder.io/version: v0.2.5
creationTimestamp: null
name: repositories.github.go.hein.dev
spec:
Expand Down
12 changes: 12 additions & 0 deletions config/manager/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: config.go.hein.dev/v1
kind: GithubConfiguration
spec:
port: 9443
metricsBindAddress: ":8081"
leaderElection:
leaderElect: true
resourceName: foo
resourceNamespace: default
renewDeadline: 10m
leaseDuration: 1h
retryPeriod: 2m
9 changes: 9 additions & 0 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
resources:
- manager.yaml

generatorOptions:
disableNameSuffixHash: true

configMapGenerator:
- name: manager-config
files:
- config.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
Expand Down
3 changes: 2 additions & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
Expand All @@ -49,7 +50,7 @@ func TestAPIs(t *testing.T) {

RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{envtest.NewlineReporter{}})
[]Reporter{printer.NewlineReporter{}})
}

var _ = BeforeSuite(func(done Done) {
Expand Down
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ require (
github.com/go-logr/logr v0.1.0
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-github/v28 v28.1.1
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
k8s.io/apimachinery v0.0.0-20190913080033-27d36303b655
k8s.io/client-go v0.0.0-20190918160344-1fbdaa4c8d90
k8s.io/apimachinery v0.18.0
k8s.io/client-go v0.18.0
k8s.io/component-base v0.18.0
sigs.k8s.io/controller-runtime v0.4.0
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e // indirect
)

replace sigs.k8s.io/controller-runtime => go.hein.dev/controller-runtime v0.4.1-0.20200408104450-cef7fb64b6cd
166 changes: 166 additions & 0 deletions go.sum

Large diffs are not rendered by default.

33 changes: 21 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import (
"context"
"flag"
"os"
"time"

githubv1alpha1 "go.hein.dev/github-controller/api/v1alpha1"
"go.hein.dev/github-controller/controllers"
"go.hein.dev/github-controller/git"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
ctrlv1alpha1 "sigs.k8s.io/controller-runtime/pkg/api/config/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
Expand All @@ -39,37 +40,45 @@ var (
)

func init() {
_ = clientgoscheme.AddToScheme(scheme)

_ = githubv1alpha1.AddToScheme(scheme)
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(ctrlv1alpha1.AddToScheme(scheme))
utilruntime.Must(githubv1alpha1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

func main() {
var resyncTimeout = time.Minute * 30
var port int
var metricsAddr string
var enableLeaderElection bool
var actualDelete bool
var config string
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.IntVar(&port, "port", 9443, "The port the server binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&actualDelete, "actual-delete", false, "default: false; when true it will actually delete repos when the manifest is deleted.")
flag.StringVar(&config, "config", "", "Config file for loading configurations.")

flag.Parse()

ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
}))

options := ctrl.Options{Scheme: scheme}
var err error

if config != "" {
options, err = ctrl.NewOptionsFromComponentConfig(scheme, config, &ctrlv1alpha1.DefaultControllerConfiguration{})
if err != nil {
setupLog.Error(err, "unable to update options")
os.Exit(1)
}
}

gitclient := git.New(context.Background(), os.Getenv("GITHUB_AUTH_TOKEN"))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
Port: 9443,
SyncPeriod: &resyncTimeout,
})
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand Down