Skip to content

Commit 6884fd7

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
1 parent 04f02c1 commit 6884fd7

File tree

5 files changed

+106
-16
lines changed

5 files changed

+106
-16
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ import (
5353
// SolrCloudReconciler reconciles a SolrCloud object
5454
type SolrCloudReconciler struct {
5555
client.Client
56-
Scheme *runtime.Scheme
56+
Scheme *runtime.Scheme
57+
IsOpenShift bool
5758
}
5859

5960
var useZkCRD bool
@@ -328,7 +329,7 @@ func (r *SolrCloudReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
328329
var statefulSet *appsv1.StatefulSet
329330
if !blockReconciliationOfStatefulSet {
330331
// Generate StatefulSet that should exist
331-
expectedStatefulSet := util.GenerateStatefulSet(instance, &newStatus, hostNameIpMap, reconcileConfigInfo, tls, security)
332+
expectedStatefulSet := util.GenerateStatefulSet(instance, &newStatus, hostNameIpMap, reconcileConfigInfo, tls, security, r.IsOpenShift)
332333

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

controllers/suite_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ var _ = BeforeSuite(func(ctx context.Context) {
106106
// Start up Reconcilers
107107
By("starting the reconcilers")
108108
Expect((&SolrCloudReconciler{
109-
Client: k8sManager.GetClient(),
110-
Scheme: k8sManager.GetScheme(),
109+
Client: k8sManager.GetClient(),
110+
Scheme: k8sManager.GetScheme(),
111+
IsOpenShift: false,
111112
}).SetupWithManager(k8sManager)).To(Succeed())
112113

113114
Expect((&SolrPrometheusExporterReconciler{

controllers/util/solr_util.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var (
8484
// replicas: the number of replicas for the SolrCloud instance
8585
// storage: the size of the storage for the SolrCloud instance (e.g. 100Gi)
8686
// 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 {
87+
func GenerateStatefulSet(solrCloud *solr.SolrCloud, solrCloudStatus *solr.SolrCloudStatus, hostNameIPs map[string]string, reconcileConfigInfo map[string]string, tls *TLSCerts, security *SecurityConfig, isOpenShift bool) *appsv1.StatefulSet {
8888
terminationGracePeriod := int64(60)
8989
shareProcessNamespace := false
9090
solrPodPort := solrCloud.Spec.SolrAddressability.PodPort
@@ -549,19 +549,20 @@ func GenerateStatefulSet(solrCloud *solr.SolrCloud, solrCloudStatus *solr.SolrCl
549549
Spec: corev1.PodSpec{
550550
TerminationGracePeriodSeconds: &terminationGracePeriod,
551551
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,
552+
SecurityContext: &corev1.PodSecurityContext{},
553+
Volumes: solrVolumes,
554+
InitContainers: initContainers,
555+
HostAliases: hostAliases,
556+
Containers: containers,
557+
ReadinessGates: podReadinessGates,
560558
},
561559
},
562560
VolumeClaimTemplates: pvcs,
563561
},
564562
}
563+
if !isOpenShift {
564+
stateful.Spec.Template.Spec.SecurityContext.FSGroup = &defaultFSGroup
565+
}
565566
if solrCloud.UsesHeadlessService() {
566567
stateful.Spec.Template.Spec.Subdomain = solrCloud.HeadlessServiceName()
567568
}
@@ -598,7 +599,7 @@ func GenerateStatefulSet(solrCloud *solr.SolrCloud, solrCloudStatus *solr.SolrCl
598599

599600
if customPodOptions.PodSecurityContext != nil {
600601
stateful.Spec.Template.Spec.SecurityContext = customPodOptions.PodSecurityContext
601-
if stateful.Spec.Template.Spec.SecurityContext.FSGroup == nil {
602+
if stateful.Spec.Template.Spec.SecurityContext.FSGroup == nil && !isOpenShift {
602603
stateful.Spec.Template.Spec.SecurityContext.FSGroup = &defaultFSGroup
603604
}
604605
}

main.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,26 @@ func main() {
198198
}
199199
}
200200

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

0 commit comments

Comments
 (0)