Skip to content

Commit

Permalink
wekbook block changing activation spec
Browse files Browse the repository at this point in the history
  • Loading branch information
RemindD committed Jun 12, 2024
1 parent c8bac85 commit 6e5318d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (t *ActivationsManager) ReportStatus(ctx context.Context, name string, name
}

current.UpdateTime = time.Now().Format(time.RFC3339) // TODO: is this correct? Shouldn't it be reported?
activationState.Spec = nil
activationState.Status = &current

entry.Body = activationState
Expand Down
14 changes: 13 additions & 1 deletion api/pkg/apis/v1alpha1/providers/states/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func (s *K8sStateProvider) Upsert(ctx context.Context, entry states.UpsertReques
}

j, _ := json.Marshal(entry.Value.Body)
item, err := s.DynamicClient.Resource(resourceId).Namespace(namespace).Get(ctx, entry.Value.ID, metav1.GetOptions{})
var item *unstructured.Unstructured
item, err = s.DynamicClient.Resource(resourceId).Namespace(namespace).Get(ctx, entry.Value.ID, metav1.GetOptions{})
if err != nil {
template := fmt.Sprintf(`{"apiVersion":"%s/v1", "kind": "%s", "metadata": {}}`, group, kind)
var unc *unstructured.Unstructured
Expand Down Expand Up @@ -252,6 +253,7 @@ func (s *K8sStateProvider) Upsert(ctx context.Context, entry states.UpsertReques
item.SetLabels(metadata.Labels)
item.SetAnnotations(metadata.Annotations)
}
getResourceVersion := false
if v, ok := dict["spec"]; ok {
item.Object["spec"] = v

Expand All @@ -260,8 +262,18 @@ func (s *K8sStateProvider) Upsert(ctx context.Context, entry states.UpsertReques
sLog.Errorf(" P (K8s State): failed to update object: %v", err)
return "", err
}
getResourceVersion = true
}
if v, ok := dict["status"]; ok {
if getResourceVersion {
// Get latest resource version in case the the object spec is also updated
item, err = s.DynamicClient.Resource(resourceId).Namespace(namespace).Get(ctx, entry.Value.ID, metav1.GetOptions{})
if err != nil {
sLog.Errorf(" P (K8s State): failed to get object when trying to update status: %v", err)
return "", err
}
}

if vMap, ok := v.(map[string]interface{}); ok {
status := &unstructured.Unstructured{
Object: map[string]interface{}{
Expand Down
42 changes: 40 additions & 2 deletions k8s/apis/workflow/v1/activation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package v1

import (
"context"
"fmt"
"gopls-workspace/apis/metrics/v1"
"strings"
"time"
Expand Down Expand Up @@ -92,8 +93,29 @@ func (r *Activation) ValidateCreate() (admission.Warnings, error) {
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Activation) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
activationlog.Info("validate update", "name", r.Name)

return nil, nil
validateUpdateTime := time.Now()
oldActivation, ok := old.(*Activation)
if !ok {
return nil, fmt.Errorf("expected an Activation object")
}
// Compare the Spec of the current and old Activation objects
validationError := oldActivation.validateSpecOnUpdate(oldActivation)
if validationError != nil {
activationWebhookValidationMetrics.ControllerValidationLatency(
validateUpdateTime,
metrics.UpdateOperationType,
metrics.InvalidResource,
metrics.InstanceResourceType,
)
} else {
activationWebhookValidationMetrics.ControllerValidationLatency(
validateUpdateTime,
metrics.UpdateOperationType,
metrics.ValidResource,
metrics.InstanceResourceType,
)
}
return nil, validationError
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
Expand Down Expand Up @@ -137,3 +159,19 @@ func (r *Activation) validateCampaignOnCreate() *field.Error {
}
return nil
}

func (r *Activation) validateSpecOnUpdate(oldActivation *Activation) *field.Error {
if r.Spec.Campaign != oldActivation.Spec.Campaign {
return field.Invalid(field.NewPath("spec").Child("campaign"), r.Spec.Campaign, "updates to activation spec.Campaign are not allowed")
}
if r.Spec.Stage != oldActivation.Spec.Stage {
return field.Invalid(field.NewPath("spec").Child("stage"), r.Spec.Stage, "updates to activation spec.Stage are not allowed")
}
if r.Spec.Inputs.String() != oldActivation.Spec.Inputs.String() {
return field.Invalid(field.NewPath("spec").Child("inputs"), r.Spec.Inputs, "updates to activation spec.Inputs are not allowed")
}
if r.Spec.Generation != oldActivation.Spec.Generation {
return field.Invalid(field.NewPath("spec").Child("generation"), r.Spec.Generation, "updates to activation spec.Generation are not allowed")
}
return nil
}

0 comments on commit 6e5318d

Please sign in to comment.