Skip to content

Commit

Permalink
copy "dummy" CSVs to target namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Peeler committed Oct 2, 2018
1 parent 7d606c2 commit 5d3c426
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const (
CSVReasonComponentUnhealthy ConditionReason = "ComponentUnhealthy"
CSVReasonBeingReplaced ConditionReason = "BeingReplaced"
CSVReasonReplaced ConditionReason = "Replaced"
CSVReasonCopied ConditionReason = "Copied"
)

// Conditions appear in the status as a record of state transitions on the ClusterServiceVersion
Expand Down
56 changes: 40 additions & 16 deletions pkg/controller/operators/olm/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,23 +298,23 @@ func namespacesChanged(clusterNamespaces []corev1.Namespace, statusNamespaces []
return false
}

func (a *Operator) updateDeploymentAnnotation(op *v1alpha2.OperatorGroup) error {
func (a *Operator) updateDeploymentAnnotation(op *v1alpha2.OperatorGroup) (error, []corev1.Namespace) {
// NOTE: if a CSV modification is required in the future, copy the original
// data as done in the bottom of this method first.

selector, err := metav1.LabelSelectorAsSelector(&op.Spec.Selector)
if err != nil {
return err
return err, nil
}
operatorGroupOpts := metav1.ListOptions{LabelSelector: selector.String()}
namespaceList, err := a.OpClient.KubernetesInterface().CoreV1().Namespaces().List(operatorGroupOpts)
if err != nil {
return err
return err, nil
}

if !namespacesChanged(namespaceList.Items, op.Status.Namespaces) {
// status is current with correct namespaces, so no further updates required
return nil
return nil, namespaceList.Items
}
op.Status.Namespaces = namespaceList.Items
op.Status.LastUpdated = timeNow()
Expand All @@ -324,12 +324,12 @@ func (a *Operator) updateDeploymentAnnotation(op *v1alpha2.OperatorGroup) error
for csvName, csv := range csvsInNamespace {
strategy, err := a.resolver.UnmarshalStrategy(csv.Spec.InstallStrategy)
if err != nil {
return fmt.Errorf("error unmarshaling strategy from ClusterServiceVersion '%s' with error: %s", csvName, err)
return fmt.Errorf("error unmarshaling strategy from ClusterServiceVersion '%s' with error: %s", csvName, err), namespaceList.Items
}

strategyDetailsDeployment, ok := strategy.(*install.StrategyDetailsDeployment)
if !ok {
return fmt.Errorf("could not assert strategy implementation as deployment for CSV %s", csvName)
return fmt.Errorf("could not assert strategy implementation as deployment for CSV %s", csvName), namespaceList.Items
}

managerPolicyRules := []rbacv1.PolicyRule{}
Expand Down Expand Up @@ -358,7 +358,7 @@ func (a *Operator) updateDeploymentAnnotation(op *v1alpha2.OperatorGroup) error
clusterRole.SetGenerateName(fmt.Sprintf("owned-crd-manager-%s-", csv.Spec.DisplayName))
_, err = a.OpClient.KubernetesInterface().RbacV1().ClusterRoles().Create(clusterRole)
if err != nil {
return err
return err, namespaceList.Items
}

// operator group specific roles
Expand All @@ -370,7 +370,7 @@ func (a *Operator) updateDeploymentAnnotation(op *v1alpha2.OperatorGroup) error
}
_, err = a.OpClient.KubernetesInterface().RbacV1().ClusterRoles().Create(operatorGroupEditClusterRole)
if err != nil {
return err
return err, namespaceList.Items
}
operatorGroupViewClusterRole := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -380,7 +380,7 @@ func (a *Operator) updateDeploymentAnnotation(op *v1alpha2.OperatorGroup) error
}
_, err = a.OpClient.KubernetesInterface().RbacV1().ClusterRoles().Create(operatorGroupViewClusterRole)
if err != nil {
return err
return err, namespaceList.Items
}

var nsList []string
Expand All @@ -391,27 +391,27 @@ func (a *Operator) updateDeploymentAnnotation(op *v1alpha2.OperatorGroup) error
for _, deploy := range strategyDetailsDeployment.DeploymentSpecs {
originalData, err := json.Marshal(csv)
if err != nil {
return err
return err, namespaceList.Items
}
deploy.Spec.Template.Annotations["olm.targetNamespaces"] = strings.Join(nsList, ",")
modifiedData, err := json.Marshal(csv)
if err != nil {
return err
return err, namespaceList.Items
}
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(originalData, modifiedData, v1alpha1.ClusterServiceVersion{})
if err != nil {
return err
return err, namespaceList.Items
}

_, err = a.client.Operators().ClusterServiceVersions(currentNamespace).Patch(csvName, types.StrategicMergePatchType, patchBytes)
if err != nil {
return fmt.Errorf("CSV update for '%v' failed: %v\n", csvName, err)
return fmt.Errorf("CSV update for '%v' failed: %v\n", csvName, err), namespaceList.Items
}
//a.requeueCSV(csvName, currentNamespace)
}
}

return nil
return nil, namespaceList.Items
}

func (a *Operator) syncOperatorGroups(obj interface{}) error {
Expand All @@ -421,10 +421,34 @@ func (a *Operator) syncOperatorGroups(obj interface{}) error {
return fmt.Errorf("casting OperatorGroup failed")
}

if err := a.updateDeploymentAnnotation(op); err != nil {
err, targetedNamespaces := a.updateDeploymentAnnotation(op)
if err != nil {
return err
}

for _, ns := range targetedNamespaces {
csvsInNamespace := a.csvsInNamespace(ns.Name)
for _, csv := range csvsInNamespace {
if csv.Status.Phase == v1alpha1.CSVPhaseSucceeded {
newCSV := csv.DeepCopy()
copiedStatus := v1alpha1.ClusterServiceVersionStatus{
Message: "CSV copied to target namespace",
Reason: v1alpha1.CSVReasonCopied,
LastUpdateTime: timeNow(),
}
newCSV.Status = copiedStatus
newCSV.Annotations["OriginalCSV"] = fmt.Sprintf("Namespace:%v, ResourceVersion:%v", csv.GetNamespace(), csv.GetResourceVersion())
ownerutil.AddNonBlockingOwner(newCSV, csv)
if newCSV.GetNamespace() != ns.Name {
_, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(newCSV.GetNamespace()).Create(newCSV)
if err != nil {
return err
}
}
}
}
}

return nil
}

Expand Down Expand Up @@ -750,7 +774,7 @@ func (a *Operator) syncNamespace(obj interface{}) (syncError error) {
}

for op := range opGroupUpdate {
if err := a.updateDeploymentAnnotation(op); err != nil {
if err, _ := a.updateDeploymentAnnotation(op); err != nil {
return err
}
}
Expand Down

0 comments on commit 5d3c426

Please sign in to comment.