Skip to content

Commit 5748764

Browse files
authored
feat: Support OpenShift external IDP (#2078)
* feat: Support OpenShift external IDP Signed-off-by: Anatolii Bazko <abazko@redhat.com>
1 parent fa711e6 commit 5748764

File tree

68 files changed

+394
-291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+394
-291
lines changed

api/checluster_conversion_from_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
"k8s.io/apimachinery/pkg/api/resource"
1919

2020
devfile "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
21-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
2221
chev1 "github.com/eclipse-che/che-operator/api/v1"
2322
chev2 "github.com/eclipse-che/che-operator/api/v2"
23+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
2424
"github.com/stretchr/testify/assert"
2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -68,7 +68,7 @@ func TestConvertFromIngressOnK8s(t *testing.T) {
6868
}
6969

7070
func TestConvertFromIngressOnOpenShift(t *testing.T) {
71-
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
71+
infrastructure.InitializeForTesting(infrastructure.OpenShiftV4)
7272

7373
checlusterv2 := &chev2.CheCluster{
7474
ObjectMeta: metav1.ObjectMeta{
@@ -523,7 +523,7 @@ func TestConvertFrom(t *testing.T) {
523523
}
524524

525525
func TestShouldConvertFromWhenOnlyMemoryResourceSpecified(t *testing.T) {
526-
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
526+
infrastructure.InitializeForTesting(infrastructure.OpenShiftV4)
527527

528528
memoryRequest := resource.MustParse("128Mi")
529529
memoryLimit := resource.MustParse("228Mi")

api/checluster_conversion_to_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
"k8s.io/utils/pointer"
2020

2121
devfile "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
22-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
2322
chev1 "github.com/eclipse-che/che-operator/api/v1"
2423
chev2 "github.com/eclipse-che/che-operator/api/v2"
2524
"github.com/eclipse-che/che-operator/pkg/common/constants"
25+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
2626
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
2727
"github.com/stretchr/testify/assert"
2828
corev1 "k8s.io/api/core/v1"
@@ -38,7 +38,7 @@ func TestConvertToEmptyCheCluster(t *testing.T) {
3838
}
3939

4040
func TestConvertToIngressOnOpenShift(t *testing.T) {
41-
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
41+
infrastructure.InitializeForTesting(infrastructure.OpenShiftV4)
4242

4343
checlusterv1 := &chev1.CheCluster{
4444
ObjectMeta: metav1.ObjectMeta{

api/checluster_round_conversion_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package org
1515
import (
1616
"testing"
1717

18-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
18+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
1919
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
2020
corev1 "k8s.io/api/core/v1"
2121
"k8s.io/apimachinery/pkg/api/resource"
@@ -325,6 +325,6 @@ func onKubernetes(f func()) {
325325
}
326326

327327
func onOpenShift(f func()) {
328-
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
328+
infrastructure.InitializeForTesting(infrastructure.OpenShiftV4)
329329
f()
330330
}

api/init_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
package org
1414

1515
import (
16-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
16+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
1717
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
1818
"github.com/eclipse-che/che-operator/pkg/common/test"
1919
)
2020

2121
func init() {
2222
test.EnableTestMode()
2323

24-
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
24+
infrastructure.InitializeForTesting(infrastructure.OpenShiftV4)
2525
defaults.InitializeForTesting("../config/manager/manager.yaml")
2626
}

api/v1/checluster_conversion_from.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121

2222
"github.com/eclipse-che/che-operator/pkg/common/utils"
2323

24-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
2524
chev2 "github.com/eclipse-che/che-operator/api/v2"
2625
"github.com/eclipse-che/che-operator/pkg/common/constants"
26+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
2727
k8shelper "github.com/eclipse-che/che-operator/pkg/common/k8s-helper"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"sigs.k8s.io/controller-runtime/pkg/conversion"

api/v1/checluster_conversion_to.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
"github.com/eclipse-che/che-operator/pkg/common/utils"
2424
ctrl "sigs.k8s.io/controller-runtime"
2525

26-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
2726
chev2 "github.com/eclipse-che/che-operator/api/v2"
2827
"github.com/eclipse-che/che-operator/pkg/common/constants"
28+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
2929
k8shelper "github.com/eclipse-che/che-operator/pkg/common/k8s-helper"
3030
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
3131
corev1 "k8s.io/api/core/v1"

api/v2/checluster_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import (
1919
"strconv"
2020
"strings"
2121

22+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
2223
"k8s.io/utils/pointer"
2324

2425
ctrl "sigs.k8s.io/controller-runtime"
2526

2627
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
2728

28-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
2929
"github.com/eclipse-che/che-operator/pkg/common/constants"
3030

3131
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
@@ -1098,7 +1098,7 @@ func (c *CheCluster) GetIdentityToken() string {
10981098
return c.Spec.Networking.Auth.IdentityToken
10991099
}
11001100

1101-
if infrastructure.IsOpenShift() {
1101+
if infrastructure.IsOpenShiftOAuthEnabled() {
11021102
return constants.AccessToken
11031103
}
11041104
return constants.IdToken

api/v2/checluster_types_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import (
1616
"reflect"
1717
"testing"
1818

19-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
19+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
2020
"github.com/stretchr/testify/assert"
2121
)
2222

23-
func TestIsAccesTokenConfigured(t *testing.T) {
23+
func TestIsAccessTokenConfigured(t *testing.T) {
2424
t.Run("TestIsAccesTokenConfigured when access_token defined", func(t *testing.T) {
2525
cheCluster := &CheCluster{
2626
Spec: CheClusterSpec{
@@ -98,7 +98,7 @@ func TestGetIdentityToken(t *testing.T) {
9898
},
9999
}},
100100
}
101-
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
101+
infrastructure.InitializeForTesting(infrastructure.OpenShiftV4)
102102

103103
assert.Equal(t, "access_token", cheCluster.GetIdentityToken(),
104104
"'access_token' should be used")
@@ -113,7 +113,7 @@ func TestGetIdentityToken(t *testing.T) {
113113
},
114114
}},
115115
}
116-
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
116+
infrastructure.InitializeForTesting(infrastructure.OpenShiftV4)
117117

118118
assert.Equal(t, "id_token", cheCluster.GetIdentityToken(),
119119
"'id_token' should be used")
@@ -126,7 +126,7 @@ func TestGetIdentityToken(t *testing.T) {
126126
Auth: Auth{},
127127
}},
128128
}
129-
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
129+
infrastructure.InitializeForTesting(infrastructure.OpenShiftV4)
130130

131131
assert.Equal(t, "access_token", cheCluster.GetIdentityToken(),
132132
"'access_token' should be used")
@@ -141,9 +141,8 @@ func TestGetDefaultIdentityToken(t *testing.T) {
141141
infrastructure infrastructure.Type
142142
identityToken string
143143
}{
144-
{infrastructure.OpenShiftv4, "access_token"},
144+
{infrastructure.OpenShiftV4, "access_token"},
145145
{infrastructure.Kubernetes, "id_token"},
146-
{infrastructure.Unsupported, "id_token"},
147146
}
148147
for _, test := range tests {
149148
infrastructure.InitializeForTesting(test.infrastructure)

api/v2/checluster_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2222

23-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
23+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
2424
"k8s.io/utils/pointer"
2525

2626
"github.com/eclipse-che/che-operator/pkg/common/constants"

cmd/main.go

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
"os"
1818
"time"
1919

20+
dwInfra "github.com/devfile/devworkspace-operator/pkg/infrastructure"
21+
"github.com/eclipse-che/che-operator/pkg/common/infrastructure"
22+
oauthv1 "github.com/openshift/api/oauth/v1"
23+
userv1 "github.com/openshift/api/user/v1"
2024
"sigs.k8s.io/controller-runtime/pkg/webhook"
2125

2226
"github.com/eclipse-che/che-operator/controllers/namespacecache"
@@ -33,8 +37,6 @@ import (
3337
securityv1 "github.com/openshift/api/security/v1"
3438

3539
dwoApi "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
36-
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
37-
devworkspaceinfra "github.com/devfile/devworkspace-operator/pkg/infrastructure"
3840
"go.uber.org/zap/zapcore"
3941

4042
"github.com/eclipse-che/che-operator/pkg/common/constants"
@@ -62,11 +64,9 @@ import (
6264
"github.com/go-logr/logr"
6365
configv1 "github.com/openshift/api/config/v1"
6466
consolev1 "github.com/openshift/api/console/v1"
65-
oauthv1 "github.com/openshift/api/oauth/v1"
6667
templatev1 "github.com/openshift/api/template/v1"
6768

6869
checontroller "github.com/eclipse-che/che-operator/controllers/che"
69-
"github.com/eclipse-che/che-operator/pkg/common/utils"
7070

7171
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
7272
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -79,7 +79,6 @@ import (
7979
imagepullerapi "github.com/che-incubator/kubernetes-image-puller-operator/api/v1alpha1"
8080
projectv1 "github.com/openshift/api/project/v1"
8181
routev1 "github.com/openshift/api/route/v1"
82-
userv1 "github.com/openshift/api/user/v1"
8382
appsv1 "k8s.io/api/apps/v1"
8483
batchv1 "k8s.io/api/batch/v1"
8584
corev1 "k8s.io/api/core/v1"
@@ -101,10 +100,6 @@ var (
101100
renewDeadline = 30 * time.Second
102101
)
103102

104-
const (
105-
leasesApiResourceName = "leases"
106-
)
107-
108103
func init() {
109104
flag.StringVar(&metricsAddr, "metrics-bind-address", ":60000", "The address the metric endpoint binds to.")
110105
flag.StringVar(&probeAddr, "health-probe-bind-address", ":6789", "The address the probe endpoint binds to.")
@@ -122,11 +117,6 @@ func init() {
122117
logger := zap.New(zap.UseFlagOptions(&opts))
123118
ctrl.SetLogger(logger)
124119

125-
if err := infrastructure.Initialize(); err != nil {
126-
logger.Error(err, "Unable determine installation platform")
127-
os.Exit(1)
128-
}
129-
130120
defaults.Initialize()
131121

132122
printVersion(logger)
@@ -145,15 +135,20 @@ func init() {
145135
utilruntime.Must(corev1.AddToScheme(scheme))
146136

147137
if infrastructure.IsOpenShift() {
148-
utilruntime.Must(routev1.AddToScheme(scheme))
149-
utilruntime.Must(oauthv1.AddToScheme(scheme))
150-
utilruntime.Must(userv1.AddToScheme(scheme))
151-
utilruntime.Must(configv1.AddToScheme(scheme))
152-
utilruntime.Must(consolev1.AddToScheme(scheme))
153-
utilruntime.Must(projectv1.AddToScheme(scheme))
138+
utilruntime.Must(routev1.Install(scheme))
139+
utilruntime.Must(configv1.Install(scheme))
140+
utilruntime.Must(consolev1.Install(scheme))
141+
utilruntime.Must(projectv1.Install(scheme))
154142
utilruntime.Must(securityv1.Install(scheme))
155143
utilruntime.Must(templatev1.Install(scheme))
156144
}
145+
146+
// User and OAuthClient API are disabled in case of external IDP
147+
// Check API before adding to the scheme
148+
if infrastructure.IsOpenShiftOAuthEnabled() {
149+
utilruntime.Must(userv1.Install(scheme))
150+
utilruntime.Must(oauthv1.Install(scheme))
151+
}
157152
}
158153

159154
func getLogLevel() zapcore.Level {
@@ -181,7 +176,7 @@ func printVersion(logger logr.Logger) {
181176

182177
infra := "Kubernetes"
183178
if infrastructure.IsOpenShift() {
184-
infra = "OpenShift v4.x"
179+
infra = "OpenShift"
185180
}
186181
logger.Info("Operator is running on ", "Infrastructure", infra)
187182
}
@@ -202,6 +197,11 @@ func getWatchNamespace() (string, error) {
202197
}
203198

204199
func main() {
200+
if err := dwInfra.Initialize(); err != nil {
201+
setupLog.Error(err, "Failed to initialize infrastructure")
202+
os.Exit(1)
203+
}
204+
205205
watchNamespace, err := getWatchNamespace()
206206
if err != nil {
207207
setupLog.Error(err, "unable to get WatchNamespace, "+
@@ -216,8 +216,8 @@ func main() {
216216
os.Exit(1)
217217
}
218218

219-
if !utils.IsK8SResourceServed(discoveryClient, leasesApiResourceName) {
220-
setupLog.Info("Leader election was disabled", "Cause:", leasesApiResourceName+"k8s api resource is an absent.")
219+
if !infrastructure.IsLeaderElectionEnabled() {
220+
setupLog.Info("Leader election disabled")
221221
enableLeaderElection = false
222222
}
223223

@@ -227,13 +227,6 @@ func main() {
227227
os.Exit(1)
228228
}
229229

230-
// DWO use the infrastructure package for openshift detection. It needs to be initialized
231-
// but only supports OpenShift v4 or Kubernetes.
232-
if err := devworkspaceinfra.Initialize(); err != nil {
233-
setupLog.Error(err, "failed to evaluate infrastructure which is needed for DevWorkspace support")
234-
os.Exit(1)
235-
}
236-
237230
cacheFunction, err := getCacheFunc()
238231
if err != nil {
239232
setupLog.Error(err, "failed to create cache function")
@@ -394,11 +387,14 @@ func getCacheFunc() (cache.NewCacheFunc, error) {
394387
}
395388

396389
if infrastructure.IsOpenShift() {
397-
selectors[&oauthv1.OAuthClient{}] = cache.ByObject{Label: partOfCheObjectSelector}
398390
selectors[&routev1.Route{}] = cache.ByObject{Label: partOfCheObjectSelector}
399391
selectors[&templatev1.Template{}] = cache.ByObject{Label: partOfCheObjectSelector}
400392
}
401393

394+
if infrastructure.IsOpenShiftOAuthEnabled() {
395+
selectors[&oauthv1.OAuthClient{}] = cache.ByObject{Label: partOfCheObjectSelector}
396+
}
397+
402398
return func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
403399
opts.ByObject = selectors
404400
return cache.New(config, opts)

0 commit comments

Comments
 (0)