Skip to content

Commit

Permalink
Update with dry-run before diffing deployments (kserve#2490)
Browse files Browse the repository at this point in the history
* Use dry-run to detect differences between calculated and actual deployment

Signed-off-by: Curtis Maddalozzo <[email protected]>

* Add deployment name to error log

Signed-off-by: Curtis Maddalozzo <[email protected]>

Signed-off-by: Curtis Maddalozzo <[email protected]>
  • Loading branch information
cmaddalozzo authored Nov 18, 2022
1 parent 9139c07 commit 377a39a
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"knative.dev/pkg/kmp"
"sigs.k8s.io/controller-runtime/pkg/client"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

var log = logf.Log.WithName("DeploymentReconciler")

// DeploymentReconciler reconciles the raw kubernetes deployment resource
type DeploymentReconciler struct {
client client.Client
client kclient.Client
scheme *runtime.Scheme
Deployment *appsv1.Deployment
componentExt *v1beta1.ComponentExtensionSpec
}

func NewDeploymentReconciler(client client.Client,
func NewDeploymentReconciler(client kclient.Client,
scheme *runtime.Scheme,
componentMeta metav1.ObjectMeta,
componentExt *v1beta1.ComponentExtensionSpec,
Expand Down Expand Up @@ -82,7 +82,7 @@ func createRawDeployment(componentMeta metav1.ObjectMeta,
}

// checkDeploymentExist checks if the deployment exists?
func (r *DeploymentReconciler) checkDeploymentExist(client client.Client) (constants.CheckResultType, *appsv1.Deployment, error) {
func (r *DeploymentReconciler) checkDeploymentExist(client kclient.Client) (constants.CheckResultType, *appsv1.Deployment, error) {
//get deployment
existingDeployment := &appsv1.Deployment{}
err := client.Get(context.TODO(), types.NamespacedName{
Expand All @@ -98,6 +98,12 @@ func (r *DeploymentReconciler) checkDeploymentExist(client client.Client) (const
//existed, check equivalence
//for HPA scaling, we should ignore Replicas of Deployment
ignoreFields := cmpopts.IgnoreFields(appsv1.DeploymentSpec{}, "Replicas")
// Do a dry-run update. This will populate our local deployment object with any default values
// that are present on the remote version.
if err := client.Update(context.TODO(), r.Deployment, kclient.DryRunAll); err != nil {
log.Error(err, "Failed to perform dry-run update of deployment", "Deployment", r.Deployment.Name)
return constants.CheckResultUnknown, nil, err
}
if diff, err := kmp.SafeDiff(r.Deployment.Spec, existingDeployment.Spec, ignoreFields); err != nil {
return constants.CheckResultUnknown, nil, err
} else if diff != "" {
Expand Down

0 comments on commit 377a39a

Please sign in to comment.