Skip to content

Commit 3af45bc

Browse files
committed
Use cache for machine configuration resources
1 parent 63782a9 commit 3af45bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1580
-2039
lines changed

install/0000_00_update-status-controller_02_rbac-DevPreviewNoUpgrade.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,15 @@ rules:
129129
- get
130130
- list
131131
- watch
132-
- apiGroups:
133-
- machineconfiguration.openshift.io
134-
resources:
135-
- machineconfigpools
136-
verbs:
137-
- list
138132
- apiGroups:
139133
- machineconfiguration.openshift.io
140134
resources:
141135
- machineconfigs
136+
- machineconfigpools
142137
verbs:
143138
- get
139+
- list
140+
- watch
144141
- apiGroups:
145142
- config.openshift.io
146143
resources:

pkg/updatestatus/nodeinformer.go

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package updatestatus
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"strings"
87
"time"
@@ -20,19 +19,22 @@ import (
2019

2120
machineconfigv1 "github.com/openshift/api/machineconfiguration/v1"
2221
configv1client "github.com/openshift/client-go/config/clientset/versioned"
23-
machineconfigv1client "github.com/openshift/client-go/machineconfiguration/clientset/versioned"
24-
"github.com/openshift/cluster-version-operator/pkg/updatestatus/mco"
22+
machineconfiginformers "github.com/openshift/client-go/machineconfiguration/informers/externalversions"
23+
machineconfigv1listers "github.com/openshift/client-go/machineconfiguration/listers/machineconfiguration/v1"
2524
"github.com/openshift/library-go/pkg/controller/factory"
2625
"github.com/openshift/library-go/pkg/operator/events"
26+
27+
"github.com/openshift/cluster-version-operator/pkg/updatestatus/mco"
2728
)
2829

