Skip to content

Commit

Permalink
update operator and daemon package to use the new drain method
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Sch <sebassch@gmail.com>
  • Loading branch information
SchSeba committed Dec 5, 2023
1 parent d6fb675 commit 1382ccf
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 474 deletions.
8 changes: 8 additions & 0 deletions cmd/sriov-network-config-daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/connrotation"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

configv1 "github.com/openshift/api/config/v1"
Expand Down Expand Up @@ -176,6 +177,12 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
return err
}

kClient, err := client.New(config, client.Options{Scheme: scheme.Scheme})
if err != nil {
setupLog.Error(err, "couldn't create client")
os.Exit(1)
}

snclient := snclientset.NewForConfigOrDie(config)
kubeclient := kubernetes.NewForConfigOrDie(config)

Expand Down Expand Up @@ -236,6 +243,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error {

setupLog.V(0).Info("Starting SriovNetworkConfigDaemon")
err = daemon.New(
kClient,
snclient,
kubeclient,
vendorHelpers,
Expand Down
85 changes: 79 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ import (
"fmt"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
openshiftconfigv1 "github.com/openshift/api/config/v1"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
_ "k8s.io/client-go/plugin/pkg/client/auth"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -51,7 +53,10 @@ import (
"github.com/k8snetworkplumbingwg/sriov-network-operator/controllers"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/leaderelection"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars"
snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
//+kubebuilder:scaffold:imports
)
Expand Down Expand Up @@ -121,13 +126,21 @@ func main() {
os.Exit(1)
}

mgrGlobal.GetCache().IndexField(context.Background(), &sriovnetworkv1.SriovNetwork{}, "spec.networkNamespace", func(o client.Object) []string {
err = mgrGlobal.GetCache().IndexField(context.Background(), &sriovnetworkv1.SriovNetwork{}, "spec.networkNamespace", func(o client.Object) []string {
return []string{o.(*sriovnetworkv1.SriovNetwork).Spec.NetworkNamespace}
})
if err != nil {
setupLog.Error(err, "unable to create index field for cache")
os.Exit(1)
}

mgrGlobal.GetCache().IndexField(context.Background(), &sriovnetworkv1.SriovIBNetwork{}, "spec.networkNamespace", func(o client.Object) []string {
err = mgrGlobal.GetCache().IndexField(context.Background(), &sriovnetworkv1.SriovIBNetwork{}, "spec.networkNamespace", func(o client.Object) []string {
return []string{o.(*sriovnetworkv1.SriovIBNetwork).Spec.NetworkNamespace}
})
if err != nil {
setupLog.Error(err, "unable to create index field for cache")
os.Exit(1)
}

if err := initNicIDMap(); err != nil {
setupLog.Error(err, "unable to init NicIdMap")
Expand Down Expand Up @@ -181,6 +194,33 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "SriovNetworkPoolConfig")
os.Exit(1)
}

// we need a client that doesn't use the local cache for the objects
drainKClient, err := client.New(restConfig, client.Options{
Scheme: scheme,
Cache: &client.CacheOptions{
DisableFor: []client.Object{
&sriovnetworkv1.SriovNetworkNodeState{},
&corev1.Node{},
&mcfgv1.MachineConfigPool{},
},
},
})
if err != nil {
setupLog.Error(err, "unable to create drain kubernetes client")
os.Exit(1)
}

drainController, err := controllers.NewDrainReconcileController(drainKClient, mgr.GetScheme(), platformsHelper)
if err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DrainReconcile")
os.Exit(1)
}

if err = drainController.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup controller with manager", "controller", "DrainReconcile")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

// Create a default SriovNetworkNodePolicy
Expand All @@ -197,6 +237,12 @@ func main() {
os.Exit(1)
}

err = createDefaultPoolConfig(kubeClient)
if err != nil {
setupLog.Error(err, "unable to create default SriovNetworkPoolConfig")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
Expand Down Expand Up @@ -295,3 +341,30 @@ func createDefaultOperatorConfig(c client.Client) error {
}
return nil
}

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)
if err != nil {
if errors.IsNotFound(err) {
logger.Info("Create default SriovNetworkPoolConfig")
config.Namespace = namespace
config.Name = constants.DefaultConfigName
maxun := intstr.Parse("1")
config.Spec = sriovnetworkv1.SriovNetworkPoolConfigSpec{
MaxUnavailable: &maxun,
NodeSelector: &metav1.LabelSelector{},
}
err = c.Create(context.TODO(), config)
if err != nil {
return err
}
}
// Error reading the object - requeue the request.
return err
}
return nil
}
Loading

0 comments on commit 1382ccf

Please sign in to comment.