Skip to content

Commit 9d67bd8

Browse files
committed
GITOPS-6284: refactor schema registration code in main.go
Assisted-by: < Cursor/Gemini etc> Signed-off-by: Alka Kumari <alkumari@redhat.com>
1 parent ec2d78a commit 9d67bd8

File tree

3 files changed

+166
-70
lines changed

3 files changed

+166
-70
lines changed

cmd/main.go

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ import (
2525
"strings"
2626

2727
"github.com/argoproj/argo-cd/v3/util/env"
28-
appsv1 "github.com/openshift/api/apps/v1"
29-
configv1 "github.com/openshift/api/config/v1"
30-
oauthv1 "github.com/openshift/api/oauth/v1"
31-
routev1 "github.com/openshift/api/route/v1"
32-
templatev1 "github.com/openshift/api/template/v1"
33-
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
3428
"k8s.io/client-go/kubernetes"
3529
"sigs.k8s.io/controller-runtime/pkg/cache"
3630
"sigs.k8s.io/controller-runtime/pkg/client/config"
@@ -40,6 +34,7 @@ import (
4034
"github.com/argoproj-labs/argocd-operator/common"
4135
"github.com/argoproj-labs/argocd-operator/controllers/argocd"
4236
"github.com/argoproj-labs/argocd-operator/controllers/argocdexport"
37+
operatorscheme "github.com/argoproj-labs/argocd-operator/schema"
4338

4439
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4540

@@ -52,13 +47,10 @@ import (
5247
"go.uber.org/zap/zapcore"
5348
"k8s.io/apimachinery/pkg/labels"
5449
"k8s.io/apimachinery/pkg/runtime"
55-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
56-
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
5750
ctrl "sigs.k8s.io/controller-runtime"
5851
"sigs.k8s.io/controller-runtime/pkg/healthz"
5952
"sigs.k8s.io/controller-runtime/pkg/log/zap"
6053

61-
v1alpha1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
6254
v1beta1 "github.com/argoproj-labs/argocd-operator/api/v1beta1"
6355
"github.com/argoproj-labs/argocd-operator/version"
6456
//+kubebuilder:scaffold:imports
@@ -69,14 +61,6 @@ var (
6961
setupLog = ctrl.Log.WithName("setup")
7062
)
7163

72-
func init() {
73-
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
74-
75-
utilruntime.Must(v1alpha1.AddToScheme(scheme))
76-
utilruntime.Must(v1beta1.AddToScheme(scheme))
77-
//+kubebuilder:scaffold:scheme
78-
}
79-
8064
func printVersion() {
8165
setupLog.Info(fmt.Sprintf("Go Version: %s", goruntime.Version()))
8266
setupLog.Info(fmt.Sprintf("Go OS/Arch: %s/%s", goruntime.GOOS, goruntime.GOARCH))
@@ -190,65 +174,15 @@ func main() {
190174

191175
setupLog.Info("Registering Components.")
192176

193-
// Setup Scheme for all resources
194-
if err := v1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
195-
setupLog.Error(err, "")
196-
os.Exit(1)
197-
}
198-
199-
if err := v1beta1.AddToScheme(mgr.GetScheme()); err != nil {
200-
setupLog.Error(err, "")
201-
os.Exit(1)
202-
}
203-
204-
// Setup Scheme for Prometheus if available.
205-
if argocd.IsPrometheusAPIAvailable() {
206-
if err := monitoringv1.AddToScheme(mgr.GetScheme()); err != nil {
207-
setupLog.Error(err, "")
208-
os.Exit(1)
209-
}
210-
}
211-
212-
// Setup Scheme for OpenShift Routes if available.
213-
if argocd.IsRouteAPIAvailable() {
214-
if err := routev1.Install(mgr.GetScheme()); err != nil {
215-
setupLog.Error(err, "")
216-
os.Exit(1)
217-
}
218-
}
219-
220-
// Set up the scheme for openshift config if available
221-
if argocd.IsVersionAPIAvailable() {
222-
if err := configv1.Install(mgr.GetScheme()); err != nil {
223-
setupLog.Error(err, "")
224-
os.Exit(1)
225-
}
226-
}
227-
228-
// Setup Schemes for SSO if template instance is available.
229-
if argocd.CanUseKeycloakWithTemplate() {
230-
setupLog.Info("Keycloak instance can be managed using OpenShift Template")
231-
if err := templatev1.Install(mgr.GetScheme()); err != nil {
232-
setupLog.Error(err, "")
233-
os.Exit(1)
234-
}
235-
if err := appsv1.Install(mgr.GetScheme()); err != nil {
236-
setupLog.Error(err, "")
237-
os.Exit(1)
238-
}
239-
if err := oauthv1.Install(mgr.GetScheme()); err != nil {
240-
setupLog.Error(err, "")
241-
os.Exit(1)
242-
}
243-
} else {
244-
setupLog.Info("Keycloak instance cannot be managed using OpenShift Template, as DeploymentConfig/Template API is not present")
245-
}
177+
// Setup Scheme for all resources using the centralized scheme package
178+
operatorscheme.SetupScheme(mgr.GetScheme())
246179

247180
k8sClient, err := initK8sClient()
248181
if err != nil {
249182
setupLog.Error(err, "Failed to initialize Kubernetes client")
250183
os.Exit(1)
251184
}
185+
252186
if err = (&argocd.ReconcileArgoCD{
253187
Client: mgr.GetClient(),
254188
Scheme: mgr.GetScheme(),

schema/scheme.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright 2021.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package scheme provides centralized scheme registration for all API types
18+
package schema
19+
20+
import (
21+
appsv1 "github.com/openshift/api/apps/v1"
22+
configv1 "github.com/openshift/api/config/v1"
23+
oauthv1 "github.com/openshift/api/oauth/v1"
24+
routev1 "github.com/openshift/api/route/v1"
25+
templatev1 "github.com/openshift/api/template/v1"
26+
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
29+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
30+
"os"
31+
ctrl "sigs.k8s.io/controller-runtime"
32+
33+
v1alpha1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
34+
v1beta1 "github.com/argoproj-labs/argocd-operator/api/v1beta1"
35+
"github.com/argoproj-labs/argocd-operator/controllers/argocd"
36+
)
37+
38+
var (
39+
schemeLog = ctrl.Log.WithName("scheme")
40+
)
41+
42+
func SetupScheme(scheme *runtime.Scheme) {
43+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
44+
registerArgoCDAPIs(scheme)
45+
registerPrometheusAPIsIfAvailable(scheme)
46+
registerOpenShiftAPIsIfAvailable(scheme)
47+
schemeLog.Info("Scheme setup complete.")
48+
}
49+
50+
func registerArgoCDAPIs(scheme *runtime.Scheme) {
51+
utilruntime.Must(v1alpha1.AddToScheme(scheme))
52+
utilruntime.Must(v1beta1.AddToScheme(scheme))
53+
}
54+
55+
func registerPrometheusAPIsIfAvailable(scheme *runtime.Scheme) {
56+
if argocd.IsPrometheusAPIAvailable() {
57+
if err := monitoringv1.AddToScheme(scheme); err != nil {
58+
schemeLog.Error(err, "")
59+
os.Exit(1)
60+
}
61+
}
62+
}
63+
64+
func registerOpenShiftAPIsIfAvailable(scheme *runtime.Scheme) {
65+
// Setup Scheme for OpenShift Routes if available.
66+
if argocd.IsRouteAPIAvailable() {
67+
if err := routev1.Install(scheme); err != nil {
68+
schemeLog.Error(err, "")
69+
os.Exit(1)
70+
}
71+
}
72+
73+
// Setup the scheme for openshift config if available
74+
if argocd.IsVersionAPIAvailable() {
75+
if err := configv1.Install(scheme); err != nil {
76+
schemeLog.Error(err, "")
77+
os.Exit(1)
78+
}
79+
}
80+
81+
// Setup Schemes for SSO if template instance is available.
82+
if argocd.CanUseKeycloakWithTemplate() {
83+
schemeLog.Info("Keycloak instance can be managed using OpenShift Template.")
84+
if err := appsv1.Install(scheme); err != nil {
85+
schemeLog.Error(err, "")
86+
os.Exit(1)
87+
}
88+
if err := templatev1.Install(scheme); err != nil {
89+
schemeLog.Error(err, "")
90+
os.Exit(1)
91+
}
92+
if err := oauthv1.Install(scheme); err != nil {
93+
schemeLog.Error(err, "")
94+
os.Exit(1)
95+
}
96+
} else {
97+
schemeLog.Info("Keycloak instance cannot be managed using OpenShift Template, as //DeploymentConfig/Template API is not present.")
98+
}
99+
}

schema/scheme_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package schema
2+
3+
import (
4+
"testing"
5+
6+
"github.com/argoproj-labs/argocd-operator/controllers/argocd"
7+
"github.com/stretchr/testify/assert"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/apimachinery/pkg/runtime/schema"
10+
)
11+
12+
func TestSetupScheme(t *testing.T) {
13+
t.Run("SetupScheme should register all required schemes", func(t *testing.T) {
14+
scheme := runtime.NewScheme()
15+
16+
// Call SetupScheme
17+
SetupScheme(scheme)
18+
19+
// Verify the scheme is not nil
20+
assert.NotNil(t, scheme, "Scheme should not be nil")
21+
22+
// Verify that ArgoCD API schemes are registered
23+
v1alpha1GVK := schema.GroupVersionKind{Group: "argoproj.io", Version: "v1alpha1", Kind: "ArgoCD"}
24+
v1beta1GVK := schema.GroupVersionKind{Group: "argoproj.io", Version: "v1beta1", Kind: "ArgoCD"}
25+
assert.True(t, scheme.Recognizes(v1alpha1GVK), "v1alpha1 ArgoCD scheme should be registered")
26+
assert.True(t, scheme.Recognizes(v1beta1GVK), "v1beta1 ArgoCD scheme should be registered")
27+
28+
// Verify conditional schemes based on API availability
29+
if argocd.IsPrometheusAPIAvailable() {
30+
prometheusGVK := schema.GroupVersionKind{Group: "monitoring.coreos.com", Version: "v1", Kind: "Prometheus"}
31+
assert.True(t, scheme.Recognizes(prometheusGVK), "Prometheus scheme should be registered when API is available")
32+
}
33+
34+
if argocd.IsRouteAPIAvailable() {
35+
routeGVK := schema.GroupVersionKind{Group: "route.openshift.io", Version: "v1", Kind: "Route"}
36+
assert.True(t, scheme.Recognizes(routeGVK), "OpenShift Route scheme should be registered when API is available")
37+
}
38+
39+
if argocd.IsVersionAPIAvailable() {
40+
clusterVersionGVK := schema.GroupVersionKind{Group: "config.openshift.io", Version: "v1", Kind: "ClusterVersion"}
41+
assert.True(t, scheme.Recognizes(clusterVersionGVK), "OpenShift Config scheme should be registered when API is available")
42+
}
43+
44+
if argocd.CanUseKeycloakWithTemplate() {
45+
templateGVK := schema.GroupVersionKind{Group: "template.openshift.io", Version: "v1", Kind: "Template"}
46+
deploymentConfigGVK := schema.GroupVersionKind{Group: "apps.openshift.io", Version: "v1", Kind: "DeploymentConfig"}
47+
oauthClientGVK := schema.GroupVersionKind{Group: "oauth.openshift.io", Version: "v1", Kind: "OAuthClient"}
48+
49+
assert.True(t, scheme.Recognizes(templateGVK), "OpenShift Template scheme should be registered when Keycloak can use templates")
50+
assert.True(t, scheme.Recognizes(deploymentConfigGVK), "OpenShift DeploymentConfig scheme should be registered when Keycloak can use templates")
51+
assert.True(t, scheme.Recognizes(oauthClientGVK), "OpenShift OAuth scheme should be registered when Keycloak can use templates")
52+
}
53+
})
54+
55+
t.Run("SetupScheme should not panic with empty scheme", func(t *testing.T) {
56+
scheme := runtime.NewScheme()
57+
58+
// This should not panic
59+
assert.NotPanics(t, func() {
60+
SetupScheme(scheme)
61+
}, "SetupScheme should not panic with empty scheme")
62+
})
63+
}

0 commit comments

Comments
 (0)