@@ -17,6 +17,7 @@ limitations under the License.
1717package e2e
1818
1919import (
20+ "encoding/json"
2021 "fmt"
2122 "os"
2223 "os/exec"
@@ -30,6 +31,7 @@ import (
3031 v1 "k8s.io/api/core/v1"
3132 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233 "k8s.io/apimachinery/pkg/types"
34+ funcfn "knative.dev/func/pkg/functions"
3335)
3436
3537// expectFunctionConditionTrue returns a Gomega function that checks if a Function
@@ -746,4 +748,133 @@ var _ = Describe("Operator", func() {
746748 Eventually (functionNotReadyWithAuthError (functionName , functionNamespace ), 2 * time .Minute ).Should (Succeed ())
747749 })
748750 })
751+ // This test verifies that the operator passes the registry auth secret as
752+ // --image-pull-secret to the func CLI during a redeploy, which causes the
753+ // func CLI to set imagePullSecrets on the function's pod spec.
754+ //
755+ // It uses a dummy dockerconfigjson secret and the unauthenticated kind-registry
756+ // because the kind-registry's built-in htpasswd auth is all-or-nothing (no
757+ // per-repository scoping), so enabling auth would break all other tests. The
758+ // unit tests in function_controller_test.go verify the --image-pull-secret flag
759+ // is passed; this test confirms the Knative Service's pod template actually
760+ // receives the imagePullSecrets after a real redeploy.
761+ Context ("with a registry auth secret" , func () {
762+ var repoURL string
763+ var repoDir string
764+ var functionName , functionNamespace string
765+
766+ BeforeEach (func () {
767+ if os .Getenv ("DEFAULT_DEPLOYER" ) == "keda" ||
768+ os .Getenv ("DEFAULT_DEPLOYER" ) == "raw" {
769+ Skip ("Skipping registry auth test for Keda & raw deployer, " +
770+ "as test inspect KService directly" )
771+ }
772+
773+ var err error
774+
775+ username , password , _ , cleanup , err := repoProvider .CreateRandomUser ()
776+ Expect (err ).NotTo (HaveOccurred ())
777+ utils .DeferCleanupOnSuccess (cleanup )
778+
779+ _ , repoURL , cleanup , err = repoProvider .CreateRandomRepo (username , false )
780+ Expect (err ).NotTo (HaveOccurred ())
781+ utils .DeferCleanupOnSuccess (cleanup )
782+
783+ functionNamespace , err = utils .GetTestNamespace ()
784+ Expect (err ).NotTo (HaveOccurred ())
785+ utils .DeferCleanupOnSuccess (cleanupNamespaces , functionNamespace )
786+
787+ oldFuncVersion := "v1.20.2"
788+ repoDir , err = utils .InitializeRepoWithFunction (
789+ repoURL ,
790+ username ,
791+ password ,
792+ "go" ,
793+ utils .WithCliVersion (oldFuncVersion ))
794+ Expect (err ).NotTo (HaveOccurred ())
795+ utils .DeferCleanupOnSuccess (os .RemoveAll , repoDir )
796+
797+ out , err := utils .RunFuncDeploy (repoDir ,
798+ utils .WithNamespace (functionNamespace ),
799+ utils .WithDeployCliVersion (oldFuncVersion ))
800+ Expect (err ).NotTo (HaveOccurred ())
801+ _ , _ = fmt .Fprint (GinkgoWriter , out )
802+
803+ utils .DeferCleanupOnSuccess (func () {
804+ _ , _ = utils .RunFunc ("delete" , "--path" , repoDir , "--namespace" , functionNamespace )
805+ })
806+
807+ err = utils .CommitAndPush (repoDir , "Update func.yaml after deploy" , "func.yaml" )
808+ Expect (err ).NotTo (HaveOccurred ())
809+ })
810+
811+ AfterEach (func () {
812+ logFailedTestDetails (functionName , functionNamespace )
813+ })
814+
815+ It ("should set imagePullSecrets on the Knative Service" , func () {
816+ funcMetadata , err := funcfn .NewFunction (repoDir )
817+ Expect (err ).NotTo (HaveOccurred ())
818+ deployedFunctionName := funcMetadata .Name
819+
820+ secret := & v1.Secret {
821+ ObjectMeta : metav1.ObjectMeta {
822+ GenerateName : "registry-auth-" ,
823+ Namespace : functionNamespace ,
824+ },
825+ Type : v1 .SecretTypeDockerConfigJson ,
826+ Data : map [string ][]byte {
827+ v1 .DockerConfigJsonKey : []byte (`{"auths":{"kind-registry:5000":{"auth":"dGVzdDp0ZXN0"}}}` ),
828+ },
829+ }
830+ err = k8sClient .Create (ctx , secret )
831+ Expect (err ).NotTo (HaveOccurred ())
832+ utils .DeferCleanupOnSuccess (func () {
833+ _ = k8sClient .Delete (ctx , secret )
834+ })
835+
836+ function := & functionsdevv1alpha1.Function {
837+ ObjectMeta : metav1.ObjectMeta {
838+ GenerateName : "my-function-pullsecret-" ,
839+ Namespace : functionNamespace ,
840+ },
841+ Spec : functionsdevv1alpha1.FunctionSpec {
842+ Repository : functionsdevv1alpha1.FunctionSpecRepository {
843+ URL : repoURL ,
844+ },
845+ Registry : functionsdevv1alpha1.FunctionSpecRegistry {
846+ AuthSecretRef : & v1.LocalObjectReference {
847+ Name : secret .Name ,
848+ },
849+ },
850+ },
851+ }
852+
853+ err = k8sClient .Create (ctx , function )
854+ Expect (err ).NotTo (HaveOccurred ())
855+ utils .DeferCleanupOnSuccess (func () {
856+ _ , _ = utils .RunCmd ("kubectl" , "delete" , "function" , function .Name , "--namespace" , function .Namespace )
857+ })
858+
859+ functionName = function .Name
860+
861+ Eventually (functionBecomesReady (functionName , functionNamespace )).Should (Succeed ())
862+
863+ // Verify the Knative Service has imagePullSecrets set on its pod template.
864+ Eventually (func (g Gomega ) {
865+ cmd := exec .Command ("kubectl" , "get" , "ksvc" , deployedFunctionName ,
866+ "-n" , functionNamespace ,
867+ "-o" , "jsonpath={.spec.template.spec.imagePullSecrets}" )
868+ out , err := utils .Run (cmd )
869+ g .Expect (err ).NotTo (HaveOccurred ())
870+ g .Expect (out ).NotTo (BeEmpty (), "imagePullSecrets not set on Knative Service %s" , deployedFunctionName )
871+
872+ var pullSecrets []v1.LocalObjectReference
873+ g .Expect (json .Unmarshal ([]byte (out ), & pullSecrets )).To (Succeed ())
874+ g .Expect (pullSecrets ).To (ContainElement (v1.LocalObjectReference {
875+ Name : secret .Name ,
876+ }))
877+ }, 2 * time .Minute ).Should (Succeed ())
878+ })
879+ })
749880})
0 commit comments