Skip to content

Commit ab32432

Browse files
Merge pull request #1962 from cgwalters/nodecontroller-events
Bug 1852047: controller: Emit events
2 parents 16d243c + 4e133e7 commit ab32432

File tree

7 files changed

+200
-24
lines changed

7 files changed

+200
-24
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRole
3+
metadata:
4+
name: machine-config-controller-events
5+
namespace: {{.TargetNamespace}}
6+
rules:
7+
- apiGroups: [""]
8+
resources: ["events"]
9+
verbs: ["create", "patch"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: RoleBinding
3+
metadata:
4+
name: machine-config-controller-events
5+
namespace: default
6+
roleRef:
7+
kind: ClusterRole
8+
name: machine-config-controller-events
9+
subjects:
10+
- kind: ServiceAccount
11+
namespace: {{.TargetNamespace}}
12+
name: machine-config-controller
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: RoleBinding
3+
metadata:
4+
name: machine-config-controller-events
5+
namespace: {{.TargetNamespace}}
6+
roleRef:
7+
kind: ClusterRole
8+
name: machine-config-controller-events
9+
subjects:
10+
- kind: ServiceAccount
11+
namespace: {{.TargetNamespace}}
12+
name: machine-config-controller

pkg/controller/node/node_controller.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,16 @@ func (ctrl *Controller) addNode(obj interface{}) {
409409
}
410410
}
411411

