forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ymqytw
committed
Jan 8, 2018
1 parent
5615ad8
commit 372384e
Showing
1 changed file
with
84 additions
and
0 deletions.
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,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) | ||
} |