Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 187: Webhook rejects requests when cluster is upgrading #190

Merged
merged 3 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"fmt"
"net/http"

corev1 "k8s.io/api/core/v1"

pravegav1alpha1 "github.com/pravega/pravega-operator/pkg/apis/pravega/v1alpha1"
"github.com/pravega/pravega-operator/pkg/util"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -60,6 +62,10 @@ func (pwh *pravegaWebhookHandler) Handle(ctx context.Context, req admissiontypes
}
copy := pravega.DeepCopy()

if err := pwh.clusterIsAvailable(ctx, copy); err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that, in this case, we should return a server error (5xx) instead of a user error (4xx). BadRequest is telling the user that the request is malformed, which is not the case, it's just that the cluster status does not allow this type of request temporarily.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it!

}

if err := pwh.mutatePravegaManifest(ctx, copy); err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
}
Expand Down Expand Up @@ -139,6 +145,29 @@ func (pwh *pravegaWebhookHandler) mutatePravegaVersion(ctx context.Context, p *p
return nil
}

func (pwh *pravegaWebhookHandler) clusterIsAvailable(ctx context.Context, p *pravegav1alpha1.PravegaCluster) error {
found := &pravegav1alpha1.PravegaCluster{}
nn := types.NamespacedName{
Namespace: p.Namespace,
Name: p.Name,
}
err := pwh.client.Get(context.TODO(), nn, found)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("failed to obtain PravegaCluster resource: %v", err)
}

_, upgrade := found.Status.GetClusterCondition(pravegav1alpha1.ClusterConditionUpgrading)
if upgrade.Status == corev1.ConditionTrue {
return fmt.Errorf("failed to process the request, cluster is upgrading")
}

// Add other conditions here
return nil
}

// pravegaWebhookHandler implements inject.Client.
var _ inject.Client = &pravegaWebhookHandler{}

Expand Down
26 changes: 26 additions & 0 deletions pkg/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,31 @@ var _ = Describe("Admission webhook", func() {
})
})
})
Context("Reject request when upgrading", func() {
var (
client client.Client
err error
)

BeforeEach(func() {
p.Spec = v1alpha1.ClusterSpec{
Version: "0.5.0-001",
}
p.Status.SetUpgradingConditionTrue()
client = fake.NewFakeClient(p)
pwh = &pravegaWebhookHandler{client: client}
})

Context("Sending request when upgrading", func() {
It("should not pass", func() {
p.Spec = v1alpha1.ClusterSpec{
Version: "0.5.0-002",
}
err = pwh.clusterIsAvailable(context.TODO(), p)
Ω(err).ShouldNot(BeNil())
})
})

})
})
})