-
Notifications
You must be signed in to change notification settings - Fork 453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pod level inject webhook #716
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aabeba0
Add pod level inject webhook.
wuchunghsuan 8cccde6
Implement sidecar injection (Hard code container)
wuchunghsuan bb42b18
Inject metrics collector as a sidecar
wuchunghsuan 4f06a88
Update metrics-collector to satisfy sidecar
wuchunghsuan 5f9e8b5
Clean up test logs
wuchunghsuan e2831e7
Get experiment name and job kind
wuchunghsuan 9dbb823
Update common labels
wuchunghsuan 12608c5
Separate the sidecar metrics collector
wuchunghsuan c7c5aab
Merge remote-tracking branch 'upstream/master' into pod-webhook
wuchunghsuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,108 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package pod | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/inject" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
// "github.com/kubeflow/katib/pkg/webhook/v1alpha2/experiment/injector" | ||
) | ||
|
||
// For debug | ||
var log = logf.Log.WithName("injector-webhook") | ||
|
||
// sidecarInjector that inject metrics collect sidecar into master pod | ||
type sidecarInjector struct { | ||
client client.Client | ||
decoder types.Decoder | ||
// injector.Injector | ||
} | ||
|
||
var _ admission.Handler = &sidecarInjector{} | ||
|
||
func (s *sidecarInjector) Handle(ctx context.Context, req types.Request) types.Response { | ||
pod := &v1.Pod{} | ||
err := s.decoder.Decode(req, pod) | ||
if err != nil { | ||
return admission.ErrorResponse(http.StatusBadRequest, err) | ||
} | ||
|
||
// Check whether the pod need to be mutated | ||
if !s.MutationRequired(pod) { | ||
log.Info("Skipping mutation for " + pod.Name + " due to policy check") | ||
return admission.ValidationResponse(true, "") | ||
} | ||
|
||
// Do mutation | ||
mutatedPod := s.Mutate(pod) | ||
// if err != nil { | ||
// return admission.ErrorResponse(http.StatusBadRequest, err) | ||
// } | ||
|
||
return admission.PatchResponse(pod, mutatedPod) | ||
} | ||
|
||
var _ inject.Client = &sidecarInjector{} | ||
|
||
func (s *sidecarInjector) InjectClient(c client.Client) error { | ||
s.client = c | ||
return nil | ||
} | ||
|
||
var _ inject.Decoder = &sidecarInjector{} | ||
|
||
func (s *sidecarInjector) InjectDecoder(d types.Decoder) error { | ||
s.decoder = d | ||
return nil | ||
} | ||
|
||
func NewSidecarInjector(c client.Client) *sidecarInjector { | ||
return &sidecarInjector{ | ||
client: c, | ||
} | ||
} | ||
|
||
func (s *sidecarInjector) MutationRequired(pod *v1.Pod) bool { | ||
labels := pod.Labels | ||
for k, v := range labels { | ||
log.Info("FOR TEST: pod label {" + k + ": " + v + "}") | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (s *sidecarInjector) Mutate(pod *v1.Pod) *v1.Pod { | ||
mutatedPod := pod.DeepCopy() | ||
|
||
// Hard code container, just for test | ||
injectContainer := v1.Container{ | ||
Name: "sidecar-nginxk", | ||
Image: "nginx:1.12.2", | ||
} | ||
mutatedPod.Spec.Containers = append(mutatedPod.Spec.Containers, injectContainer) | ||
|
||
return mutatedPod | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe we can rename it to one which is katib related
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, do you have any suggestion about the name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should follow xx.$group.$domain style (group should be
katib
and domain iskubeflow.org
here), we'd better make all others consistent with this style.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM, it is a historical problem. Can we do the change in another PR? I think we could keep the name here and update it after the PR is merged, with other changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure