Skip to content

Commit

Permalink
Add logic to set the operator's status in the NFD CR
Browse files Browse the repository at this point in the history
Add logic that sets the operator's status in the NFD CR based on
whether one or more of NFD's resources is: degraded, progressing,
upgradeable, or available. Also add a "applyComponents" function
to simplify readability.

Signed-off-by: Carlos Eduardo Arango Gutierrez <carangog@redhat.com>
Co-Authored-by: Courtney Pacheco <cpacheco@redhat.com>
  • Loading branch information
ArangoGutierrez and courtneypacheco committed Oct 1, 2021
1 parent 86a205c commit 6f12e13
Show file tree
Hide file tree
Showing 7 changed files with 681 additions and 49 deletions.
3 changes: 1 addition & 2 deletions api/v1/nodefeaturediscovery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1

import (
conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -92,7 +91,7 @@ type ConfigMap struct {
type NodeFeatureDiscoveryStatus struct {
// Conditions represents the latest available observations of current state.
// +optional
Conditions []conditionsv1.Condition `json:"conditions,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
5 changes: 3 additions & 2 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 101 additions & 3 deletions controllers/nodefeaturediscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,115 @@ func (r *NodeFeatureDiscoveryReconciler) Reconcile(ctx context.Context, req ctrl

r.Log.Info("Ready to apply components")
nfd.init(r, instance)
result, err := applyComponents()
if err != nil {
return ctrl.Result{Requeue: true}, err
}

// Check the status of the NFD Operator Worker ServiceAccount
if rstatus, err := r.getWorkerServiceAccountConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDWorkerServiceAccount, err.Error())
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, conditionNFDWorkerServiceAccountDegraded, "nfd-worker service account has been degraded")
}

// Check the status of the NFD Operator Master ServiceAccount
if rstatus, err := r.getMasterServiceAccountConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDMasterServiceAccount, err.Error())
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, conditionNFDMasterServiceAccountDegraded, "nfd-master service account has been degraded")
}

// Check the status of the NFD Operator role
if rstatus, err := r.getRoleConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionNFDRoleDegraded, err.Error())
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, conditionNFDRoleDegraded, "nfd-worker role has been degraded")
}

// Check the status of the NFD Operator cluster role
if rstatus, err := r.getClusterRoleConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionNFDClusterRoleDegraded, err.Error())
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, conditionNFDClusterRoleDegraded, "nfd ClusterRole has been degraded")
}

// Check the status of the NFD Operator cluster role binding
if rstatus, err := r.getClusterRoleBindingConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDClusterRoleBinding, err.Error())
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, conditionNFDClusterRoleBindingDegraded, "nfd ClusterRole has been degraded")
}

// Check the status of the NFD Operator role binding
if rstatus, err := r.getRoleBindingConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDRoleBinding, err.Error())
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, conditionNFDRoleBindingDegraded, "nfd ClusterRoleBinding has been degraded")
}

// Check the status of the NFD Operator Service
if rstatus, err := r.getServiceConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDService, err.Error())
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, conditionNFDServiceDegraded, "nfd Service has been degraded")
}

// Check the status of the NFD Operator worker ConfigMap
if rstatus, err := r.getWorkerConfigConditions(nfd); err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDWorkerConfig, err.Error())
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, conditionNFDWorkerConfigDegraded, "nfd-worker ConfigMap has been degraded")
}

// Check the status of the NFD Operator Worker DaemonSet
if rstatus, err := r.getWorkerDaemonSetConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDWorkerDaemonSet, err.Error())
} else if rstatus.isProgressing {
return r.updateProgressingCondition(instance, err.Error(), "nfd-worker Daemonset is progressing")
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), "nfd-worker Daemonset has been degraded")
}

// Check the status of the NFD Operator Master DaemonSet
if rstatus, err := r.getMasterDaemonSetConditions(ctx); err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDMasterDaemonSet, err.Error())
} else if rstatus.isProgressing {
return r.updateProgressingCondition(instance, err.Error(), "nfd-master Daemonset is progressing")
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), "nfd-master Daemonset has been degraded")
}

// Get available conditions
conditions := r.getAvailableConditions()

// Update the status of the resource on the CRD
if err := r.updateStatus(instance, conditions); err != nil {
if result != nil {
return *result, err
}
return reconcile.Result{}, err
}

if result != nil {
return *result, nil
}

// All objects are healthy during reconcile loop
return ctrl.Result{}, nil
}

func applyComponents() (*reconcile.Result, error) {

// Run through all control functions, return an error on any NotReady resource.
for {
err := nfd.step()
if err != nil {
return reconcile.Result{}, err
return &reconcile.Result{}, err
}
if nfd.last() {
break
}
}

return ctrl.Result{}, nil
return &ctrl.Result{}, nil
}
51 changes: 51 additions & 0 deletions controllers/nodefeaturediscovery_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -28,6 +29,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// assetsFromFile is the content of an asset file as raw data
Expand Down Expand Up @@ -162,3 +164,52 @@ func panicIfError(err error) {
panic(err)
}
}

// getServiceAccount gets one of the NFD Operator's ServiceAccounts
func (r *NodeFeatureDiscoveryReconciler) getServiceAccount(ctx context.Context, namespace string, name string) (*corev1.ServiceAccount, error) {
sa := &corev1.ServiceAccount{}
err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, sa)
return sa, err
}

// getDaemonSet gets one of the NFD Operator's DaemonSets
func (r *NodeFeatureDiscoveryReconciler) getDaemonSet(ctx context.Context, namespace string, name string) (*appsv1.DaemonSet, error) {
ds := &appsv1.DaemonSet{}
err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, ds)
return ds, err
}

// getService gets one of the NFD Operator's Services
func (r *NodeFeatureDiscoveryReconciler) getService(ctx context.Context, namespace string, name string) (*corev1.Service, error) {
svc := &corev1.Service{}
err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, svc)
return svc, err
}

// getRole gets one of the NFD Operator's Roles
func (r *NodeFeatureDiscoveryReconciler) getRole(ctx context.Context, namespace string, name string) (*rbacv1.Role, error) {
role := &rbacv1.Role{}
err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, role)
return role, err
}

// getRoleBinding gets one of the NFD Operator's RoleBindings
func (r *NodeFeatureDiscoveryReconciler) getRoleBinding(ctx context.Context, namespace string, name string) (*rbacv1.RoleBinding, error) {
rb := &rbacv1.RoleBinding{}
err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, rb)
return rb, err
}

// getClusterRole gets one of the NFD Operator's ClusterRoles
func (r *NodeFeatureDiscoveryReconciler) getClusterRole(ctx context.Context, namespace string, name string) (*rbacv1.ClusterRole, error) {
cr := &rbacv1.ClusterRole{}
err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, cr)
return cr, err
}

// getClusterRoleBinding gets one of the NFD Operator's ClusterRoleBindings
func (r *NodeFeatureDiscoveryReconciler) getClusterRoleBinding(ctx context.Context, namespace string, name string) (*rbacv1.ClusterRoleBinding, error) {
crb := &rbacv1.ClusterRoleBinding{}
err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, crb)
return crb, err
}
Loading

0 comments on commit 6f12e13

Please sign in to comment.