diff --git a/pkg/controller/networkaddonsconfig/networkaddonsconfig_controller.go b/pkg/controller/networkaddonsconfig/networkaddonsconfig_controller.go index 48f3e54f83..9eb0ea2b48 100644 --- a/pkg/controller/networkaddonsconfig/networkaddonsconfig_controller.go +++ b/pkg/controller/networkaddonsconfig/networkaddonsconfig_controller.go @@ -22,6 +22,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" @@ -517,9 +518,36 @@ func (r *ReconcileNetworkAddonsConfig) updateClusterInfo() error { r.clusterInfo.IsSingleReplica = isSingleReplica } + clientset, err := getClientInterface() + if err != nil { + return fmt.Errorf("failed to get clientset: %v", err) + } + netAttachDefAvailable, err := isNetworkAttachmentDefinitionAvailable(clientset) + if err != nil { + return fmt.Errorf("failed to check whether netAttachDef resource is available: %v", err) + } + if r.clusterInfo.NetAttachDefAvailable != netAttachDefAvailable { + log.Printf("NetworkAttachmentDefinition Availability changed to: %v", netAttachDefAvailable) + } + r.clusterInfo.NetAttachDefAvailable = netAttachDefAvailable + return nil } +func getClientInterface() (kubernetes.Interface, error) { + cfg, err := rest.InClusterConfig() + if err != nil { + return nil, fmt.Errorf("unable to create in-cluster config: %v", err) + } + + clientset, err := kubernetes.NewForConfig(cfg) + if err != nil { + return nil, fmt.Errorf("unable to create a client: %v", err) + } + + return clientset, nil +} + func updateObjectsLabels(crLabels map[string]string, objs []*unstructured.Unstructured) error { var err error for _, obj := range objs { @@ -624,6 +652,10 @@ func isSCCAvailable(c kubernetes.Interface) (bool, error) { return isResourceAvailable(c, "securitycontextconstraints", "security.openshift.io", "v1") } +func isNetworkAttachmentDefinitionAvailable(c kubernetes.Interface) (bool, error) { + return isResourceAvailable(c, "network-attachment-definitions", "k8s.cni.cncf.io", "v1") +} + // isMonitoringAvailable checks if we can deploy the monitoring component func IsMonitoringAvailable(c kubernetes.Interface) (bool, error) { prometheusRuleResourceAvailable, err := isResourceAvailable(c, "customresourcedefinitions/prometheusrules.monitoring.coreos.com", "apiextensions.k8s.io", "v1") diff --git a/pkg/network/cluster-info.go b/pkg/network/cluster-info.go index b90b83c6af..6736e976d9 100644 --- a/pkg/network/cluster-info.go +++ b/pkg/network/cluster-info.go @@ -1,8 +1,9 @@ package network type ClusterInfo struct { - SCCAvailable bool - OpenShift4 bool - MonitoringAvailable bool - IsSingleReplica bool + SCCAvailable bool + OpenShift4 bool + MonitoringAvailable bool + IsSingleReplica bool + NetAttachDefAvailable bool }