Skip to content

Commit

Permalink
update vars to add the namespace in one please
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Sch <[email protected]>
  • Loading branch information
SchSeba committed Dec 8, 2023
1 parent 8788997 commit aeb73b9
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 60 deletions.
3 changes: 1 addition & 2 deletions cmd/sriov-network-config-daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
}
setupLog.Info("Running on", "platform", vars.PlatformType.String())

var namespace = os.Getenv("NAMESPACE")
if err := sriovnetworkv1.InitNicIDMapFromConfigMap(kubeclient, namespace); err != nil {
if err := sriovnetworkv1.InitNicIDMapFromConfigMap(kubeclient, vars.Namespace); err != nil {
setupLog.Error(err, "failed to run init NicIdMap")
return err
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/sriovibnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars"
)

// SriovIBNetworkReconciler reconciles a SriovIBNetwork object
Expand All @@ -58,7 +59,7 @@ type SriovIBNetworkReconciler struct {
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *SriovIBNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// The SriovNetwork CR shall only be defined in operator namespace.
req.Namespace = namespace
req.Namespace = vars.Namespace
reqLogger := log.FromContext(ctx).WithValues("sriovnetwork", req.NamespacedName)
reqLogger.Info("Reconciling SriovIBNetwork")
var err error
Expand Down
3 changes: 2 additions & 1 deletion controllers/sriovnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars"
)

// SriovNetworkReconciler reconciles a SriovNetwork object
Expand All @@ -57,7 +58,7 @@ type SriovNetworkReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *SriovNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
req.Namespace = namespace
req.Namespace = vars.Namespace
reqLogger := log.FromContext(ctx).WithValues("sriovnetwork", req.NamespacedName)

reqLogger.Info("Reconciling SriovNetwork")
Expand Down
30 changes: 15 additions & 15 deletions controllers/sriovnetworknodepolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"strings"
"time"

utils "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"

errs "github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -52,7 +50,9 @@ import (
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/apply"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
)

const nodePolicySyncEventName = "node-policy-sync-event"
Expand Down Expand Up @@ -86,11 +86,11 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct
reqLogger.Info("Reconciling")

