Skip to content

Commit

Permalink
conf: allow user to define cluster-type
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
synarete authored and mergify[bot] committed Mar 23, 2023
1 parent 6d10110 commit 2c5a5ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions controllers/smbcommonconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
type SmbCommonConfigReconciler struct {
client.Client
Log logr.Logger
ClusterType resources.ClusterType
ClusterType string
}

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

Expand Down
13 changes: 13 additions & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"github.com/spf13/viper"
)

const (
// ClusterTypeDefault defines the default value for cluster type
ClusterTypeDefault = "default"
// ClusterTypeOpenShift defines the type-name for OpenShift clusters
ClusterTypeOpenShift = "openshift"
)

// DefaultOperatorConfig holds the default values of OperatorConfig.
var DefaultOperatorConfig = OperatorConfig{
SmbdContainerImage: "quay.io/samba.org/samba-server:latest",
Expand All @@ -25,6 +32,7 @@ var DefaultOperatorConfig = OperatorConfig{
MetricsExporterMode: "disabled",
ImagePullPolicy: "IfNotPresent",
DefaultNodeSelector: "",
ClusterType: "",
}

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

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

Expand Down
30 changes: 10 additions & 20 deletions internal/resources/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ import (
"github.com/samba-in-kubernetes/samba-operator/internal/conf"
)

// ClusterType reperesnts the sub-kind of k8s cluster type.
type ClusterType string

const (
// ClusterTypeDefault defines the default value for cluster type
ClusterTypeDefault = "default"
// ClusterTypeOpenshift defines the type-name for OpenShift clusters
ClusterTypeOpenshift = "openshift"
)

const (
openshiftFinalizer = "samba-operator.samba.org/openshiftFinalizer"
serviceAccountName = "samba"
Expand All @@ -46,7 +36,7 @@ type OpenShiftManager struct {
client rtclient.Client
logger logr.Logger
cfg *conf.OperatorConfig
ClusterType ClusterType
ClusterType string
}

// NewOpenShiftManager creates a ServiceAccountManager instance
Expand All @@ -68,7 +58,7 @@ func (m *OpenShiftManager) Process(
nsname types.NamespacedName) Result {
// Do-nothing if not on OpenShift
m.resolveClusterType(ctx)
if m.ClusterType != ClusterTypeOpenshift {
if m.ClusterType != conf.ClusterTypeOpenShift {
return Done
}

Expand Down Expand Up @@ -103,9 +93,9 @@ func (m *OpenShiftManager) Process(
// Cache cluster type
func (m *OpenShiftManager) resolveClusterType(ctx context.Context) {
if IsOpenShiftCluster(ctx, m.client, m.cfg) {
m.ClusterType = ClusterTypeOpenshift
m.ClusterType = conf.ClusterTypeOpenShift
} else {
m.ClusterType = ClusterTypeDefault
m.ClusterType = conf.ClusterTypeDefault
}
}

Expand Down Expand Up @@ -657,24 +647,24 @@ func IsOpenShiftCluster(ctx context.Context,
Name: cfg.PodName,
}
clusterType, err := resolveClusterTypeByPod(ctx, reader, key)
return (err == nil) && (clusterType == ClusterTypeOpenshift)
return (err == nil) && (clusterType == conf.ClusterTypeOpenShift)
}

// resolveClusterTypeByPod finds the kind of K8s cluster via annotation of one
// of its running pods.
func resolveClusterTypeByPod(ctx context.Context,
reader rtclient.Reader,
podKey rtclient.ObjectKey) (ClusterType, error) {
podKey rtclient.ObjectKey) (string, error) {
pod, err := getPod(ctx, reader, podKey)
if err != nil {
return ClusterTypeDefault, err
return conf.ClusterTypeDefault, err
}
for key := range pod.Annotations {
if strings.Contains(key, ClusterTypeOpenshift) {
return ClusterTypeOpenshift, nil
if strings.Contains(key, conf.ClusterTypeOpenShift) {
return conf.ClusterTypeOpenShift, nil
}
}
return ClusterTypeDefault, nil
return conf.ClusterTypeDefault, nil
}

func getPod(ctx context.Context,
Expand Down

0 comments on commit 2c5a5ba

Please sign in to comment.