Skip to content

Commit

Permalink
feat(operator): add deployment mutation hook
Browse files Browse the repository at this point in the history
fixes #528
  • Loading branch information
aslakknutsen committed Sep 25, 2020
1 parent 9947ae3 commit 7c40a40
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 113 additions & 0 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package webhook

import (
"encoding/json"
"time"

istiov1alpha1 "github.com/maistra/istio-workspace/pkg/apis/maistra/v1alpha1"
"github.com/maistra/istio-workspace/pkg/internal/session"
"github.com/maistra/istio-workspace/pkg/model"
admission "k8s.io/api/admission/v1"
appsv1 "k8s.io/api/apps/v1"
)

type data struct {
Object appsv1.Deployment
}

func (d *data) IsIkeable() bool {
return d.Object.Labels["ike.target"] != ""
}

func (d *data) Join() bool {
_, ok := d.Object.Labels["ike.session"]
return ok
}

func (d *data) Target() string {
return d.Object.Labels["ike.target"]
}

func (d *data) Session() string {
return d.Object.Labels["ike.session"]
}

func (d *data) Namespace() string {
return d.Object.Namespace
}

func Hook(review admission.AdmissionReview) admission.AdmissionReview {

// if review.Request.DryRun don't do stuff with sideeffects....

data, err := parseRequest(review.Request)
if err != nil {
// TODO: createErrorReview
}

if !data.IsIkeable() {
review.Response = &admission.AdmissionResponse{
UID: review.Request.UID,
Allowed: true,
}
return review
}

session, err := createSessionAndWait(data)
if err != nil {
// TODO: createErrorReview
}

patch := createLabelsPatch(session)

patchType := admission.PatchTypeJSONPatch
review.Response = &admission.AdmissionResponse{
UID: review.Request.UID,
Allowed: true,
PatchType: &patchType,
Patch: []byte(patch),
}
return review
}

func parseRequest(request *admission.AdmissionRequest) (data, error) {
var dep appsv1.Deployment

err := json.Unmarshal(request.Object.Raw, &dep)
if err != nil {
return data{}, err
}
return data{
Object: dep,
}, nil
}

func createSessionAndWait(d data) (*istiov1alpha1.RefStatus, error) {
checkDuration := time.Millisecond * 100
options := session.Options{
DeploymentName: d.Target(),
SessionName: d.Session(),
NamespaceName: d.Namespace(),
Strategy: model.StrategyExisting,
Duration: &checkDuration,
// TODO: add support for what to wait for WaitExpression: FoundDeployment
}

c, err := session.DefaultClient(options.NamespaceName)
if err != nil {
return nil, err
}

state, remove, err := session.CreateOrJoinHandler(options, c)
if err != nil {
remove()
return nil, err
}

return &state.RefStatus, nil
}

func createLabelsPatch(ref *istiov1alpha1.RefStatus) string {
// for each Resource, if Deployment, Make label path
return ""
}

0 comments on commit 7c40a40

Please sign in to comment.