Skip to content

Commit

Permalink
controller: Add cluster-info net-attach-def resource available param
Browse files Browse the repository at this point in the history
This param will check if net-attach-def is installed on the cluster on
every reconcile run.
This param is added in order be consumed in future commits to deploy
net-attach-def resource.

Signed-off-by: Ram Lavi <[email protected]>
  • Loading branch information
RamLavi committed Aug 5, 2024
1 parent 3613643 commit 00ee34c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 5 additions & 4 deletions pkg/network/cluster-info.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 00ee34c

Please sign in to comment.