Skip to content

Commit

Permalink
Fiddle with main controlelr
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-muller committed Jul 19, 2024
1 parent ac8eeaf commit 95c06fc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
17 changes: 12 additions & 5 deletions pkg/updatestatus/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"context"
"time"

configv1alpha1 "github.com/openshift/api/config/v1alpha1"
configv1client "github.com/openshift/client-go/config/clientset/versioned"
configv1alpha1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1alpha1"
configinformers "github.com/openshift/client-go/config/informers/externalversions"
mcfgclient "github.com/openshift/client-go/machineconfiguration/clientset/versioned"
mcfginformers "github.com/openshift/client-go/machineconfiguration/informers/externalversions"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
coreinformers "k8s.io/client-go/informers"
corev1client "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
Expand All @@ -17,6 +20,12 @@ import (
"github.com/openshift/library-go/pkg/controller/factory"
)

var cfgScheme = runtime.NewScheme()

func init() {
utilruntime.Must(configv1alpha1.Install(cfgScheme))
}

func Run(ctx context.Context, cc *controllercmd.ControllerContext) error {
configV1Alpha1Client, err := configv1alpha1client.NewForConfig(cc.KubeConfig)
if err != nil {
Expand Down Expand Up @@ -44,9 +53,9 @@ func Run(ctx context.Context, cc *controllercmd.ControllerContext) error {

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

usc := newUpdateStatusController(configV1Alpha1Client.UpdateStatuses(), cc.EventRecorder)
cpInformer, getControlPlaneUpdateStatus := newControlPlaneUpdateInformer(configInformers, cc.EventRecorder)
controllers := []factory.Controller{
newUpdateStatusController(configV1Alpha1Client.UpdateStatuses(), cc.EventRecorder),
_ = []factory.Controller{
newUpdateInsightScraper(getControlPlaneUpdateStatus, cc.EventRecorder),
cpInformer,
newWorkerPoolsUpdateInformer(coreInformers, mcfgInformers, cc.EventRecorder),
Expand All @@ -56,9 +65,7 @@ func Run(ctx context.Context, cc *controllercmd.ControllerContext) error {
coreInformers.Start(ctx.Done())
mcfgInformers.Start(ctx.Done())

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

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

Expand Down
58 changes: 52 additions & 6 deletions pkg/updatestatus/updatestatuscontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
type updateStatusController struct {
updateStatusClient configclientv1alpha1.UpdateStatusInterface
recorder events.Recorder

// updateStatus is the resource instance local to the controller, on which the USC maintains
// the most recently observed state. This local status is propagated to the cluster by this controller
// when it changes.
updateStatus *configv1alpha1.UpdateStatus
}

func newUpdateStatusController(updateStatusClient configclientv1alpha1.UpdateStatusInterface, eventsRecorder events.Recorder) factory.Controller {
Expand All @@ -30,17 +35,58 @@ func newUpdateStatusController(updateStatusClient configclientv1alpha1.UpdateSta
}

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) {
klog.Info("USC :: SYNC")
var exists bool
updateStatus, err := c.updateStatusClient.Get(ctx, "another-cluster", metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
exists = err == nil

if c.updateStatus == nil && exists {
c.updateStatus = updateStatus
return nil
}

if c.updateStatus == nil && !exists {
updateStatus = &configv1alpha1.UpdateStatus{
// TypeMeta: metav1.TypeMeta{
// Kind: "UpdateStatus",
// APIVersion: "config.openshift.io/v1alpha1",
// },
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
Name: "another-cluster",
},
Spec: configv1alpha1.UpdateStatusSpec{},
Status: configv1alpha1.UpdateStatusStatus{
ControlPlane: configv1alpha1.ControlPlaneUpdateStatus{
Summary: configv1alpha1.ControlPlaneUpdateStatusSummary{
Assessment: "aaaa",
Versions: configv1alpha1.ControlPlaneUpdateVersions{},
Completion: 0,
StartedAt: metav1.Time{},
CompletedAt: metav1.Time{},
EstimatedCompletedAt: metav1.Time{},
},
Informers: []configv1alpha1.UpdateInformer{
{
Name: "YADA",
},
},
Conditions: nil,
},
WorkerPools: nil,
Conditions: nil,
},
}
updateStatus, err := c.updateStatusClient.Create(ctx, updateStatus, metav1.CreateOptions{})
if err == nil {
c.updateStatus = updateStatus
syncCtx.Queue().AddRateLimited(syncCtx.QueueKey())
}
_, err := c.updateStatusClient.Create(ctx, updateStatus, metav1.CreateOptions{})
return err
}

return nil
_, err = c.updateStatusClient.UpdateStatus(ctx, c.updateStatus, metav1.UpdateOptions{})
return err
}

0 comments on commit 95c06fc

Please sign in to comment.