Skip to content

Commit

Permalink
[wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
tkashem committed Jun 26, 2019
1 parent 6bf64d0 commit 22ce783
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion pkg/controller/operators/olm/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
}

// Check for APIServices ownership conflicts
if syncError = a.apiServiceOwnerConflicts(out); syncError != nil {
if syncError = a.apiServiceOwnerConflictsNew(out); syncError != nil {
if syncError == ErrAPIServiceOwnerConflict {
out.SetPhaseWithEvent(v1alpha1.CSVPhaseFailed, v1alpha1.CSVReasonOwnerConflict, syncError.Error(), now, a.recorder)
}
Expand Down Expand Up @@ -1416,6 +1416,73 @@ func (a *Operator) apiServiceOwnerConflicts(csv *v1alpha1.ClusterServiceVersion)
return nil
}

func (a *Operator) apiServiceOwnerConflictsNew(csv *v1alpha1.ClusterServiceVersion) error {
// Get replacing CSV if exists
replacing, err := a.lister.OperatorsV1alpha1().ClusterServiceVersionLister().ClusterServiceVersions(csv.GetNamespace()).Get(csv.Spec.Replaces)
if err != nil && !k8serrors.IsNotFound(err) && !k8serrors.IsGone(err) {
return err
}

owners := []ownerutil.Owner{csv}
if replacing != nil {
owners = append(owners, replacing)
}

for _, desc := range csv.GetOwnedAPIServiceDescriptions() {
// Check if the APIService exists
apiService, err := a.lister.APIRegistrationV1().APIServiceLister().Get(desc.GetName())
if err != nil && !k8serrors.IsNotFound(err) && !k8serrors.IsGone(err) {
return err
}

if apiService == nil {
continue
}

labels := apiService.GetLabels()
ownerKind := labels[ownerutil.OwnerKind]
ownerName := labels[ownerutil.OwnerKey]
ownerNamespace := labels[ownerutil.OwnerNamespaceKey]

if ownerKind == "" || ownerNamespace == "" || ownerName == "" {
// If owner is set, do we always expect all three labels to be set?
// Question: Can a cluster scoped object ever own an APIService?
return ErrAPIServiceOwnerConflict
}

// Is it owned by this csv or the one it replaces.
if ownerutil.AdoptableLabels(apiService.GetLabels(), true, owners...) {
continue
}

// If we are here, it is owned by a separate resource
targetKind := csv.GetObjectKind().GroupVersionKind().Kind
if ownerKind == targetKind {
currentOwnerCSV, err := a.lister.OperatorsV1alpha1().ClusterServiceVersionLister().ClusterServiceVersions(ownerNamespace).Get(ownerName)
if err != nil {
if !k8serrors.IsNotFound(err) && !k8serrors.IsGone(err) {
return err
}

// The current owner CSV no longer exists, so we can adopt the APIService
continue
}

if currentOwnerCSV.Status.Phase == v1alpha1.CSVPhaseReplacing ||
currentOwnerCSV.Status.Phase == v1alpha1.CSVPhaseDeleting {
continue
}
}

// Note: We are not handling the case that an APIService may be owned by a different entity.
// If we are here, some other entity actively owns the APIService object.
// We can't adopt it, it's a conflict.
return ErrAPIServiceOwnerConflict
}

return nil
}

func (a *Operator) isBeingReplaced(in *v1alpha1.ClusterServiceVersion, csvsInNamespace map[string]*v1alpha1.ClusterServiceVersion) (replacedBy *v1alpha1.ClusterServiceVersion) {
return a.csvReplaceFinder.IsBeingReplaced(in, csvsInNamespace)
}
Expand Down

0 comments on commit 22ce783

Please sign in to comment.