Skip to content

Commit

Permalink
sync addonconfiguration controller code from ocm
Browse files Browse the repository at this point in the history
Signed-off-by: haoqing0110 <qhao@redhat.com>
  • Loading branch information
haoqing0110 committed Sep 18, 2023
1 parent 9676b4f commit 08e4f8d
Show file tree
Hide file tree
Showing 15 changed files with 935 additions and 590 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module open-cluster-management.io/addon-framework

go 1.20

replace open-cluster-management.io/api => github.com/haoqing0110/api v0.0.0-20230913055320-e3f65e9707c7

require (
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/fatih/structs v1.1.0
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,16 @@ 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=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
github.com/haoqing0110/api v0.0.0-20230913055320-e3f65e9707c7 h1:DTrVePAMwvoTxLojJAl6pJlsTyXSfF1Z3X+gk38Luno=
github.com/haoqing0110/api v0.0.0-20230913055320-e3f65e9707c7/go.mod h1:/CZhelEH+30/pX7vXGSZOzLMX0zvjthYOkT/5ZTzVTQ=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down Expand Up @@ -819,8 +821,6 @@ 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=
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 08e4f8d

Please sign in to comment.