Skip to content

Commit f635163

Browse files
authored
Handle k8s resource conflict due to same CR names (#475)
* Add utility method to look for existing deployments * Add previous deployments check * Update dev.sh bundle push * Code review updates
1 parent 577bd52 commit f635163

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

controllers/runtimecomponent_controller.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
135135
return reconcile.Result{}, err
136136
}
137137

138+
isKnativeSupported, err := r.IsGroupVersionSupported(servingv1.SchemeGroupVersion.String(), "Service")
139+
if err != nil {
140+
r.ManageError(err, common.StatusConditionTypeReconciled, instance)
141+
} else if !isKnativeSupported {
142+
reqLogger.V(1).Info(fmt.Sprintf("%s is not supported on the cluster", servingv1.SchemeGroupVersion.String()))
143+
}
144+
145+
// Check if there is an existing Deployment, Statefulset or Knative service by this name
146+
// not managed by this operator
147+
err = appstacksutils.CheckForNameConflicts("RuntimeComponent", instance.Name, instance.Namespace, r.GetClient(), req, isKnativeSupported)
148+
if err != nil {
149+
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
150+
}
151+
138152
// initialize the RuntimeComponent instance
139153
instance.Initialize()
140154
_, err = appstacksutils.Validate(instance)
@@ -227,13 +241,6 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
227241
return r.ManageError(saErr, common.StatusConditionTypeReconciled, instance)
228242
}
229243

230-
isKnativeSupported, err := r.IsGroupVersionSupported(servingv1.SchemeGroupVersion.String(), "Service")
231-
if err != nil {
232-
r.ManageError(err, common.StatusConditionTypeReconciled, instance)
233-
} else if !isKnativeSupported {
234-
reqLogger.V(1).Info(fmt.Sprintf("%s is not supported on the cluster", servingv1.SchemeGroupVersion.String()))
235-
}
236-
237244
if instance.Spec.CreateKnativeService != nil && *instance.Spec.CreateKnativeService {
238245
// Clean up non-Knative resources
239246
resources := []client.Object{

scripts/dev.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ bundle() {
268268
echo "------------"
269269
echo "bundle-push"
270270
echo "------------"
271-
make -C $MAKEFILE_DIR bundle-push PUBLISH_REGISTRY=$OCP_REGISTRY_URL BUNDLE_IMG=$NAMESPACE/$OPERATOR_NAME-bundle:$VVERSION TLS_VERIFY=false
271+
make -C $MAKEFILE_DIR bundle-push VERSION=$VVERSION IMG=$IMG IMAGE_TAG_BASE=$IMAGE_TAG_BASE BUNDLE_IMG=$BUNDLE_IMG CATALOG_IMG=$CATALOG_IMG TLS_VERIFY=false
272272
}
273273

274274
###################################

utils/utils.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"k8s.io/apimachinery/pkg/types"
3939
"k8s.io/apimachinery/pkg/util/intstr"
4040
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
41+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4142
)
4243

4344
var APIVersionNotFoundError = errors.New("APIVersion is not available")
@@ -1570,3 +1571,49 @@ func GetIssuerResourceVersion(client client.Client, certificate *certmanagerv1.C
15701571
return issuer.ResourceVersion, nil
15711572
}
15721573
}
1574+
1575+
func CheckForNameConflicts(operator string, name string, namespace string, client client.Client, request reconcile.Request, isKnativeSupported bool) error {
1576+
defaultMeta := metav1.ObjectMeta{
1577+
Name: name,
1578+
Namespace: namespace,
1579+
}
1580+
// Check to see if the knative service is owned by us
1581+
if isKnativeSupported {
1582+
ksvc := &servingv1.Service{ObjectMeta: defaultMeta}
1583+
err := client.Get(context.TODO(), request.NamespacedName, ksvc)
1584+
if err == nil {
1585+
for _, element := range ksvc.OwnerReferences {
1586+
if element.Kind != operator {
1587+
message := "Existing Knative Service \"" + name + "\" is not managed by this operator. It is being managed by \"" + element.Kind + "\". Resolve the naming conflict."
1588+
err = errors.New(message)
1589+
return err
1590+
}
1591+
}
1592+
}
1593+
}
1594+
// Check to see if the existing deployment or statefulsets are owned by us
1595+
deploy := &appsv1.Deployment{ObjectMeta: defaultMeta}
1596+
err := client.Get(context.TODO(), request.NamespacedName, deploy)
1597+
if err == nil {
1598+
for _, element := range deploy.OwnerReferences {
1599+
if element.Kind != operator {
1600+
message := "Existing Deployment \"" + name + "\" is not managed by this operator. It is being managed by \"" + element.Kind + "\". Resolve the naming conflict."
1601+
err = errors.New(message)
1602+
return err
1603+
}
1604+
}
1605+
}
1606+
// Check to see if the existing statefulset is owned by us
1607+
statefulSet := &appsv1.StatefulSet{ObjectMeta: defaultMeta}
1608+
err = client.Get(context.TODO(), request.NamespacedName, statefulSet)
1609+
if err == nil {
1610+
for _, element := range statefulSet.OwnerReferences {
1611+
if element.Kind != operator {
1612+
message := "Existing StatefulSet \"" + name + "\" is not managed by this operator. It is being managed by \"" + element.Kind + "\". Resolve the naming conflict."
1613+
err = errors.New(message)
1614+
return err
1615+
}
1616+
}
1617+
}
1618+
return nil
1619+
}

0 commit comments

Comments
 (0)