From a3893efffd3ea5e6344171f5131d06e95fbc770c Mon Sep 17 00:00:00 2001 From: Aniruddha Basak Date: Fri, 10 May 2024 21:44:16 +0200 Subject: [PATCH] use cluster stack content hash instead of git commit hash Signed-off-by: Aniruddha Basak --- pkg/clusterstack/mode.go | 22 +++++--------- pkg/cmd/create.go | 5 +--- pkg/hash/hash.go | 30 +++++++++++++++++-- tests/cluster-stacks/docker/ferrol/csctl.yaml | 2 -- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/pkg/clusterstack/mode.go b/pkg/clusterstack/mode.go index ad6057d8..4b48da26 100644 --- a/pkg/clusterstack/mode.go +++ b/pkg/clusterstack/mode.go @@ -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" ) @@ -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) @@ -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. diff --git a/pkg/cmd/create.go b/pkg/cmd/create.go index e9863e2d..ea02dd12 100644 --- a/pkg/cmd/create.go +++ b/pkg/cmd/create.go @@ -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 { diff --git a/pkg/hash/hash.go b/pkg/hash/hash.go index 95c0f280..a5ba1605 100644 --- a/pkg/hash/hash.go +++ b/pkg/hash/hash.go @@ -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"` @@ -50,6 +51,16 @@ 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) { @@ -57,7 +68,7 @@ func GetHash(path string) (ReleaseHash, error) { 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: @@ -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))) } } @@ -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] +} diff --git a/tests/cluster-stacks/docker/ferrol/csctl.yaml b/tests/cluster-stacks/docker/ferrol/csctl.yaml index b4f914ed..592fd44c 100644 --- a/tests/cluster-stacks/docker/ferrol/csctl.yaml +++ b/tests/cluster-stacks/docker/ferrol/csctl.yaml @@ -5,5 +5,3 @@ config: provider: type: docker apiVersion: docker.csctl.clusterstack.x-k8s.io/v1alpha1 - config: - dummyKey: dummyValue