412+
func (ctrl *Controller) logPool(pool *mcfgv1.MachineConfigPool, format string, args ...interface{}) {
413+
msg := fmt.Sprintf(format, args...)
414+
glog.Infof("Pool %s: %s", pool.Name, msg)
415+
}
416+
417+
func (ctrl *Controller) logPoolNode(pool *mcfgv1.MachineConfigPool, node *corev1.Node, format string, args ...interface{}) {
418+
msg := fmt.Sprintf(format, args...)
419+
glog.Infof("Pool %s: node %s: %s", pool.Name, node.Name, msg)
420+
}
421+
412422
func (ctrl *Controller) updateNode(old, cur interface{}) {
413423
oldNode := old.(*corev1.Node)
414424
curNode := cur.(*corev1.Node)
@@ -441,16 +451,16 @@ func (ctrl *Controller) updateNode(old, cur interface{}) {
441451
if oldReady != newReady {
442452
changed = true
443453
if newReadyErr != nil {
444-
glog.Infof("Pool %s: node %s is now reporting unready: %v", pool.Name, curNode.Name, newReadyErr)
454+
ctrl.logPoolNode(pool, curNode, "Reporting unready: %v", newReadyErr)
445455
} else {
446-
glog.Infof("Pool %s: node %s is now reporting ready", pool.Name, curNode.Name)
456+
ctrl.logPoolNode(pool, curNode, "Reporting ready")
447457
}
448458
}
449459

450460
// Specifically log when a node has completed an update so the MCC logs are a useful central aggregate of state changes
451461
if oldNode.Annotations[daemonconsts.CurrentMachineConfigAnnotationKey] != oldNode.Annotations[daemonconsts.DesiredMachineConfigAnnotationKey] &&
452462
isNodeDone(curNode) {
453-
glog.Infof("Pool %s: node %s has completed update to %s", pool.Name, curNode.Name, curNode.Annotations[daemonconsts.DesiredMachineConfigAnnotationKey])
463+
ctrl.logPoolNode(pool, curNode, "Completed update to %s", curNode.Annotations[daemonconsts.DesiredMachineConfigAnnotationKey])
454464
changed = true
455465
} else {
456466
annos := []string{
@@ -459,13 +469,18 @@ func (ctrl *Controller) updateNode(old, cur interface{}) {
459469
daemonconsts.MachineConfigDaemonStateAnnotationKey,
460470
}
461471
for _, anno := range annos {
462-
if oldNode.Annotations[anno] != curNode.Annotations[anno] {
463-
glog.Infof("Pool %s: node %s changed %s = %s", pool.Name, curNode.Name, anno, curNode.Annotations[anno])
472+
newValue := curNode.Annotations[anno]
473+
if oldNode.Annotations[anno] != newValue {
474+
ctrl.logPoolNode(pool, curNode, "changed annotation %s = %s", anno, newValue)
464475
changed = true
476+
// For the control plane, emit events for these since they're important
477+
if pool.Name == masterPoolName {
478+
ctrl.eventRecorder.Eventf(pool, corev1.EventTypeNormal, "AnnotationChange", "Node %s now has %s=%s", curNode.Name, anno, newValue)
479+
}
465480
}
466481
}
467482
if !reflect.DeepEqual(oldNode.Labels, curNode.Labels) {
468-
glog.Infof("Pool %s: node %s changed labels", pool.Name, curNode.Name)
483+
ctrl.logPoolNode(pool, curNode, "changed labels")
469484
changed = true
470485
}
471486
}
@@ -742,7 +757,7 @@ func (ctrl *Controller) syncMachineConfigPool(key string) error {
742757

743758
candidates, capacity := getAllCandidateMachines(pool, nodes, maxunavail)
744759
if len(candidates) > 0 {
745-
glog.Infof("Pool %s: %d candidate nodes for update, capacity: %d", pool.Name, len(candidates), capacity)
760+
ctrl.logPool(pool, "%d candidate nodes for update, capacity: %d", len(candidates), capacity)
746761
if err := ctrl.updateCandidateMachines(pool, candidates, capacity); err != nil {
747762
if syncErr := ctrl.syncStatusOnly(pool); syncErr != nil {
748763
return goerrs.Wrapf(err, "error setting desired machine config annotation for pool %q, sync error: %v", pool.Name, syncErr)
@@ -783,7 +798,6 @@ func (ctrl *Controller) getNodesForPool(pool *mcfgv1.MachineConfigPool) ([]*core
783798
}
784799

785800
func (ctrl *Controller) setDesiredMachineConfigAnnotation(nodeName, currentConfig string) error {
786-
glog.Infof("Setting node %s to desired config %s", nodeName, currentConfig)
787801
return clientretry.RetryOnConflict(nodeUpdateBackoff, func() error {
788802
oldNode, err := ctrl.kubeClient.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
789803
if err != nil {
@@ -869,7 +883,7 @@ func (ctrl *Controller) getCurrentEtcdLeader(candidates []*corev1.Node) (*corev1
869883
// filterControlPlaneCandidateNodes adjusts the candidates and capacity specifically
870884
// for the control plane, e.g. based on which node is the etcd leader at the time.
871885
// nolint:unparam
872-
func (ctrl *Controller) filterControlPlaneCandidateNodes(candidates []*corev1.Node, capacity uint) ([]*corev1.Node, uint, error) {
886+
func (ctrl *Controller) filterControlPlaneCandidateNodes(pool *mcfgv1.MachineConfigPool, candidates []*corev1.Node, capacity uint) ([]*corev1.Node, uint, error) {
873887
if len(candidates) <= 1 {
874888
return candidates, capacity, nil
875889
}
@@ -880,6 +894,8 @@ func (ctrl *Controller) filterControlPlaneCandidateNodes(candidates []*corev1.No
880894
var newCandidates []*corev1.Node
881895
for _, node := range candidates {
882896
if node == etcdLeader {
897+
// For now make this an event so we know it's working, even though it's more of a non-event
898+
ctrl.eventRecorder.Eventf(pool, corev1.EventTypeNormal, "DeferringEtcdLeaderUpdate", "Deferring update of etcd leader %s", node.Name)
883899
glog.Infof("Deferring update of etcd leader: %s", node.Name)
884900
continue
885901
}
@@ -892,23 +908,31 @@ func (ctrl *Controller) filterControlPlaneCandidateNodes(candidates []*corev1.No
892908
func (ctrl *Controller) updateCandidateMachines(pool *mcfgv1.MachineConfigPool, candidates []*corev1.Node, capacity uint) error {
893909
if pool.Name == masterPoolName {
894910
var err error
895-
candidates, capacity, err = ctrl.filterControlPlaneCandidateNodes(candidates, capacity)
911+
candidates, capacity, err = ctrl.filterControlPlaneCandidateNodes(pool, candidates, capacity)
896912
if err != nil {
897913
return err
898914
}
899915
// In practice right now these counts will be 1 but let's stay general to support 5 etcd nodes in the future
900-
glog.Infof("Pool %s: filtered to %d candidate nodes for update, capacity: %d", pool.Name, len(candidates), capacity)
916+
ctrl.logPool(pool, "filtered to %d candidate nodes for update, capacity: %d", len(candidates), capacity)
901917
}
902918
if capacity < uint(len(candidates)) {
903919
// Arbitrarily pick the first N candidates; no attempt at sorting.
904920
// Perhaps later we allow admins to weight somehow, or do something more intelligent.
905921
candidates = candidates[:capacity]
906922
}
923+
targetConfig := pool.Spec.Configuration.Name
907924
for _, node := range candidates {
908-
if err := ctrl.setDesiredMachineConfigAnnotation(node.Name, pool.Spec.Configuration.Name); err != nil {
925+
ctrl.logPool(pool, "Setting node %s target to %s", node.Name, targetConfig)
926+
if err := ctrl.setDesiredMachineConfigAnnotation(node.Name, targetConfig); err != nil {
909927
return goerrs.Wrapf(err, "setting desired config for node %s", node.Name)
910928
}
911929
}
930+
if len(candidates) == 1 {
931+
candidate := candidates[0]
932+
ctrl.eventRecorder.Eventf(pool, corev1.EventTypeNormal, "SetDesiredConfig", "Targeted node %s to config %s", candidate.Name, targetConfig)
933+
} else {
934+
ctrl.eventRecorder.Eventf(pool, corev1.EventTypeNormal, "SetDesiredConfig", "Set target for %d nodes to config %s", targetConfig)
935+
}
912936
return nil
913937
}
914938

pkg/controller/node/status.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ func (ctrl *Controller) syncStatusOnly(pool *mcfgv1.MachineConfigPool) error {
2727
newPool := pool
2828
newPool.Status = newStatus
2929
_, err = ctrl.client.MachineconfigurationV1().MachineConfigPools().UpdateStatus(context.TODO(), newPool, metav1.UpdateOptions{})
30+
if pool.Spec.Configuration.Name != newPool.Spec.Configuration.Name {
31+
ctrl.eventRecorder.Eventf(pool, corev1.EventTypeNormal, "Updating", "Pool %s now targeting %s", pool.Name, newPool.Spec.Configuration.Name)
32+
}
33+
if pool.Status.Configuration.Name != newPool.Status.Configuration.Name {
34+
ctrl.eventRecorder.Eventf(pool, corev1.EventTypeNormal, "Completed", "Pool %s has completed update to %s", pool.Name, newPool.Status.Configuration.Name)
35+
}
3036
return err
3137
}
3238

pkg/operator/assets/bindata.go

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
// manifests/machineconfigcontroller/clusterrolebinding.yaml
1111
// manifests/machineconfigcontroller/controllerconfig.yaml
1212
// manifests/machineconfigcontroller/deployment.yaml
13+
// manifests/machineconfigcontroller/events-clusterrole.yaml
14+
// manifests/machineconfigcontroller/events-rolebinding-default.yaml
15+
// manifests/machineconfigcontroller/events-rolebinding-target.yaml
1316
// manifests/machineconfigcontroller/sa.yaml
1417
// manifests/machineconfigdaemon/clusterrole.yaml
1518
// manifests/machineconfigdaemon/clusterrolebinding.yaml
@@ -1161,6 +1164,90 @@ func manifestsMachineconfigcontrollerDeploymentYaml() (*asset, error) {
11611164
return a, nil
11621165
}
11631166

1167+
var _manifestsMachineconfigcontrollerEventsClusterroleYaml = []byte(`apiVersion: rbac.authorization.k8s.io/v1
1168+
kind: ClusterRole
1169+
metadata:
1170+
name: machine-config-controller-events
1171+
namespace: {{.TargetNamespace}}
1172+
rules:
1173+
- apiGroups: [""]
1174+
resources: ["events"]
1175+
verbs: ["create", "patch"]
1176+
`)
1177+
1178+
func manifestsMachineconfigcontrollerEventsClusterroleYamlBytes() ([]byte, error) {
1179+
return _manifestsMachineconfigcontrollerEventsClusterroleYaml, nil
1180+
}
1181+
1182+
func manifestsMachineconfigcontrollerEventsClusterroleYaml() (*asset, error) {
1183+
bytes, err := manifestsMachineconfigcontrollerEventsClusterroleYamlBytes()
1184+
if err != nil {
1185+
return nil, err
1186+
}
1187+
1188+
info := bindataFileInfo{name: "manifests/machineconfigcontroller/events-clusterrole.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
1189+
a := &asset{bytes: bytes, info: info}
1190+
return a, nil
1191+
}
1192+
1193+
var _manifestsMachineconfigcontrollerEventsRolebindingDefaultYaml = []byte(`apiVersion: rbac.authorization.k8s.io/v1
1194+
kind: RoleBinding
1195+
metadata:
1196+
name: machine-config-controller-events
1197+
namespace: default
1198+
roleRef:
1199+
kind: ClusterRole
1200+
name: machine-config-controller-events
1201+
subjects:
1202+
- kind: ServiceAccount
1203+
namespace: {{.TargetNamespace}}
1204+
name: machine-config-controller
1205+
`)
1206+
1207+
func manifestsMachineconfigcontrollerEventsRolebindingDefaultYamlBytes() ([]byte, error) {
1208+
return _manifestsMachineconfigcontrollerEventsRolebindingDefaultYaml, nil
1209+
}
1210+
1211+
func manifestsMachineconfigcontrollerEventsRolebindingDefaultYaml() (*asset, error) {
1212+
bytes, err := manifestsMachineconfigcontrollerEventsRolebindingDefaultYamlBytes()
1213+
if err != nil {
1214+
return nil, err
1215+
}
1216+
1217+
info := bindataFileInfo{name: "manifests/machineconfigcontroller/events-rolebinding-default.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
1218+
a := &asset{bytes: bytes, info: info}
1219+
return a, nil
1220+
}
1221+
1222+
var _manifestsMachineconfigcontrollerEventsRolebindingTargetYaml = []byte(`apiVersion: rbac.authorization.k8s.io/v1
1223+
kind: RoleBinding
1224+
metadata:
1225+
name: machine-config-controller-events
1226+
namespace: {{.TargetNamespace}}
1227+
roleRef:
1228+
kind: ClusterRole
1229+
name: machine-config-controller-events
1230+
subjects:
1231+
- kind: ServiceAccount
1232+
namespace: {{.TargetNamespace}}
1233+
name: machine-config-controller
1234+
`)
1235+
1236+
func manifestsMachineconfigcontrollerEventsRolebindingTargetYamlBytes() ([]byte, error) {
1237+
return _manifestsMachineconfigcontrollerEventsRolebindingTargetYaml, nil
1238+
}
1239+
1240+
func manifestsMachineconfigcontrollerEventsRolebindingTargetYaml() (*asset, error) {
1241+
bytes, err := manifestsMachineconfigcontrollerEventsRolebindingTargetYamlBytes()
1242+
if err != nil {
1243+
return nil, err
1244+
}
1245+
1246+
info := bindataFileInfo{name: "manifests/machineconfigcontroller/events-rolebinding-target.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
1247+
a := &asset{bytes: bytes, info: info}
1248+
return a, nil
1249+
}
1250+
11641251
var _manifestsMachineconfigcontrollerSaYaml = []byte(`apiVersion: v1
11651252
kind: ServiceAccount
11661253
metadata:
@@ -2761,6 +2848,9 @@ var _bindata = map[string]func() (*asset, error){
27612848
"manifests/machineconfigcontroller/clusterrolebinding.yaml": manifestsMachineconfigcontrollerClusterrolebindingYaml,
27622849
"manifests/machineconfigcontroller/controllerconfig.yaml": manifestsMachineconfigcontrollerControllerconfigYaml,
27632850
"manifests/machineconfigcontroller/deployment.yaml": manifestsMachineconfigcontrollerDeploymentYaml,
2851+
"manifests/machineconfigcontroller/events-clusterrole.yaml": manifestsMachineconfigcontrollerEventsClusterroleYaml,
2852+
"manifests/machineconfigcontroller/events-rolebinding-default.yaml": manifestsMachineconfigcontrollerEventsRolebindingDefaultYaml,
2853+
"manifests/machineconfigcontroller/events-rolebinding-target.yaml": manifestsMachineconfigcontrollerEventsRolebindingTargetYaml,
27642854
"manifests/machineconfigcontroller/sa.yaml": manifestsMachineconfigcontrollerSaYaml,
27652855
"manifests/machineconfigdaemon/clusterrole.yaml": manifestsMachineconfigdaemonClusterroleYaml,
27662856
"manifests/machineconfigdaemon/clusterrolebinding.yaml": manifestsMachineconfigdaemonClusterrolebindingYaml,
@@ -2847,11 +2937,14 @@ var _bintree = &bintree{nil, map[string]*bintree{
28472937
"bootstrap-pod-v2.yaml": &bintree{manifestsBootstrapPodV2Yaml, map[string]*bintree{}},
28482938
"controllerconfig.crd.yaml": &bintree{manifestsControllerconfigCrdYaml, map[string]*bintree{}},
28492939
"machineconfigcontroller": &bintree{nil, map[string]*bintree{
2850-
"clusterrole.yaml": &bintree{manifestsMachineconfigcontrollerClusterroleYaml, map[string]*bintree{}},
2851-
"clusterrolebinding.yaml": &bintree{manifestsMachineconfigcontrollerClusterrolebindingYaml, map[string]*bintree{}},
2852-
"controllerconfig.yaml": &bintree{manifestsMachineconfigcontrollerControllerconfigYaml, map[string]*bintree{}},
2853-
"deployment.yaml": &bintree{manifestsMachineconfigcontrollerDeploymentYaml, map[string]*bintree{}},
2854-
"sa.yaml": &bintree{manifestsMachineconfigcontrollerSaYaml, map[string]*bintree{}},
2940+
"clusterrole.yaml": &bintree{manifestsMachineconfigcontrollerClusterroleYaml, map[string]*bintree{}},
2941+
"clusterrolebinding.yaml": &bintree{manifestsMachineconfigcontrollerClusterrolebindingYaml, map[string]*bintree{}},
2942+
"controllerconfig.yaml": &bintree{manifestsMachineconfigcontrollerControllerconfigYaml, map[string]*bintree{}},
2943+
"deployment.yaml": &bintree{manifestsMachineconfigcontrollerDeploymentYaml, map[string]*bintree{}},
2944+
"events-clusterrole.yaml": &bintree{manifestsMachineconfigcontrollerEventsClusterroleYaml, map[string]*bintree{}},
2945+
"events-rolebinding-default.yaml": &bintree{manifestsMachineconfigcontrollerEventsRolebindingDefaultYaml, map[string]*bintree{}},
2946+
"events-rolebinding-target.yaml": &bintree{manifestsMachineconfigcontrollerEventsRolebindingTargetYaml, map[string]*bintree{}},
2947+
"sa.yaml": &bintree{manifestsMachineconfigcontrollerSaYaml, map[string]*bintree{}},
28552948
}},
28562949
"machineconfigdaemon": &bintree{nil, map[string]*bintree{
28572950
"clusterrole.yaml": &bintree{manifestsMachineconfigdaemonClusterroleYaml, map[string]*bintree{}},

pkg/operator/sync.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,34 @@ func (optr *Operator) syncMachineConfigPools(config *renderConfig) error {
395395
}
396396

397397
func (optr *Operator) syncMachineConfigController(config *renderConfig) error {
398-
crBytes, err := renderAsset(config, "manifests/machineconfigcontroller/clusterrole.yaml")
399-
if err != nil {
400-
return err
398+
for _, path := range []string{
399+
"manifests/machineconfigcontroller/clusterrole.yaml",
400+
"manifests/machineconfigcontroller/events-clusterrole.yaml",
401+
} {
402+
crBytes, err := renderAsset(config, path)
403+
if err != nil {
404+
return err
405+
}
406+
cr := resourceread.ReadClusterRoleV1OrDie(crBytes)
407+
_, _, err = resourceapply.ApplyClusterRole(optr.kubeClient.RbacV1(), cr)
408+
if err != nil {
409+
return err
410+
}
401411
}
402-
cr := resourceread.ReadClusterRoleV1OrDie(crBytes)
403-
_, _, err = resourceapply.ApplyClusterRole(optr.kubeClient.RbacV1(), cr)
404-
if err != nil {
405-
return err
412+
413+
for _, path := range []string{
414+
"manifests/machineconfigcontroller/events-rolebinding-default.yaml",
415+
"manifests/machineconfigcontroller/events-rolebinding-target.yaml",
416+
} {
417+
crbBytes, err := renderAsset(config, path)
418+
if err != nil {
419+
return err
420+
}
421+
crb := resourceread.ReadRoleBindingV1OrDie(crbBytes)
422+
_, _, err = resourceapply.ApplyRoleBinding(optr.kubeClient.RbacV1(), crb)
423+
if err != nil {
424+
return err
425+
}
406426
}
407427

408428
crbBytes, err := renderAsset(config, "manifests/machineconfigcontroller/clusterrolebinding.yaml")

0 commit comments

Comments
 (0)