defaultPolicy := &sriovnetworkv1.SriovNetworkNodePolicy{}
err := r.Get(ctx, types.NamespacedName{Name: constants.DefaultPolicyName, Namespace: namespace}, defaultPolicy)
err := r.Get(ctx, types.NamespacedName{Name: constants.DefaultPolicyName, Namespace: vars.Namespace}, defaultPolicy)
if err != nil {
if errors.IsNotFound(err) {
// Default policy object not found, create it.
defaultPolicy.SetNamespace(namespace)
defaultPolicy.SetNamespace(vars.Namespace)
defaultPolicy.SetName(constants.DefaultPolicyName)
defaultPolicy.Spec = sriovnetworkv1.SriovNetworkNodePolicySpec{
NumVfs: 0,
Expand All @@ -99,7 +99,7 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct
}
err = r.Create(ctx, defaultPolicy)
if err != nil {
reqLogger.Error(err, "Failed to create default Policy", "Namespace", namespace, "Name", constants.DefaultPolicyName)
reqLogger.Error(err, "Failed to create default Policy", "Namespace", vars.Namespace, "Name", constants.DefaultPolicyName)
return reconcile.Result{}, err
}
reqLogger.Info("Default policy created")
Expand Down Expand Up @@ -129,7 +129,7 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct
"kubernetes.io/os": "linux",
}
defaultOpConf := &sriovnetworkv1.SriovOperatorConfig{}
if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: constants.DefaultConfigName}, defaultOpConf); err != nil {
if err := r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: constants.DefaultConfigName}, defaultOpConf); err != nil {
return reconcile.Result{}, err
}
if len(defaultOpConf.Spec.ConfigDaemonNodeSelector) > 0 {
Expand Down Expand Up @@ -220,7 +220,7 @@ func (r *SriovNetworkNodePolicyReconciler) syncDevicePluginConfigMap(ctx context
},
ObjectMeta: metav1.ObjectMeta{
Name: constants.ConfigMapName,
Namespace: namespace,
Namespace: vars.Namespace,
},
Data: configData,
}
Expand Down Expand Up @@ -250,14 +250,14 @@ func (r *SriovNetworkNodePolicyReconciler) syncAllSriovNetworkNodeStates(ctx con
logger := log.Log.WithName("syncAllSriovNetworkNodeStates")
logger.V(1).Info("Start to sync all SriovNetworkNodeState custom resource")
found := &corev1.ConfigMap{}
if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: constants.ConfigMapName}, found); err != nil {
if err := r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: constants.ConfigMapName}, found); err != nil {
logger.V(1).Info("Fail to get", "ConfigMap", constants.ConfigMapName)
}
for _, node := range nl.Items {
logger.V(1).Info("Sync SriovNetworkNodeState CR", "name", node.Name)
ns := &sriovnetworkv1.SriovNetworkNodeState{}
ns.Name = node.Name
ns.Namespace = namespace
ns.Namespace = vars.Namespace
j, _ := json.Marshal(ns)
logger.V(2).Info("SriovNetworkNodeState CR", "content", j)
if err := r.syncSriovNetworkNodeState(ctx, np, npl, ns, &node, utils.HashConfigMap(found)); err != nil {
Expand Down Expand Up @@ -368,7 +368,7 @@ func (r *SriovNetworkNodePolicyReconciler) syncPluginDaemonObjs(ctx context.Cont

// render plugin manifests
data := render.MakeRenderData()
data.Data["Namespace"] = namespace
data.Data["Namespace"] = vars.Namespace
data.Data["SRIOVDevicePluginImage"] = os.Getenv("SRIOV_DEVICE_PLUGIN_IMAGE")
data.Data["ReleaseVersion"] = os.Getenv("RELEASEVERSION")
data.Data["ResourcePrefix"] = os.Getenv("RESOURCE_PREFIX")
Expand All @@ -384,7 +384,7 @@ func (r *SriovNetworkNodePolicyReconciler) syncPluginDaemonObjs(ctx context.Cont

defaultConfig := &sriovnetworkv1.SriovOperatorConfig{}
err = r.Get(ctx, types.NamespacedName{
Name: constants.DefaultConfigName, Namespace: namespace}, defaultConfig)
Name: constants.DefaultConfigName, Namespace: vars.Namespace}, defaultConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -436,7 +436,7 @@ func (r *SriovNetworkNodePolicyReconciler) syncPluginDaemonObjs(ctx context.Cont

func (r *SriovNetworkNodePolicyReconciler) deleteSriovCniManifests(ctx context.Context) error {
ds := &appsv1.DaemonSet{}
err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "sriov-cni"}, ds)
err := r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: "sriov-cni"}, ds)
if err != nil {
if !errors.IsNotFound(err) {
return err
Expand All @@ -449,7 +449,7 @@ func (r *SriovNetworkNodePolicyReconciler) deleteSriovCniManifests(ctx context.C
}

rb := &rbacv1.RoleBinding{}
err = r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "sriov-cni"}, rb)
err = r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: "sriov-cni"}, rb)
if err != nil {
if !errors.IsNotFound(err) {
return err
Expand All @@ -462,7 +462,7 @@ func (r *SriovNetworkNodePolicyReconciler) deleteSriovCniManifests(ctx context.C
}

sa := &corev1.ServiceAccount{}
err = r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "sriov-cni"}, sa)
err = r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: "sriov-cni"}, sa)
if err != nil {
if !errors.IsNotFound(err) {
return err
Expand Down Expand Up @@ -629,7 +629,7 @@ func (r *SriovNetworkNodePolicyReconciler) renderDevicePluginConfigData(ctx cont
}

nodeState := &sriovnetworkv1.SriovNetworkNodeState{}
err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: node.Name}, nodeState)
err := r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: node.Name}, nodeState)
if err != nil {
return rcl, err
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/sriovnetworknodepolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
v1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars"
)

func TestNodeSelectorMerge(t *testing.T) {
Expand Down Expand Up @@ -213,7 +214,7 @@ func TestRenderDevicePluginConfigData(t *testing.T) {
reconciler := SriovNetworkNodePolicyReconciler{}

node := corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "node1"}}
nodeState := sriovnetworkv1.SriovNetworkNodeState{ObjectMeta: metav1.ObjectMeta{Name: node.Name, Namespace: namespace}}
nodeState := sriovnetworkv1.SriovNetworkNodeState{ObjectMeta: metav1.ObjectMeta{Name: node.Name, Namespace: vars.Namespace}}

