Skip to content

Commit

Permalink
Annotate submariner broker ns on clusterSet object
Browse files Browse the repository at this point in the history
This will be used by ACM Console/UI to identify the
associated broker namespace of the ManagedClusterSet.

Related to: stolostron/backlog#19293
Signed-off-by: Sridhar Gaddam <sgaddam@redhat.com>
  • Loading branch information
sridhargaddam authored and openshift-merge-robot committed Feb 10, 2022
1 parent 2721b56 commit 93e7984
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions pkg/hub/submarinerbroker/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import (

"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/pkg/errors"
"github.com/stolostron/submariner-addon/pkg/constants"
brokerinfo "github.com/stolostron/submariner-addon/pkg/hub/submarinerbrokerinfo"
"github.com/stolostron/submariner-addon/pkg/resource"
"github.com/submariner-io/admiral/pkg/finalizer"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
clientset "open-cluster-management.io/api/client/cluster/clientset/versioned/typed/cluster/v1beta1"
clusterinformerv1beta1 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta1"
Expand All @@ -25,6 +27,7 @@ import (

const (
brokerFinalizer = "cluster.open-cluster-management.io/submariner-cleanup"
submBrokerNamespace = "cluster.open-cluster-management.io/submariner-broker-ns"
ipSecPSKSecretLength = 48
)

Expand Down Expand Up @@ -74,7 +77,7 @@ func (c *submarinerBrokerController) sync(ctx context.Context, syncCtx factory.S
klog.V(4).Infof("Reconciling ClusterSet %q", clusterSetName)

clusterSet, err := c.clusterSetLister.Get(clusterSetName)
if errors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
// ClusterSet not found, could have been deleted, do nothing.
return nil
}
Expand Down Expand Up @@ -112,12 +115,19 @@ func (c *submarinerBrokerController) sync(ctx context.Context, syncCtx factory.S
return err
}

if clusterSet.GetAnnotations()[submBrokerNamespace] != config.SubmarinerNamespace {
err = c.annotateClusterSetWithBrokerNamespace(config.SubmarinerNamespace, clusterSetName)
if err != nil {
return err
}
}

return c.createIPSecPSKSecret(config.SubmarinerNamespace)
}

func (c *submarinerBrokerController) createIPSecPSKSecret(brokerNamespace string) error {
_, err := c.kubeClient.CoreV1().Secrets(brokerNamespace).Get(context.TODO(), constants.IPSecPSKSecretName, metav1.GetOptions{})
if errors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
psk := make([]byte, ipSecPSKSecretLength)
if _, err := rand.Read(psk); err != nil {
return err
Expand All @@ -137,3 +147,31 @@ func (c *submarinerBrokerController) createIPSecPSKSecret(brokerNamespace string

return err
}

func (c *submarinerBrokerController) annotateClusterSetWithBrokerNamespace(brokerNamespace, clusterSetName string) error {
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
clusterSet, err := c.clustersetClient.Get(context.TODO(), clusterSetName, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "unable to get clusterSet info for %q", clusterSetName)
}

annotations := clusterSet.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}

annotations[submBrokerNamespace] = brokerNamespace
clusterSet.SetAnnotations(annotations)
_, updateErr := c.clustersetClient.Update(context.TODO(), clusterSet, metav1.UpdateOptions{})

return updateErr
})

if retryErr != nil {
return errors.Wrapf(retryErr, "error updating clusterSet annotation %q", clusterSetName)
}

klog.Infof("Successfully annotated clusterSet %q with brokerNamespace %q", clusterSetName, brokerNamespace)

return nil
}

0 comments on commit 93e7984

Please sign in to comment.