Skip to content

Commit

Permalink
Issue 187: Webhook rejects requests when cluster is upgrading (#190)
Browse files Browse the repository at this point in the history
* Reject request when cluster is upgrading

Signed-off-by: wenqimou <[email protected]>

* Change http code from 400 to 503

Signed-off-by: wenqimou <[email protected]>

* Allow roll back

Signed-off-by: wenqimou <[email protected]>
  • Loading branch information
Wenqi authored and adrianmo committed May 22, 2019
1 parent b1d57e8 commit ab37930
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
32 changes: 32 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.StatusServiceUnavailable, err)
}

if err := pwh.mutatePravegaManifest(ctx, copy); err != nil {
return admission.ErrorResponse(http.StatusBadRequest, err)
}
Expand Down Expand Up @@ -139,6 +145,32 @@ 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 != nil && upgrade.Status == corev1.ConditionTrue {
// Reject the request if the requested version is new.
if p.Spec.Version != found.Spec.Version && p.Spec.Version != found.Status.CurrentVersion {
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())
})
})

})
})
})

0 comments on commit ab37930

Please sign in to comment.