PodPreset是一种K8sAPI资源,用于在创建 Pod 时注入其他运行时需要的信息,这些信息包括 secrets、volume mounts、environment variables等,我们可以使用标签选择器来指定某个或某些 Pod,来将 PodPreset 预设信息应用上去。使用 PodPreset 的好处就是我们可以将一些常用 Pod 预设信息配置为模板,这样就不需要显式为每个 Pod 提供所有信息,简化 Pod 初始化配置,还能起到配置统一的效果.
// filterPodPresets returns list of PodPresets which match given Pod. funcfilterPodPresets(logger logr.Logger, list redhatcopv1alpha1.PodPresetList, pod *corev1.Pod) ([]*redhatcopv1alpha1.PodPreset, error) { var matchingPPs []*redhatcopv1alpha1.PodPreset
podnamerequiredvalue, found := selector.RequiresExactMatch("podnamerequired") logger.Info("checking if found RequiresExactMatch podnamerequired")
if found { logger.Info("podnamerequiredvalue=" + podnamerequiredvalue) if podnamerequiredvalue != pod.GetName() { logger.Info("podnamerequiredvalue not matching the pod name:" + pod.GetName() + "!=" + podnamerequiredvalue + "=====> next loop") continue } else { logger.Info("podnamerequiredvalue matching pod:" + pod.GetName() + "==" + podnamerequiredvalue) // check if general matching lbls := pod.Labels lbls["podnamerequired"] = pod.GetName() logger.Info("labels.Set(lbls).String()=" + labels.Set(lbls).String()) // check if the pod labels match the selector if !selector.Matches(labels.Set(lbls)) { logger.Info("!selector.Matches(labels.Set(lbls)=====> next loop") continue } } } else { // check if the pod labels match the selector (generic case, no requirements on pod name) if !selector.Matches(labels.Set(pod.Labels)) { logger.Info("!selector.Matches(labels.Set(lbls)=====> next loop") continue } } // fixed this matchingPPs = append(matchingPPs, &list.Items[i]) } iflen(matchingPPs) == 0 { logger.Info("######### no final preset for pod=" + pod.GetName()) } else { for _, ppr := range matchingPPs { logger.Info("##############final preset for pod=" + pod.GetName() + " is name=" + ppr.GetName()) } } return matchingPPs, nil }