Some time creating a Resource(ex. development) lead to ERROR "Resource is already exist"
DeamonMV opened this issue · comments
DeamonMV commented
Code check if Deployment exists
founddep := &appsv1.Deployment{}
log.Info("Before Deployment NAME:")
log.Info(webpage.Name)
err = r.Get(ctx, types.NamespacedName{Name: webpage.Name, Namespace: webpage.Namespace}, founddep)
if err != nil && apierrors.IsNotFound(err) {
log.Info("CREATE DEPLOYMENT 111:")
log.Info(err.Error())
// Define a new deployment
dep, err := r.deploymentForWebpage(webpage)
And then here ERROR appears
log.Info("Creating a new Deployment",
"Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
// Re-fetch the webpage Custom Resource before update the status
// so that we have the latest state of the resource on the cluster, and we will avoid
// raise the issue "the object has been modified, please apply
// your changes to the latest version and try again" which would re-trigger the reconciliation
if err := r.Get(ctx, req.NamespacedName, webpage); err != nil {
log.Error(err, "Failed to re-fetch webpage")
return ctrl.Result{}, err
}
if err = r.Create(ctx, dep); err != nil {
log.Error(err, "Failed to create new Deployment",
"Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
return ctrl.Result{}, err
}
Log:
1.6766570220461705e+09 INFO Before Deployment NAME: {"controller": "static", "controllerGroup": "webpage.daemon.io", "controllerKind": "Static", "Static": {"name":"sample-1","namespace":"static-webpage-k8s-operator-system"}, "namespace": "static-webpage-k8s-operator-system", "name": "sample-1", "reconcileID": "9e208d0c-3900-47e9-a3a2-b999367d77b2"}
1.6766570220462317e+09 INFO sample-1 {"controller": "static", "controllerGroup": "webpage.daemon.io", "controllerKind": "Static", "Static": {"name":"sample-1","namespace":"static-webpage-k8s-operator-system"}, "namespace": "static-webpage-k8s-operator-system", "name": "sample-1", "reconcileID": "9e208d0c-3900-47e9-a3a2-b999367d77b2"}
1.6766570220462468e+09 INFO CREATE DEPLOYMENT 111: {"controller": "static", "controllerGroup": "webpage.daemon.io", "controllerKind": "Static", "Static": {"name":"sample-1","namespace":"static-webpage-k8s-operator-system"}, "namespace": "static-webpage-k8s-operator-system", "name": "sample-1", "reconcileID": "9e208d0c-3900-47e9-a3a2-b999367d77b2"}
1.6766570220462515e+09 INFO Deployment.apps "sample-1" not found {"controller": "static", "controllerGroup": "webpage.daemon.io", "controllerKind": "Static", "Static": {"name":"sample-1","namespace":"static-webpage-k8s-operator-system"}, "namespace": "static-webpage-k8s-operator-system", "name": "sample-1", "reconcileID": "9e208d0c-3900-47e9-a3a2-b999367d77b2"}
1.6766570220464165e+09 INFO Creating a new Deployment {"controller": "static", "controllerGroup": "webpage.daemon.io", "controllerKind": "Static", "Static": {"name":"sample-1","namespace":"static-webpage-k8s-operator-system"},
"namespace": "static-webpage-k8s-operator-system", "name": "sample-1", "reconcileID": "9e208d0c-3900-47e9-a3a2-b999367d77b2", "Deployment.Namespace": "static-webpage-k8s-operator-system", "Deployment.Name": "sample-1"}
1.6766570220648246e+09 ERROR Failed to create new Deployment {"controller": "static", "controllerGroup": "webpage.daemon.io", "controllerKind": "Static", "Static": {"name":"sample-1","namespace":"static-webpage-k8s-operator-system"},
"namespace": "static-webpage-k8s-operator-system", "name": "sample-1", "reconcileID": "9e208d0c-3900-47e9-a3a2-b999367d77b2", "Deployment.Namespace": "static-webpage-k8s-operator-system", "Deployment.Name": "sample-1", "error": "deployments.apps \"sample-1\" already exists"}
github.com/DeamonMV/static-webpage-k8s-operator/controllers.(*StaticReconciler).Reconcile
/workspace/controllers/static_controller.go:269
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Reconcile
/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.13.0/pkg/internal/controller/controller.go:121
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler
/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.13.0/pkg/internal/controller/controller.go:320
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem
/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.13.0/pkg/internal/controller/controller.go:273
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2
/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.13.0/pkg/internal/controller/controller.go:234