Skip to content

Commit

Permalink
sync addonconfiguration controller code from ocm (#208)
Browse files Browse the repository at this point in the history
Signed-off-by: haoqing0110 <qhao@redhat.com>
  • Loading branch information
haoqing0110 authored Sep 20, 2023
1 parent 9676b4f commit 65bcbb4
Show file tree
Hide file tree
Showing 15 changed files with 1,090 additions and 599 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
k8s.io/component-base v0.26.7
k8s.io/klog/v2 v2.80.1
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448
open-cluster-management.io/api v0.11.1-0.20230905055724-cf1ead467a83
open-cluster-management.io/api v0.11.1-0.20230919033310-0146ddfab71c
sigs.k8s.io/controller-runtime v0.14.4
)

Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down Expand Up @@ -819,8 +819,8 @@ k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+O
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280/go.mod h1:+Axhij7bCpeqhklhUTe3xmOn6bWxolyZEeyaFpjGtl4=
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 h1:KTgPnR10d5zhztWptI952TNtt/4u5h3IzDXkdIMuo2Y=
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
open-cluster-management.io/api v0.11.1-0.20230905055724-cf1ead467a83 h1:3zbT3sT/tEAQbpjIk6uRiTQGknQ3kQlfd11ElVuXyyQ=
open-cluster-management.io/api v0.11.1-0.20230905055724-cf1ead467a83/go.mod h1:nsQ/G5JpfjQUg7dHpblyywWC6BRqklNaF6fIswVCHyY=
open-cluster-management.io/api v0.11.1-0.20230919033310-0146ddfab71c h1:p73vRGhWgBucvoYmMHKlVjABz5SWBT0rmfzKqXnF1I0=
open-cluster-management.io/api v0.11.1-0.20230919033310-0146ddfab71c/go.mod h1:/CZhelEH+30/pX7vXGSZOzLMX0zvjthYOkT/5ZTzVTQ=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (d *managedClusterAddonConfigurationReconciler) reconcile(
ctx context.Context, cma *addonv1alpha1.ClusterManagementAddOn, graph *configurationGraph) (*addonv1alpha1.ClusterManagementAddOn, reconcileState, error) {
var errs []error

for _, addon := range graph.addonToUpdate() {
for _, addon := range graph.getAddonsToUpdate() {
mca := d.mergeAddonConfig(addon.mca, addon.desiredConfigs)
err := d.patchAddonStatus(ctx, mca, addon.mca)
if err != nil {
Expand Down

Large diffs are not rendered by default.

59 changes: 23 additions & 36 deletions pkg/manager/controllers/addonconfiguration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
addoninformerv1alpha1 "open-cluster-management.io/api/client/addon/informers/externalversions/addon/v1alpha1"
addonlisterv1alpha1 "open-cluster-management.io/api/client/addon/listers/addon/v1alpha1"
clusterinformersv1beta1 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta1"
clusterlister "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta1"
clusterlisterv1beta1 "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta1"
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"

Expand All @@ -32,6 +33,7 @@ type addonConfigurationController struct {
addonFilterFunc factory.EventFilterFunc
placementLister clusterlisterv1beta1.PlacementLister
placementDecisionLister clusterlisterv1beta1.PlacementDecisionLister
placementDecisionGetter PlacementDecisionGetter

reconcilers []addonConfigurationReconcile
}
Expand Down Expand Up @@ -93,6 +95,7 @@ func NewAddonConfigurationController(
WithInformersQueueKeysFunc(index.ClusterManagementAddonByPlacementQueueKey(clusterManagementAddonInformers), placementInformer.Informer())
c.placementLister = placementInformer.Lister()
c.placementDecisionLister = placementDecisionInformer.Lister()
c.placementDecisionGetter = PlacementDecisionGetter{Client: placementDecisionInformer.Lister()}
}

return controllerFactory.WithSync(c.sync).ToController("addon-configuration-controller")
Expand Down Expand Up @@ -125,6 +128,13 @@ func (c *addonConfigurationController) sync(ctx context.Context, syncCtx factory
return err
}

// generate the rollout result before calling reconcile()
// so that all the reconcilers are using the same rollout result
err = graph.generateRolloutResult()
if err != nil {
return err
}

var state reconcileState
var errs []error
for _, reconciler := range c.reconcilers {
Expand Down Expand Up @@ -160,50 +170,27 @@ func (c *addonConfigurationController) buildConfigurationGraph(cma *addonv1alpha
// check each install strategy in status
var errs []error
for _, installProgression := range cma.Status.InstallProgressions {
clusters, err := c.getClustersByPlacement(installProgression.PlacementRef.Name, installProgression.PlacementRef.Namespace)
if errors.IsNotFound(err) {
klog.V(2).Infof("placement %s/%s is not found for addon %s", installProgression.PlacementRef.Namespace, installProgression.PlacementRef.Name, cma.Name)
continue
}
if err != nil {
errs = append(errs, err)
continue
}

for _, installStrategy := range cma.Spec.InstallStrategy.Placements {
if installStrategy.PlacementRef == installProgression.PlacementRef {
graph.addPlacementNode(installStrategy, installProgression, clusters)
if installStrategy.PlacementRef != installProgression.PlacementRef {
continue
}

// add placement node
err = graph.addPlacementNode(installStrategy, installProgression, c.placementLister, c.placementDecisionGetter)
if err != nil {
errs = append(errs, err)
continue
}
}
}

return graph, utilerrors.NewAggregate(errs)
}

func (c *addonConfigurationController) getClustersByPlacement(name, namespace string) ([]string, error) {
var clusters []string
if c.placementLister == nil || c.placementDecisionLister == nil {
return clusters, nil
}
_, err := c.placementLister.Placements(namespace).Get(name)
if err != nil {
return clusters, err
}

decisionSelector := labels.SelectorFromSet(labels.Set{
clusterv1beta1.PlacementLabel: name,
})
decisions, err := c.placementDecisionLister.PlacementDecisions(namespace).List(decisionSelector)
if err != nil {
return clusters, err
}

for _, d := range decisions {
for _, sd := range d.Status.Decisions {
clusters = append(clusters, sd.ClusterName)
}
}
type PlacementDecisionGetter struct {
Client clusterlister.PlacementDecisionLister
}

return clusters, nil
func (pdl PlacementDecisionGetter) List(selector labels.Selector, namespace string) ([]*clusterv1beta1.PlacementDecision, error) {
return pdl.Client.PlacementDecisions(namespace).List(selector)
}
Loading

0 comments on commit 65bcbb4

Please sign in to comment.