// annotateForExecutorOnSlave sets the BindingHostKey annotation which // marks the pod to be processed by the scheduler and launched as a Mesos // task. The executor on the slave will to the final binding to finish the // scheduling in the kubernetes sense. func annotateForExecutorOnSlave(pod *api.Pod, slave string) { if pod.Annotations == nil { pod.Annotations = make(map[string]string) } else { oemAnn := pod.Annotations pod.Annotations = make(map[string]string) for k, v := range oemAnn { pod.Annotations[k] = v } } pod.Annotations[annotation.BindingHostKey] = slave }
func Convert_v1_Pod_To_api_Pod(in *Pod, out *api.Pod, s conversion.Scope) error { // TODO: when we move init container to beta, remove these conversions if value, ok := in.Annotations[PodInitContainersAnnotationKey]; ok { var values []Container if err := json.Unmarshal([]byte(value), &values); err != nil { return err } in.Spec.InitContainers = values } if value, ok := in.Annotations[PodInitContainerStatusesAnnotationKey]; ok { var values []ContainerStatus if err := json.Unmarshal([]byte(value), &values); err != nil { return err } in.Status.InitContainerStatuses = values } if err := autoConvert_v1_Pod_To_api_Pod(in, out, s); err != nil { return err } if len(out.Annotations) > 0 { old := out.Annotations out.Annotations = make(map[string]string, len(old)) for k, v := range old { out.Annotations[k] = v } delete(out.Annotations, PodInitContainersAnnotationKey) delete(out.Annotations, PodInitContainerStatusesAnnotationKey) } return nil }
func applyDefaults(pod *api.Pod, source string, isFile bool, nodeName string) error { if len(pod.UID) == 0 { hasher := md5.New() if isFile { fmt.Fprintf(hasher, "host:%s", nodeName) fmt.Fprintf(hasher, "file:%s", source) } else { fmt.Fprintf(hasher, "url:%s", source) } hash.DeepHashObject(hasher, pod) pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:])) glog.V(5).Infof("Generated UID %q pod %q from %s", pod.UID, pod.Name, source) } pod.Name = generatePodName(pod.Name, nodeName) glog.V(5).Infof("Generated Name %q for UID %q from URL %s", pod.Name, pod.UID, source) if pod.Namespace == "" { pod.Namespace = kubetypes.NamespaceDefault } glog.V(5).Infof("Using namespace %q for pod %q from %s", pod.Namespace, pod.Name, source) // Set the Host field to indicate this pod is scheduled on the current node. pod.Spec.NodeName = nodeName pod.ObjectMeta.SelfLink = getSelfLink(pod.Name, pod.Namespace) if pod.Annotations == nil { pod.Annotations = make(map[string]string) } // The generated UID is the hash of the file. pod.Annotations[kubetypes.ConfigHashAnnotationKey] = string(pod.UID) return nil }
// Sets the name of the profile to use with the container. func SetProfileName(pod *api.Pod, containerName, profileName string) error { if pod.Annotations == nil { pod.Annotations = map[string]string{} } pod.Annotations[ContainerAnnotationKeyPrefix+containerName] = profileName return nil }
func Convert_v1_Pod_To_api_Pod(in *Pod, out *api.Pod, s conversion.Scope) error { // If there is a beta annotation, copy to alpha key. // See commit log for PR #31026 for why we do this. if valueBeta, okBeta := in.Annotations[PodInitContainersBetaAnnotationKey]; okBeta { in.Annotations[PodInitContainersAnnotationKey] = valueBeta } // TODO: sometime after we move init container to stable, remove these conversions // Move the annotation to the internal repr. field if value, ok := in.Annotations[PodInitContainersAnnotationKey]; ok { var values []Container if err := json.Unmarshal([]byte(value), &values); err != nil { return err } // Conversion from external to internal version exists more to // satisfy the needs of the decoder than it does to be a general // purpose tool. And Decode always creates an intermediate object // to decode to. Thus the caller of UnsafeConvertToVersion is // taking responsibility to ensure mutation of in is not exposed // back to the caller. in.Spec.InitContainers = values } // If there is a beta annotation, copy to alpha key. // See commit log for PR #31026 for why we do this. if valueBeta, okBeta := in.Annotations[PodInitContainerStatusesBetaAnnotationKey]; okBeta { in.Annotations[PodInitContainerStatusesAnnotationKey] = valueBeta } if value, ok := in.Annotations[PodInitContainerStatusesAnnotationKey]; ok { var values []ContainerStatus if err := json.Unmarshal([]byte(value), &values); err != nil { return err } // Conversion from external to internal version exists more to // satisfy the needs of the decoder than it does to be a general // purpose tool. And Decode always creates an intermediate object // to decode to. Thus the caller of UnsafeConvertToVersion is // taking responsibility to ensure mutation of in is not exposed // back to the caller. in.Status.InitContainerStatuses = values } if err := autoConvert_v1_Pod_To_api_Pod(in, out, s); err != nil { return err } if len(out.Annotations) > 0 { old := out.Annotations out.Annotations = make(map[string]string, len(old)) for k, v := range old { out.Annotations[k] = v } delete(out.Annotations, PodInitContainersAnnotationKey) delete(out.Annotations, PodInitContainersBetaAnnotationKey) delete(out.Annotations, PodInitContainerStatusesAnnotationKey) delete(out.Annotations, PodInitContainerStatusesBetaAnnotationKey) } return nil }
// ApplyOverrides applies configured overrides to a build in a build pod func (b BuildOverrides) ApplyOverrides(pod *kapi.Pod) error { if b.config == nil { return nil } build, version, err := buildadmission.GetBuildFromPod(pod) if err != nil { return err } glog.V(4).Infof("Applying overrides to build %s/%s", build.Namespace, build.Name) if b.config.ForcePull { if build.Spec.Strategy.DockerStrategy != nil { glog.V(5).Infof("Setting docker strategy ForcePull to true in build %s/%s", build.Namespace, build.Name) build.Spec.Strategy.DockerStrategy.ForcePull = true } if build.Spec.Strategy.SourceStrategy != nil { glog.V(5).Infof("Setting source strategy ForcePull to true in build %s/%s", build.Namespace, build.Name) build.Spec.Strategy.SourceStrategy.ForcePull = true } if build.Spec.Strategy.CustomStrategy != nil { err := applyForcePullToPod(pod) if err != nil { return err } glog.V(5).Infof("Setting custom strategy ForcePull to true in build %s/%s", build.Namespace, build.Name) build.Spec.Strategy.CustomStrategy.ForcePull = true } } // Apply label overrides for _, lbl := range b.config.ImageLabels { glog.V(5).Infof("Overriding image label %s=%s in build %s/%s", lbl.Name, lbl.Value, build.Namespace, build.Name) overrideLabel(lbl, &build.Spec.Output.ImageLabels) } if len(b.config.NodeSelector) != 0 && pod.Spec.NodeSelector == nil { pod.Spec.NodeSelector = map[string]string{} } for k, v := range b.config.NodeSelector { glog.V(5).Infof("Adding override nodeselector %s=%s to build pod %s/%s", k, v, pod.Namespace, pod.Name) pod.Spec.NodeSelector[k] = v } if len(b.config.Annotations) != 0 && pod.Annotations == nil { pod.Annotations = map[string]string{} } for k, v := range b.config.Annotations { glog.V(5).Infof("Adding override annotation %s=%s to build pod %s/%s", k, v, pod.Namespace, pod.Name) pod.Annotations[k] = v } return buildadmission.SetBuildInPod(pod, build, version) }
// updateAnnotations returns an Annotation map containing the api annotation map plus // locally managed annotations func updateAnnotations(existing, ref *api.Pod) { annotations := make(map[string]string, len(ref.Annotations)+len(localAnnotations)) for k, v := range ref.Annotations { annotations[k] = v } for _, k := range localAnnotations { if v, ok := existing.Annotations[k]; ok { annotations[k] = v } } existing.Annotations = annotations }
func (r *fakeRegistry) bind(taskID string, pod *api.Pod) error { r.Lock() defer r.Unlock() pod.Annotations = map[string]string{ "k8s.mesosphere.io/taskId": taskID, } r.boundTasks[taskID] = pod // the normal registry sends a bind.. r.updates <- &PodEvent{pod: pod, taskID: taskID, eventType: PodEventBound} return nil }
func (b BuildDefaults) applyPodDefaults(pod *kapi.Pod) { if len(b.config.NodeSelector) != 0 && pod.Spec.NodeSelector == nil { // only apply nodeselector defaults if the pod has no nodeselector labels // already. pod.Spec.NodeSelector = map[string]string{} for k, v := range b.config.NodeSelector { addDefaultNodeSelector(k, v, pod.Spec.NodeSelector) } } if len(b.config.Annotations) != 0 && pod.Annotations == nil { pod.Annotations = map[string]string{} } for k, v := range b.config.Annotations { addDefaultAnnotations(k, v, pod.Annotations) } }
func (c *Controller) updateInstanceMetadata( pod *api.Pod, nic *types.VirtualMachineInterface, address, gateway string) { doUpdate := false if pod.Annotations == nil { pod.Annotations = make(map[string]string) doUpdate = true } var newValue, oldValue InstanceMetadata if data, ok := pod.Annotations[MetadataAnnotationTag]; ok { json.Unmarshal([]byte(data), &oldValue) } newValue.Uuid = nic.GetUuid() var mac_address string addressArr := nic.GetVirtualMachineInterfaceMacAddresses() if len(addressArr.MacAddress) > 0 { mac_address = addressArr.MacAddress[0] } else { glog.Errorf("interface %s: no mac-addresses", nic.GetName()) } newValue.MacAddress = mac_address newValue.IpAddress = address newValue.Gateway = gateway if !doUpdate && reflect.DeepEqual(newValue, oldValue) { return } encoded, err := json.Marshal(&newValue) if err != nil { glog.Errorf("JSON encode: %v", err) return } pod.Annotations[MetadataAnnotationTag] = string(encoded) _, err = c.kube.Pods(pod.Namespace).Update(pod) if err != nil { // Update will return an error if the pod object that we are // working with is stale. glog.Infof("Pod Update %s: %v", pod.Name, err) } }
func (f *typeBasedFactoryStrategy) CreateBuildPod(build *buildapi.Build) (*kapi.Pod, error) { var pod *kapi.Pod var err error switch build.Spec.Strategy.Type { case buildapi.DockerBuildStrategyType: pod, err = f.DockerBuildStrategy.CreateBuildPod(build) case buildapi.SourceBuildStrategyType: pod, err = f.SourceBuildStrategy.CreateBuildPod(build) case buildapi.CustomBuildStrategyType: pod, err = f.CustomBuildStrategy.CreateBuildPod(build) default: return nil, fmt.Errorf("no supported build strategy defined for Build %s/%s with type %s", build.Namespace, build.Name, build.Spec.Strategy.Type) } if pod != nil { if pod.Annotations == nil { pod.Annotations = map[string]string{} } pod.Annotations[buildapi.BuildAnnotation] = build.Name } return pod, err }
// assignSecurityContext creates a security context for each container in the pod // and validates that the sc falls within the psp constraints. All containers must validate against // the same psp or is not considered valid. func assignSecurityContext(provider psp.Provider, pod *api.Pod, fldPath *field.Path) field.ErrorList { generatedSCs := make([]*api.SecurityContext, len(pod.Spec.Containers)) var generatedInitSCs []*api.SecurityContext errs := field.ErrorList{} psc, pscAnnotations, err := provider.CreatePodSecurityContext(pod) if err != nil { errs = append(errs, field.Invalid(field.NewPath("spec", "securityContext"), pod.Spec.SecurityContext, err.Error())) } // save the original PSC and validate the generated PSC. Leave the generated PSC // set for container generation/validation. We will reset to original post container // validation. originalPSC := pod.Spec.SecurityContext pod.Spec.SecurityContext = psc originalAnnotations := maps.CopySS(pod.Annotations) pod.Annotations = pscAnnotations errs = append(errs, provider.ValidatePodSecurityContext(pod, field.NewPath("spec", "securityContext"))...) // Note: this is not changing the original container, we will set container SCs later so long // as all containers validated under the same PSP. for i, containerCopy := range pod.Spec.InitContainers { // We will determine the effective security context for the container and validate against that // since that is how the sc provider will eventually apply settings in the runtime. // This results in an SC that is based on the Pod's PSC with the set fields from the container // overriding pod level settings. containerCopy.SecurityContext = sc.InternalDetermineEffectiveSecurityContext(pod, &containerCopy) sc, scAnnotations, err := provider.CreateContainerSecurityContext(pod, &containerCopy) if err != nil { errs = append(errs, field.Invalid(field.NewPath("spec", "initContainers").Index(i).Child("securityContext"), "", err.Error())) continue } generatedInitSCs = append(generatedInitSCs, sc) containerCopy.SecurityContext = sc pod.Annotations = scAnnotations errs = append(errs, provider.ValidateContainerSecurityContext(pod, &containerCopy, field.NewPath("spec", "initContainers").Index(i).Child("securityContext"))...) } // Note: this is not changing the original container, we will set container SCs later so long // as all containers validated under the same PSP. for i, containerCopy := range pod.Spec.Containers { // We will determine the effective security context for the container and validate against that // since that is how the sc provider will eventually apply settings in the runtime. // This results in an SC that is based on the Pod's PSC with the set fields from the container // overriding pod level settings. containerCopy.SecurityContext = sc.InternalDetermineEffectiveSecurityContext(pod, &containerCopy) sc, scAnnotations, err := provider.CreateContainerSecurityContext(pod, &containerCopy) if err != nil { errs = append(errs, field.Invalid(field.NewPath("spec", "containers").Index(i).Child("securityContext"), "", err.Error())) continue } generatedSCs[i] = sc containerCopy.SecurityContext = sc pod.Annotations = scAnnotations errs = append(errs, provider.ValidateContainerSecurityContext(pod, &containerCopy, field.NewPath("spec", "containers").Index(i).Child("securityContext"))...) } if len(errs) > 0 { // ensure psc is not mutated if there are errors pod.Spec.SecurityContext = originalPSC pod.Annotations = originalAnnotations return errs } // if we've reached this code then we've generated and validated an SC for every container in the // pod so let's apply what we generated. Note: the psc is already applied. for i, sc := range generatedInitSCs { pod.Spec.InitContainers[i].SecurityContext = sc } for i, sc := range generatedSCs { pod.Spec.Containers[i].SecurityContext = sc } return nil }
// assignSecurityContext creates a security context for each container in the pod // and validates that the sc falls within the scc constraints. All containers must validate against // the same scc or is not considered valid. func assignSecurityContext(provider scc.SecurityContextConstraintsProvider, pod *kapi.Pod, fldPath *field.Path) field.ErrorList { generatedSCs := make([]*kapi.SecurityContext, len(pod.Spec.InitContainers)+len(pod.Spec.Containers)) errs := field.ErrorList{} psc, generatedAnnotations, err := provider.CreatePodSecurityContext(pod) if err != nil { errs = append(errs, field.Invalid(field.NewPath("spec", "securityContext"), pod.Spec.SecurityContext, err.Error())) } // save the original PSC and validate the generated PSC. Leave the generated PSC // set for container generation/validation. We will reset to original post container // validation. originalPSC := pod.Spec.SecurityContext originalAnnotations := pod.Annotations pod.Spec.SecurityContext = psc pod.Annotations = generatedAnnotations errs = append(errs, provider.ValidatePodSecurityContext(pod, field.NewPath("spec", "securityContext"))...) // Note: this is not changing the original container, we will set container SCs later so long // as all containers validated under the same SCC. containerPath := field.NewPath("spec", "initContainers") for i, containerCopy := range pod.Spec.InitContainers { csc, resolutionErrs := resolveContainerSecurityContext(provider, pod, &containerCopy, containerPath.Index(i)) errs = append(errs, resolutionErrs...) if len(resolutionErrs) > 0 { continue } generatedSCs[i] = csc } base := len(pod.Spec.InitContainers) // Note: this is not changing the original container, we will set container SCs later so long // as all containers validated under the same SCC. containerPath = field.NewPath("spec", "containers") for i, containerCopy := range pod.Spec.Containers { csc, resolutionErrs := resolveContainerSecurityContext(provider, pod, &containerCopy, containerPath.Index(i)) errs = append(errs, resolutionErrs...) if len(resolutionErrs) > 0 { continue } generatedSCs[i+base] = csc } if len(errs) > 0 { // ensure psc is not mutated if there are errors pod.Spec.SecurityContext = originalPSC pod.Annotations = originalAnnotations return errs } // if we've reached this code then we've generated and validated an SC for every container in the // pod so let's apply what we generated. Note: the psc is already applied. for i := range pod.Spec.InitContainers { pod.Spec.InitContainers[i].SecurityContext = generatedSCs[i] } for i := range pod.Spec.Containers { pod.Spec.Containers[i].SecurityContext = generatedSCs[i+base] } return nil }