github.com/kubevela/workflow@v0.6.0/pkg/webhook/v1alpha1/workflowrun/mutating_handler.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package workflowrun
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"net/http"
    24  
    25  	"sigs.k8s.io/controller-runtime/pkg/manager"
    26  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    27  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    28  
    29  	"github.com/kubevela/workflow/api/v1alpha1"
    30  )
    31  
    32  // MutatingHandler adding user info to application annotations
    33  type MutatingHandler struct {
    34  	Decoder *admission.Decoder
    35  }
    36  
    37  var _ admission.Handler = &MutatingHandler{}
    38  
    39  // Handle mutate application
    40  func (h *MutatingHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
    41  	wr := &v1alpha1.WorkflowRun{}
    42  	if err := h.Decoder.Decode(req, wr); err != nil {
    43  		return admission.Errored(http.StatusBadRequest, err)
    44  	}
    45  	if wr.Spec.WorkflowSpec != nil {
    46  		for i, step := range wr.Spec.WorkflowSpec.Steps {
    47  			if step.Name == "" {
    48  				wr.Spec.WorkflowSpec.Steps[i].Name = fmt.Sprintf("step-%d", i)
    49  			}
    50  			for j, sub := range step.SubSteps {
    51  				if sub.Name == "" {
    52  					wr.Spec.WorkflowSpec.Steps[i].SubSteps[j].Name = fmt.Sprintf("step-%d-%d", i, j)
    53  				}
    54  			}
    55  		}
    56  	}
    57  	bs, err := json.Marshal(wr)
    58  	if err != nil {
    59  		return admission.Errored(http.StatusInternalServerError, err)
    60  	}
    61  	return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, bs)
    62  }
    63  
    64  var _ admission.DecoderInjector = &MutatingHandler{}
    65  
    66  // InjectDecoder .
    67  func (h *MutatingHandler) InjectDecoder(d *admission.Decoder) error {
    68  	h.Decoder = d
    69  	return nil
    70  }
    71  
    72  // RegisterMutatingHandler will register workflow mutation handler to the webhook
    73  func RegisterMutatingHandler(mgr manager.Manager) {
    74  	server := mgr.GetWebhookServer()
    75  	handler := &MutatingHandler{}
    76  	server.Register("/mutating-core-oam-dev-v1alpha1-workflowruns", &webhook.Admission{Handler: handler})
    77  }