Memoize Workload.Deployment() result
so that we can call it multiple times with no cost.
@@ -34,6 +34,7 @@ type AppServiceBindList []AppServiceBind
// Workload manages applications that are deployed. It provides workload
// (deployments) specific actions for the application model.
type Workload struct {
+ deployment *appsv1.Deployment // memoization
app models.AppRef
cluster *kubernetes.Cluster
}
@@ -300,10 +301,16 @@ func (a *Workload) Restart(ctx context.Context) error {
}
// Deployment is a helper, it returns the kube deployment resource of the workload.
+// The result is memoized so that subsequent calls to this method, don't call
+// the kubernetes api.
func (a *Workload) Deployment(ctx context.Context) (*appsv1.Deployment, error) {
- return a.cluster.Kubectl.AppsV1().Deployments(a.app.Namespace).Get(
- ctx, a.app.Name, metav1.GetOptions{},
- )
+ var err error
+ if a.deployment == nil {
+ a.deployment, err = a.cluster.Kubectl.AppsV1().
+ Deployments(a.app.Namespace).Get(ctx, a.app.Name, metav1.GetOptions{})
+ }
+
+ return a.deployment, err
}
// Pods is a helper, it returns the Pods belonging to the Deployment of the workload.
