Skip to content

Commit

Permalink
getter registration
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-muller committed Jul 26, 2024
1 parent 3b82a3f commit 947aff1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
17 changes: 13 additions & 4 deletions pkg/updatestatus/controlplaneupdateinformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func queueKeys(obj runtime.Object) []string {
return nil
}

func newControlPlaneUpdateInformer(configInformers configv1informers.SharedInformerFactory, eventsRecorder events.Recorder) (factory.Controller, func() controlPlaneUpdateStatus) {
func newControlPlaneUpdateInformer(configInformers configv1informers.SharedInformerFactory, eventsRecorder events.Recorder) (factory.Controller, *controlPlaneUpdateInformer) {
c := controlPlaneUpdateInformer{
clusterVersionLister: configInformers.Config().V1().ClusterVersions().Lister(),
clusterOperatorLister: configInformers.Config().V1().ClusterOperators().Lister(),
Expand All @@ -74,7 +74,7 @@ func newControlPlaneUpdateInformer(configInformers configv1informers.SharedInfor
configInformers.Config().V1().ClusterOperators().Informer(),
).ResyncEvery(10*time.Minute).
WithSync(c.sync).
ToController("ControlPlaneUpdateInformer", eventsRecorder.WithComponentSuffix("control-plane-update-informer")), c.getControlPlaneUpdateStatus
ToController("ControlPlaneUpdateInformer", eventsRecorder.WithComponentSuffix("control-plane-update-informer")), &c
}

func versionsFromHistory(history []configv1.UpdateHistory, cvScope configv1alpha.ResourceRef, controlPlaneCompleted bool) (versions, []configv1alpha.UpdateInsight) {
Expand Down Expand Up @@ -166,11 +166,19 @@ func (c *controlPlaneUpdateInformer) sync(ctx context.Context, syncCtx factory.S
c.statusLock.Lock()
defer c.statusLock.Unlock()

c.status.versions, _ = versionsFromHistory(cv.Status.History, configv1alpha.ResourceRef{
versions, insights := versionsFromHistory(cv.Status.History, configv1alpha.ResourceRef{
APIGroup: "config.openshift.io/v1",
Kind: "ClusterVersion",
Name: cv.Name,
}, cvProgressing.Status == configv1.ConditionTrue)
}, cvProgressing.Status == configv1.ConditionFalse)

c.status.versions = versions

// TODO: Merge instead of replace
c.insightsLock.Lock()
c.insights = nil
c.insights = append(c.insights, insights...)
c.insightsLock.Unlock()

progressing := meta.FindStatusCondition(c.status.conditions, "UpdateProgressing")
if progressing == nil {
Expand All @@ -194,6 +202,7 @@ func (c *controlPlaneUpdateInformer) sync(ctx context.Context, syncCtx factory.S
progressing.Message = cvProgressing.Message
progressing.LastTransitionTime = cvProgressing.LastTransitionTime
}

case "co":
klog.Infof("Control Plane Update Informer :: SYNC :: ClusterOperator :: %s (TODO)", name)
default:
Expand Down
2 changes: 1 addition & 1 deletion pkg/updatestatus/controlplaneupdateinformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func Test_controlPlaneUpdateInformer_sync_versions(t *testing.T) {
name: "Update completed from a partial update",
history: []configv1.UpdateHistory{
{
State: configv1.PartialUpdate,
State: configv1.CompletedUpdate,
StartedTime: minutesAgo[10],
CompletionTime: &minutesAgo[5],
Version: "v2-updating",
Expand Down
34 changes: 28 additions & 6 deletions pkg/updatestatus/insightscraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ import (
"k8s.io/klog/v2"
)

type getInsightsFunc func() []v1alpha1.UpdateInsight

type updateInsightScraper struct {
controlPlaneUpdateStatus controlPlaneUpdateStatus
getControlPlaneUpdateStatus func() controlPlaneUpdateStatus

insightsGetters map[string]getInsightsFunc

getUpdateStatus getUpdateStatusFunc

recorder events.Recorder
}

func newUpdateInsightScraper(getControlPlaneUpdateStatus func() controlPlaneUpdateStatus, getUpdateStatus getUpdateStatusFunc, eventsRecorder events.Recorder) factory.Controller {
func newUpdateInsightScraper(getControlPlaneUpdateStatus func() controlPlaneUpdateStatus, getUpdateStatus getUpdateStatusFunc, eventsRecorder events.Recorder) (factory.Controller, *updateInsightScraper) {
c := updateInsightScraper{
getControlPlaneUpdateStatus: getControlPlaneUpdateStatus,
getUpdateStatus: getUpdateStatus,
Expand All @@ -31,20 +35,24 @@ func newUpdateInsightScraper(getControlPlaneUpdateStatus func() controlPlaneUpda

return factory.New().WithInformers().ResyncEvery(time.Minute).
WithSync(c.sync).
ToController("UpdateInsightScraper", eventsRecorder.WithComponentSuffix("update-insight-scraper"))
ToController("UpdateInsightScraper", eventsRecorder.WithComponentSuffix("update-insight-scraper")), &c
}

func (c *updateInsightScraper) registerInsightsInformer(name string, getter getInsightsFunc) {
c.insightsGetters[name] = getter
}

func (c *updateInsightScraper) sync(ctx context.Context, syncCtx factory.SyncContext) error {
klog.Info("Update Insight Scraper :: SYNC")
c.controlPlaneUpdateStatus = c.getControlPlaneUpdateStatus()

progressing := meta.FindStatusCondition(c.controlPlaneUpdateStatus.conditions, "Progressing")
progressing := meta.FindStatusCondition(c.controlPlaneUpdateStatus.conditions, "UpdateProgressing")
if progressing == nil {
klog.Info("Update Insight Scraper :: No Progressing condition found")
klog.Info("Update Insight Scraper :: No UpdateProgressing condition found")
return nil
}

klog.Infof("Update Insight Scraper :: Progressing=%s", progressing.Status)
klog.Infof("Update Insight Scraper :: UpdateProgressing=%s", progressing.Status)

updateStatus := c.getUpdateStatus()
updateStatus.Lock()
Expand All @@ -64,8 +72,22 @@ func (c *updateInsightScraper) sync(ctx context.Context, syncCtx factory.SyncCon
Previous: c.controlPlaneUpdateStatus.versions.previous,
IsPreviousPartial: c.controlPlaneUpdateStatus.versions.isPreviousPartial,
Target: c.controlPlaneUpdateStatus.versions.target,
IsTargetInstall: c.controlPlaneUpdateStatus.versions.isTargetInstall,

IsTargetInstall: c.controlPlaneUpdateStatus.versions.isTargetInstall,
}

cpStatus.Informers = nil

for name, getter := range c.insightsGetters {
informer := v1alpha1.UpdateInformer{Name: name}
for _, insight := range getter() {
if insight.Scope.Type == v1alpha1.ScopeTypeControlPlane {
informer.Insights = append(informer.Insights, insight)
}
}
if len(informer.Insights) > 0 {
cpStatus.Informers = append(cpStatus.Informers, informer)
}
}
return nil
}
10 changes: 5 additions & 5 deletions pkg/updatestatus/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func Run(ctx context.Context, cc *controllercmd.ControllerContext) error {
klog.Info("Run :: Created clients")

usc, getUpdateStatus := newUpdateStatusController(configV1Alpha1Client.UpdateStatuses(), cc.EventRecorder)
cpInformer, getControlPlaneUpdateStatus := newControlPlaneUpdateInformer(configInformers, cc.EventRecorder)
insightScraper := newUpdateInsightScraper(getControlPlaneUpdateStatus, getUpdateStatus, cc.EventRecorder)
cpInformerController, cpInfomer := newControlPlaneUpdateInformer(configInformers, cc.EventRecorder)
insightScraperController, insightScraper := newUpdateInsightScraper(cpInfomer.getControlPlaneUpdateStatus, getUpdateStatus, cc.EventRecorder)
insightScraper.registerInsightsInformer("controlPlaneUpdate", cpInfomer.getInsights)
_ = []factory.Controller{
cpInformer,
newWorkerPoolsUpdateInformer(coreInformers, mcfgInformers, cc.EventRecorder),
}

Expand All @@ -66,8 +66,8 @@ func Run(ctx context.Context, cc *controllercmd.ControllerContext) error {
mcfgInformers.Start(ctx.Done())

go usc.Run(ctx, 1)
go insightScraper.Run(ctx, 1)
go cpInformer.Run(ctx, 1)
go insightScraperController.Run(ctx, 1)
go cpInformerController.Run(ctx, 1)

klog.Info("Run :: Launched controllers")

Expand Down

0 comments on commit 947aff1

Please sign in to comment.