Skip to content

Commit 5224c9c

Browse files
authored
🌱 Add --skip-webhooks, show logs in envTests (#1597)
1 parent 926a4c7 commit 5224c9c

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

controllers/hetznercluster_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
334334
})
335335

336336
Context("load balancer", func() {
337-
It("should create load balancer and update it accordingly", func() {
337+
It("should create load balancer and update it accordingly (flaky)", func() {
338338
Expect(testEnv.Create(ctx, instance)).To(Succeed())
339339

340340
Eventually(func() bool {
@@ -400,7 +400,7 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
400400
}
401401

402402
return true
403-
}, timeout, 1*time.Second).Should(BeTrue())
403+
}, 2*timeout, 1*time.Second).Should(BeTrue())
404404
})
405405

406406
It("should update extra targets", func() {

main.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ var (
8585
syncPeriod time.Duration
8686
rateLimitWaitTime time.Duration
8787
preProvisionCommand string
88+
skipWebhooks bool
8889
)
8990

9091
func main() {
@@ -105,6 +106,7 @@ func main() {
105106
fs.DurationVar(&rateLimitWaitTime, "rate-limit", 5*time.Minute, "The rate limiting for HCloud controller (e.g. 5m)")
106107
fs.BoolVar(&hcloudclient.DebugAPICalls, "debug-hcloud-api-calls", false, "Debug all calls to the hcloud API.")
107108
fs.StringVar(&preProvisionCommand, "pre-provision-command", "", "Command to run (in rescue-system) before installing the image on bare metal servers. You can use that to check if the machine is healthy before installing the image. If the exit value is non-zero, the machine is considered unhealthy. This command must be accessible by the controller pod. You can use an initContainer to copy the command to a shared emptyDir.")
109+
fs.BoolVar(&skipWebhooks, "skip-webhooks", false, "Skip setting up of webhooks. Together with --leader-elect=false, you can use `go run main.go` to run CAPH in a cluster connected via KUBECONFIG. You should scale down the caph deployment to 0 before doing that. This is only for testing!")
108110
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
109111
pflag.Parse()
110112

@@ -133,13 +135,10 @@ func main() {
133135
}
134136
}
135137

136-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
137-
Scheme: scheme,
138-
Metrics: metricsserver.Options{BindAddress: metricsAddr},
139-
HealthProbeBindAddress: probeAddr,
140-
WebhookServer: webhook.NewServer(webhook.Options{
141-
Port: 9443,
142-
}),
138+
options := ctrl.Options{
139+
Scheme: scheme,
140+
Metrics: metricsserver.Options{BindAddress: metricsAddr},
141+
HealthProbeBindAddress: probeAddr,
143142
LeaderElection: enableLeaderElection,
144143
LeaderElectionID: "hetzner.cluster.x-k8s.io",
145144
LeaderElectionNamespace: leaderElectionNamespace,
@@ -150,7 +149,15 @@ func main() {
150149
SyncPeriod: &syncPeriod,
151150
DefaultNamespaces: watchNamespaces,
152151
},
153-
})
152+
}
153+
154+
if !skipWebhooks {
155+
options.WebhookServer = webhook.NewServer(webhook.Options{
156+
Port: 9443,
157+
})
158+
}
159+
160+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
154161
if err != nil {
155162
setupLog.Error(err, "unable to start manager")
156163
os.Exit(1)
@@ -245,18 +252,20 @@ func main() {
245252
os.Exit(1)
246253
}
247254

248-
setUpWebhookWithManager(mgr)
255+
if !skipWebhooks {
256+
setUpWebhookWithManager(mgr)
249257

250-
//+kubebuilder:scaffold:builder
258+
//+kubebuilder:scaffold:builder
251259

252-
if err := mgr.AddReadyzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
253-
setupLog.Error(err, "unable to create ready check")
254-
os.Exit(1)
255-
}
260+
if err := mgr.AddReadyzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
261+
setupLog.Error(err, "unable to create ready check")
262+
os.Exit(1)
263+
}
256264

257-
if err := mgr.AddHealthzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
258-
setupLog.Error(err, "unable to create health check")
259-
os.Exit(1)
265+
if err := mgr.AddHealthzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
266+
setupLog.Error(err, "unable to create health check")
267+
os.Exit(1)
268+
}
260269
}
261270

262271
setupLog.Info("starting manager", "version", caphversion.Get().String())

pkg/services/hcloud/network/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (s *Service) createNetwork(ctx context.Context) (*hcloud.Network, error) {
9999
return nil, fmt.Errorf("failed to create network: %w", err)
100100
}
101101

102-
record.Eventf(s.scope.HetznerCluster, "NetworkCreated", "Created network with opts %s", opts)
102+
record.Eventf(s.scope.HetznerCluster, "NetworkCreated", "Created network with opts %+v", opts)
103103
return resp, nil
104104
}
105105

test/helpers/envtest.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
4343
"sigs.k8s.io/cluster-api/cmd/clusterctl/log"
4444
"sigs.k8s.io/cluster-api/util/kubeconfig"
45+
"sigs.k8s.io/cluster-api/util/record"
4546
ctrl "sigs.k8s.io/controller-runtime"
4647
"sigs.k8s.io/controller-runtime/pkg/cache"
4748
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -61,7 +62,7 @@ import (
6162

6263
func init() {
6364
klog.InitFlags(nil)
64-
logger := textlogger.NewLogger(textlogger.NewConfig())
65+
logger := textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(4)))
6566

6667
// use klog as the internal logger for this envtest environment.
6768
log.SetLogger(logger)
@@ -149,6 +150,8 @@ func NewTestEnvironment() *TestEnvironment {
149150
klog.Fatalf("unable to create manager: %s", err)
150151
}
151152

153+
record.InitFromRecorder(mgr.GetEventRecorderFor("hetzner-controller"))
154+
152155
if err := (&infrav1.HetznerCluster{}).SetupWebhookWithManager(mgr); err != nil {
153156
klog.Fatalf("failed to set up webhook with manager for HetznerCluster: %s", err)
154157
}

0 commit comments

Comments
 (0)