Skip to content

Commit

Permalink
Merge pull request kubernetes#199 from mengqiy/ptv
Browse files Browse the repository at this point in the history
pod template visitor
  • Loading branch information
k8s-ci-robot authored Jan 8, 2018
2 parents 5d2a978 + 372384e commit 61ee51f
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions pkg/kinflate/pod_template_visitor/pod_template_visitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2017 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_template_visitor

import (
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kapps "k8s.io/kubectl/pkg/kinflate/apps"
)

type PodTemplateSpecVisitor struct {
Object *unstructured.Unstructured
MungeFn func(podTemplateSpec map[string]interface{}) error
Err error
}

var _ kapps.KindVisitor = &PodTemplateSpecVisitor{}

func (v *PodTemplateSpecVisitor) VisitDeployment(kind kapps.GroupKindElement) {
v.Err = v.mungePodTemplateSpec([]string{"spec", "template"})
}

func (v *PodTemplateSpecVisitor) VisitStatefulSet(kind kapps.GroupKindElement) {
v.Err = v.mungePodTemplateSpec([]string{"spec", "template"})
}

func (v *PodTemplateSpecVisitor) VisitDaemonSet(kind kapps.GroupKindElement) {
v.Err = v.mungePodTemplateSpec([]string{"spec", "template"})
}

func (v *PodTemplateSpecVisitor) VisitJob(kind kapps.GroupKindElement) {
v.Err = v.mungePodTemplateSpec([]string{"spec", "template"})
}

func (v *PodTemplateSpecVisitor) VisitReplicaSet(kind kapps.GroupKindElement) {
v.Err = v.mungePodTemplateSpec([]string{"spec", "template"})
}

func (v *PodTemplateSpecVisitor) VisitPod(kind kapps.GroupKindElement) {}

func (v *PodTemplateSpecVisitor) VisitReplicationController(kind kapps.GroupKindElement) {
v.Err = v.mungePodTemplateSpec([]string{"spec", "template"})
}

func (v *PodTemplateSpecVisitor) VisitCronJob(kind kapps.GroupKindElement) {
v.Err = v.mungePodTemplateSpec([]string{"spec", "jobTemplate", "spec", "template"})
}

func walkMapPath(start map[string]interface{}, path []string) (map[string]interface{}, error) {
finish := start
for i := 0; i < len(path); i++ {
var ok bool
finish, ok = finish[path[i]].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("key:%s of path:%v not found in map:%v", path[i], path, start)
}
}

return finish, nil
}

func (v *PodTemplateSpecVisitor) mungePodTemplateSpec(pathToPodTemplateSpec []string) error {
obj := v.Object.UnstructuredContent()
podTemplateSpec, err := walkMapPath(obj, pathToPodTemplateSpec)
if err != nil {
return err
}
return v.MungeFn(podTemplateSpec)
}

0 comments on commit 61ee51f

Please sign in to comment.