@@ -38,6 +38,8 @@ type approveOptions struct {
3838 hubClusterContext string
3939 kind string
4040 name string
41+
42+ hubClient client.Client
4143}
4244
4345func NewCmdApprove () * cobra.Command {
@@ -56,14 +58,17 @@ Currently supported kinds:
5658 Args : cobra .ExactArgs (1 ),
5759 RunE : func (cmd * cobra.Command , args []string ) error {
5860 o .kind = args [0 ]
61+ if err := o .setupClient (); err != nil {
62+ return err
63+ }
5964 return o .run (cmd .Context ())
6065 },
6166 }
6267
6368 cmd .Flags ().StringVar (& o .hubClusterContext , "hubClusterContext" , "" , "The name of the kubeconfig context to use for the hub cluster" )
6469 cmd .Flags ().StringVar (& o .name , "name" , "" , "The name of the resource to approve" )
6570
66- // Mark required flags
71+ // Mark required flags.
6772 _ = cmd .MarkFlagRequired ("hubClusterContext" )
6873 _ = cmd .MarkFlagRequired ("name" )
6974
@@ -78,24 +83,19 @@ func (o *approveOptions) run(ctx context.Context) error {
7883 return fmt .Errorf ("resource name is required" )
7984 }
8085
81- // Validate that we only support clusterapprovalrequest for now
86+ // Validate that we only support clusterapprovalrequest for now.
8287 if o .kind != "clusterapprovalrequest" {
8388 return fmt .Errorf ("unsupported resource kind %q, only 'clusterapprovalrequest' is supported" , o .kind )
8489 }
8590
86- _ , hubClient , err := o .setupClient ()
87- if err != nil {
88- return err
89- }
90-
91- // Patch the ClusterApprovalRequest status with approved condition
92- err = retry .RetryOnConflict (retry .DefaultRetry , func () error {
91+ // Patch the ClusterApprovalRequest status with approved condition.
92+ err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
9393 var car placementv1beta1.ClusterApprovalRequest
94- if err := hubClient .Get (ctx , types.NamespacedName {Name : o .name }, & car ); err != nil {
95- return err
94+ if err := o . hubClient .Get (ctx , types.NamespacedName {Name : o .name }, & car ); err != nil {
95+ return fmt . Errorf ( "failed to get ClusterApprovalRequest %q: %w" , o . name , err )
9696 }
9797
98- // Add the Approved condition
98+ // Add the Approved condition.
9999 approvedCondition := metav1.Condition {
100100 Type : string (placementv1beta1 .ApprovalRequestConditionApproved ),
101101 Status : metav1 .ConditionTrue ,
@@ -104,35 +104,36 @@ func (o *approveOptions) run(ctx context.Context) error {
104104 ObservedGeneration : car .Generation ,
105105 }
106106
107- // Update or add the condition
107+ // Update or add the condition.
108108 meta .SetStatusCondition (& car .Status .Conditions , approvedCondition )
109109
110- return hubClient .Status ().Update (ctx , & car )
110+ return o . hubClient .Status ().Update (ctx , & car )
111111 })
112112
113113 if err != nil {
114114 return fmt .Errorf ("failed to approve ClusterApprovalRequest %q: %w" , o .name , err )
115115 }
116116
117- fmt .Printf ("ClusterApprovalRequest %q approved successfully\n " , o .name )
117+ log .Printf ("ClusterApprovalRequest %q approved successfully\n " , o .name )
118118 return nil
119119}
120120
121121// setupClient creates and configures the Kubernetes client
122- func (o * approveOptions ) setupClient () ( * runtime. Scheme , client. Client , error ) {
122+ func (o * approveOptions ) setupClient () error {
123123 scheme := runtime .NewScheme ()
124124
125125 if err := clusterv1beta1 .AddToScheme (scheme ); err != nil {
126- log . Fatalf ("failed to add custom APIs (cluster) to the runtime scheme: %v" , err )
126+ return fmt . Errorf ("failed to add custom APIs (cluster) to the runtime scheme: %v" , err )
127127 }
128128 if err := placementv1beta1 .AddToScheme (scheme ); err != nil {
129- log . Fatalf ("failed to add custom APIs (placement) to the runtime scheme: %v" , err )
129+ return fmt . Errorf ("failed to add custom APIs (placement) to the runtime scheme: %v" , err )
130130 }
131131
132132 hubClient , err := toolsutils .GetClusterClientFromClusterContext (o .hubClusterContext , scheme )
133133 if err != nil {
134- log . Fatalf ("failed to create hub cluster client: %v" , err )
134+ return fmt . Errorf ("failed to create hub cluster client: %v" , err )
135135 }
136136
137- return scheme , hubClient , nil
137+ o .hubClient = hubClient
138+ return nil
138139}
0 commit comments