scheme := runtime.NewScheme()
utilruntime.Must(sriovnetworkv1.AddToScheme(scheme))
Expand Down
19 changes: 18 additions & 1 deletion controllers/sriovnetworkpoolconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (
"reflect"

mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -53,11 +56,25 @@ func (r *SriovNetworkPoolConfigReconciler) Reconcile(ctx context.Context, req ct
}
logger.Info("Reconciling")

// // Fetch SriovNetworkPoolConfig
// Fetch SriovNetworkPoolConfig
instance := &sriovnetworkv1.SriovNetworkPoolConfig{}
err := r.Get(ctx, req.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// looks like someone remove the default pool lets recreate it...
if req.Name == constants.DefaultConfigName {
config := &sriovnetworkv1.SriovNetworkPoolConfig{}
config.Namespace = vars.Namespace
config.Name = constants.DefaultConfigName
maxUnv := intstr.Parse("1")
config.Spec = sriovnetworkv1.SriovNetworkPoolConfigSpec{
MaxUnavailable: &maxUnv,
NodeSelector: &metav1.LabelSelector{},
}
err = r.Create(context.TODO(), config)
return reconcile.Result{}, err
}

// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
Expand Down
14 changes: 7 additions & 7 deletions controllers/sriovoperatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
}
defaultConfig := &sriovnetworkv1.SriovOperatorConfig{}
err := r.Get(ctx, types.NamespacedName{
Name: consts.DefaultConfigName, Namespace: namespace}, defaultConfig)
Name: consts.DefaultConfigName, Namespace: vars.Namespace}, defaultConfig)
if err != nil {
if apierrors.IsNotFound(err) {
singleNode, err := utils.IsSingleNodeCluster(r.Client)
Expand All @@ -86,7 +86,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
}

// Default Config object not found, create it.
defaultConfig.SetNamespace(namespace)
defaultConfig.SetNamespace(vars.Namespace)
defaultConfig.SetName(consts.DefaultConfigName)
defaultConfig.Spec = sriovnetworkv1.SriovOperatorConfigSpec{
EnableInjector: func() *bool { b := enableAdmissionController; return &b }(),
Expand All @@ -100,7 +100,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
err = r.Create(ctx, defaultConfig)
if err != nil {
logger.Error(err, "Failed to create default Operator Config", "Namespace",
namespace, "Name", consts.DefaultConfigName)
vars.Namespace, "Name", consts.DefaultConfigName)
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
Expand All @@ -109,7 +109,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
return reconcile.Result{}, err
}

if req.Namespace != namespace {
if req.Namespace != vars.Namespace {
return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -160,7 +160,7 @@ func (r *SriovOperatorConfigReconciler) syncPluginDaemonSet(ctx context.Context,
names := []string{"sriov-cni", "sriov-device-plugin"}

for _, name := range names {
err := r.Client.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, ds)
err := r.Client.Get(ctx, types.NamespacedName{Name: name, Namespace: vars.Namespace}, ds)
if err != nil {
if apierrors.IsNotFound(err) {
continue
Expand Down Expand Up @@ -189,7 +189,7 @@ func (r *SriovOperatorConfigReconciler) syncConfigDaemonSet(ctx context.Context,

data := render.MakeRenderData()
data.Data["Image"] = os.Getenv("SRIOV_NETWORK_CONFIG_DAEMON_IMAGE")
data.Data["Namespace"] = namespace
data.Data["Namespace"] = vars.Namespace
data.Data["SRIOVCNIImage"] = os.Getenv("SRIOV_CNI_IMAGE")
data.Data["SRIOVInfiniBandCNIImage"] = os.Getenv("SRIOV_INFINIBAND_CNI_IMAGE")
data.Data["ReleaseVersion"] = os.Getenv("RELEASEVERSION")
Expand Down Expand Up @@ -247,7 +247,7 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(ctx context.Context, dc
for name, path := range webhooks {
// Render Webhook manifests
data := render.MakeRenderData()
data.Data["Namespace"] = namespace
data.Data["Namespace"] = vars.Namespace
data.Data["SRIOVMutatingWebhookName"] = name
data.Data["NetworkResourcesInjectorImage"] = os.Getenv("NETWORK_RESOURCES_INJECTOR_IMAGE")
data.Data["SriovNetworkWebhookImage"] = os.Getenv("SRIOV_NETWORK_WEBHOOK_IMAGE")
Expand Down
21 changes: 8 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func main() {

le := leaderelection.GetLeaderElectionConfig(kubeClient, enableLeaderElection)

namespace := os.Getenv("NAMESPACE")
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme,
Metrics: server.Options{BindAddress: metricsAddr},
Expand All @@ -111,7 +110,7 @@ func main() {
RenewDeadline: &le.RenewDeadline,
RetryPeriod: &le.RetryPeriod,
LeaderElectionID: "a56def2a.openshift.io",
Cache: cache.Options{DefaultNamespaces: map[string]cache.Config{namespace: {}}},
Cache: cache.Options{DefaultNamespaces: map[string]cache.Config{vars.Namespace: {}}},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -271,9 +270,8 @@ func main() {
}

func initNicIDMap() error {
namespace := os.Getenv("NAMESPACE")
kubeclient := kubernetes.NewForConfigOrDie(ctrl.GetConfigOrDie())
if err := sriovnetworkv1.InitNicIDMapFromConfigMap(kubeclient, namespace); err != nil {
if err := sriovnetworkv1.InitNicIDMapFromConfigMap(kubeclient, vars.Namespace); err != nil {
return err
}

Expand All @@ -289,12 +287,11 @@ func createDefaultPolicy(c client.Client) error {
NicSelector: sriovnetworkv1.SriovNetworkNicSelector{},
},
}
namespace := os.Getenv("NAMESPACE")
err := c.Get(context.TODO(), types.NamespacedName{Name: constants.DefaultPolicyName, Namespace: namespace}, policy)
err := c.Get(context.TODO(), types.NamespacedName{Name: constants.DefaultPolicyName, Namespace: vars.Namespace}, policy)
if err != nil {
if errors.IsNotFound(err) {
logger.Info("Create a default SriovNetworkNodePolicy")
policy.Namespace = namespace
policy.Namespace = vars.Namespace
policy.Name = constants.DefaultPolicyName
err = c.Create(context.TODO(), policy)
if err != nil {
Expand Down Expand Up @@ -324,12 +321,11 @@ func createDefaultOperatorConfig(c client.Client) error {
DisableDrain: singleNode,
},
}
namespace := os.Getenv("NAMESPACE")
err = c.Get(context.TODO(), types.NamespacedName{Name: constants.DefaultConfigName, Namespace: namespace}, config)
err = c.Get(context.TODO(), types.NamespacedName{Name: constants.DefaultConfigName, Namespace: vars.Namespace}, config)
if err != nil {
if errors.IsNotFound(err) {
logger.Info("Create default SriovOperatorConfig")
config.Namespace = namespace
config.Namespace = vars.Namespace
config.Name = constants.DefaultConfigName
err = c.Create(context.TODO(), config)
if err != nil {
Expand All @@ -346,12 +342,11 @@ func createDefaultPoolConfig(c client.Client) error {
logger := setupLog.WithName("createDefaultOperatorConfig")

config := &sriovnetworkv1.SriovNetworkPoolConfig{}
namespace := os.Getenv("NAMESPACE")
err := c.Get(context.TODO(), types.NamespacedName{Name: constants.DefaultConfigName, Namespace: namespace}, config)
err := c.Get(context.TODO(), types.NamespacedName{Name: constants.DefaultConfigName, Namespace: vars.Namespace}, config)
if err != nil {
if errors.IsNotFound(err) {
logger.Info("Create default SriovNetworkPoolConfig")
config.Namespace = namespace
config.Namespace = vars.Namespace
config.Name = constants.DefaultConfigName
maxun := intstr.Parse("1")
config.Spec = sriovnetworkv1.SriovNetworkPoolConfigSpec{
Expand Down
Loading

0 comments on commit aeb73b9

Please sign in to comment.