Skip to content

Commit

Permalink
allow using file:// to manage packages
Browse files Browse the repository at this point in the history
  • Loading branch information
freeznet committed Feb 19, 2025
1 parent 51b9363 commit 9801dec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
30 changes: 26 additions & 4 deletions pkg/connection/reconcile_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,30 @@ func (r *PulsarPackageReconciler) ReconcilePackage(ctx context.Context, pulsarAd
return nil
}

filePath, err := createTmpFile(pkg.Spec.FileURL)
parsedFileURL, err := url.Parse(pkg.Spec.FileURL)
if err != nil {
log.Error(err, "Failed to download the package file")
return err
return fmt.Errorf("invalid FileURL: %v", err)
}
if !isSupportedScheme(parsedFileURL.Scheme) {
return fmt.Errorf("unsupported scheme: %s", parsedFileURL.Scheme)
}

filePath := ""
switch parsedFileURL.Scheme {
case "http", "https":
filePath, err = createTmpFile(pkg.Spec.FileURL)
if err != nil {
log.Error(err, "Failed to download the package file")
return err
}
defer os.Remove(filePath)
case "file":
// check parsedFileUrl.Path is a valid file path
if _, err := os.Stat(parsedFileURL.Path); err != nil {
return fmt.Errorf("invalid file path: %s", parsedFileURL.Path)
}
filePath = parsedFileURL.Path
}
defer os.Remove(filePath)

updated := false
if exist, err := pulsarAdmin.CheckPulsarPackageExist(pkg.Spec.PackageURL); err != nil {
Expand Down Expand Up @@ -198,3 +216,7 @@ func createTmpFile(fileURL string) (string, error) {

return tmpFile.Name(), nil
}

func isSupportedScheme(scheme string) bool {
return scheme == "http" || scheme == "https" || scheme == "file"
}
19 changes: 19 additions & 0 deletions tests/operator/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var _ = Describe("Resources", func() {
psource *v1alphav1.PulsarSource
pnsisolationpolicy *v1alphav1.PulsarNSIsolationPolicy
psourcepackageurl string = "builtin://data-generator"
invalidFileURL string = "file://invalid-file-path"
)

BeforeEach(func() {
Expand Down Expand Up @@ -496,6 +497,24 @@ var _ = Describe("Resources", func() {
})
})

Context("PulsarPackage operation with invalid file URL", func() {
It("should create the pulsarpackage failed with invalid file URL", func() {
ppackage.Spec.FileURL = invalidFileURL
ppackage.Spec.PackageURL = "function://public/default/file@invalid"
err := k8sClient.Create(ctx, ppackage)
Expect(err != nil).Should(BeTrue())
})
})

Context("PulsarPackage operation with valid file URL", func() {
It("should create the pulsarpackage successfully with valid file URL", func() {
ppackage.Spec.FileURL = "file:///manager" // we use the manager binary as the file URL
ppackage.Spec.PackageURL = "function://public/default/file@valid"
err := k8sClient.Create(ctx, ppackage)
Expect(err == nil || apierrors.IsAlreadyExists(err)).Should(BeTrue())
})
})

AfterAll(func() {
Eventually(func(g Gomega) {
t := &v1alphav1.PulsarTopic{}
Expand Down

0 comments on commit 9801dec

Please sign in to comment.