Skip to content

Update to be able to track both DaemonSets and Deployments #102

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

Merged
Merged
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
48 changes: 0 additions & 48 deletions pkg/controller/networkconfig/daemonset_controller.go

This file was deleted.

25 changes: 19 additions & 6 deletions pkg/controller/networkconfig/networkconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newReconciler(mgr manager.Manager, status *clusteroperator.StatusManager) *
scheme: mgr.GetScheme(),
status: status,

daemonSetReconciler: newDaemonSetReconciler(status),
podReconciler: newPodReconciler(status),
}
}

Expand All @@ -71,15 +71,19 @@ func add(mgr manager.Manager, r *ReconcileNetworkConfig) error {
return err
}

// Likewise for the DaemonSet reconciler
c, err = controller.New("daemonset-controller", mgr, controller.Options{Reconciler: r.daemonSetReconciler})
// Likewise for the Pod reconciler
c, err = controller.New("pod-controller", mgr, controller.Options{Reconciler: r.podReconciler})
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &appsv1.DaemonSet{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}

return nil
}
Expand All @@ -94,7 +98,7 @@ type ReconcileNetworkConfig struct {
scheme *runtime.Scheme
status *clusteroperator.StatusManager

daemonSetReconciler *ReconcileDaemonSets
podReconciler *ReconcilePods
}

// Reconcile updates the state of the cluster to match that which is desired
Expand Down Expand Up @@ -185,15 +189,24 @@ func (r *ReconcileNetworkConfig) Reconcile(request reconcile.Request) (reconcile
}
objs = append([]*uns.Unstructured{app}, objs...)

// Set up the DaemonSet reconciler before we start creating the DaemonSets
// Set up the Pod reconciler before we start creating DaemonSets/Deployments
r.status.SetConfigSuccess()
daemonSets := []types.NamespacedName{}
deployments := []types.NamespacedName{}
for _, obj := range objs {
if obj.GetAPIVersion() == "apps/v1" && obj.GetKind() == "DaemonSet" {
daemonSets = append(daemonSets, types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()})
} else if obj.GetAPIVersion() == "apps/v1" && obj.GetKind() == "Deployment" {
deployments = append(deployments, types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()})
}
}
r.daemonSetReconciler.SetDaemonSets(daemonSets)
r.status.SetDaemonSets(daemonSets)
r.status.SetDeployments(deployments)

allResources := []types.NamespacedName{}
Copy link
Contributor

Choose a reason for hiding this comment

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

I realize this is a bit pathological, but you could totally have a NamespacedName collision between daemonsets and deployments.

Copy link
Contributor

Choose a reason for hiding this comment

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

Especially if you moved from one Kind to the other.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, now I see why that doesn't really matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I started working on a controller-runtime PR to make it include a GroupVersionKind in reconcile.Request. But as you figured out, it doesn't actually matter here.

allResources = append(allResources, daemonSets...)
allResources = append(allResources, deployments...)
r.podReconciler.SetResources(allResources)

// Apply the objects to the cluster
for _, obj := range objs {
Expand Down
48 changes: 48 additions & 0 deletions pkg/controller/networkconfig/pod_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package networkconfig

import (
"log"

"github.com/openshift/cluster-network-operator/pkg/util/clusteroperator"

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

// newPodReconciler returns a new reconcile.Reconciler
func newPodReconciler(status *clusteroperator.StatusManager) *ReconcilePods {
return &ReconcilePods{status: status}
}

var _ reconcile.Reconciler = &ReconcilePods{}

// ReconcilePods watches for updates to specified resources and then updates its StatusManager
type ReconcilePods struct {
status *clusteroperator.StatusManager

resources []types.NamespacedName
}

func (r *ReconcilePods) SetResources(resources []types.NamespacedName) {
r.resources = resources
}

// Reconcile updates the ClusterOperator.Status to match the current state of the
// watched Deployments/DaemonSets
func (r *ReconcilePods) Reconcile(request reconcile.Request) (reconcile.Result, error) {
found := false
for _, name := range r.resources {
if name.Namespace == request.Namespace && name.Name == request.Name {
found = true
break
}
}
if !found {
return reconcile.Result{}, nil
}

log.Printf("Reconciling update to %s/%s\n", request.Namespace, request.Name)
r.status.SetFromPods()

return reconcile.Result{RequeueAfter: ResyncPeriod}, nil
}
47 changes: 42 additions & 5 deletions pkg/util/clusteroperator/status_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type StatusManager struct {
version string

configFailure bool

daemonSets []types.NamespacedName
deployments []types.NamespacedName
}

func NewStatusManager(client client.Client, name, version string) *StatusManager {
Expand Down Expand Up @@ -114,17 +117,25 @@ func (status *StatusManager) SetFailing(reason string, err error) error {
)
}

// SetFromDaemonSets sets the operator status to Failing, Progressing, or Available, based
// on the current status of the indicated DaemonSets. However, this is a no-op if the
// StatusManager is currently marked as failing due to a configuration error.
func (status *StatusManager) SetFromDaemonSets(daemonSets []types.NamespacedName) error {
func (status *StatusManager) SetDaemonSets(daemonSets []types.NamespacedName) {
status.daemonSets = daemonSets
}

func (status *StatusManager) SetDeployments(deployments []types.NamespacedName) {
status.deployments = deployments
}

// SetFromPods sets the operator status to Failing, Progressing, or Available, based on
// the current status of the manager's DaemonSets and Deployments. However, this is a
// no-op if the StatusManager is currently marked as failing due to a configuration error.
func (status *StatusManager) SetFromPods() error {
if status.configFailure {
return nil
}

progressing := []string{}

for _, dsName := range daemonSets {
for _, dsName := range status.daemonSets {
ns := &corev1.Namespace{}
if err := status.client.Get(context.TODO(), types.NamespacedName{Name: dsName.Namespace}, ns); err != nil {
if errors.IsNotFound(err) {
Expand All @@ -150,6 +161,32 @@ func (status *StatusManager) SetFromDaemonSets(daemonSets []types.NamespacedName
}
}

for _, depName := range status.deployments {
ns := &corev1.Namespace{}
if err := status.client.Get(context.TODO(), types.NamespacedName{Name: depName.Namespace}, ns); err != nil {
if errors.IsNotFound(err) {
return status.SetFailing("NoNamespace", fmt.Errorf("Namespace %q does not exist", depName.Namespace))
} else {
return status.SetFailing("InternalError", err)
}
}

dep := &appsv1.Deployment{}
if err := status.client.Get(context.TODO(), depName, dep); err != nil {
if errors.IsNotFound(err) {
return status.SetFailing("NoDeployment", fmt.Errorf("Deployment %q does not exist", depName.String()))
} else {
return status.SetFailing("InternalError", err)
}
}

if dep.Status.UnavailableReplicas > 0 {
progressing = append(progressing, fmt.Sprintf("Deployment %q is not available (awaiting %d nodes)", depName.String(), dep.Status.UnavailableReplicas))
} else if dep.Status.AvailableReplicas == 0 {
progressing = append(progressing, fmt.Sprintf("Deployment %q is not yet scheduled on any nodes", depName.String()))
}
}

if len(progressing) > 0 {
return status.Set(
&configv1.ClusterOperatorStatusCondition{
Expand Down
Loading