Skip to content

Commit 13809fc

Browse files
Daniel-Fanbitscuit
andauthored
Handle NamespaceScope ENV Variable Patching issue (#258)
* List all CSVs for given package manifest, Use DeepEqual to compare CSVs containing nil struct, Add more information logs for debugging purpose Signed-off-by: Daniel Fan <[email protected]> * avoid Implicit memory aliasing in for loop Signed-off-by: Daniel Fan <[email protected]> * Keep short for temporary variable Co-authored-by: Henry Li <[email protected]> * fix format issue --------- Signed-off-by: Daniel Fan <[email protected]> Co-authored-by: Henry Li <[email protected]>
1 parent 41d6c59 commit 13809fc

File tree

2 files changed

+56
-50
lines changed

2 files changed

+56
-50
lines changed

controllers/common/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ func GetOperatorNamespace() (string, error) {
125125
if len(ns) == 0 {
126126
return "", fmt.Errorf("operator namespace is empty")
127127
}
128-
klog.V(1).Info("Found namespace", "Namespace", ns)
128+
klog.V(1).Infof("Found namespace: %s", ns)
129129
return ns, nil
130130
}

controllers/namespacescope_controller.go

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,64 +1083,70 @@ func (r *NamespaceScopeReconciler) CSVReconcile(req ctrl.Request) (ctrl.Result,
10831083
continue
10841084
}
10851085
patchedCSVList = append(patchedCSVList, packageName.(string))
1086-
csv := csvList.Items[0]
1087-
csvOriginal := csv.DeepCopy()
1088-
if csv.Spec.InstallStrategy.StrategyName != "deployment" {
1089-
continue
1090-
}
1091-
deploymentSpecs := csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs
1092-
for _, deploy := range deploymentSpecs {
1093-
podTemplate := deploy.Spec.Template
1094-
// Insert restartlabel into operator pods
1095-
if podTemplate.Labels == nil {
1096-
podTemplate.Labels = make(map[string]string)
1097-
}
1098-
for k, v := range instance.Spec.RestartLabels {
1099-
podTemplate.Labels[k] = v
1086+
1087+
for _, c := range csvList.Items {
1088+
// avoid Implicit memory aliasing in for loop
1089+
csv := c
1090+
klog.V(2).Infof("Found CSV %s for packageManifest %s", csv.Name, packageName.(string))
1091+
csvOriginal := csv.DeepCopy()
1092+
if csv.Spec.InstallStrategy.StrategyName != "deployment" {
1093+
continue
11001094
}
1101-
// Insert WATCH_NAMESPACE into operator pod environment variables
1102-
for containerIndex, container := range podTemplate.Spec.Containers {
1103-
var found bool
1104-
optional := true
1105-
configmapEnv := corev1.EnvVar{
1106-
Name: "WATCH_NAMESPACE",
1107-
ValueFrom: &corev1.EnvVarSource{
1108-
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
1109-
Optional: &optional,
1110-
Key: "namespaces",
1111-
LocalObjectReference: corev1.LocalObjectReference{
1112-
Name: configmapName,
1095+
deploymentSpecs := csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs
1096+
for _, deploy := range deploymentSpecs {
1097+
podTemplate := deploy.Spec.Template
1098+
// Insert restartlabel into operator pods
1099+
if podTemplate.Labels == nil {
1100+
podTemplate.Labels = make(map[string]string)
1101+
}
1102+
for k, v := range instance.Spec.RestartLabels {
1103+
podTemplate.Labels[k] = v
1104+
}
1105+
// Insert WATCH_NAMESPACE into operator pod environment variables
1106+
for containerIndex, container := range podTemplate.Spec.Containers {
1107+
var found bool
1108+
optional := true
1109+
configmapEnv := corev1.EnvVar{
1110+
Name: "WATCH_NAMESPACE",
1111+
ValueFrom: &corev1.EnvVarSource{
1112+
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
1113+
Optional: &optional,
1114+
Key: "namespaces",
1115+
LocalObjectReference: corev1.LocalObjectReference{
1116+
Name: configmapName,
1117+
},
11131118
},
11141119
},
1115-
},
1116-
}
1117-
for index, env := range container.Env {
1118-
if env.Name == "WATCH_NAMESPACE" {
1119-
found = true
1120-
if env.ValueFrom.ConfigMapKeyRef != nil && env.ValueFrom.ConfigMapKeyRef.Key == "namespaces" && env.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name == configmapName {
1121-
continue
1120+
}
1121+
for index, env := range container.Env {
1122+
if env.Name == "WATCH_NAMESPACE" {
1123+
found = true
1124+
if env.ValueFrom.ConfigMapKeyRef != nil && env.ValueFrom.ConfigMapKeyRef.Key == "namespaces" && env.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name == configmapName {
1125+
klog.V(2).Infof("WATCH_NAMESPACE ENV variable is found in CSV %s, and match the configmap %s, skip it", csv.Name, configmapName)
1126+
continue
1127+
}
1128+
klog.V(2).Infof("WATCH_NAMESPACE ENV variable is found in CSV %s, but not match the configmap %s, replace it", csv.Name, configmapName)
1129+
container.Env[index] = configmapEnv
11221130
}
1123-
container.Env[index] = configmapEnv
11241131
}
1132+
if !found {
1133+
klog.V(2).Infof("WATCH_NAMESPACE ENV variable is not found in CSV %s, insert it", csv.Name)
1134+
container.Env = append(container.Env, configmapEnv)
1135+
}
1136+
podTemplate.Spec.Containers[containerIndex] = container
11251137
}
1126-
if !found {
1127-
container.Env = append(container.Env, configmapEnv)
1128-
}
1129-
podTemplate.Spec.Containers[containerIndex] = container
11301138
}
1131-
}
1132-
1133-
if equality.Semantic.DeepDerivative(csvOriginal.Spec.InstallStrategy.StrategySpec.DeploymentSpecs, csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs) {
1134-
klog.V(3).Infof("No updates in the CSV, skip patching the CSV %s ", csv.Name)
1135-
continue
1136-
}
1139+
if equality.Semantic.DeepEqual(csvOriginal.Spec.InstallStrategy.StrategySpec.DeploymentSpecs, csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs) {
1140+
klog.V(3).Infof("No updates in the CSV, skip patching the CSV %s ", csv.Name)
1141+
continue
1142+
}
11371143

1138-
if err := r.Client.Patch(ctx, &csv, client.MergeFrom(csvOriginal)); err != nil {
1139-
klog.Error(err)
1140-
return ctrl.Result{}, err
1144+
if err := r.Client.Patch(ctx, &csv, client.MergeFrom(csvOriginal)); err != nil {
1145+
klog.Error(err)
1146+
return ctrl.Result{}, err
1147+
}
1148+
klog.V(1).Infof("WATCH_NAMESPACE and restart labels are inserted into CSV %s ", csv.Name)
11411149
}
1142-
klog.V(1).Infof("WATCH_NAMESPACE and restart labels are inserted into CSV %s ", csv.Name)
1143-
11441150
}
11451151

11461152
if util.CheckListDifference(instance.Status.ManagedCSVList, managedCSVList) {

0 commit comments

Comments
 (0)