Skip to content

Commit

Permalink
fix: fixes version check for Artifact Hub generation
Browse files Browse the repository at this point in the history
Signed-off-by: Nilekh Chaudhari <1626598+nilekhc@users.noreply.github.com>
  • Loading branch information
nilekhc committed May 4, 2023
1 parent e8e0b71 commit 377600b
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions scripts/artifacthub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -123,9 +124,11 @@ func main() {
panic(err)
}

githubSourceRelativePath := filepath.Join(entryPoint, entry.Name(), dir.Name(), "template.yaml")
createVersionDirectory(
rootDir,
filepath.Join(entryPoint, entry.Name(), dir.Name()),
githubSourceRelativePath,
constraintTemplate,
)
}
Expand All @@ -134,7 +137,7 @@ func main() {
}
}

func createVersionDirectory(rootDir, basePath string, constraintTemplate map[string]interface{}) {
func createVersionDirectory(rootDir, basePath, githubSourceRelativePath string, constraintTemplate map[string]interface{}) {
version := fmt.Sprintf("%s", constraintTemplate["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})["metadata.gatekeeper.sh/version"])

// create directory if not exists
Expand All @@ -152,7 +155,7 @@ func createVersionDirectory(rootDir, basePath string, constraintTemplate map[str

// create artifacthub-pkg.yml file first then copy rest of the files. This will avoid unnecessary diff if there is any error while generating or updating artifacthub-pkg.yml
// add artifact hub metadata
addArtifactHubMetadata(filepath.Base(source), destination, ahBasePath, constraintTemplate)
addArtifactHubMetadata(filepath.Base(source), destination, ahBasePath, githubSourceRelativePath, constraintTemplate)

// copy directory content
err := copyDirectory(source, destination)
Expand All @@ -162,7 +165,7 @@ func createVersionDirectory(rootDir, basePath string, constraintTemplate map[str
}
}

func addArtifactHubMetadata(sourceDirectory, destinationPath, ahBasePath string, constraintTemplate map[string]interface{}) {
func addArtifactHubMetadata(sourceDirectory, destinationPath, ahBasePath, githubSourceRelativePath string, constraintTemplate map[string]interface{}) {
metadataFilePath := filepath.Join(destinationPath, "artifacthub-pkg.yml")

constraintTemplateHash := getConstraintTemplateHash(constraintTemplate)
Expand Down Expand Up @@ -199,7 +202,7 @@ func addArtifactHubMetadata(sourceDirectory, destinationPath, ahBasePath string,
}
} else {
// when metadata file already exists, check version to make sure it's updated if constraint template is changed
err := checkVersion(artifactHubMetadata, constraintTemplate, constraintTemplateHash)
err := checkVersion(artifactHubMetadata, constraintTemplate, githubSourceRelativePath)
if err != nil {
panic(err)
}
Expand All @@ -221,11 +224,38 @@ func addArtifactHubMetadata(sourceDirectory, destinationPath, ahBasePath string,
}
}

func checkVersion(artifactHubMetadata *ArtifactHubMetadata, constraintTemplate map[string]interface{}, newConstraintTemplateHash string) error {
// compare hash
if artifactHubMetadata.Digest != newConstraintTemplateHash {
func checkVersion(artifactHubMetadata *ArtifactHubMetadata, constraintTemplate map[string]interface{}, githubSourceRelativePath string) error {
// compare hash with template.yaml in github
githubTemplateURL := sourceURL + githubSourceRelativePath
resp, err := http.Get(githubTemplateURL)
if err != nil {
fmt.Println("error while getting constraint template from github")
panic(err)
}
if resp.StatusCode == http.StatusNotFound {
fmt.Printf("constraint template %s not found in github. It is likely that constraint template is being updated locally and not merged to github yet.\n", githubSourceRelativePath)

return nil
}
defer resp.Body.Close()

githubConstraintTemplateBytes, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("error while reading constraint template from github")
panic(err)
}

githubConstraintTemplate := make(map[string]interface{})
err = yaml.Unmarshal(githubConstraintTemplateBytes, &githubConstraintTemplate)
if err != nil {
fmt.Println("error while unmarshaling constraint template from github")
panic(err)
}

githubConstraintTemplateHash := getConstraintTemplateHash(githubConstraintTemplate)
if artifactHubMetadata.Digest != githubConstraintTemplateHash {
// compare version
if artifactHubMetadata.Version == constraintTemplate["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})["metadata.gatekeeper.sh/version"].(string) {
if artifactHubMetadata.Version == githubConstraintTemplate["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})["metadata.gatekeeper.sh/version"].(string) {
// panic if version is same but hash is different
return fmt.Errorf("looks like template.yaml is updated but the version is not. Please update the 'metadata.gatekeeper.sh/version' annotation in the template.yaml source")
}
Expand Down

0 comments on commit 377600b

Please sign in to comment.