Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to set the operator's status in the NFD CR #84

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unneeded empty line. Ditto X times below

} else if err != nil {
return r.updateDegradedCondition(instance, conditionFailedGettingNFDScc, err)
}
Comment on lines +153 to +155
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always first check for error, then other conditions. Ditto X times below

Also, you could streamline a bit. Suggestion:

if rstatus, err := r.getSecurityContextConstraintsConditions(ctx); err != nil {
...
} else if rstatus.isDegraded {
...
}


// 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
}
Comment on lines +257 to +259
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks suspicious, masking the error 😦

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
Comment on lines +317 to +318
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unrelated

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