Skip to content

Commit

Permalink
Use Patch instead of Update for ensuring ingress resources;
Browse files Browse the repository at this point in the history
  • Loading branch information
ycliuhw committed Apr 14, 2021
1 parent 9f34465 commit c843c4c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
19 changes: 17 additions & 2 deletions caas/kubernetes/provider/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (
networkingv1beta1 "k8s.io/api/networking/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8slabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
k8stypes "k8s.io/apimachinery/pkg/types"

"github.com/juju/juju/caas/kubernetes/provider/constants"
"github.com/juju/juju/caas/kubernetes/provider/resources"
k8sspecs "github.com/juju/juju/caas/kubernetes/provider/specs"
"github.com/juju/juju/caas/kubernetes/provider/utils"
k8sannotations "github.com/juju/juju/core/annotations"
Expand Down Expand Up @@ -90,7 +93,13 @@ func (k *kubernetesClient) ensureIngressV1beta1(appName string, spec *networking
return cleanUp, errors.NewAlreadyExists(nil, fmt.Sprintf("existing ingress %q found which does not belong to %q", spec.GetName(), appName))
}
}
_, err = api.Update(context.TODO(), spec, metav1.UpdateOptions{})
data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, spec)
if err != nil {
return cleanUp, errors.Trace(err)
}
_, err = api.Patch(context.TODO(), spec.GetName(), k8stypes.StrategicMergePatchType, data, metav1.PatchOptions{
FieldManager: resources.JujuFieldManager,
})
return cleanUp, errors.Trace(err)
}

Expand All @@ -117,7 +126,13 @@ func (k *kubernetesClient) ensureIngressV1(appName string, spec *networkingv1.In
return cleanUp, errors.NewAlreadyExists(nil, fmt.Sprintf("existing ingress %q found which does not belong to %q", spec.GetName(), appName))
}
}
_, err = api.Update(context.TODO(), spec, metav1.UpdateOptions{})
data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, spec)
if err != nil {
return cleanUp, errors.Trace(err)
}
_, err = api.Patch(context.TODO(), spec.GetName(), k8stypes.StrategicMergePatchType, data, metav1.PatchOptions{
FieldManager: resources.JujuFieldManager,
})
return cleanUp, errors.Trace(err)
}

Expand Down
17 changes: 15 additions & 2 deletions caas/kubernetes/provider/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/juju/juju/caas"
Expand Down Expand Up @@ -235,11 +238,16 @@ func (s *K8sBrokerSuite) TestEnsureServiceIngressResourcesUpdateV1Beta1(c *gc.C)
Rules: []networkingv1beta1.IngressRule{ingress1Rule1},
},
}
data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, ingress)
c.Assert(err, jc.ErrorIsNil)
s.assertIngressResources(
c, IngressResources, "",
s.mockIngressV1Beta1.EXPECT().Create(gomock.Any(), ingress, metav1.CreateOptions{}).Return(nil, s.k8sAlreadyExistsError()),
s.mockIngressV1Beta1.EXPECT().Get(gomock.Any(), "test-ingress", metav1.GetOptions{}).Return(ingress, nil),
s.mockIngressV1Beta1.EXPECT().Update(gomock.Any(), ingress, metav1.UpdateOptions{}).Return(ingress, nil),
// s.mockIngressV1Beta1.EXPECT().Update(gomock.Any(), ingress, metav1.UpdateOptions{}).Return(ingress, nil),
s.mockIngressV1Beta1.EXPECT().
Patch(gomock.Any(), ingress.GetName(), k8stypes.StrategicMergePatchType, data, metav1.PatchOptions{FieldManager: "juju"}).
Return(ingress, nil),
)
}

Expand Down Expand Up @@ -430,11 +438,16 @@ func (s *K8sBrokerSuite) TestEnsureServiceIngressResourcesUpdateV1(c *gc.C) {
Rules: []networkingv1.IngressRule{ingress1Rule1},
},
}
data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, ingress)
c.Assert(err, jc.ErrorIsNil)
s.assertIngressResources(
c, IngressResources, "",
s.mockIngressV1.EXPECT().Create(gomock.Any(), ingress, gomock.Any()).Return(nil, s.k8sAlreadyExistsError()),
s.mockIngressV1.EXPECT().Get(gomock.Any(), "test-ingress", metav1.GetOptions{}).Return(ingress, nil),
s.mockIngressV1.EXPECT().Update(gomock.Any(), ingress, gomock.Any()).Return(ingress, nil),
// s.mockIngressV1.EXPECT().Update(gomock.Any(), ingress, gomock.Any()).Return(ingress, nil),
s.mockIngressV1.EXPECT().
Patch(gomock.Any(), ingress.GetName(), k8stypes.StrategicMergePatchType, data, metav1.PatchOptions{FieldManager: "juju"}).
Return(ingress, nil),
)
}

Expand Down

0 comments on commit c843c4c

Please sign in to comment.