Skip to content
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

[Fix]Cache - Revert objectSelector in mutatingwebhookconfiguration #3433

Merged
merged 18 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update filter logic
  • Loading branch information
rui5i committed Mar 31, 2020
commit 7cdb734df22ed809ad0ecd44fc62443a7caed02f
3 changes: 3 additions & 0 deletions backend/src/cache/deployer/cache-configmap.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ webhooks:
namespace: ${NAMESPACE}
path: "/mutate"
caBundle: ${CA_BUNDLE}
objectSelector:
matchLabels:
pipelines.kubeflow.org/cache_enabled: true
rules:
- operations: [ "CREATE" ]
apiGroups: [""]
Expand Down
39 changes: 6 additions & 33 deletions backend/src/cache/server/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func MutatePodIfCached(req *v1beta1.AdmissionRequest, clientMgr ClientManagerInt
}

// Pod filtering to only cache KFP argo pods except TFX pods
if !isValidPod(&pod) {
log.Printf("This pod %s is not a valid pod.", pod.ObjectMeta.Name)
if isTFXPod(&pod) {
log.Printf("This pod %s is created by tfx pipelines.", pod.ObjectMeta.Name)
return nil, nil
}

Expand Down Expand Up @@ -195,41 +195,14 @@ func getValueFromSerializedMap(serializedMap string, key string) string {
return value
}

func isValidPod(pod *corev1.Pod) bool {
annotations := pod.ObjectMeta.Annotations
if annotations == nil || len(annotations) == 0 {
log.Printf("The annotation of this pod %s is empty.", pod.ObjectMeta.Name)
return false
}
if !isKFPArgoPod(&annotations, pod.ObjectMeta.Name) {
log.Printf("This pod %s is not created by KFP.", pod.ObjectMeta.Name)
return false
}
func isTFXPod(pod *corev1.Pod) bool {
containers := pod.Spec.Containers
if containers != nil && len(containers) != 0 && isTFXPod(&containers) {
log.Printf("This pod %s is created by TFX pipelines.", pod.ObjectMeta.Name)
return false
}
return true
}

func isKFPArgoPod(annotations *map[string]string, podName string) bool {
// is argo pod or not
if _, exists := (*annotations)[ArgoWorkflowNodeName]; !exists {
log.Printf("This pod %s is not created by Argo.", podName)
return false
}
// is KFP pod or not
if v, exists := (*annotations)[KFPAnnotationKeyCacheEnabled]; exists && v == KFPAnnotationValueCacheEnabled {
if containers == nil || len(containers) == 0 {
log.Printf("This pod container does not exist.")
return true
}

return false
}

func isTFXPod(containers *[]corev1.Container) bool {
var mainContainers []corev1.Container
for _, c := range *containers {
for _, c := range containers {
if c.Name != "" && c.Name == "main" {
mainContainers = append(mainContainers, c)
}
Expand Down
24 changes: 4 additions & 20 deletions backend/src/cache/server/mutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ var (
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
ArgoWorkflowNodeName: "test_node",
ArgoWorkflowTemplate: `{"name": "test_template"}`,
KFPAnnotationKeyCacheEnabled: KFPAnnotationValueCacheEnabled,
ArgoWorkflowNodeName: "test_node",
ArgoWorkflowTemplate: `{"name": "test_template"}`,
},
Labels: map[string]string{
ArgoCompleteLabelKey: "true",
ArgoCompleteLabelKey: "true",
KFPAnnotationKeyCacheEnabled: KFPAnnotationValueCacheEnabled,
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -107,22 +107,6 @@ func TestMutatePodIfCachedWithDecodeError(t *testing.T) {
assert.Contains(t, err.Error(), "could not deserialize pod object")
}

func TestMutatePodIfCachedWithNonKFPPod(t *testing.T) {
nonKFPPod := *fakePod
delete(nonKFPPod.Annotations, KFPAnnotationKeyCacheEnabled)
patchOperation, err := MutatePodIfCached(GetFakeRequestFromPod(&nonKFPPod), fakeClientManager)
assert.Nil(t, patchOperation)
assert.Nil(t, err)
}

func TestMutatePodIfCachedWithNonArgoPod(t *testing.T) {
nonArgoPod := *fakePod
delete(nonArgoPod.Annotations, ArgoWorkflowNodeName)
patchOperation, err := MutatePodIfCached(GetFakeRequestFromPod(&nonArgoPod), fakeClientManager)
assert.Nil(t, patchOperation)
assert.Nil(t, err)
}

func TestMutatePodIfCachedWithTFXPod(t *testing.T) {
tfxPod := *fakePod
mainContainerCommand := append(tfxPod.Spec.Containers[0].Command, "/tfx-src/"+TFXPodSuffix)
Expand Down