forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[node-agent] Introduce
Node
controller (gardener#8632)
* Documentation * Controller + Reconciler incl. business logic * Integration test * Address PR review feedback
- Loading branch information
Showing
11 changed files
with
646 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package node | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
"github.com/gardener/gardener/pkg/nodeagent/dbus" | ||
) | ||
|
||
// ControllerName is the name of this controller. | ||
const ControllerName = "node" | ||
|
||
// AddToManager adds Reconciler to the given manager. | ||
func (r *Reconciler) AddToManager(mgr manager.Manager) error { | ||
if r.Client == nil { | ||
r.Client = mgr.GetClient() | ||
} | ||
if r.Recorder == nil { | ||
r.Recorder = mgr.GetEventRecorderFor(ControllerName) | ||
} | ||
if r.DBus == nil { | ||
r.DBus = dbus.New() | ||
} | ||
|
||
node := &metav1.PartialObjectMetadata{} | ||
node.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Node")) | ||
|
||
return builder. | ||
ControllerManagedBy(mgr). | ||
Named(ControllerName). | ||
For(node, builder.WithPredicates(r.NodePredicate())). | ||
WithOptions(controller.Options{MaxConcurrentReconciles: 1}). | ||
Complete(r) | ||
} | ||
|
||
// NodePredicate returns 'true' when the annotation describing which systemd services should be restarted gets set or | ||
// changed. When it's removed, 'false' is returned. | ||
func (r *Reconciler) NodePredicate() predicate.Predicate { | ||
return predicate.Funcs{ | ||
CreateFunc: func(e event.CreateEvent) bool { | ||
return e.Object.GetAnnotations()[annotationRestartSystemdServices] != "" | ||
}, | ||
UpdateFunc: func(e event.UpdateEvent) bool { | ||
return e.ObjectOld.GetAnnotations()[annotationRestartSystemdServices] != e.ObjectNew.GetAnnotations()[annotationRestartSystemdServices] && | ||
e.ObjectNew.GetAnnotations()[annotationRestartSystemdServices] != "" | ||
}, | ||
DeleteFunc: func(_ event.DeleteEvent) bool { return false }, | ||
GenericFunc: func(_ event.GenericEvent) bool { return false }, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package node_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
. "github.com/gardener/gardener/pkg/nodeagent/controller/node" | ||
) | ||
|
||
var _ = Describe("Add", func() { | ||
Describe("#NodePredicate", func() { | ||
var ( | ||
p predicate.Predicate | ||
node *corev1.Node | ||
) | ||
|
||
BeforeEach(func() { | ||
p = (&Reconciler{}).NodePredicate() | ||
node = &corev1.Node{} | ||
}) | ||
|
||
Describe("#Create", func() { | ||
It("should return false because annotation is not present", func() { | ||
Expect(p.Create(event.CreateEvent{Object: node})).To(BeFalse()) | ||
}) | ||
|
||
It("should return true because annotation is present", func() { | ||
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "worker.gardener.cloud/restart-systemd-services", "foo") | ||
Expect(p.Create(event.CreateEvent{Object: node})).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Describe("#Update", func() { | ||
It("should return false because annotation is not present", func() { | ||
Expect(p.Update(event.UpdateEvent{ObjectOld: node, ObjectNew: node})).To(BeFalse()) | ||
}) | ||
|
||
It("should return true because annotation got set", func() { | ||
oldNode := node.DeepCopy() | ||
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "worker.gardener.cloud/restart-systemd-services", "foo") | ||
Expect(p.Update(event.UpdateEvent{ObjectOld: oldNode, ObjectNew: node})).To(BeTrue()) | ||
}) | ||
|
||
It("should return true because annotation got changed", func() { | ||
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "worker.gardener.cloud/restart-systemd-services", "foo") | ||
oldNode := node.DeepCopy() | ||
metav1.SetMetaDataAnnotation(&node.ObjectMeta, "worker.gardener.cloud/restart-systemd-services", "bar") | ||
Expect(p.Update(event.UpdateEvent{ObjectOld: oldNode, ObjectNew: node})).To(BeTrue()) | ||
}) | ||
|
||
It("should return false because annotation got removed", func() { | ||
oldNode := node.DeepCopy() | ||
metav1.SetMetaDataAnnotation(&oldNode.ObjectMeta, "worker.gardener.cloud/restart-systemd-services", "foo") | ||
Expect(p.Update(event.UpdateEvent{ObjectOld: oldNode, ObjectNew: node})).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
Describe("#Delete", func() { | ||
It("should return false", func() { | ||
Expect(p.Delete(event.DeleteEvent{})).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
Describe("#Generic", func() { | ||
It("should return false", func() { | ||
Expect(p.Generic(event.GenericEvent{})).To(BeFalse()) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package node_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestNode(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "NodeAgent Controller Node Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package node | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-logr/logr" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/gardener/gardener/pkg/controllerutils" | ||
nodeagentv1alpha1 "github.com/gardener/gardener/pkg/nodeagent/apis/config/v1alpha1" | ||
"github.com/gardener/gardener/pkg/nodeagent/dbus" | ||
) | ||
|
||
const annotationRestartSystemdServices = "worker.gardener.cloud/restart-systemd-services" | ||
|
||
// Reconciler checks for node annotation changes and restarts the specified systemd services. | ||
type Reconciler struct { | ||
Client client.Client | ||
Recorder record.EventRecorder | ||
DBus dbus.DBus | ||
} | ||
|
||
// Reconcile checks for node annotation changes and restarts the specified systemd services. | ||
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
log := logf.FromContext(ctx) | ||
|
||
ctx, cancel := controllerutils.GetMainReconciliationContext(ctx, controllerutils.DefaultReconciliationTimeout) | ||
defer cancel() | ||
|
||
node := &metav1.PartialObjectMetadata{} | ||
node.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Node")) | ||
if err := r.Client.Get(ctx, request.NamespacedName, node); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
log.V(1).Info("Object is gone, stop reconciling") | ||
return reconcile.Result{}, nil | ||
} | ||
return reconcile.Result{}, fmt.Errorf("error retrieving object from store: %w", err) | ||
} | ||
|
||
services, ok := node.Annotations[annotationRestartSystemdServices] | ||
if !ok { | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
var restartGardenerNodeAgent bool | ||
for _, serviceName := range strings.Split(services, ",") { | ||
// If the gardener-node-agent itself should be restarted, we have to first remove the annotation from the node. | ||
// Otherwise, the annotation is never removed and it restarts itself indefinitely. | ||
if strings.HasPrefix(serviceName, "gardener-node-agent") { | ||
restartGardenerNodeAgent = true | ||
continue | ||
} | ||
r.restartService(ctx, log, node, serviceName) | ||
} | ||
|
||
log.Info("Removing annotation from node", "annotation", annotationRestartSystemdServices) | ||
patch := client.MergeFrom(node.DeepCopy()) | ||
delete(node.Annotations, annotationRestartSystemdServices) | ||
if err := r.Client.Patch(ctx, node, patch); err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
if restartGardenerNodeAgent { | ||
r.restartService(ctx, log, node, nodeagentv1alpha1.UnitName) | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} | ||
|
||
func (r *Reconciler) restartService(ctx context.Context, log logr.Logger, node client.Object, serviceName string) { | ||
log.Info("Restarting systemd service", "serviceName", serviceName) | ||
if err := r.DBus.Restart(ctx, r.Recorder, node, serviceName); err != nil { | ||
// We don't return the error here since we don't want to repeatedly try to restart services again and again. | ||
// In both cases (success or failure), an event will be recorded on the Node so that users can check whether | ||
// the restart worked. | ||
log.Error(err, "Failed restarting systemd service", "serviceName", serviceName) | ||
} | ||
} |
Oops, something went wrong.