@@ -149,10 +149,10 @@ func deserializeNetworkAttachmentDefinition(ar *v1beta1.AdmissionReview) (cniv1.
149149 return netAttachDef , err
150150}
151151
152- func deserializePod (ar * v1beta1.AdmissionReview ) (corev1.Pod , error ) {
152+ func deserializePod (ar * v1beta1.AdmissionReview ) (* corev1.Pod , error ) {
153153 /* unmarshal Pod from AdmissionReview request */
154- pod := corev1.Pod {}
155- err := json .Unmarshal (ar .Request .Object .Raw , & pod )
154+ pod := & corev1.Pod {}
155+ err := json .Unmarshal (ar .Request .Object .Raw , pod )
156156 if pod .ObjectMeta .Namespace != "" {
157157 return pod , err
158158 }
@@ -165,7 +165,7 @@ func deserializePod(ar *v1beta1.AdmissionReview) (corev1.Pod, error) {
165165
166166 ownerRef := pod .ObjectMeta .OwnerReferences
167167 if len (ownerRef ) > 0 {
168- namespace , err := getNamespaceFromOwnerReference (pod .ObjectMeta .OwnerReferences [0 ])
168+ namespace , err := getNamespaceFromOwnerReference (& pod .ObjectMeta .OwnerReferences [0 ])
169169 if err != nil {
170170 return pod , err
171171 }
@@ -178,7 +178,7 @@ func deserializePod(ar *v1beta1.AdmissionReview) (corev1.Pod, error) {
178178 return pod , err
179179}
180180
181- func getNamespaceFromOwnerReference (ownerRef metav1.OwnerReference ) (namespace string , err error ) {
181+ func getNamespaceFromOwnerReference (ownerRef * metav1.OwnerReference ) (namespace string , err error ) {
182182 namespace = ""
183183 switch ownerRef .Kind {
184184 case "ReplicaSet" :
@@ -187,9 +187,9 @@ func getNamespaceFromOwnerReference(ownerRef metav1.OwnerReference) (namespace s
187187 if err != nil {
188188 return
189189 }
190- for _ , replicaSet := range replicaSets .Items {
191- if replicaSet . ObjectMeta .Name == ownerRef .Name && replicaSet .ObjectMeta .UID == ownerRef .UID {
192- namespace = replicaSet .ObjectMeta .Namespace
190+ for i := range replicaSets .Items {
191+ if replicaSets . Items [ i ]. ObjectMeta .Name == ownerRef .Name && replicaSets . Items [ i ] .ObjectMeta .UID == ownerRef .UID {
192+ namespace = replicaSets . Items [ i ] .ObjectMeta .Namespace
193193 err = nil
194194 break
195195 }
@@ -200,9 +200,9 @@ func getNamespaceFromOwnerReference(ownerRef metav1.OwnerReference) (namespace s
200200 if err != nil {
201201 return
202202 }
203- for _ , daemonSet := range daemonSets .Items {
204- if daemonSet . ObjectMeta .Name == ownerRef .Name && daemonSet .ObjectMeta .UID == ownerRef .UID {
205- namespace = daemonSet .ObjectMeta .Namespace
203+ for i := range daemonSets .Items {
204+ if daemonSets . Items [ i ]. ObjectMeta .Name == ownerRef .Name && daemonSets . Items [ i ] .ObjectMeta .UID == ownerRef .UID {
205+ namespace = daemonSets . Items [ i ] .ObjectMeta .Namespace
206206 err = nil
207207 break
208208 }
@@ -213,9 +213,9 @@ func getNamespaceFromOwnerReference(ownerRef metav1.OwnerReference) (namespace s
213213 if err != nil {
214214 return
215215 }
216- for _ , statefulSet := range statefulSets .Items {
217- if statefulSet . ObjectMeta .Name == ownerRef .Name && statefulSet .ObjectMeta .UID == ownerRef .UID {
218- namespace = statefulSet .ObjectMeta .Namespace
216+ for i := range statefulSets .Items {
217+ if statefulSets . Items [ i ]. ObjectMeta .Name == ownerRef .Name && statefulSets . Items [ i ] .ObjectMeta .UID == ownerRef .UID {
218+ namespace = statefulSets . Items [ i ] .ObjectMeta .Namespace
219219 err = nil
220220 break
221221 }
@@ -226,9 +226,10 @@ func getNamespaceFromOwnerReference(ownerRef metav1.OwnerReference) (namespace s
226226 if err != nil {
227227 return
228228 }
229- for _ , replicationController := range replicationControllers .Items {
230- if replicationController .ObjectMeta .Name == ownerRef .Name && replicationController .ObjectMeta .UID == ownerRef .UID {
231- namespace = replicationController .ObjectMeta .Namespace
229+ for i := range replicationControllers .Items {
230+ if replicationControllers .Items [i ].ObjectMeta .Name == ownerRef .Name &&
231+ replicationControllers .Items [i ].ObjectMeta .UID == ownerRef .UID {
232+ namespace = replicationControllers .Items [i ].ObjectMeta .Namespace
232233 err = nil
233234 break
234235 }
@@ -517,8 +518,7 @@ func createVolPatch(patch []jsonPatchOperation, hugepageResourceList []hugepageR
517518 return patch
518519}
519520
520- func addEnvVar (patch []jsonPatchOperation , containerIndex int , firstElement bool ,
521- envName string , envVal string ) []jsonPatchOperation {
521+ func addEnvVar (patch []jsonPatchOperation , containerIndex int , firstElement bool , envName , envVal string ) []jsonPatchOperation {
522522 env := corev1.EnvVar {
523523 Name : envName ,
524524 Value : envVal ,
@@ -541,13 +541,12 @@ func addEnvVar(patch []jsonPatchOperation, containerIndex int, firstElement bool
541541 return patch
542542}
543543
544- func createEnvPatch (patch []jsonPatchOperation , container * corev1.Container ,
545- containerIndex int , envName string , envVal string ) []jsonPatchOperation {
544+ func createEnvPatch (patch []jsonPatchOperation , envVar []corev1.EnvVar , containerIndex int , envName , envVal string ) []jsonPatchOperation {
546545 // Determine if requested ENV already exists
547546 found := false
548547 firstElement := false
549- if len (container . Env ) != 0 {
550- for _ , env := range container . Env {
548+ if len (envVar ) != 0 {
549+ for _ , env := range envVar {
551550 if env .Name == envName {
552551 found = true
553552 if env .Value != envVal {
@@ -676,7 +675,7 @@ func getResourceList(resourceRequests map[string]int64) *corev1.ResourceList {
676675 return & resourceList
677676}
678677
679- func appendPodAnnotation (patch []jsonPatchOperation , pod corev1.Pod , userDefinedPatch jsonPatchOperation ) []jsonPatchOperation {
678+ func appendPodAnnotation (patch []jsonPatchOperation , pod * corev1.Pod , userDefinedPatch jsonPatchOperation ) []jsonPatchOperation {
680679 annotMap := make (map [string ]string )
681680 for k , v := range pod .ObjectMeta .Annotations {
682681 annotMap [k ] = v
@@ -692,7 +691,7 @@ func appendPodAnnotation(patch []jsonPatchOperation, pod corev1.Pod, userDefined
692691 return patch
693692}
694693
695- func createCustomizedPatch (pod corev1.Pod ) ( []jsonPatchOperation , error ) {
694+ func createCustomizedPatch (pod * corev1.Pod ) []jsonPatchOperation {
696695 var userDefinedPatch []jsonPatchOperation
697696
698697 // lock for reading
@@ -707,10 +706,10 @@ func createCustomizedPatch(pod corev1.Pod) ([]jsonPatchOperation, error) {
707706 userDefinedPatch = append (userDefinedPatch , v )
708707 }
709708 }
710- return userDefinedPatch , nil
709+ return userDefinedPatch
711710}
712711
713- func appendCustomizedPatch (patch []jsonPatchOperation , pod corev1.Pod , userDefinedPatch []jsonPatchOperation ) []jsonPatchOperation {
712+ func appendCustomizedPatch (patch []jsonPatchOperation , pod * corev1.Pod , userDefinedPatch []jsonPatchOperation ) []jsonPatchOperation {
714713 for _ , p := range userDefinedPatch {
715714 if p .Path == annotPath {
716715 patch = appendPodAnnotation (patch , pod , p )
@@ -719,7 +718,7 @@ func appendCustomizedPatch(patch []jsonPatchOperation, pod corev1.Pod, userDefin
719718 return patch
720719}
721720
722- func getNetworkSelections (annotationKey string , pod corev1.Pod , userDefinedPatch []jsonPatchOperation ) (string , bool ) {
721+ func getNetworkSelections (annotationKey string , pod * corev1.Pod , userDefinedPatch []jsonPatchOperation ) (string , bool ) {
723722 // User defined annotateKey takes precedence than userDefined injections
724723 glog .Infof ("search %s in original pod annotations" , annotationKey )
725724 nets , exists := pod .ObjectMeta .Annotations [annotationKey ]
@@ -767,11 +766,7 @@ func MutateHandler(w http.ResponseWriter, req *http.Request) {
767766 }
768767 glog .Infof ("AdmissionReview request received for pod %s/%s" , pod .ObjectMeta .Namespace , pod .ObjectMeta .Name )
769768
770- userDefinedPatch , err := createCustomizedPatch (pod )
771- if err != nil {
772- glog .Warningf ("failed to create user-defined injection patch for pod %s/%s, err: %v" ,
773- pod .ObjectMeta .Namespace , pod .ObjectMeta .Name , err )
774- }
769+ userDefinedPatch := createCustomizedPatch (pod )
775770
776771 defaultNetSelection , defExist := getNetworkSelections (defaultNetworkAnnotationKey , pod , userDefinedPatch )
777772 additionalNetSelections , addExists := getNetworkSelections (networksAnnotationKey , pod , userDefinedPatch )
@@ -850,46 +845,52 @@ func MutateHandler(w http.ResponseWriter, req *http.Request) {
850845
851846 // Determine if hugepages are being requested for a given container,
852847 // and if so, expose the value to the container via Downward API.
853- var hugepageResourceList []hugepageResourceData
848+ var (
849+ hugepageResourceList []hugepageResourceData
850+ res corev1.ResourceRequirements
851+ name string
852+ )
854853 glog .Infof ("injectHugepageDownAPI=%v" , injectHugepageDownAPI )
855854 if injectHugepageDownAPI {
856- for containerIndex , container := range pod .Spec .Containers {
855+ for i := range pod .Spec .Containers {
857856 found := false
858- if len (container .Resources .Requests ) != 0 {
859- if quantity , exists := container .Resources .Requests ["hugepages-1Gi" ]; exists && ! quantity .IsZero () {
857+ res = pod .Spec .Containers [i ].Resources
858+ name = pod .Spec .Containers [i ].Name
859+ if len (res .Requests ) != 0 {
860+ if quantity , exists := res .Requests ["hugepages-1Gi" ]; exists && ! quantity .IsZero () {
860861 hugepageResource := hugepageResourceData {
861862 ResourceName : "requests.hugepages-1Gi" ,
862- ContainerName : container . Name ,
863- Path : types .Hugepages1GRequestPath + "_" + container . Name ,
863+ ContainerName : name ,
864+ Path : types .Hugepages1GRequestPath + "_" + name ,
864865 }
865866 hugepageResourceList = append (hugepageResourceList , hugepageResource )
866867 found = true
867868 }
868- if quantity , exists := container . Resources .Requests ["hugepages-2Mi" ]; exists && ! quantity .IsZero () {
869+ if quantity , exists := res .Requests ["hugepages-2Mi" ]; exists && ! quantity .IsZero () {
869870 hugepageResource := hugepageResourceData {
870871 ResourceName : "requests.hugepages-2Mi" ,
871- ContainerName : container . Name ,
872- Path : types .Hugepages2MRequestPath + "_" + container . Name ,
872+ ContainerName : name ,
873+ Path : types .Hugepages2MRequestPath + "_" + name ,
873874 }
874875 hugepageResourceList = append (hugepageResourceList , hugepageResource )
875876 found = true
876877 }
877878 }
878- if len (container . Resources .Limits ) != 0 {
879- if quantity , exists := container . Resources .Limits ["hugepages-1Gi" ]; exists && ! quantity .IsZero () {
879+ if len (res .Limits ) != 0 {
880+ if quantity , exists := res .Limits ["hugepages-1Gi" ]; exists && ! quantity .IsZero () {
880881 hugepageResource := hugepageResourceData {
881882 ResourceName : "limits.hugepages-1Gi" ,
882- ContainerName : container . Name ,
883- Path : types .Hugepages1GLimitPath + "_" + container . Name ,
883+ ContainerName : name ,
884+ Path : types .Hugepages1GLimitPath + "_" + name ,
884885 }
885886 hugepageResourceList = append (hugepageResourceList , hugepageResource )
886887 found = true
887888 }
888- if quantity , exists := container . Resources .Limits ["hugepages-2Mi" ]; exists && ! quantity .IsZero () {
889+ if quantity , exists := res .Limits ["hugepages-2Mi" ]; exists && ! quantity .IsZero () {
889890 hugepageResource := hugepageResourceData {
890891 ResourceName : "limits.hugepages-2Mi" ,
891- ContainerName : container . Name ,
892- Path : types .Hugepages2MLimitPath + "_" + container . Name ,
892+ ContainerName : name ,
893+ Path : types .Hugepages2MLimitPath + "_" + name ,
893894 }
894895 hugepageResourceList = append (hugepageResourceList , hugepageResource )
895896 found = true
@@ -900,12 +901,11 @@ func MutateHandler(w http.ResponseWriter, req *http.Request) {
900901 // 'container.Name' as an environment variable to the container
901902 // so container knows its name and can process hugepages properly.
902903 if found {
903- patch = createEnvPatch (patch , & container , containerIndex ,
904- types .EnvNameContainerName , container .Name )
904+ patch = createEnvPatch (patch , pod .Spec .Containers [i ].Env , i , types .EnvNameContainerName , name )
905905 }
906906 }
907907 }
908- patch = createVolPatch (patch , hugepageResourceList , & pod )
908+ patch = createVolPatch (patch , hugepageResourceList , pod )
909909 patch = appendCustomizedPatch (patch , pod , userDefinedPatch )
910910 }
911911 patch = createNodeSelectorPatch (patch , pod .Spec .NodeSelector , desiredNsMap )
@@ -997,7 +997,7 @@ func SetCustomizedInjections(injections *corev1.ConfigMap) {
997997 }
998998 }
999999 // remove stale entries from userDefined configMap
1000- for k , _ := range userDefinedPatchs {
1000+ for k := range userDefinedPatchs {
10011001 if _ , ok := injections .Data [k ]; ok {
10021002 continue
10031003 }
0 commit comments