From 8b06b206d2e4f35c055e1a1e2c4a3e87df04ed8c Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Thu, 11 Jun 2020 12:35:39 -0400 Subject: [PATCH] fix: k8s trigger operations (#695) --- sensors/triggers/standard-k8s/starndard-k8s.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/sensors/triggers/standard-k8s/starndard-k8s.go b/sensors/triggers/standard-k8s/starndard-k8s.go index 339b5de76b..5fa3badad0 100644 --- a/sensors/triggers/standard-k8s/starndard-k8s.go +++ b/sensors/triggers/standard-k8s/starndard-k8s.go @@ -26,6 +26,7 @@ import ( "github.com/imdario/mergo" "github.com/pkg/errors" "github.com/sirupsen/logrus" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -163,8 +164,11 @@ func (k8sTrigger *StandardK8sTrigger) Execute(resource interface{}) (interface{} k8sTrigger.Logger.Infoln("updating the object...") oldObj, err := k8sTrigger.namespableDynamicClient.Namespace(namespace).Get(obj.GetName(), metav1.GetOptions{}) - if err != nil { - return nil, err + if err != nil && apierrors.IsNotFound(err) { + k8sTrigger.Logger.Infoln("object not found, creating the object...") + return k8sTrigger.namespableDynamicClient.Namespace(namespace).Create(obj, metav1.CreateOptions{}) + } else if err != nil { + return nil, errors.Errorf("failed to retrieve existing object. err: %+v\n", err) } if err := mergo.Merge(oldObj, obj, mergo.WithOverride); err != nil { @@ -176,6 +180,14 @@ func (k8sTrigger *StandardK8sTrigger) Execute(resource interface{}) (interface{} case v1alpha1.Patch: k8sTrigger.Logger.Infoln("patching the object...") + _, err := k8sTrigger.namespableDynamicClient.Namespace(namespace).Get(obj.GetName(), metav1.GetOptions{}) + if err != nil && apierrors.IsNotFound(err) { + k8sTrigger.Logger.Infoln("object not found, creating the object...") + return k8sTrigger.namespableDynamicClient.Namespace(namespace).Create(obj, metav1.CreateOptions{}) + } else if err != nil { + return nil, errors.Errorf("failed to retrieve existing object. err: %+v\n", err) + } + if k8sTrigger.Trigger.Template.K8s.PatchStrategy == "" { k8sTrigger.Trigger.Template.K8s.PatchStrategy = k8stypes.MergePatchType }