Skip to content

Commit 2c5a5ba

Browse files
synaretemergify[bot]
authored andcommitted
conf: allow user to define cluster-type
Make cluster-type an optional conf settings. Only when not defined by user try to probe is from within the cluster itself. Signed-off-by: Shachar Sharon <[email protected]>
1 parent 6d10110 commit 2c5a5ba

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

controllers/smbcommonconfig_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
type SmbCommonConfigReconciler struct {
3333
client.Client
3434
Log logr.Logger
35-
ClusterType resources.ClusterType
35+
ClusterType string
3636
}
3737

3838
//revive:disable kubebuilder directives
@@ -60,7 +60,7 @@ func (r *SmbCommonConfigReconciler) Reconcile(
6060
// 1) Unknown cluster type due to first-time reconcile
6161
// 2) Known to be running over OpenShift by in-memory cached state from
6262
// previous reconcile loop.
63-
if r.ClusterType != "" && r.ClusterType != resources.ClusterTypeOpenshift {
63+
if r.ClusterType != "" && r.ClusterType != conf.ClusterTypeOpenShift {
6464
return ctrl.Result{}, nil
6565
}
6666

internal/conf/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import (
99
"github.com/spf13/viper"
1010
)
1111

12+
const (
13+
// ClusterTypeDefault defines the default value for cluster type
14+
ClusterTypeDefault = "default"
15+
// ClusterTypeOpenShift defines the type-name for OpenShift clusters
16+
ClusterTypeOpenShift = "openshift"
17+
)
18+
1219
// DefaultOperatorConfig holds the default values of OperatorConfig.
1320
var DefaultOperatorConfig = OperatorConfig{
1421
SmbdContainerImage: "quay.io/samba.org/samba-server:latest",
@@ -25,6 +32,7 @@ var DefaultOperatorConfig = OperatorConfig{
2532
MetricsExporterMode: "disabled",
2633
ImagePullPolicy: "IfNotPresent",
2734
DefaultNodeSelector: "",
35+
ClusterType: "",
2836
}
2937

3038
// OperatorConfig is a type holding general configuration values.
@@ -83,6 +91,10 @@ type OperatorConfig struct {
8391
// a set of key-value pairs that will be used for all default node
8492
// selection. If left blank, internal defaults will be used.
8593
DefaultNodeSelector string `mapstructure:"default-node-selector"`
94+
// ClusterType is a string which defines the type of underlying K8S
95+
// cluster (minikube, OpenShift etc). If not provided, the operator will
96+
// try to figure it out.
97+
ClusterType string `mapstructure:"cluster-type"`
8698
}
8799

88100
// Validate the OperatorConfig returning an error if the config is not
@@ -134,6 +146,7 @@ func NewSource() *Source {
134146
v.SetDefault("pod-ip", d.PodIP)
135147
v.SetDefault("image-pull-policy", d.ImagePullPolicy)
136148
v.SetDefault("default-node-selector", d.DefaultNodeSelector)
149+
v.SetDefault("cluster-type", d.ClusterType)
137150
return &Source{v: v}
138151
}
139152

internal/resources/openshift.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ import (
1919
"github.com/samba-in-kubernetes/samba-operator/internal/conf"
2020
)
2121

22-
// ClusterType reperesnts the sub-kind of k8s cluster type.
23-
type ClusterType string
24-
25-
const (
26-
// ClusterTypeDefault defines the default value for cluster type
27-
ClusterTypeDefault = "default"
28-
// ClusterTypeOpenshift defines the type-name for OpenShift clusters
29-
ClusterTypeOpenshift = "openshift"
30-
)
31-
3222
const (
3323
openshiftFinalizer = "samba-operator.samba.org/openshiftFinalizer"
3424
serviceAccountName = "samba"
@@ -46,7 +36,7 @@ type OpenShiftManager struct {
4636
client rtclient.Client
4737
logger logr.Logger
4838
cfg *conf.OperatorConfig
49-
ClusterType ClusterType
39+
ClusterType string
5040
}
5141

5242
// NewOpenShiftManager creates a ServiceAccountManager instance
@@ -68,7 +58,7 @@ func (m *OpenShiftManager) Process(
6858
nsname types.NamespacedName) Result {
6959
// Do-nothing if not on OpenShift
7060
m.resolveClusterType(ctx)
71-
if m.ClusterType != ClusterTypeOpenshift {
61+
if m.ClusterType != conf.ClusterTypeOpenShift {
7262
return Done
7363
}
7464

@@ -103,9 +93,9 @@ func (m *OpenShiftManager) Process(
10393
// Cache cluster type
10494
func (m *OpenShiftManager) resolveClusterType(ctx context.Context) {
10595
if IsOpenShiftCluster(ctx, m.client, m.cfg) {
106-
m.ClusterType = ClusterTypeOpenshift
96+
m.ClusterType = conf.ClusterTypeOpenShift
10797
} else {
108-
m.ClusterType = ClusterTypeDefault
98+
m.ClusterType = conf.ClusterTypeDefault
10999
}
110100
}
111101

@@ -657,24 +647,24 @@ func IsOpenShiftCluster(ctx context.Context,
657647
Name: cfg.PodName,
658648
}
659649
clusterType, err := resolveClusterTypeByPod(ctx, reader, key)
660-
return (err == nil) && (clusterType == ClusterTypeOpenshift)
650+
return (err == nil) && (clusterType == conf.ClusterTypeOpenShift)
661651
}
662652

663653
// resolveClusterTypeByPod finds the kind of K8s cluster via annotation of one
664654
// of its running pods.
665655
func resolveClusterTypeByPod(ctx context.Context,
666656
reader rtclient.Reader,
667-
podKey rtclient.ObjectKey) (ClusterType, error) {
657+
podKey rtclient.ObjectKey) (string, error) {
668658
pod, err := getPod(ctx, reader, podKey)
669659
if err != nil {
670-
return ClusterTypeDefault, err
660+
return conf.ClusterTypeDefault, err
671661
}
672662
for key := range pod.Annotations {
673-
if strings.Contains(key, ClusterTypeOpenshift) {
674-
return ClusterTypeOpenshift, nil
663+
if strings.Contains(key, conf.ClusterTypeOpenShift) {
664+
return conf.ClusterTypeOpenShift, nil
675665
}
676666
}
677-
return ClusterTypeDefault, nil
667+
return conf.ClusterTypeDefault, nil
678668
}
679669

680670
func getPod(ctx context.Context,

0 commit comments

Comments
 (0)