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.
  • Loading branch information
courtneypacheco committed Aug 30, 2021
1 parent 568b364 commit d97cfd2
Show file tree
Hide file tree
Showing 4 changed files with 758 additions and 4 deletions.
134 changes: 131 additions & 3 deletions controllers/nodefeaturediscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,144 @@ 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 SecurityContextConstraints
rstatus, err := r.getSecurityContextConstraintsConditions(ctx)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDScc, err)
}

// Check the status of the NFD Operator Worker ServiceAccount
rstatus, err = r.getWorkerServiceAccountConditions(ctx)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDWorkerServiceAccount, err)
}

// Check the status of the NFD Operator Master ServiceAccount
rstatus, err = r.getMasterServiceAccountConditions(ctx)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDMasterServiceAccount, err)
}

// Check the status of the NFD Operator role
rstatus, err = r.getRoleConditions(ctx)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionNFDRoleDegraded, err)
}

// Check the status of the NFD Operator cluster role
rstatus, err = r.getClusterRoleConditions(ctx)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionNFDClusterRoleDegraded, err)
}

// Check the status of the NFD Operator cluster role binding
rstatus, err = r.getClusterRoleBindingConditions(ctx)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionNFDClusterRoleBindingDegraded, err)
}

// Check the status of the NFD Operator role binding
rstatus, err = r.getRoleBindingConditions(ctx)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDRoleBinding, err)
}

// Check the status of the NFD Operator Service
rstatus, err = r.getServiceConditions(ctx)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDService, err)
}

// Check the status of the NFD Operator worker ConfigMap
rstatus, err = r.getWorkerConfigConditions(nfd)
if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDWorkerConfig, err)
}

// Check the status of the NFD Operator Worker DaemonSet
rstatus, err = r.getWorkerDaemonSetConditions(ctx)
if rstatus.isProgressing {
return r.updateProgressingCondition(instance, err.Error(), err)
} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDWorkerDaemonSet, err)
}

// Check the status of the NFD Operator Master DaemonSet
rstatus, err = r.getMasterDaemonSetConditions(ctx)
if rstatus.isProgressing {
return r.updateProgressingCondition(instance, err.Error(), err)

} else if rstatus.isDegraded {
return r.updateDegradedCondition(instance, err.Error(), err)

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDMasterDaemonSet, err)
}

// 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, nil
}
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) {

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
}
3 changes: 2 additions & 1 deletion controllers/nodefeaturediscovery_controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ func DaemonSet(n NFD) (ResourceStatus, error) {
if obj.ObjectMeta.Name == "nfd-master" {
var args []string
port := defaultServicePort

// update ports
if n.ins.Spec.Operand.ServicePort != 0 {
port = n.ins.Spec.Operand.ServicePort
}
Expand Down Expand Up @@ -378,7 +380,6 @@ func Service(n NFD) (ResourceStatus, error) {
state := n.idx
obj := n.resources[state].Service

// update ports
if n.ins.Spec.Operand.ServicePort != 0 {
obj.Spec.Ports[0].Port = int32(n.ins.Spec.Operand.ServicePort)
obj.Spec.Ports[0].TargetPort = intstr.FromInt(n.ins.Spec.Operand.ServicePort)
Expand Down
58 changes: 58 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 @@ -30,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type assetsFromFile []byte
Expand Down Expand Up @@ -158,3 +160,59 @@ 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
}

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

0 comments on commit d97cfd2

Please sign in to comment.