Skip to content

Commit a37ab26

Browse files
committed
cvo: separate option validation and cvo execution
Extract option validation to a new `ValidateAndComplete()` method, and run it separate from `Run()` through cobra `PreRunE`
1 parent 55b7593 commit a37ab26

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

cmd/cluster-version-operator/start.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
56
"github.com/spf13/cobra"
67
"k8s.io/klog/v2"
78

@@ -15,10 +16,12 @@ func init() {
1516
Use: "start",
1617
Short: "Starts Cluster Version Operator",
1718
Long: "",
18-
Run: func(cmd *cobra.Command, args []string) {
19+
PreRunE: func(_ *cobra.Command, _ []string) error {
1920
// To help debugging, immediately log version
2021
klog.Info(version.String)
21-
22+
return opts.ValidateAndComplete()
23+
},
24+
Run: func(_ *cobra.Command, _ []string) {
2225
if err := opts.Run(context.Background()); err != nil {
2326
klog.Fatalf("error: %v", err)
2427
}

pkg/start/start.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func NewOptions() *Options {
132132
}
133133
}
134134

135-
func (o *Options) Run(ctx context.Context) error {
135+
func (o *Options) ValidateAndComplete() error {
136136
if o.NodeName == "" {
137137
return fmt.Errorf("node-name is required")
138138
}
@@ -152,6 +152,20 @@ func (o *Options) Run(ctx context.Context) error {
152152
(o.PromQLTarget.KubeSvc.Namespace == "" || o.PromQLTarget.KubeSvc.Name == "") {
153153
return fmt.Errorf("--use-dns-for-services is disabled, so --metrics-service and --metrics-namespace must be set")
154154
}
155+
156+
if parsed, err := url.Parse(o.PrometheusURLString); err != nil {
157+
return fmt.Errorf("error parsing promql url: %v", err)
158+
} else {
159+
o.PromQLTarget.URL = parsed
160+
}
161+
162+
// Inject the cluster ID into PromQL queries in HyperShift
163+
o.InjectClusterIdIntoPromQL = o.HyperShift
164+
165+
return nil
166+
}
167+
168+
func (o *Options) Run(ctx context.Context) error {
155169
if len(o.PayloadOverride) > 0 {
156170
klog.Warningf("Using an override payload directory for testing only: %s", o.PayloadOverride)
157171
}
@@ -163,16 +177,6 @@ func (o *Options) Run(ctx context.Context) error {
163177
return fmt.Errorf("--always-enable-capabilities was set with unknown capabilities: %v", unknownCaps)
164178
}
165179

166-
// Inject the cluster ID into PromQL queries in HyperShift
167-
o.InjectClusterIdIntoPromQL = o.HyperShift
168-
169-
// parse the prometheus url
170-
var err error
171-
o.PromQLTarget.URL, err = url.Parse(o.PrometheusURLString)
172-
if err != nil {
173-
return fmt.Errorf("error parsing promql url: %v", err)
174-
}
175-
176180
// initialize the core objects
177181
cb, err := newClientBuilder(o.Kubeconfig)
178182
if err != nil {

0 commit comments

Comments
 (0)