-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): add deployment mutation hook
fixes #528
- Loading branch information
1 parent
9947ae3
commit 7c40a40
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |