This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit e570a930b172d4d44fbded6e266f97b21e55b5b5
Author: nferraro <[email protected]>
AuthorDate: Thu Oct 4 15:47:07 2018 +0200

    Fixed errors during redeploy
---
 pkg/stub/action/integration/deploy.go | 18 +------
 pkg/util/kubernetes/replace.go        | 97 +++++++++++++++++++++++++++++++++++
 runtime/examples/routes-rest.js       |  3 +-
 3 files changed, 100 insertions(+), 18 deletions(-)

diff --git a/pkg/stub/action/integration/deploy.go 
b/pkg/stub/action/integration/deploy.go
index c9c7f4e..9790bf8 100644
--- a/pkg/stub/action/integration/deploy.go
+++ b/pkg/stub/action/integration/deploy.go
@@ -24,8 +24,6 @@ import (
        "github.com/operator-framework/operator-sdk/pkg/sdk"
        "github.com/pkg/errors"
        "github.com/sirupsen/logrus"
-       k8serrors "k8s.io/apimachinery/pkg/api/errors"
-       "k8s.io/apimachinery/pkg/runtime"
 )
 
 // NewDeployAction create an action that handles integration deploy
@@ -56,7 +54,7 @@ func (action *deployAction) Handle(integration 
*v1alpha1.Integration) error {
                return errors.Wrap(err, "error during trait customization")
        }
        // TODO we should look for objects that are no longer present in the 
collection and remove them
-       err = action.createOrUpdateObjects(resources.Items(), integration)
+       err = kubernetes.ReplaceResources(resources.Items())
        if err != nil {
                return err
        }
@@ -67,17 +65,3 @@ func (action *deployAction) Handle(integration 
*v1alpha1.Integration) error {
 
        return sdk.Update(target)
 }
-
-func (action *deployAction) createOrUpdateObjects(objects []runtime.Object, 
integration *v1alpha1.Integration) error {
-       for _, object := range objects {
-               err := sdk.Create(object)
-               if err != nil && k8serrors.IsAlreadyExists(err) {
-                       err = sdk.Update(object)
-               }
-               if err != nil {
-                       return errors.Wrap(err, "could not create or replace 
resource for integration "+integration.Name)
-               }
-       }
-
-       return nil
-}
diff --git a/pkg/util/kubernetes/replace.go b/pkg/util/kubernetes/replace.go
new file mode 100644
index 0000000..45b908d
--- /dev/null
+++ b/pkg/util/kubernetes/replace.go
@@ -0,0 +1,97 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package kubernetes
+
+import (
+       routev1 "github.com/openshift/api/route/v1"
+       "github.com/operator-framework/operator-sdk/pkg/sdk"
+       "github.com/pkg/errors"
+       corev1 "k8s.io/api/core/v1"
+       k8serrors "k8s.io/apimachinery/pkg/api/errors"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+       "k8s.io/apimachinery/pkg/runtime"
+)
+
+// ReplaceResources allows to completely replace a list of resources on 
Kubernetes, taking care of immutable fields and resource versions
+func ReplaceResources(objects []runtime.Object) error {
+       for _, object := range objects {
+               err := ReplaceResource(object)
+               if err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+// ReplaceResource allows to completely replace a resource on Kubernetes, 
taking care of immutable fields and resource versions
+func ReplaceResource(res runtime.Object) error {
+       err := sdk.Create(res)
+       if err != nil && k8serrors.IsAlreadyExists(err) {
+               existing := res.DeepCopyObject()
+               err = sdk.Get(existing)
+               if err != nil {
+                       return err
+               }
+               mapRequiredMeta(existing, res)
+               mapRequiredServiceData(existing, res)
+               mapRequiredRouteData(existing, res)
+               err = sdk.Update(res)
+       }
+       if err != nil {
+               return errors.Wrap(err, "could not create or replace 
"+findResourceDetails(res))
+       }
+       return nil
+}
+
+func mapRequiredMeta(from runtime.Object, to runtime.Object) {
+       if fromC, ok := from.(metav1.Object); ok {
+               if toC, ok := to.(metav1.Object); ok {
+                       toC.SetResourceVersion(fromC.GetResourceVersion())
+               }
+       }
+}
+
+func mapRequiredServiceData(from runtime.Object, to runtime.Object) {
+       if fromC, ok := from.(*corev1.Service); ok {
+               if toC, ok := to.(*corev1.Service); ok {
+                       toC.Spec.ClusterIP = fromC.Spec.ClusterIP
+               }
+       }
+}
+
+func mapRequiredRouteData(from runtime.Object, to runtime.Object) {
+       if fromC, ok := from.(*routev1.Route); ok {
+               if toC, ok := to.(*routev1.Route); ok {
+                       toC.Spec.Host = fromC.Spec.Host
+               }
+       }
+}
+
+func findResourceDetails(res runtime.Object) string {
+       if res == nil {
+               return "nil resource"
+       }
+       if meta, ok := res.(metav1.Object); ok {
+               name := meta.GetName()
+               if ty, ok := res.(metav1.Type); ok {
+                       return ty.GetKind() + " " + name
+               }
+               return "resource " + name
+       }
+       return "unnamed resource"
+}
diff --git a/runtime/examples/routes-rest.js b/runtime/examples/routes-rest.js
index e888019..97d41ec 100644
--- a/runtime/examples/routes-rest.js
+++ b/runtime/examples/routes-rest.js
@@ -17,7 +17,7 @@ l.exchangeFormatter = function(e) {
 
 c = restConfiguration()
 c.component = 'undertow'
-c.port = 8081
+c.port = 8080
 
 // ****************
 //
@@ -37,6 +37,7 @@ function proc(e) {
 
 rest()
     .path('/say/hello')
+    .produces("text/plain")
     .get().route()
         .transform().constant("Hello World");
 

Reply via email to