Merge pull request #47 from fluxcd/namespaces-events-logging
GOTK conformance for watching namespaces, events, logging
This commit is contained in:
commit
d228da0513
|
|
@ -28,13 +28,27 @@ spec:
|
|||
ports:
|
||||
- containerPort: 8080
|
||||
name: http-prom
|
||||
- containerPort: 9440
|
||||
name: healthz
|
||||
protocol: TCP
|
||||
env:
|
||||
- name: RUNTIME_NAMESPACE
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: metadata.namespace
|
||||
args:
|
||||
- --watch-all-namespaces
|
||||
- --log-level=info
|
||||
- --log-json
|
||||
- --enable-leader-election
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /readyz
|
||||
port: healthz
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /healthz
|
||||
port: healthz
|
||||
resources:
|
||||
limits:
|
||||
cpu: 1000m
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ import (
|
|||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
kuberecorder "k8s.io/client-go/tools/record"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/handler"
|
||||
|
|
@ -43,6 +44,7 @@ import (
|
|||
"sigs.k8s.io/controller-runtime/pkg/source"
|
||||
|
||||
imagev1_reflect "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
|
||||
"github.com/fluxcd/pkg/runtime/events"
|
||||
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
|
||||
"github.com/fluxcd/source-controller/pkg/git"
|
||||
|
||||
|
|
@ -64,8 +66,9 @@ const imagePolicyKey = ".spec.update.imagePolicy"
|
|||
// ImageUpdateAutomationReconciler reconciles a ImageUpdateAutomation object
|
||||
type ImageUpdateAutomationReconciler struct {
|
||||
client.Client
|
||||
Log logr.Logger
|
||||
Scheme *runtime.Scheme
|
||||
Log logr.Logger
|
||||
Scheme *runtime.Scheme
|
||||
EventRecorder kuberecorder.EventRecorder
|
||||
}
|
||||
|
||||
// +kubebuilder:rbac:groups=image.toolkit.fluxcd.io,resources=imageupdateautomations,verbs=get;list;watch;create;update;patch;delete
|
||||
|
|
@ -132,6 +135,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(req ctrl.Request) (ctrl.Resu
|
|||
}
|
||||
|
||||
if err := updateAccordingToSetters(ctx, tmp, policies.Items); err != nil {
|
||||
r.event(auto, events.EventSeverityError, err.Error())
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
default:
|
||||
|
|
@ -143,14 +147,17 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(req ctrl.Request) (ctrl.Resu
|
|||
log.V(debug).Info("ran updates to working dir", "working", tmp)
|
||||
|
||||
var commitMade bool
|
||||
|
||||
if rev, err := commitAllAndPush(ctx, repo, access, &auto.Spec.Commit); err != nil {
|
||||
if err == errNoChanges {
|
||||
log.Info("no changes made in working directory; no commit")
|
||||
} else {
|
||||
r.event(auto, events.EventSeverityError, err.Error())
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
} else {
|
||||
commitMade = true
|
||||
r.event(auto, events.EventSeverityInfo, "committed and pushed change "+rev)
|
||||
log.V(debug).Info("pushed commit to origin", "revision", rev)
|
||||
}
|
||||
|
||||
|
|
@ -329,6 +336,14 @@ func commitAllAndPush(ctx context.Context, repo *gogit.Repository, access repoAc
|
|||
})
|
||||
}
|
||||
|
||||
// --- events
|
||||
|
||||
func (r *ImageUpdateAutomationReconciler) event(auto imagev1.ImageUpdateAutomation, severity, msg string) {
|
||||
if r.EventRecorder != nil {
|
||||
r.EventRecorder.Event(&auto, "Normal", severity, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// --- updates
|
||||
|
||||
// updateAccordingToSetters updates files under the root by treating
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -8,6 +8,7 @@ require (
|
|||
github.com/fluxcd/image-automation-controller/api v0.0.0-00010101000000-000000000000
|
||||
github.com/fluxcd/image-reflector-controller v0.0.0-20200810165546-c2265d9b49b9
|
||||
github.com/fluxcd/pkg/gittestserver v0.0.2
|
||||
github.com/fluxcd/pkg/runtime v0.2.0
|
||||
github.com/fluxcd/source-controller v0.2.2
|
||||
// If you bump this, change TOOLKIT_VERSION in the Makefile to match
|
||||
github.com/fluxcd/source-controller/api v0.2.2
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -295,6 +295,7 @@ github.com/fluxcd/pkg/gittestserver v0.0.2 h1:11aKRVuuHiyeaicdN4wPNSMy/dUarQkrPr
|
|||
github.com/fluxcd/pkg/gittestserver v0.0.2/go.mod h1:GW8N9d1o8/+mXWnSzs02qCB5WlArWQHdMpDPf7b/GZg=
|
||||
github.com/fluxcd/pkg/helmtestserver v0.0.1/go.mod h1:GR8LriiU7PqZSTH4Xe6Cimpig2VVPB29PeUXJjNJYfA=
|
||||
github.com/fluxcd/pkg/lockedfile v0.0.5/go.mod h1:uAtPUBId6a2RqO84MTH5HKGX0SbM1kNW3Wr/FhYyDVA=
|
||||
github.com/fluxcd/pkg/runtime v0.2.0 h1:aZmSLuyA9pF/KANf4wi7pZIICE19BKTYFSPRbl6WHtY=
|
||||
github.com/fluxcd/pkg/runtime v0.2.0/go.mod h1:P1/S8TOSuJgVPU0SRahWzbNxLWYoUwvBcPCNGc+dWWg=
|
||||
github.com/fluxcd/pkg/ssh v0.0.5 h1:rnbFZ7voy2JBlUfMbfyqArX2FYaLNpDhccGFC3qW83A=
|
||||
github.com/fluxcd/pkg/ssh v0.0.5/go.mod h1:7jXPdXZpc0ttMNz2kD9QuMi3RNn/e0DOFbj0Tij/+Hs=
|
||||
|
|
@ -573,7 +574,9 @@ github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyN
|
|||
github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
|
||||
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
|
||||
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
|
||||
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
|
||||
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
|
||||
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
|
|
@ -581,6 +584,7 @@ github.com/hashicorp/go-multierror v0.0.0-20161216184304-ed905158d874/go.mod h1:
|
|||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||
github.com/hashicorp/go-retryablehttp v0.6.4/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
|
||||
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
|
||||
github.com/hashicorp/go-retryablehttp v0.6.7 h1:8/CAEZt/+F7kR7GevNHulKkUjLht3CPmn7egmhieNKo=
|
||||
github.com/hashicorp/go-retryablehttp v0.6.7/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
|
||||
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
|
||||
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
|
||||
|
|
|
|||
59
main.go
59
main.go
|
|
@ -24,9 +24,10 @@ import (
|
|||
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
||||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||
"sigs.k8s.io/controller-runtime/pkg/healthz"
|
||||
|
||||
imagev1alpha1_reflect "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
|
||||
"github.com/fluxcd/pkg/runtime/logger"
|
||||
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
|
||||
|
||||
imagev1alpha1_auto "github.com/fluxcd/image-automation-controller/api/v1alpha1"
|
||||
|
|
@ -49,32 +50,54 @@ func init() {
|
|||
}
|
||||
|
||||
func main() {
|
||||
var metricsAddr string
|
||||
var enableLeaderElection bool
|
||||
var (
|
||||
metricsAddr string
|
||||
healthAddr string
|
||||
enableLeaderElection bool
|
||||
logLevel string
|
||||
logJSON bool
|
||||
watchAllNamespaces bool
|
||||
)
|
||||
|
||||
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
|
||||
flag.StringVar(&healthAddr, "health-addr", ":9440", "The address the health endpoint 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.StringVar(&logLevel, "log-level", "info", "Set logging level. Can be debug, info or error.")
|
||||
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
|
||||
flag.BoolVar(&watchAllNamespaces, "watch-all-namespaces", true,
|
||||
"Watch for custom resources in all namespaces, if set to false it will only watch the runtime namespace.")
|
||||
flag.Parse()
|
||||
|
||||
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
|
||||
ctrl.SetLogger(logger.NewLogger(logLevel, logJSON))
|
||||
|
||||
watchNamespace := ""
|
||||
if !watchAllNamespaces {
|
||||
watchNamespace = os.Getenv("RUNTIME_NAMESPACE")
|
||||
}
|
||||
|
||||
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
|
||||
Scheme: scheme,
|
||||
MetricsBindAddress: metricsAddr,
|
||||
Port: 9443,
|
||||
LeaderElection: enableLeaderElection,
|
||||
LeaderElectionID: "79628f79.fluxcd.io",
|
||||
Scheme: scheme,
|
||||
MetricsBindAddress: metricsAddr,
|
||||
HealthProbeBindAddress: healthAddr,
|
||||
Port: 9443,
|
||||
LeaderElection: enableLeaderElection,
|
||||
LeaderElectionID: "79628f79.fluxcd.io",
|
||||
Namespace: watchNamespace,
|
||||
})
|
||||
if err != nil {
|
||||
setupLog.Error(err, "unable to start manager")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
setupChecks(mgr)
|
||||
|
||||
if err = (&controllers.ImageUpdateAutomationReconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Log: ctrl.Log.WithName("controllers").WithName("ImageUpdateAutomation"),
|
||||
Scheme: mgr.GetScheme(),
|
||||
Client: mgr.GetClient(),
|
||||
Log: ctrl.Log.WithName("controllers").WithName("ImageUpdateAutomation"),
|
||||
Scheme: mgr.GetScheme(),
|
||||
EventRecorder: mgr.GetEventRecorderFor("image-automation-controller"),
|
||||
}).SetupWithManager(mgr); err != nil {
|
||||
setupLog.Error(err, "unable to create controller", "controller", "ImageUpdateAutomation")
|
||||
os.Exit(1)
|
||||
|
|
@ -87,3 +110,15 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func setupChecks(mgr ctrl.Manager) {
|
||||
if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
|
||||
setupLog.Error(err, "unable to create ready check")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
|
||||
setupLog.Error(err, "unable to create health check")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue