Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-muller committed Jul 19, 2024
1 parent f7aaeb0 commit ac8eeaf
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 11 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
toolchain go1.22.2

require (
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df
github.com/blang/semver/v4 v4.0.0
github.com/davecgh/go-spew v1.1.1
github.com/ghodss/yaml v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 h1:4daAzAu0S6Vi7/lbWECcX0j45yZReDZ56BQsrVBOEEY=
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df h1:GSoSVRLoBaFpOOds6QyY1L8AX7uoY+Ln3BHc22W40X0=
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df/go.mod h1:hiVxq5OP2bUGBRNS3Z/bt/reCLFNbdcST6gISi1fiOM=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
Expand Down
111 changes: 106 additions & 5 deletions pkg/updatestatus/controlplaneupdateinformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,137 @@ package updatestatus

import (
"context"
"strings"
"sync"
"time"

"github.com/barkimedes/go-deepcopy"
configv1 "github.com/openshift/api/config/v1"
configv1informers "github.com/openshift/client-go/config/informers/externalversions"
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
"github.com/openshift/cluster-version-operator/lib/resourcemerge"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
)

type controlPlaneUpdateStatus struct {
conditions []metav1.Condition
}

type controlPlaneUpdateInformer struct {
clusterVersionLister configv1listers.ClusterVersionLister
clusterOperatorLister configv1listers.ClusterOperatorLister

statusLock sync.Mutex
status controlPlaneUpdateStatus

recorder events.Recorder
}

func newControlPlaneUpdateInformer(configInformers configv1informers.SharedInformerFactory, eventsRecorder events.Recorder) factory.Controller {
func queueKeys(obj runtime.Object) []string {
if obj == nil {
return nil
}

switch controlPlaneObj := obj.(type) {
case *configv1.ClusterVersion:
return []string{"cv/" + controlPlaneObj.Name}
case *configv1.ClusterOperator:
return []string{"co/" + controlPlaneObj.Name}
}

return nil
}

func newControlPlaneUpdateInformer(configInformers configv1informers.SharedInformerFactory, eventsRecorder events.Recorder) (factory.Controller, func() controlPlaneUpdateStatus) {
c := controlPlaneUpdateInformer{
clusterVersionLister: configInformers.Config().V1().ClusterVersions().Lister(),
clusterOperatorLister: configInformers.Config().V1().ClusterOperators().Lister(),
recorder: eventsRecorder,

recorder: eventsRecorder,
}

return factory.New().WithInformers(
return factory.New().WithInformersQueueKeysFunc(
queueKeys,
configInformers.Config().V1().ClusterVersions().Informer(),
configInformers.Config().V1().ClusterOperators().Informer(),
).ResyncEvery(time.Minute).
).ResyncEvery(10*time.Minute).
WithSync(c.sync).
ToController("ControlPlaneUpdateInformer", eventsRecorder.WithComponentSuffix("control-plane-update-informer"))
ToController("ControlPlaneUpdateInformer", eventsRecorder.WithComponentSuffix("control-plane-update-informer")), c.getControlPlaneUpdateStatus
}

func (c *controlPlaneUpdateInformer) sync(ctx context.Context, syncCtx factory.SyncContext) error {
klog.Infof("Control Plane Update Informer :: SYNC :: %s", syncCtx.QueueKey())

queueKey := syncCtx.QueueKey()

if queueKey == factory.DefaultQueueKey {
klog.Info("Control Plane Update Informer :: SYNC :: Full Relist")
return nil
}

kindName := strings.Split("/", queueKey)
if len(kindName) != 2 {
klog.Errorf("Control Plane Update Informer :: SYNC :: Invalid Queue Key %s", queueKey)
return nil
}

kind := kindName[0]
name := kindName[1]

switch kind {
case "cv":
klog.Infof("Control Plane Update Informer :: SYNC :: ClusterVersion :: %s", name)
cv, err := c.clusterVersionLister.Get(name)
if err != nil {
klog.Errorf("Control Plane Update Informer :: SYNC :: ClusterVersion :: %s :: %v", name, err)
return nil
}

cvProgressing := resourcemerge.FindOperatorStatusCondition(cv.Status.Conditions, configv1.OperatorProgressing)
if cvProgressing == nil {
klog.Errorf("Control Plane Update Informer :: SYNC :: ClusterVersion :: %s :: no Progressing condition", name)
return nil
}

c.statusLock.Lock()
defer c.statusLock.Unlock()
progressing := meta.FindStatusCondition(c.status.conditions, "UpdateProgressing")
if progressing == nil {
progressing = &metav1.Condition{Type: "UpdateProgressing"}
c.status.conditions = append(c.status.conditions, *progressing)
}

if cvProgressing.Status == configv1.ConditionTrue {
progressing.Status = metav1.ConditionTrue
progressing.Reason = "ClusterVersionProgressing"
progressing.Message = cvProgressing.Message
progressing.LastTransitionTime = cvProgressing.LastTransitionTime
if len(cv.Status.History) > 0 {
progressing.LastTransitionTime = metav1.NewTime(cv.Status.History[0].StartedTime.Time)
}
} else {
progressing.Status = metav1.ConditionFalse
progressing.Reason = "ClusterVersionNotProgressing"
progressing.Message = cvProgressing.Message
progressing.LastTransitionTime = cvProgressing.LastTransitionTime
}
case "co":
klog.Infof("Control Plane Update Informer :: SYNC :: ClusterOperator :: %s (TODO)", name)
default:
klog.Errorf("Control Plane Update Informer :: SYNC :: Invalid Kind %s", kind)
return nil
}

return nil
}

func (c *controlPlaneUpdateInformer) getControlPlaneUpdateStatus() controlPlaneUpdateStatus {
c.statusLock.Lock()
defer c.statusLock.Unlock()
return deepcopy.MustAnything(c.status).(controlPlaneUpdateStatus)
}
21 changes: 19 additions & 2 deletions pkg/updatestatus/insightscraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import (

"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/klog/v2"
)

type updateInsightScraper struct {
controlPlaneUpdateStatus controlPlaneUpdateStatus
getControlPlaneUpdateStatus func() controlPlaneUpdateStatus

recorder events.Recorder
}

func newUpdateInsightScraper(eventsRecorder events.Recorder) factory.Controller {
func newUpdateInsightScraper(getControlPlaneUpdateStatus func() controlPlaneUpdateStatus, eventsRecorder events.Recorder) factory.Controller {
c := updateInsightScraper{
recorder: eventsRecorder,
getControlPlaneUpdateStatus: getControlPlaneUpdateStatus,
recorder: eventsRecorder,
}

return factory.New().WithInformers().ResyncEvery(time.Minute).
Expand All @@ -23,5 +29,16 @@ func newUpdateInsightScraper(eventsRecorder events.Recorder) factory.Controller
}

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")
if progressing == nil {
klog.Info("Update Insight Scraper :: SYNC :: No Progressing condition found")
return nil
}

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

return nil
}
11 changes: 8 additions & 3 deletions pkg/updatestatus/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ func Run(ctx context.Context, cc *controllercmd.ControllerContext) error {

klog.Info("Run :: Created clients")

cpInformer, getControlPlaneUpdateStatus := newControlPlaneUpdateInformer(configInformers, cc.EventRecorder)
controllers := []factory.Controller{
newUpdateStatusController(configV1Alpha1Client.UpdateStatuses(), cc.EventRecorder),
newUpdateInsightScraper(cc.EventRecorder),
newControlPlaneUpdateInformer(configInformers, cc.EventRecorder),
newUpdateInsightScraper(getControlPlaneUpdateStatus, cc.EventRecorder),
cpInformer,
newWorkerPoolsUpdateInformer(coreInformers, mcfgInformers, cc.EventRecorder),
}

for _, controller := range controllers {
configInformers.Start(ctx.Done())
coreInformers.Start(ctx.Done())
mcfgInformers.Start(ctx.Done())

for _, controller := range controllers[2:3] {
go controller.Run(ctx, 1)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/updatestatus/updatestatuscontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package updatestatus

import (
"context"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"

configv1alpha1 "github.com/openshift/api/config/v1alpha1"
configclientv1alpha1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1alpha1"
Expand All @@ -23,11 +25,12 @@ func newUpdateStatusController(updateStatusClient configclientv1alpha1.UpdateSta
recorder: eventsRecorder,
}

return factory.New().WithSync(c.sync).ToController("UpdateStatusController", eventsRecorder.WithComponentSuffix("update-status-controller"))
return factory.New().WithSync(c.sync).ResyncEvery(time.Minute).ToController("UpdateStatusController", eventsRecorder.WithComponentSuffix("update-status-controller"))

}

func (c *updateStatusController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
klog.Info("Update Status Controller :: SYNC")
updateStatus, err := c.updateStatusClient.Get(ctx, "cluster", metav1.GetOptions{})
if apierrors.IsNotFound(err) {
updateStatus = &configv1alpha1.UpdateStatus{
Expand Down
2 changes: 2 additions & 0 deletions pkg/updatestatus/workerpoolsupdateinformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"k8s.io/client-go/informers"
"k8s.io/klog/v2"

mcfginformers "github.com/openshift/client-go/machineconfiguration/informers/externalversions"

Expand All @@ -31,5 +32,6 @@ func newWorkerPoolsUpdateInformer(coreInformers informers.SharedInformerFactory,
}

func (c *workerPoolsUpdateInformer) sync(ctx context.Context, syncCtx factory.SyncContext) error {
klog.Info("Worker Pools Update Informer :: SYNC")
return nil
}
16 changes: 16 additions & 0 deletions vendor/github.com/barkimedes/go-deepcopy/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions vendor/github.com/barkimedes/go-deepcopy/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/barkimedes/go-deepcopy/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ac8eeaf

Please sign in to comment.