Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ use cluster stack content hash instead of git commit hash #122

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 8 additions & 14 deletions pkg/clusterstack/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"

"github.com/SovereignCloudStack/cluster-stack-operator/pkg/version"
"github.com/SovereignCloudStack/csctl/pkg/git"
"github.com/SovereignCloudStack/csctl/pkg/hash"
)

Expand All @@ -35,7 +34,6 @@ func HandleStableMode(gitHubReleasePath string, currentReleaseHash, latestReleas
if err != nil {
return nil, fmt.Errorf("failed to bump cluster stack: %w", err)
}
fmt.Printf("Bumped ClusterStack Version: %s\n", metadata.Versions.ClusterStack)

if currentReleaseHash.ClusterAddonDir != latestReleaseHash.ClusterAddonDir || currentReleaseHash.ClusterAddonValues != latestReleaseHash.ClusterAddonValues {
metadata.Versions.Components.ClusterAddon, err = BumpVersion(metadata.Versions.Components.ClusterAddon)
Expand Down Expand Up @@ -64,26 +62,22 @@ func HandleStableMode(gitHubReleasePath string, currentReleaseHash, latestReleas
return metadata, nil
}

// HandleHashMode returns metadata of Hash mode.
func HandleHashMode(kubernetesVersion string) (*MetaData, error) {
commitHash, err := git.GetLatestGitCommit("./")
if err != nil {
return nil, fmt.Errorf("failed to get latest commit hash: %w", err)
}

commitHash = fmt.Sprintf("v0-sha.%s", commitHash)
// HandleHashMode handles the hash mode with the cluster stack hash.
func HandleHashMode(currentRelease hash.ReleaseHash, kubernetesVersion string) *MetaData {
clusterStackHash := currentRelease.GetClusterStackHash()
clusterStackHash = fmt.Sprintf("v0-sha.%s", clusterStackHash)

return &MetaData{
APIVersion: "metadata.clusterstack.x-k8s.io/v1alpha1",
Versions: Versions{
Kubernetes: kubernetesVersion,
ClusterStack: commitHash,
ClusterStack: clusterStackHash,
Components: Component{
ClusterAddon: commitHash,
NodeImage: commitHash,
ClusterAddon: clusterStackHash,
NodeImage: clusterStackHash,
},
},
}, nil
}
}

// HandleCustomMode handles custom mode with version for all components.
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ func GetCreateOptions(ctx context.Context, clusterStackPath string) (*CreateOpti

switch mode {
case hashMode:
createOption.Metadata, err = clusterstack.HandleHashMode(config.Config.KubernetesVersion)
if err != nil {
return nil, fmt.Errorf("failed to handle hash mode: %w", err)
}
createOption.Metadata = clusterstack.HandleHashMode(createOption.CurrentReleaseHash, config.Config.KubernetesVersion)
case stableMode:
gc, err := client.NewFactory().NewClient(ctx)
if err != nil {
Expand Down
30 changes: 28 additions & 2 deletions pkg/hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (

// ReleaseHash contains the information of release hash.
type ReleaseHash struct {
ClusterStack string `json:"clusterStack"`
ClusterAddonDir string `json:"clusterAddonDir"`
ClusterAddonValues string `json:"clusterAddonValues"`
NodeImageDir string `json:"nodeImageDir,omitempty"`
Expand All @@ -50,14 +51,24 @@ func GetHash(path string) (ReleaseHash, error) {
}

releaseHash := ReleaseHash{}

hash, err := dirhash.HashDir(path, "", dirhash.DefaultHash)
if err != nil {
return ReleaseHash{}, fmt.Errorf("failed to calculate cluster stack hash: %w", err)
}
hash = clean(hash)
fmt.Printf("path %q: cluster stack hash: %q\n", path, hash)

releaseHash.ClusterStack = hash

for _, entry := range entries {
entryPath := filepath.Join(path, entry.Name())
if entry.IsDir() && (entry.Name() == clusterAddonDirName || entry.Name() == nodeImageDirName) {
hash, err := dirhash.HashDir(entryPath, "", dirhash.DefaultHash)
if err != nil {
return ReleaseHash{}, fmt.Errorf("failed to hash dir: %w", err)
}
hash = strings.TrimPrefix(hash, "h1:")
hash = clean(hash)

switch entry.Name() {
case clusterAddonDirName:
Expand All @@ -72,7 +83,7 @@ func GetHash(path string) (ReleaseHash, error) {
if _, err := io.Copy(fileHash, file); err != nil {
return ReleaseHash{}, fmt.Errorf("failed to copy dir: %w", err)
}
releaseHash.ClusterAddonValues = base64.StdEncoding.EncodeToString(fileHash.Sum(nil))
releaseHash.ClusterAddonValues = clean(base64.StdEncoding.EncodeToString(fileHash.Sum(nil)))
}
}

Expand All @@ -89,3 +100,18 @@ func (r ReleaseHash) ValidateWithLatestReleaseHash(latestReleaseHash ReleaseHash

return nil
}

func clean(hash string) string {
hash = strings.TrimPrefix(hash, "h1:")
hash = strings.ReplaceAll(hash, "/", "")
hash = strings.ReplaceAll(hash, "=", "")
hash = strings.ReplaceAll(hash, "+", "")
hash = strings.ToLower(hash)

return hash
}

// GetClusterStackHash returns the 7 character hash of the cluster stack content.
func (r ReleaseHash) GetClusterStackHash() string {
return r.ClusterStack[:7]
}
2 changes: 0 additions & 2 deletions tests/cluster-stacks/docker/ferrol/csctl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ config:
provider:
type: docker
apiVersion: docker.csctl.clusterstack.x-k8s.io/v1alpha1
config:
dummyKey: dummyValue