2930
// nodeInformerController is the controller that monitors health of the node resources
3031
// and produces insights for node update.
3132
type nodeInformerController struct {
32-
configClient configv1client.Interface
33-
machineConfigClient machineconfigv1client.Interface
34-
nodes corelistersv1.NodeLister
35-
recorder events.Recorder
33+
configClient configv1client.Interface
34+
machineConfigs machineconfigv1listers.MachineConfigLister
35+
machineConfigPools machineconfigv1listers.MachineConfigPoolLister
36+
nodes corelistersv1.NodeLister
37+
recorder events.Recorder
3638

3739
// sendInsight should be called to send produced insights to the update status controller
3840
sendInsight sendInsightFn
@@ -43,19 +45,20 @@ type nodeInformerController struct {
4345

4446
func newNodeInformerController(
4547
configClient configv1client.Interface,
46-
machineConfigClient machineconfigv1client.Interface,
4748
coreInformers kubeinformers.SharedInformerFactory,
49+
machineConfigInformers machineconfiginformers.SharedInformerFactory,
4850
recorder events.Recorder,
4951
sendInsight sendInsightFn,
5052
) factory.Controller {
5153
cpiRecorder := recorder.WithComponentSuffix("node-informer")
5254

5355
c := &nodeInformerController{
54-
configClient: configClient,
55-
machineConfigClient: machineConfigClient,
56-
nodes: coreInformers.Core().V1().Nodes().Lister(),
57-
recorder: cpiRecorder,
58-
sendInsight: sendInsight,
56+
configClient: configClient,
57+
machineConfigs: machineConfigInformers.Machineconfiguration().V1().MachineConfigs().Lister(),
58+
machineConfigPools: machineConfigInformers.Machineconfiguration().V1().MachineConfigPools().Lister(),
59+
nodes: coreInformers.Core().V1().Nodes().Lister(),
60+
recorder: cpiRecorder,
61+
sendInsight: sendInsight,
5962

6063
now: metav1.Now,
6164
}
@@ -91,27 +94,24 @@ func (c *nodeInformerController) sync(ctx context.Context, syncCtx factory.SyncC
9194
return err
9295
}
9396

94-
mcpList, err := c.machineConfigClient.MachineconfigurationV1().MachineConfigPools().List(ctx, metav1.ListOptions{})
97+
pools, err := c.machineConfigPools.List(labels.Everything())
9598
if err != nil {
9699
return err
97100
}
98-
mcp, err := whichMCP(node, mcpList.Items)
101+
mcp, err := whichMCP(node, pools)
99102
if err != nil {
100103
return fmt.Errorf("failed to determine which machine config pool the node belongs to: %w", err)
101104
}
102105

103-
machineConfigs := map[string]*machineconfigv1.MachineConfig{}
104-
for _, key := range []string{mco.CurrentMachineConfigAnnotationKey, mco.DesiredMachineConfigAnnotationKey} {
105-
machineConfigName, ok := node.Annotations[key]
106-
if !ok || machineConfigName == "" {
107-
continue
108-
}
109-
if _, ok := machineConfigs[machineConfigName]; !ok {
110-
machineConfig, err := c.machineConfigClient.MachineconfigurationV1().MachineConfigs().Get(ctx, machineConfigName, metav1.GetOptions{})
111-
if err != nil {
112-
return err
113-
}
114-
machineConfigs[machineConfigName] = machineConfig
106+
machineConfigs, err := c.machineConfigs.List(labels.Everything())
107+
if err != nil {
108+
return err
109+
}
110+
111+
machineConfigVersions := map[string]string{}
112+
for _, mc := range machineConfigs {
113+
if openshiftVersion, ok := mc.Annotations[mco.ReleaseImageVersionAnnotationKey]; ok && openshiftVersion != "" {
114+
machineConfigVersions[mc.Name] = openshiftVersion
115115
}
116116
}
117117

@@ -125,7 +125,7 @@ func (c *nodeInformerController) sync(ctx context.Context, syncCtx factory.SyncC
125125
}
126126

127127
now := c.now()
128-
if insight := assessNode(node, mcp, machineConfigs, mostRecentVersionInCVHistory, now); insight != nil {
128+
if insight := assessNode(node, mcp, machineConfigVersions, mostRecentVersionInCVHistory, now); insight != nil {
129129
msg = makeInsightMsgForNode(insight, now)
130130
}
131131
default:
@@ -158,13 +158,13 @@ func makeInsightMsgForNode(nodeInsight *NodeStatusInsight, acquiredAt metav1.Tim
158158
}
159159
}
160160

161-
func whichMCP(node *corev1.Node, pools []machineconfigv1.MachineConfigPool) (*machineconfigv1.MachineConfigPool, error) {
161+
func whichMCP(node *corev1.Node, pools []*machineconfigv1.MachineConfigPool) (*machineconfigv1.MachineConfigPool, error) {
162162
var masterSelector labels.Selector
163163
var workerSelector labels.Selector
164164
customSelectors := map[string]labels.Selector{}
165165
poolsMap := make(map[string]*machineconfigv1.MachineConfigPool, len(pools))
166166
for _, pool := range pools {
167-
poolsMap[pool.Name] = &pool
167+
poolsMap[pool.Name] = pool
168168
s, err := metav1.LabelSelectorAsSelector(pool.Spec.NodeSelector)
169169
if err != nil {
170170
return nil, fmt.Errorf("failed to get label selector from the pool %s: %w", pool.Name, err)
@@ -190,17 +190,7 @@ func whichMCP(node *corev1.Node, pools []machineconfigv1.MachineConfigPool) (*ma
190190
if workerSelector != nil && workerSelector.Matches(labels.Set(node.Labels)) {
191191
return poolsMap[mco.MachineConfigPoolWorker], nil
192192
}
193-
return nil, errors.New("failed to find a matching node selector")
194-
}
195-
196-
func getOpenShiftVersionOfMachineConfig(machineConfigs map[string]*machineconfigv1.MachineConfig, name string) (string, bool) {
197-
for _, mc := range machineConfigs {
198-
if mc.Name == name {
199-
openshiftVersion := mc.Annotations[mco.ReleaseImageVersionAnnotationKey]
200-
return openshiftVersion, openshiftVersion != ""
201-
}
202-
}
203-
return "", false
193+
return nil, fmt.Errorf("failed to find a matching node selector from %d machine config pools", len(pools))
204194
}
205195

206196
func isNodeDegraded(node *corev1.Node) bool {
@@ -332,14 +322,14 @@ func toPointer(d time.Duration) *metav1.Duration {
332322
return &v
333323
}
334324

335-
func assessNode(node *corev1.Node, mcp *machineconfigv1.MachineConfigPool, machineConfigs map[string]*machineconfigv1.MachineConfig, mostRecentVersionInCVHistory string, now metav1.Time) *NodeStatusInsight {
325+
func assessNode(node *corev1.Node, mcp *machineconfigv1.MachineConfigPool, machineConfigVersions map[string]string, mostRecentVersionInCVHistory string, now metav1.Time) *NodeStatusInsight {
336326
if node == nil || mcp == nil {
337327
return nil
338328
}
339329

340330
desiredConfig, ok := node.Annotations[mco.DesiredMachineConfigAnnotationKey]
341-
currentVersion, foundCurrent := getOpenShiftVersionOfMachineConfig(machineConfigs, node.Annotations[mco.CurrentMachineConfigAnnotationKey])
342-
desiredVersion, foundDesired := getOpenShiftVersionOfMachineConfig(machineConfigs, desiredConfig)
331+
currentVersion, foundCurrent := machineConfigVersions[node.Annotations[mco.CurrentMachineConfigAnnotationKey]]
332+
desiredVersion, foundDesired := machineConfigVersions[desiredConfig]
343333

344334
lns := mco.NewLayeredNodeState(node)
345335
isUnavailable := lns.IsUnavailable(mcp)

0 commit comments

Comments
 (0)