Skip to content

Commit fe0ec20

Browse files
committed
Adding support for OpenShift securityContext
Adding autodetection of solr-operator running on an OpenShift cluster to remove the default Solr fsGroup, and have an empty securityContext on OpenShift. Fixes #466 with '#' will be ignored, and an empty message aborts the commit. # # Date: Fri May 24 20:34:53 2024 -0600 # # On branch openshift # Your branch is up to date with 'origin/openshift'. # # Changes to be committed: # new file: controllers/autodetect.go # modified: controllers/solrcloud_controller.go # modified: controllers/suite_test.go # modified: controllers/util/solr_util.go # modified: main.go #
1 parent 04f02c1 commit fe0ec20

File tree

5 files changed

+123
-29
lines changed

5 files changed

+123
-29
lines changed

controllers/autodetect.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Copied from grafana-operator
19+
// With the Apache License: https://github.com/grafana/grafana-operator/blob/master/LICENSE
20+
// See: https://github.com/grafana/grafana-operator/blob/master/controllers/autodetect/main.go
21+
// Package autodetect is for auto-detecting traits from the environment (platform, APIs, ...).
22+
package controllers
23+
24+
import (
25+
"k8s.io/client-go/discovery"
26+
"k8s.io/client-go/rest"
27+
)
28+
29+
var _ AutoDetect = (*autoDetect)(nil)
30+
31+
// AutoDetect provides an assortment of routines that auto-detect traits based on the runtime.
32+
type AutoDetect interface {
33+
IsOpenshift() (bool, error)
34+
}
35+
36+
type autoDetect struct {
37+
dcl discovery.DiscoveryInterface
38+
}
39+
40+
// New creates a new auto-detection worker, using the given client when talking to the current cluster.
41+
func NewAutodetect(restConfig *rest.Config) (AutoDetect, error) {
42+
dcl, err := discovery.NewDiscoveryClientForConfig(restConfig)
43+
if err != nil {
44+
// it's pretty much impossible to get into this problem, as most of the
45+
// code branches from the previous call just won't fail at all,
46+
// but let's handle this error anyway...
47+
return nil, err
48+
}
49+
50+
return &autoDetect{
51+
dcl: dcl,
52+
}, nil
53+
}
54+
55+
// Platform returns the detected platform this operator is running on. Possible values: Kubernetes, OpenShift.
56+
func (a *autoDetect) IsOpenshift() (bool, error) {
57+
apiList, err := a.dcl.ServerGroups()
58+
if err != nil {
59+
return false, err
60+
}
61+
62+
apiGroups := apiList.Groups
63+
for i := range apiGroups {
64+
if apiGroups[i].Name == "route.openshift.io" {
65+
return true, nil
66+
}
67+
}
68+
69+
return false, nil
70+
}

