Skip to content

Commit e80dad1

Browse files
Merge pull request #925 from yevgeny-shnaidman/yevgeny/fixing-image-repo-secret
Fixing handling ImageRepoSecret in worker pod
2 parents 7f322c0 + 1167ba7 commit e80dad1

File tree

3 files changed

+74
-263
lines changed

3 files changed

+74
-263
lines changed

internal/controllers/mock_nmc_reconciler.go

Lines changed: 1 addition & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controllers/nmc_reconciler.go

Lines changed: 10 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"k8s.io/apimachinery/pkg/util/sets"
2929
"k8s.io/client-go/tools/record"
3030
"k8s.io/kubectl/pkg/cmd/util/podcmd"
31-
"k8s.io/utils/ptr"
3231
ctrl "sigs.k8s.io/controller-runtime"
3332
"sigs.k8s.io/controller-runtime/pkg/builder"
3433
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -749,7 +748,6 @@ type podManager interface {
749748

750749
type podManagerImpl struct {
751750
client client.Client
752-
psh pullSecretHelper
753751
scheme *runtime.Scheme
754752
workerCfg *config.Worker
755753
workerImage string
@@ -758,7 +756,6 @@ type podManagerImpl struct {
758756
func newPodManager(client client.Client, workerImage string, scheme *runtime.Scheme, workerCfg *config.Worker) podManager {
759757
return &podManagerImpl{
760758
client: client,
761-
psh: &pullSecretHelperImpl{client: client},
762759
scheme: scheme,
763760
workerCfg: workerCfg,
764761
workerImage: workerImage,
@@ -982,11 +979,6 @@ func (p *podManagerImpl) baseWorkerPod(ctx context.Context, nmc client.Object, i
982979

983980
hostPathDirectory := v1.HostPathDirectory
984981

985-
psv, psvm, err := p.psh.VolumesAndVolumeMounts(ctx, item)
986-
if err != nil {
987-
return nil, fmt.Errorf("could not list pull secrets for worker Pod: %v", err)
988-
}
989-
990982
volumes := []v1.Volume{
991983
{
992984
Name: volumeNameConfig,
@@ -1052,6 +1044,11 @@ func (p *podManagerImpl) baseWorkerPod(ctx context.Context, nmc client.Object, i
10521044
},
10531045
}
10541046

1047+
var imagePullSecrets []v1.LocalObjectReference
1048+
if item.ImageRepoSecret != nil {
1049+
imagePullSecrets = append(imagePullSecrets, *item.ImageRepoSecret)
1050+
}
1051+
10551052
nodeName := nmc.GetName()
10561053
pod := v1.Pod{
10571054
ObjectMeta: metav1.ObjectMeta{
@@ -1088,7 +1085,7 @@ func (p *podManagerImpl) baseWorkerPod(ctx context.Context, nmc client.Object, i
10881085
{
10891086
Name: workerContainerName,
10901087
Image: p.workerImage,
1091-
VolumeMounts: append(volumeMounts, psvm...),
1088+
VolumeMounts: volumeMounts,
10921089
Resources: v1.ResourceRequirements{
10931090
Requests: requests,
10941091
Limits: limits,
@@ -1098,17 +1095,18 @@ func (p *podManagerImpl) baseWorkerPod(ctx context.Context, nmc client.Object, i
10981095
NodeName: nodeName,
10991096
RestartPolicy: v1.RestartPolicyOnFailure,
11001097
ServiceAccountName: item.ServiceAccountName,
1101-
Volumes: append(volumes, psv...),
1098+
ImagePullSecrets: imagePullSecrets,
1099+
Volumes: volumes,
11021100
},
11031101
}
11041102

1105-
if err = ctrl.SetControllerReference(nmc, &pod, p.scheme); err != nil {
1103+
if err := ctrl.SetControllerReference(nmc, &pod, p.scheme); err != nil {
11061104
return nil, fmt.Errorf("could not set the owner as controller: %v", err)
11071105
}
11081106

11091107
kmodsPathContainerImg := filepath.Join(moduleConfig.Modprobe.DirName, "lib", "modules", moduleConfig.KernelVersion)
11101108
kmodsPathWorkerImg := filepath.Join(sharedFilesDir, moduleConfig.Modprobe.DirName, "lib", "modules")
1111-
if err = addCopyCommand(&pod, kmodsPathContainerImg, kmodsPathWorkerImg); err != nil {
1109+
if err := addCopyCommand(&pod, kmodsPathContainerImg, kmodsPathWorkerImg); err != nil {
11121110
return nil, fmt.Errorf("could not add the copy command to the init container: %v", err)
11131111
}
11141112

@@ -1260,97 +1258,3 @@ func getModulesOrderAnnotationValue(modulesNames []string) string {
12601258
}
12611259
return softDepData.String()
12621260
}
1263-
1264-
//go:generate mockgen -source=nmc_reconciler.go -package=controllers -destination=mock_nmc_reconciler.go pullSecretHelper
1265-
1266-
type pullSecretHelper interface {
1267-
VolumesAndVolumeMounts(ctx context.Context, nms *kmmv1beta1.ModuleItem) ([]v1.Volume, []v1.VolumeMount, error)
1268-
}
1269-
1270-
type pullSecretHelperImpl struct {
1271-
client client.Client
1272-
}
1273-
1274-
func (p *pullSecretHelperImpl) VolumesAndVolumeMounts(ctx context.Context, item *kmmv1beta1.ModuleItem) ([]v1.Volume, []v1.VolumeMount, error) {
1275-
logger := ctrl.LoggerFrom(ctx)
1276-
1277-
secretNames := sets.New[string]()
1278-
1279-
type pullSecret struct {
1280-
secretName string
1281-
volumeName string
1282-
optional bool
1283-
}
1284-
1285-
pullSecrets := make([]pullSecret, 0)
1286-
1287-
if irs := item.ImageRepoSecret; irs != nil {
1288-
secretNames.Insert(irs.Name)
1289-
1290-
ps := pullSecret{
1291-
secretName: irs.Name,
1292-
volumeName: volNameImageRepoSecret,
1293-
}
1294-
1295-
pullSecrets = append(pullSecrets, ps)
1296-
}
1297-
1298-
if san := item.ServiceAccountName; san != "" {
1299-
sa := v1.ServiceAccount{}
1300-
nsn := types.NamespacedName{Namespace: item.Namespace, Name: san}
1301-
1302-
logger.V(1).Info("Getting service account", "name", nsn)
1303-
1304-
if err := p.client.Get(ctx, nsn, &sa); err != nil {
1305-
return nil, nil, fmt.Errorf("could not get ServiceAccount %s: %v", nsn, err)
1306-
}
1307-
1308-
for _, s := range sa.ImagePullSecrets {
1309-
if secretNames.Has(s.Name) {
1310-
continue
1311-
}
1312-
1313-
secretNames.Insert(s.Name)
1314-
1315-
hashValue, err := hashstructure.Hash(s.Name, hashstructure.FormatV2, nil)
1316-
if err != nil {
1317-
return nil, nil, fmt.Errorf("failed to hash secret %s: %v", s.Name, err)
1318-
}
1319-
1320-
ps := pullSecret{
1321-
secretName: s.Name,
1322-
volumeName: fmt.Sprintf("pull-secret-%d", hashValue),
1323-
optional: true, // to match the node's container runtime behaviour
1324-
}
1325-
1326-
pullSecrets = append(pullSecrets, ps)
1327-
}
1328-
}
1329-
1330-
volumes := make([]v1.Volume, 0, len(pullSecrets))
1331-
volumeMounts := make([]v1.VolumeMount, 0, len(pullSecrets))
1332-
1333-
for _, s := range pullSecrets {
1334-
v := v1.Volume{
1335-
Name: s.volumeName,
1336-
VolumeSource: v1.VolumeSource{
1337-
Secret: &v1.SecretVolumeSource{
1338-
SecretName: s.secretName,
1339-
Optional: ptr.To(s.optional),
1340-
},
1341-
},
1342-
}
1343-
1344-
volumes = append(volumes, v)
1345-
1346-
vm := v1.VolumeMount{
1347-
Name: s.volumeName,
1348-
ReadOnly: true,
1349-
MountPath: filepath.Join(worker.PullSecretsDir, s.secretName),
1350-
}
1351-
1352-
volumeMounts = append(volumeMounts, vm)
1353-
}
1354-
1355-
return volumes, volumeMounts, nil
1356-
}

0 commit comments

Comments
 (0)