Skip to content

Commit be70196

Browse files
committed
feat: pass registry auth secret as --image-pull-secret to func CLI
When spec.registry.authSecretRef is set, pass the secret name via --image-pull-secret so the func CLI sets imagePullSecrets directly on the function's pod spec.
1 parent a83330e commit be70196

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

internal/controller/function_controller_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,72 @@ var _ = Describe("Function Controller", func() {
530530
},
531531
}),
532532
)
533+
534+
It("should pass ImagePullSecret to deploy when registry authSecretRef is set", func() {
535+
registrySecretName := "my-registry-secret"
536+
537+
By("Creating the registry auth secret")
538+
secret := &v1.Secret{
539+
ObjectMeta: metav1.ObjectMeta{
540+
Name: registrySecretName,
541+
Namespace: resourceNamespace,
542+
},
543+
Type: v1.SecretTypeDockerConfigJson,
544+
Data: map[string][]byte{
545+
v1.DockerConfigJsonKey: []byte(`{"auths":{"registry.example.com":{"auth":"dGVzdDp0ZXN0"}}}`),
546+
},
547+
}
548+
Expect(k8sClient.Create(ctx, secret)).To(Succeed())
549+
defer func() { _ = k8sClient.Delete(ctx, secret) }()
550+
551+
By("Creating the Function with registry authSecretRef")
552+
spec := functionsdevv1alpha1.FunctionSpec{
553+
Repository: functionsdevv1alpha1.FunctionSpecRepository{
554+
URL: "https://github.com/foo/bar",
555+
Branch: "my-branch",
556+
},
557+
Registry: functionsdevv1alpha1.FunctionSpecRegistry{
558+
AuthSecretRef: &v1.LocalObjectReference{
559+
Name: registrySecretName,
560+
},
561+
},
562+
}
563+
Expect(createFunctionResource(resourceName, resourceNamespace, spec)).To(Succeed())
564+
565+
By("Setting up mocks")
566+
funcCliManagerMock := funccli.NewMockManager(GinkgoT())
567+
gitManagerMock := git.NewMockManager(GinkgoT())
568+
569+
funcCliManagerMock.EXPECT().Describe(mock.Anything, functionName, resourceNamespace).Return(functions.Instance{
570+
Middleware: functions.Middleware{Version: "v1.0.0"},
571+
}, nil)
572+
funcCliManagerMock.EXPECT().GetLatestMiddlewareVersion(mock.Anything, mock.Anything, mock.Anything).Return("v2.0.0", nil)
573+
574+
funcCliManagerMock.EXPECT().Deploy(mock.Anything, mock.Anything, resourceNamespace, mock.MatchedBy(func(opts funccli.DeployOptions) bool {
575+
return opts.ImagePullSecret == registrySecretName && opts.RegistryAuthFile != ""
576+
})).Return(nil)
577+
578+
gitManagerMock.EXPECT().CloneRepository(mock.Anything, "https://github.com/foo/bar", "", "my-branch", mock.Anything).Return(createTmpGitRepo(functions.Function{Name: "func-go"}), nil)
579+
580+
operatorNs := fmt.Sprintf("func-operator-%s", rand.String(6))
581+
Expect(createNamespace(operatorNs)).To(Succeed())
582+
Expect(createControllerConfig(operatorNs, nil)).To(Succeed())
583+
584+
By("Reconciling")
585+
controllerReconciler := &FunctionReconciler{
586+
Client: k8sClient,
587+
Scheme: k8sClient.Scheme(),
588+
Recorder: &events.FakeRecorder{},
589+
FuncCliManager: funcCliManagerMock,
590+
GitManager: gitManagerMock,
591+
OperatorNamespace: operatorNs,
592+
}
593+
594+
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
595+
NamespacedName: typeNamespacedName,
596+
})
597+
Expect(err).NotTo(HaveOccurred())
598+
})
533599
})
534600
})
535601

internal/controller/function_deploy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (r *FunctionReconciler) deploy(ctx context.Context, function *v1alpha1.Func
4949
defer os.Remove(authFile)
5050

5151
deployOptions.RegistryAuthFile = authFile
52+
deployOptions.ImagePullSecret = function.Spec.Registry.AuthSecretRef.Name
5253
}
5354

5455
logger.Info("Deploying function", "deployOptions", deployOptions)

internal/funccli/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Manager interface {
4343

4444
type DeployOptions struct {
4545
RegistryAuthFile string
46+
ImagePullSecret string
4647
}
4748

4849
var _ Manager = &managerImpl{}
@@ -223,6 +224,10 @@ func (m *managerImpl) Deploy(ctx context.Context, repoPath string, namespace str
223224
deployArgs = append(deployArgs, "--registry-authfile", opts.RegistryAuthFile)
224225
}
225226

227+
if opts.ImagePullSecret != "" {
228+
deployArgs = append(deployArgs, "--image-pull-secret", opts.ImagePullSecret)
229+
}
230+
226231
out, err := m.Run(ctx, repoPath, deployArgs...)
227232
if err != nil {
228233
return fmt.Errorf("failed to deploy function: %q. %w", out, err)

0 commit comments

Comments
 (0)