controllers/solrcloud_controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ import (
2121
"context"
2222
"crypto/md5"
2323
"fmt"
24-
policyv1 "k8s.io/api/policy/v1"
25-
"k8s.io/apimachinery/pkg/runtime"
2624
"reflect"
2725
"sort"
2826
"strings"
2927
"time"
3028

29+
policyv1 "k8s.io/api/policy/v1"
30+
"k8s.io/apimachinery/pkg/runtime"
31+
3132
solrv1beta1 "github.com/apache/solr-operator/api/v1beta1"
3233
"github.com/apache/solr-operator/controllers/util"
3334
"github.com/go-logr/logr"
@@ -53,7 +54,8 @@ import (
5354
// SolrCloudReconciler reconciles a SolrCloud object
5455
type SolrCloudReconciler struct {
5556
client.Client
56-
Scheme *runtime.Scheme
57+
Scheme *runtime.Scheme
58+
IsOpenShift bool
5759
}
5860

5961
var useZkCRD bool
@@ -328,7 +330,7 @@ func (r *SolrCloudReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
328330
var statefulSet *appsv1.StatefulSet
329331
if !blockReconciliationOfStatefulSet {
330332
// Generate StatefulSet that should exist
331-
expectedStatefulSet := util.GenerateStatefulSet(instance, &newStatus, hostNameIpMap, reconcileConfigInfo, tls, security)
333+
expectedStatefulSet := util.GenerateStatefulSet(instance, &newStatus, hostNameIpMap, reconcileConfigInfo, tls, security, r.IsOpenShift)
332334

333335
// Check if the StatefulSet already exists
334336
statefulSetLogger := logger.WithValues("statefulSet", expectedStatefulSet.Name)

controllers/suite_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package controllers
1919

2020
import (
2121
"context"
22-
"github.com/go-logr/logr"
23-
zkApi "github.com/pravega/zookeeper-operator/api/v1beta1"
2422
"path/filepath"
25-
ctrl "sigs.k8s.io/controller-runtime"
2623
"testing"
2724
"time"
2825

26+
"github.com/go-logr/logr"
27+
zkApi "github.com/pravega/zookeeper-operator/api/v1beta1"
28+
ctrl "sigs.k8s.io/controller-runtime"
29+
2930
. "github.com/onsi/ginkgo/v2"
3031
. "github.com/onsi/gomega"
3132
"k8s.io/client-go/kubernetes/scheme"
@@ -106,8 +107,9 @@ var _ = BeforeSuite(func(ctx context.Context) {
106107
// Start up Reconcilers
107108
By("starting the reconcilers")
108109
Expect((&SolrCloudReconciler{
109-
Client: k8sManager.GetClient(),
110-
Scheme: k8sManager.GetScheme(),
110+
Client: k8sManager.GetClient(),
111+
Scheme: k8sManager.GetScheme(),
112+
IsOpenShift: false,
111113
}).SetupWithManager(k8sManager)).To(Succeed())
112114

113115
Expect((&SolrPrometheusExporterReconciler{

controllers/util/solr_util.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ package util
1919

2020
import (
2121
"fmt"
22+
"sort"
23+
"strconv"
24+
"strings"
25+
2226
solr "github.com/apache/solr-operator/api/v1beta1"
2327
appsv1 "k8s.io/api/apps/v1"
2428
corev1 "k8s.io/api/core/v1"
@@ -28,9 +32,6 @@ import (
2832
"k8s.io/apimachinery/pkg/util/intstr"
2933
"k8s.io/utils/pointer"
3034
"k8s.io/utils/ptr"
31-
"sort"
32-
"strconv"
33-
"strings"
3435
)
3536

3637
const (
@@ -84,7 +85,7 @@ var (
8485
// replicas: the number of replicas for the SolrCloud instance
8586
// storage: the size of the storage for the SolrCloud instance (e.g. 100Gi)
8687
// zkConnectionString: the connectionString of the ZK instance to connect to
87-
func GenerateStatefulSet(solrCloud *solr.SolrCloud, solrCloudStatus *solr.SolrCloudStatus, hostNameIPs map[string]string, reconcileConfigInfo map[string]string, tls *TLSCerts, security *SecurityConfig) *appsv1.StatefulSet {
88+
func GenerateStatefulSet(solrCloud *solr.SolrCloud, solrCloudStatus *solr.SolrCloudStatus, hostNameIPs map[string]string, reconcileConfigInfo map[string]string, tls *TLSCerts, security *SecurityConfig, isOpenShift bool) *appsv1.StatefulSet {
8889
terminationGracePeriod := int64(60)
8990
shareProcessNamespace := false
9091
solrPodPort := solrCloud.Spec.SolrAddressability.PodPort
@@ -549,19 +550,20 @@ func GenerateStatefulSet(solrCloud *solr.SolrCloud, solrCloudStatus *solr.SolrCl
549550
Spec: corev1.PodSpec{
550551
TerminationGracePeriodSeconds: &terminationGracePeriod,
551552
ShareProcessNamespace: &shareProcessNamespace,
552-
SecurityContext: &corev1.PodSecurityContext{
553-
FSGroup: &defaultFSGroup,
554-
},
555-
Volumes: solrVolumes,
556-
InitContainers: initContainers,
557-
HostAliases: hostAliases,
558-
Containers: containers,
559-
ReadinessGates: podReadinessGates,
553+
SecurityContext: &corev1.PodSecurityContext{},
554+
Volumes: solrVolumes,
555+
InitContainers: initContainers,
556+
HostAliases: hostAliases,
557+
Containers: containers,
558+
ReadinessGates: podReadinessGates,
560559
},
561560
},
562561
VolumeClaimTemplates: pvcs,
563562
},
564563
}
564+
if !isOpenShift {
565+
stateful.Spec.Template.Spec.SecurityContext.FSGroup = &defaultFSGroup
566+
}
565567
if solrCloud.UsesHeadlessService() {
566568
stateful.Spec.Template.Spec.Subdomain = solrCloud.HeadlessServiceName()
567569
}
@@ -598,7 +600,7 @@ func GenerateStatefulSet(solrCloud *solr.SolrCloud, solrCloudStatus *solr.SolrCl
598600

599601
if customPodOptions.PodSecurityContext != nil {
600602
stateful.Spec.Template.Spec.SecurityContext = customPodOptions.PodSecurityContext
601-
if stateful.Spec.Template.Spec.SecurityContext.FSGroup == nil {
603+
if stateful.Spec.Template.Spec.SecurityContext.FSGroup == nil && !isOpenShift {
602604
stateful.Spec.Template.Spec.SecurityContext.FSGroup = &defaultFSGroup
603605
}
604606
}

main.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ import (
2222
"crypto/x509"
2323
"flag"
2424
"fmt"
25-
"github.com/apache/solr-operator/controllers/util/solr_api"
26-
"github.com/apache/solr-operator/version"
27-
"github.com/fsnotify/fsnotify"
28-
zkApi "github.com/pravega/zookeeper-operator/api/v1beta1"
2925
"io/ioutil"
3026
"net/http"
3127
"os"
3228
"path/filepath"
3329
"runtime"
30+
"strings"
31+
32+
"github.com/apache/solr-operator/controllers/util/solr_api"
33+
"github.com/apache/solr-operator/version"
34+
"github.com/fsnotify/fsnotify"
35+
zkApi "github.com/pravega/zookeeper-operator/api/v1beta1"
3436
"sigs.k8s.io/controller-runtime/pkg/cache"
3537
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
36-
"strings"
3738

3839
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
3940
// to ensure that exec-entrypoint and run can make use of them.
@@ -198,9 +199,26 @@ func main() {
198199
}
199200
}
200201

202+
// Fetch k8s api credentials and detect platform
203+
restConfig := ctrl.GetConfigOrDie()
204+
205+
autodetect, err := controllers.NewAutodetect(restConfig)
206+
if err != nil {
207+
setupLog.Error(err, "failed to setup auto-detect routine")
208+
os.Exit(1)
209+
}
210+
211+
isOpenShift, err := autodetect.IsOpenshift()
212+
setupLog.Info("autodetect", "isOpenShift", isOpenShift)
213+
if err != nil {
214+
setupLog.Error(err, "unable to detect the platform")
215+
os.Exit(1)
216+
}
217+
201218
if err = (&controllers.SolrCloudReconciler{
202-
Client: mgr.GetClient(),
203-
Scheme: mgr.GetScheme(),
219+
Client: mgr.GetClient(),
220+
Scheme: mgr.GetScheme(),
221+
IsOpenShift: isOpenShift,
204222
}).SetupWithManager(mgr); err != nil {
205223
setupLog.Error(err, "unable to create controller", "controller", "SolrCloud")
206224
os.Exit(1)

0 commit comments

Comments
 (0)