Skip to content

Commit

Permalink
Merge pull request #561 from pliurh/sync
Browse files Browse the repository at this point in the history
Bug 1999079: Sync upstream: 2021-9-8
  • Loading branch information
openshift-merge-robot authored Sep 8, 2021
2 parents 97f9a66 + 9749770 commit fc93e4d
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 77 deletions.
13 changes: 8 additions & 5 deletions api/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,15 @@ func StringInArray(val string, array []string) bool {
return false
}

func RemoveString(s string, slice []string) (result []string) {
for _, item := range slice {
if item == s {
continue
func RemoveString(s string, slice []string) (result []string, found bool) {
if len(slice) != 0 {
for _, item := range slice {
if item == s {
found = true
continue
}
result = append(result, item)
}
result = append(result, item)
}
return
}
Expand Down
26 changes: 3 additions & 23 deletions controllers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,13 @@ package controllers

import (
"os"
"time"
)

const (
ResyncPeriod = 5 * time.Minute
DEFAULT_CONFIG_NAME = "default"
CONFIG_DAEMON_PATH = "./bindata/manifests/daemon"
INJECTOR_WEBHOOK_PATH = "./bindata/manifests/webhook"
OPERATOR_WEBHOOK_PATH = "./bindata/manifests/operator-webhook"
SERVICE_CA_CONFIGMAP_ANNOTATION = "service.beta.openshift.io/inject-cabundle"
INJECTOR_WEBHOOK_NAME = "network-resources-injector-config"
OPERATOR_WEBHOOK_NAME = "sriov-operator-webhook-config"
DEPRECATED_OPERATOR_WEBHOOK_NAME = "operator-webhook-config"
PLUGIN_PATH = "./bindata/manifests/plugins"
DAEMON_PATH = "./bindata/manifests/daemon"
DEFAULT_POLICY_NAME = "default"
CONFIGMAP_NAME = "device-plugin-config"
DP_CONFIG_FILENAME = "config.json"
OVS_HWOL_MACHINE_CONFIG_NAME_SUFFIX = "ovs-hw-offload"

linkTypeEthernet = "ether"
linkTypeInfiniband = "infiniband"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
)

var webhooks = map[string](string){
INJECTOR_WEBHOOK_NAME: INJECTOR_WEBHOOK_PATH,
OPERATOR_WEBHOOK_NAME: OPERATOR_WEBHOOK_PATH,
constants.INJECTOR_WEBHOOK_NAME: constants.INJECTOR_WEBHOOK_PATH,
constants.OPERATOR_WEBHOOK_NAME: constants.OPERATOR_WEBHOOK_PATH,
}

var namespace = os.Getenv("NAMESPACE")
9 changes: 6 additions & 3 deletions controllers/sriovibnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ func (r *SriovIBNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return reconcile.Result{}, err
}
// remove our finalizer from the list and update it.
instance.ObjectMeta.Finalizers = sriovnetworkv1.RemoveString(sriovnetworkv1.NETATTDEFFINALIZERNAME, instance.ObjectMeta.Finalizers)
if err := r.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
var found bool
instance.ObjectMeta.Finalizers, found = sriovnetworkv1.RemoveString(sriovnetworkv1.NETATTDEFFINALIZERNAME, instance.ObjectMeta.Finalizers)
if found {
if err := r.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
}
}
}
return reconcile.Result{}, err
Expand Down
9 changes: 6 additions & 3 deletions controllers/sriovnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ func (r *SriovNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request
return reconcile.Result{}, err
}
// remove our finalizer from the list and update it.
instance.ObjectMeta.Finalizers = sriovnetworkv1.RemoveString(sriovnetworkv1.NETATTDEFFINALIZERNAME, instance.ObjectMeta.Finalizers)
if err := r.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
var found bool
instance.ObjectMeta.Finalizers, found = sriovnetworkv1.RemoveString(sriovnetworkv1.NETATTDEFFINALIZERNAME, instance.ObjectMeta.Finalizers)
if found {
if err := r.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
}
}
}
return reconcile.Result{}, err
Expand Down
29 changes: 15 additions & 14 deletions controllers/sriovnetworknodepolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/apply"
render "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
)

// SriovNetworkNodePolicyReconciler reconciles a SriovNetworkNodePolicy object
Expand Down Expand Up @@ -72,20 +73,20 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct
reqLogger.Info("Reconciling")

