Skip to content

Commit d262ebb

Browse files
committed
*: use same annotation domain, add initialized condition, add deprecated --max-workers flag
1 parent 7579a77 commit d262ebb

File tree

11 files changed

+78
-31
lines changed

11 files changed

+78
-31
lines changed

example/watches.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
kind: Nginx
44
chart: ./helm-charts/nginx
55
maxConcurrentReconciles: 4
6-
reconcilePeriod: 0
6+
reconcilePeriod: 0s
77
overrideValues:
88
image.repository: nginx

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/go-logr/logr v0.1.0
77
github.com/onsi/ginkgo v1.12.0
88
github.com/onsi/gomega v1.9.0
9+
github.com/spf13/pflag v1.0.5
910
github.com/stretchr/testify v1.5.1
1011
go.uber.org/zap v1.13.0
1112
gomodules.xyz/jsonpatch/v2 v2.0.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdko
4545
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d h1:UrqY+r/OJnIp5u0s1SbQ8dVfLCZJsnvazdBP5hS4iRs=
4646
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
4747
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
48+
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
4849
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
50+
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
4951
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
5052
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
5153
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=

main.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import (
2121
"os"
2222
"time"
2323

24+
"github.com/spf13/pflag"
2425
"go.uber.org/zap"
25-
"k8s.io/apimachinery/pkg/runtime"
26-
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2726
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2827
"k8s.io/klog"
2928
ctrl "sigs.k8s.io/controller-runtime"
@@ -36,16 +35,9 @@ import (
3635
)
3736

3837
var (
39-
scheme = runtime.NewScheme()
4038
setupLog = ctrl.Log.WithName("setup")
4139
)
4240

43-
func init() {
44-
_ = clientgoscheme.AddToScheme(scheme)
45-
46-
// +kubebuilder:scaffold:scheme
47-
}
48-
4941
func main() {
5042
var (
5143
metricsAddr string
@@ -55,20 +47,36 @@ func main() {
5547
watchesFile string
5648
defaultMaxConcurrentReconciles int
5749
defaultReconcilePeriod time.Duration
50+
51+
// Deprecated: use defaultMaxConcurrentReconciles
52+
defaultMaxWorkers int
5853
)
5954

60-
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
61-
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
55+
pflag.StringVar(&metricsAddr, "metrics-addr", "0.0.0.0:8383", "The address the metric endpoint binds to.")
56+
pflag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
6257
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
63-
flag.StringVar(&leaderElectionID, "leader-election-id", "leader-lock",
58+
pflag.StringVar(&leaderElectionID, "leader-election-id", "leader-lock",
6459
"Name of the configmap that is used for holding the leader lock.")
6560

66-
flag.StringVar(&watchesFile, "watches-file", "./watches.yaml", "Path to watches.yaml file.")
67-
flag.IntVar(&defaultMaxConcurrentReconciles, "max-concurrent-reconciles", 1, "Default maximum number of concurrent reconciles for controllers.")
68-
flag.DurationVar(&defaultReconcilePeriod, "reconcile-period", 0, "Default reconcile period for controllers (use 0 to disable periodic reconciliation)")
61+
pflag.StringVar(&watchesFile, "watches-file", "./watches.yaml", "Path to watches.yaml file.")
62+
pflag.DurationVar(&defaultReconcilePeriod, "reconcile-period", 0, "Default reconcile period for controllers (use 0 to disable periodic reconciliation)")
63+
pflag.IntVar(&defaultMaxConcurrentReconciles, "max-concurrent-reconciles", 1, "Default maximum number of concurrent reconciles for controllers.")
64+
65+
// Deprecated: --max-workers flag is currently supported, but it does not align well with the name of the option it
66+
// configures on the controller (MaxConcurrentReconciles). Flag `--max-concurrent-reconciles` should be used
67+
// instead.
68+
pflag.IntVar(&defaultMaxWorkers, "max-workers", 1, "Default maximum number of concurrent reconciles for controllers.")
69+
if err := pflag.CommandLine.MarkDeprecated("max-workers", "use --default-max-concurrent-reconciles instead"); err != nil {
70+
setupLog.Error(err, "failed to mark --max-workers flag as deprecated")
71+
os.Exit(1)
72+
}
73+
if err := pflag.CommandLine.MarkHidden("max-workers"); err != nil {
74+
setupLog.Error(err, "failed to hide --max-workers flag")
75+
os.Exit(1)
76+
}
6977

7078
klog.InitFlags(flag.CommandLine)
71-
flag.Parse()
79+
pflag.Parse()
7280

7381
logLvl := zap.NewAtomicLevelAt(zap.InfoLevel)
7482
sttLvl := zap.NewAtomicLevelAt(zap.PanicLevel)
@@ -78,12 +86,17 @@ func main() {
7886
zapl.StacktraceLevel(&sttLvl),
7987
))
8088

89+
if pflag.Lookup("max-workers").Changed {
90+
if !pflag.Lookup("max-concurrent-reconciles").Changed {
91+
defaultMaxConcurrentReconciles = defaultMaxWorkers
92+
}
93+
setupLog.Info("ignoring --max-workers since --max-concurrent-reconciles is set")
94+
}
95+
8196
options := ctrl.Options{
82-
Scheme: scheme,
83-
MetricsBindAddress: metricsAddr,
97+
MetricsBindAddress: "0.0.0.0:8383",
8498
LeaderElection: enableLeaderElection,
8599
LeaderElectionID: leaderElectionID,
86-
Port: 9443,
87100
NewClient: manager.NewDelegatingClientFunc(),
88101
}
89102
manager.ConfigureWatchNamespaces(&options, setupLog)
@@ -133,7 +146,7 @@ func main() {
133146
setupLog.Info("configured watch", "gvk", w.GroupVersionKind, "chartPath", w.ChartPath, "maxConcurrentReconciles", maxConcurrentReconciles, "reconcilePeriod", reconcilePeriod)
134147
}
135148

136-
// +kubebuilder:scaffold:builder
149+
// TODO(joelanford): kube-state-metrics?
137150

138151
setupLog.Info("starting manager")
139152
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {

pkg/annotation/annotation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type InstallDisableHooks struct {
3636
var _ Install = &InstallDisableHooks{}
3737

3838
const (
39-
DefaultDomain = "helm.sdk.operatorframework.io"
39+
DefaultDomain = "helm.operator-sdk"
4040
DefaultInstallDisableHooksName = DefaultDomain + "/install-disable-hooks"
4141
DefaultUpgradeDisableHooksName = DefaultDomain + "/upgrade-disable-hooks"
4242
DefaultUninstallDisableHooksName = DefaultDomain + "/uninstall-disable-hooks"

pkg/internal/testutil/testutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ func BuildTestCR(gvk schema.GroupVersionKind) *unstructured.Unstructured {
6666
obj.SetGroupVersionKind(gvk)
6767
obj.SetUID("test-uid")
6868
obj.SetAnnotations(map[string]string{
69-
"helm.sdk.operatorframework.io/install-description": "test install description",
70-
"helm.sdk.operatorframework.io/upgrade-description": "test upgrade description",
71-
"helm.sdk.operatorframework.io/uninstall-description": "test uninstall description",
69+
"helm.operator-sdk/install-description": "test install description",
70+
"helm.operator-sdk/upgrade-description": "test upgrade description",
71+
"helm.operator-sdk/uninstall-description": "test uninstall description",
7272
})
7373
return obj
7474
}

pkg/manager/namespace.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const (
1515
)
1616

1717
func ConfigureWatchNamespaces(options *manager.Options, log logr.Logger) {
18-
namespaces, found := lookupEnv()
19-
if found && len(namespaces) != 0 {
18+
namespaces := lookupEnv()
19+
if len(namespaces) != 0 {
2020
log.Info("watching namespaces", "namespaces", namespaces)
2121
if len(namespaces) > 1 {
2222
options.NewCache = cache.MultiNamespacedCacheBuilder(namespaces)
@@ -29,19 +29,19 @@ func ConfigureWatchNamespaces(options *manager.Options, log logr.Logger) {
2929
options.Namespace = v1.NamespaceAll
3030
}
3131

32-
func lookupEnv() ([]string, bool) {
32+
func lookupEnv() []string {
3333
if watchNamespace, found := os.LookupEnv(WatchNamespaceEnvVar); found {
34-
return splitNamespaces(watchNamespace), true
34+
return splitNamespaces(watchNamespace)
3535
}
36-
return nil, false
36+
return nil
3737
}
3838

3939
func splitNamespaces(namespaces string) []string {
4040
list := strings.Split(namespaces, ",")
4141
out := []string{}
4242
for _, ns := range list {
4343
if ns != "" {
44-
out = append(out, ns)
44+
out = append(out, strings.TrimSpace(ns))
4545
}
4646
}
4747
return out

pkg/reconciler/internal/conditions/conditions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
)
2424

2525
const (
26+
TypeInitialized = "Initialized"
2627
TypeDeployed = "Deployed"
2728
TypeReleaseFailed = "ReleaseFailed"
2829
TypeIrreconcilable = "Irreconcilable"
@@ -40,6 +41,10 @@ const (
4041
ReasonUninstallError = status.ConditionReason("UninstallError")
4142
)
4243

44+
func Initialized(stat corev1.ConditionStatus, reason status.ConditionReason, message interface{}) status.Condition {
45+
return newCondition(TypeInitialized, stat, reason, message)
46+
}
47+
4348
func Deployed(stat corev1.ConditionStatus, reason status.ConditionReason, message interface{}) status.Condition {
4449
return newCondition(TypeDeployed, stat, reason, message)
4550
}

pkg/reconciler/internal/conditions/conditions_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import (
1212
)
1313

1414
var _ = Describe("Conditions", func() {
15+
var _ = Describe("Initialized", func() {
16+
It("should return an Initialized condition with the correct status, reason, and message", func() {
17+
e := status.Condition{
18+
Type: TypeInitialized,
19+
Status: corev1.ConditionTrue,
20+
Reason: "reason",
21+
Message: "message",
22+
}
23+
Expect(Initialized(e.Status, e.Reason, e.Message)).To(Equal(e))
24+
})
25+
})
26+
1527
var _ = Describe("Deployed", func() {
1628
It("should return a Deployed condition with the correct status, reason, and message", func() {
1729
e := status.Condition{

pkg/reconciler/reconciler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (res ctrl.Result, err error) {
449449
} else if err == nil {
450450
ensureDeployedRelease(&u, rel)
451451
}
452+
u.UpdateStatus(updater.EnsureCondition(conditions.Initialized(corev1.ConditionTrue, "", "")))
452453

453454
if obj.GetDeletionTimestamp() != nil {
454455
err := r.handleDeletion(ctx, actionClient, obj, log)

0 commit comments

Comments
 (0)