Skip to content

Commit 6fbf537

Browse files
committed
run all api check before returning, add olm api check before register, keep the check and register pattern same for all conditional api registration
assisted-by: claude-code for code,planning Signed-off-by: Anand Kumar Singh <anandrkskd@gmail.com>
1 parent 6d7e429 commit 6fbf537

6 files changed

Lines changed: 115 additions & 27 deletions

File tree

cmd/main.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,12 @@ func main() {
213213
registerComponentOrExit(mgr, monitoringv1.AddToScheme)
214214
}
215215

216-
registerComponentOrExit(mgr, operatorsv1.AddToScheme)
217-
registerComponentOrExit(mgr, operatorsv1alpha1.AddToScheme)
216+
// Setup Scheme for OLM if available (verified by InspectCluster)
217+
if util.IsOLMAPIFound() {
218+
registerComponentOrExit(mgr, operatorsv1.AddToScheme)
219+
registerComponentOrExit(mgr, operatorsv1alpha1.AddToScheme)
220+
}
221+
218222
registerComponentOrExit(mgr, argov1alpha1api.AddToScheme)
219223
registerComponentOrExit(mgr, argov1beta1api.AddToScheme)
220224

@@ -225,27 +229,18 @@ func main() {
225229

226230
registerComponentOrExit(mgr, rolloutManagerApi.AddToScheme)
227231

228-
// Setup Scheme for OpenShift Template if available
229-
if found, err := argoutil.VerifyAPI(templatev1.GroupName, templatev1.GroupVersion.Version); err != nil {
230-
setupLog.Error(err, "unable to verify Template API")
231-
os.Exit(1)
232-
} else if found {
232+
// Setup Scheme for OpenShift Template if available (verified by InspectCluster)
233+
if util.IsTemplateAPIFound() {
233234
registerComponentOrExit(mgr, templatev1.AddToScheme)
234235
}
235236

236-
// Setup Scheme for OpenShift Apps if available
237-
if found, err := argoutil.VerifyAPI(appsv1.GroupName, appsv1.GroupVersion.Version); err != nil {
238-
setupLog.Error(err, "unable to verify Apps API")
239-
os.Exit(1)
240-
} else if found {
237+
// Setup Scheme for OpenShift Apps if available (verified by InspectCluster)
238+
if util.IsAppsAPIFound() {
241239
registerComponentOrExit(mgr, appsv1.AddToScheme)
242240
}
243241

244-
// Setup Scheme for OpenShift OAuth if available
245-
if found, err := argoutil.VerifyAPI(oauthv1.GroupName, oauthv1.GroupVersion.Version); err != nil {
246-
setupLog.Error(err, "unable to verify OAuth API")
247-
os.Exit(1)
248-
} else if found {
242+
// Setup Scheme for OpenShift OAuth if available (verified by InspectCluster)
243+
if util.IsOAuthAPIFound() {
249244
registerComponentOrExit(mgr, oauthv1.AddToScheme)
250245
}
251246

controllers/argocd_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ func isConsoleLinkDisabled() bool {
7171

7272
// SetupWithManager sets up the controller with the Manager.
7373
func (r *ReconcileArgoCDRoute) SetupWithManager(mgr ctrl.Manager) error {
74-
// Watch for changes to argocd-server route in the default argocd instance namespace
75-
// The ConsoleLink holds the route URL and should be regenerated when route is updated
74+
if !util.IsRouteAPIFound() {
75+
return nil
76+
}
7677

7778
return ctrl.NewControllerManagedBy(mgr).
7879
For(&routev1.Route{}, builder.WithPredicates(filterPredicate(filterArgoCDRoute))).

controllers/gitopsservice_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ func (r *ReconcileGitopsService) Reconcile(ctx context.Context, request reconcil
221221
reqLogger := logs.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
222222
reqLogger.Info("Reconciling GitopsService")
223223

224+
// Fetch the GitopsService instance
224225
instance := &pipelinesv1alpha1.GitopsService{}
225226
err := r.Client.Get(ctx, types.NamespacedName{Name: serviceName}, instance)
226227
if err != nil {

controllers/gitopsservice_controller_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
func TestMain(m *testing.M) {
5353
util.SetOpenShiftClusterFound(true)
5454
util.SetMonitoringAPIFound(true)
55+
util.SetRouteAPIFound(true)
5556
os.Exit(m.Run())
5657
}
5758

controllers/util/test_util.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,23 @@ func SetConsoleAPIFound(found bool) {
3434
func SetRouteAPIFound(found bool) {
3535
routeAPIFound = found
3636
}
37+
38+
// *** THIS SHOULD ONLY BE USED FOR UNIT TESTING ***
39+
func SetTemplateAPIFound(found bool) {
40+
templateAPIFound = found
41+
}
42+
43+
// *** THIS SHOULD ONLY BE USED FOR UNIT TESTING ***
44+
func SetAppsAPIFound(found bool) {
45+
appsAPIFound = found
46+
}
47+
48+
// *** THIS SHOULD ONLY BE USED FOR UNIT TESTING ***
49+
func SetOAuthAPIFound(found bool) {
50+
oauthAPIFound = found
51+
}
52+
53+
// *** THIS SHOULD ONLY BE USED FOR UNIT TESTING ***
54+
func SetOLMAPIFound(found bool) {
55+
olmAPIFound = found
56+
}

controllers/util/util.go

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ package util
1818

1919
import (
2020
"context"
21+
stderrors "errors"
2122
"fmt"
2223
"os"
2324
"strings"
2425

2526
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
27+
oappsv1 "github.com/openshift/api/apps/v1"
2628
configv1 "github.com/openshift/api/config/v1"
2729
console "github.com/openshift/api/console/v1"
30+
oauthv1 "github.com/openshift/api/oauth/v1"
2831
routev1 "github.com/openshift/api/route/v1"
32+
templatev1 "github.com/openshift/api/template/v1"
33+
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1"
2934
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
3035
"golang.org/x/mod/semver"
3136
corev1 "k8s.io/api/core/v1"
@@ -44,6 +49,10 @@ var (
4449
routeAPIFound = false
4550
monitoringAPIFound = false
4651
openShiftClusterFound = false
52+
templateAPIFound = false
53+
appsAPIFound = false
54+
oauthAPIFound = false
55+
olmAPIFound = false
4756
)
4857

4958
// GetClusterVersion returns the OpenShift Cluster version in which the operator is installed
@@ -80,16 +89,25 @@ func InspectCluster() error {
8089
if err := verifyOpenShiftCluster(); err != nil {
8190
return err
8291
}
83-
if err := verifyRouteAPI(); err != nil {
84-
return err
85-
}
86-
if err := verifyConsoleAPI(); err != nil {
87-
return err
92+
if !openShiftClusterFound {
93+
return nil
8894
}
89-
if err := verifyMonitoringAPI(); err != nil {
90-
return err
95+
96+
var errs []error
97+
for _, check := range []func() error{
98+
verifyRouteAPI,
99+
verifyConsoleAPI,
100+
verifyMonitoringAPI,
101+
verifyTemplateAPI,
102+
verifyAppsAPI,
103+
verifyOAuthAPI,
104+
verifyOLMAPI,
105+
} {
106+
if err := check(); err != nil {
107+
errs = append(errs, err)
108+
}
91109
}
92-
return nil
110+
return stderrors.Join(errs...)
93111
}
94112

95113
func IsOpenShiftCluster() bool {
@@ -147,6 +165,58 @@ func IsMonitoringAPIFound() bool {
147165
return monitoringAPIFound
148166
}
149167

168+
func IsTemplateAPIFound() bool {
169+
return templateAPIFound
170+
}
171+
172+
func verifyTemplateAPI() error {
173+
found, err := argoutil.VerifyAPI(templatev1.GroupName, templatev1.GroupVersion.Version)
174+
if err != nil {
175+
return err
176+
}
177+
templateAPIFound = found
178+
return nil
179+
}
180+
181+
func IsAppsAPIFound() bool {
182+
return appsAPIFound
183+
}
184+
185+
func verifyAppsAPI() error {
186+
found, err := argoutil.VerifyAPI(oappsv1.GroupName, oappsv1.GroupVersion.Version)
187+
if err != nil {
188+
return err
189+
}
190+
appsAPIFound = found
191+
return nil
192+
}
193+
194+
func IsOAuthAPIFound() bool {
195+
return oauthAPIFound
196+
}
197+
198+
func verifyOAuthAPI() error {
199+
found, err := argoutil.VerifyAPI(oauthv1.GroupName, oauthv1.GroupVersion.Version)
200+
if err != nil {
201+
return err
202+
}
203+
oauthAPIFound = found
204+
return nil
205+
}
206+
207+
func IsOLMAPIFound() bool {
208+
return olmAPIFound
209+
}
210+
211+
func verifyOLMAPI() error {
212+
found, err := argoutil.VerifyAPI(operatorsv1.GroupVersion.Group, operatorsv1.GroupVersion.Version)
213+
if err != nil {
214+
return err
215+
}
216+
olmAPIFound = found
217+
return nil
218+
}
219+
150220
func ProxyEnvVars(vars ...corev1.EnvVar) []corev1.EnvVar {
151221
result := []corev1.EnvVar{}
152222
result = append(result, vars...)

0 commit comments

Comments
 (0)