defaultPolicy := &sriovnetworkv1.SriovNetworkNodePolicy{}
err := r.Get(context.TODO(), types.NamespacedName{Name: DEFAULT_POLICY_NAME, Namespace: namespace}, defaultPolicy)
err := r.Get(context.TODO(), types.NamespacedName{Name: constants.DEFAULT_POLICY_NAME, Namespace: namespace}, defaultPolicy)
if err != nil {
if errors.IsNotFound(err) {
// Default policy object not found, create it.
defaultPolicy.SetNamespace(namespace)
defaultPolicy.SetName(DEFAULT_POLICY_NAME)
defaultPolicy.SetName(constants.DEFAULT_POLICY_NAME)
defaultPolicy.Spec = sriovnetworkv1.SriovNetworkNodePolicySpec{
NumVfs: 0,
NodeSelector: make(map[string]string),
NicSelector: sriovnetworkv1.SriovNetworkNicSelector{},
}
err = r.Create(context.TODO(), defaultPolicy)
if err != nil {
reqLogger.Error(err, "Failed to create default Policy", "Namespace", namespace, "Name", DEFAULT_POLICY_NAME)
reqLogger.Error(err, "Failed to create default Policy", "Namespace", namespace, "Name", constants.DEFAULT_POLICY_NAME)
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
Expand All @@ -111,7 +112,7 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct
nodeList := &corev1.NodeList{}
lo := &client.MatchingLabels{}
defaultOpConf := &sriovnetworkv1.SriovOperatorConfig{}
if err := r.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: DEFAULT_CONFIG_NAME}, defaultOpConf); err != nil {
if err := r.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: constants.DEFAULT_CONFIG_NAME}, defaultOpConf); err != nil {
return reconcile.Result{}, err
}
if len(defaultOpConf.Spec.ConfigDaemonNodeSelector) > 0 {
Expand Down Expand Up @@ -148,7 +149,7 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct

// All was successful. Request that this be re-triggered after ResyncPeriod,
// so we can reconcile state again.
return reconcile.Result{RequeueAfter: ResyncPeriod}, nil
return reconcile.Result{RequeueAfter: constants.ResyncPeriod}, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -181,7 +182,7 @@ func (r *SriovNetworkNodePolicyReconciler) syncDevicePluginConfigMap(pl *sriovne
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: CONFIGMAP_NAME,
Name: constants.CONFIGMAP_NAME,
Namespace: namespace,
},
Data: configData,
Expand Down Expand Up @@ -212,8 +213,8 @@ func (r *SriovNetworkNodePolicyReconciler) syncAllSriovNetworkNodeStates(np *sri
logger := log.Log.WithName("syncAllSriovNetworkNodeStates")
logger.Info("Start to sync all SriovNetworkNodeState custom resource")
found := &corev1.ConfigMap{}
if err := r.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: CONFIGMAP_NAME}, found); err != nil {
logger.Info("Fail to get", "ConfigMap", CONFIGMAP_NAME)
if err := r.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: constants.CONFIGMAP_NAME}, found); err != nil {
logger.Info("Fail to get", "ConfigMap", constants.CONFIGMAP_NAME)
}
for _, node := range nl.Items {
logger.Info("Sync SriovNetworkNodeState CR", "name", node.Name)
Expand Down Expand Up @@ -327,15 +328,15 @@ func (r *SriovNetworkNodePolicyReconciler) syncPluginDaemonObjs(dp *sriovnetwork
data.Data["ReleaseVersion"] = os.Getenv("RELEASEVERSION")
data.Data["ResourcePrefix"] = os.Getenv("RESOURCE_PREFIX")

objs, err := renderDsForCR(PLUGIN_PATH, &data)
objs, err := renderDsForCR(constants.PLUGIN_PATH, &data)
if err != nil {
logger.Error(err, "Fail to render SR-IoV manifests")
return err
}

defaultConfig := &sriovnetworkv1.SriovOperatorConfig{}
err = r.Get(context.TODO(), types.NamespacedName{
Name: DEFAULT_CONFIG_NAME, Namespace: namespace}, defaultConfig)
Name: constants.DEFAULT_CONFIG_NAME, Namespace: namespace}, defaultConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -610,9 +611,9 @@ func (r *SriovNetworkNodePolicyReconciler) renderDevicePluginConfigData(pl *srio
// vfio-pci device link type is not detectable
if p.Spec.DeviceType != "vfio-pci" {
if p.Spec.LinkType != "" {
linkType := linkTypeEthernet
linkType := constants.LinkTypeEthernet
if strings.ToLower(p.Spec.LinkType) == "ib" {
linkType = linkTypeInfiniband
linkType = constants.LinkTypeInfiniband
}
if !sriovnetworkv1.StringInArray(linkType, netDeviceSelectors.LinkTypes) {
netDeviceSelectors.LinkTypes = sriovnetworkv1.UniqueAppend(netDeviceSelectors.LinkTypes, linkType)
Expand Down Expand Up @@ -675,9 +676,9 @@ func (r *SriovNetworkNodePolicyReconciler) renderDevicePluginConfigData(pl *srio
// vfio-pci device link type is not detectable
if p.Spec.DeviceType != "vfio-pci" {
if p.Spec.LinkType != "" {
linkType := linkTypeEthernet
linkType := constants.LinkTypeEthernet
if strings.ToLower(p.Spec.LinkType) == "ib" {
linkType = linkTypeInfiniband
linkType = constants.LinkTypeInfiniband
}
netDeviceSelectors.LinkTypes = sriovnetworkv1.UniqueAppend(netDeviceSelectors.LinkTypes, linkType)
}
Expand Down
14 changes: 9 additions & 5 deletions controllers/sriovnetworkpoolconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
render "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
)

Expand Down Expand Up @@ -86,15 +87,18 @@ func (r *SriovNetworkPoolConfigReconciler) Reconcile(ctx context.Context, req ct
}
}
// remove our finalizer from the list and update it.
instance.ObjectMeta.Finalizers = sriovnetworkv1.RemoveString(sriovnetworkv1.POOLCONFIGFINALIZERNAME, instance.ObjectMeta.Finalizers)
if err := r.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
var found bool
instance.ObjectMeta.Finalizers, found = sriovnetworkv1.RemoveString(sriovnetworkv1.POOLCONFIGFINALIZERNAME, instance.ObjectMeta.Finalizers)
if found {
if err := r.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
}
}
}
return reconcile.Result{}, err
}

return reconcile.Result{RequeueAfter: ResyncPeriod}, nil
return reconcile.Result{RequeueAfter: constants.ResyncPeriod}, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand All @@ -108,7 +112,7 @@ func (r *SriovNetworkPoolConfigReconciler) syncOvsHardwareOffloadMachineConfigs(
logger := log.Log.WithName("syncOvsHardwareOffloadMachineConfigs")

mcpName := nc.Spec.OvsHardwareOffloadConfig.Name
mcName := "00-" + mcpName + "-" + OVS_HWOL_MACHINE_CONFIG_NAME_SUFFIX
mcName := "00-" + mcpName + "-" + constants.OVS_HWOL_MACHINE_CONFIG_NAME_SUFFIX

foundMC := &mcfgv1.MachineConfig{}
mcp := &mcfgv1.MachineConfigPool{}
Expand Down
3 changes: 2 additions & 1 deletion controllers/sriovnetworkpoolconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
. "github.com/onsi/gomega"

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
)

Expand All @@ -22,7 +23,7 @@ var _ = Describe("Operator", func() {
config.SetName("ovs-hw-offload-config")
mcpName := "worker"
mc := &mcfgv1.MachineConfig{}
mcName := "00-" + mcpName + "-" + OVS_HWOL_MACHINE_CONFIG_NAME_SUFFIX
mcName := "00-" + mcpName + "-" + constants.OVS_HWOL_MACHINE_CONFIG_NAME_SUFFIX
err := k8sClient.Get(goctx.TODO(), types.NamespacedName{Name: mcName, Namespace: testNamespace}, mc)
Expect(errors.IsNotFound(err)).Should(BeTrue())

Expand Down
15 changes: 8 additions & 7 deletions controllers/sriovoperatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
apply "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/apply"
render "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
)

// SriovOperatorConfigReconciler reconciles a SriovOperatorConfig object
Expand Down Expand Up @@ -74,12 +75,12 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
}
defaultConfig := &sriovnetworkv1.SriovOperatorConfig{}
err := r.Get(context.TODO(), types.NamespacedName{
Name: DEFAULT_CONFIG_NAME, Namespace: namespace}, defaultConfig)
Name: constants.DEFAULT_CONFIG_NAME, Namespace: namespace}, defaultConfig)
if err != nil {
if errors.IsNotFound(err) {
// Default Config object not found, create it.
defaultConfig.SetNamespace(namespace)
defaultConfig.SetName(DEFAULT_CONFIG_NAME)
defaultConfig.SetName(constants.DEFAULT_CONFIG_NAME)
defaultConfig.Spec = sriovnetworkv1.SriovOperatorConfigSpec{
EnableInjector: func() *bool { b := enableAdmissionController; return &b }(),
EnableOperatorWebhook: func() *bool { b := enableAdmissionController; return &b }(),
Expand All @@ -89,7 +90,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
err = r.Create(context.TODO(), defaultConfig)
if err != nil {
logger.Error(err, "Failed to create default Operator Config", "Namespace",
namespace, "Name", DEFAULT_CONFIG_NAME)
namespace, "Name", constants.DEFAULT_CONFIG_NAME)
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
Expand All @@ -116,7 +117,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.
return reconcile.Result{}, err
}

return reconcile.Result{RequeueAfter: ResyncPeriod}, nil
return reconcile.Result{RequeueAfter: constants.ResyncPeriod}, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -178,7 +179,7 @@ func (r *SriovOperatorConfigReconciler) syncConfigDaemonSet(dc *sriovnetworkv1.S
logger.Info("New cni bin found", "CNIBinPath", envCniBinPath)
data.Data["CNIBinPath"] = envCniBinPath
}
objs, err := render.RenderDir(CONFIG_DAEMON_PATH, &data)
objs, err := render.RenderDir(constants.CONFIG_DAEMON_PATH, &data)
if err != nil {
logger.Error(err, "Fail to render config daemon manifests")
return err
Expand Down Expand Up @@ -230,7 +231,7 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(dc *sriovnetworkv1.Sriov
}

// Delete injector webhook
if *dc.Spec.EnableInjector != true && path == INJECTOR_WEBHOOK_PATH {
if *dc.Spec.EnableInjector != true && path == constants.INJECTOR_WEBHOOK_PATH {
for _, obj := range objs {
err = r.deleteWebhookObject(obj)
if err != nil {
Expand All @@ -243,7 +244,7 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(dc *sriovnetworkv1.Sriov
continue
}
// Delete operator webhook
if *dc.Spec.EnableOperatorWebhook != true && path == OPERATOR_WEBHOOK_PATH {
if *dc.Spec.EnableOperatorWebhook != true && path == constants.OPERATOR_WEBHOOK_PATH {
for _, obj := range objs {
err = r.deleteWebhookObject(obj)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -151,7 +152,7 @@ var _ = BeforeSuite(func(done Done) {

config := &sriovnetworkv1.SriovOperatorConfig{}
config.SetNamespace(testNamespace)
config.SetName(DEFAULT_CONFIG_NAME)
config.SetName(constants.DEFAULT_CONFIG_NAME)
config.Spec = sriovnetworkv1.SriovOperatorConfigSpec{
EnableInjector: func() *bool { b := true; return &b }(),
EnableOperatorWebhook: func() *bool { b := true; return &b }(),
Expand All @@ -162,7 +163,7 @@ var _ = BeforeSuite(func(done Done) {

poolConfig := &sriovnetworkv1.SriovNetworkPoolConfig{}
poolConfig.SetNamespace(testNamespace)
poolConfig.SetName(DEFAULT_CONFIG_NAME)
poolConfig.SetName(constants.DEFAULT_CONFIG_NAME)
poolConfig.Spec = sriovnetworkv1.SriovNetworkPoolConfigSpec{}
Expect(k8sClient.Create(context.TODO(), poolConfig)).Should(Succeed())
close(done)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func main() {
}()

// Remove all finalizers after controller is shut down
defer utils.Shutdown(mgr.GetClient())
defer utils.Shutdown()

setupLog.Info("starting manager")
if err := mgr.Start(stopCh); err != nil {
Expand Down
Loading

0 comments on commit fc93e4d

Please sign in to comment.