Skip to content

Commit 2182fa7

Browse files
author
Aniruddha Basak
authored
use cluster stack content hash instead of git commit hash (#122)
Signed-off-by: Aniruddha Basak <aniruddha.basak@syself.com>
1 parent 883e78d commit 2182fa7

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

pkg/clusterstack/mode.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121

2222
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/version"
23-
"github.com/SovereignCloudStack/csctl/pkg/git"
2423
"github.com/SovereignCloudStack/csctl/pkg/hash"
2524
)
2625

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

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

67-
// HandleHashMode returns metadata of Hash mode.
68-
func HandleHashMode(kubernetesVersion string) (*MetaData, error) {
69-
commitHash, err := git.GetLatestGitCommit("./")
70-
if err != nil {
71-
return nil, fmt.Errorf("failed to get latest commit hash: %w", err)
72-
}
73-
74-
commitHash = fmt.Sprintf("v0-sha.%s", commitHash)
65+
// HandleHashMode handles the hash mode with the cluster stack hash.
66+
func HandleHashMode(currentRelease hash.ReleaseHash, kubernetesVersion string) *MetaData {
67+
clusterStackHash := currentRelease.GetClusterStackHash()
68+
clusterStackHash = fmt.Sprintf("v0-sha.%s", clusterStackHash)
7569

7670
return &MetaData{
7771
APIVersion: "metadata.clusterstack.x-k8s.io/v1alpha1",
7872
Versions: Versions{
7973
Kubernetes: kubernetesVersion,
80-
ClusterStack: commitHash,
74+
ClusterStack: clusterStackHash,
8175
Components: Component{
82-
ClusterAddon: commitHash,
83-
NodeImage: commitHash,
76+
ClusterAddon: clusterStackHash,
77+
NodeImage: clusterStackHash,
8478
},
8579
},
86-
}, nil
80+
}
8781
}
8882

8983
// HandleCustomMode handles custom mode with version for all components.

pkg/cmd/create.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ func GetCreateOptions(ctx context.Context, clusterStackPath string) (*CreateOpti
126126

127127
switch mode {
128128
case hashMode:
129-
createOption.Metadata, err = clusterstack.HandleHashMode(config.Config.KubernetesVersion)
130-
if err != nil {
131-
return nil, fmt.Errorf("failed to handle hash mode: %w", err)
132-
}
129+
createOption.Metadata = clusterstack.HandleHashMode(createOption.CurrentReleaseHash, config.Config.KubernetesVersion)
133130
case stableMode:
134131
gc, err := client.NewFactory().NewClient(ctx)
135132
if err != nil {

pkg/hash/hash.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const (
3737

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

5253
releaseHash := ReleaseHash{}
54+
55+
hash, err := dirhash.HashDir(path, "", dirhash.DefaultHash)
56+
if err != nil {
57+
return ReleaseHash{}, fmt.Errorf("failed to calculate cluster stack hash: %w", err)
58+
}
59+
hash = clean(hash)
60+
fmt.Printf("path %q: cluster stack hash: %q\n", path, hash)
61+
62+
releaseHash.ClusterStack = hash
63+
5364
for _, entry := range entries {
5465
entryPath := filepath.Join(path, entry.Name())
5566
if entry.IsDir() && (entry.Name() == clusterAddonDirName || entry.Name() == nodeImageDirName) {
5667
hash, err := dirhash.HashDir(entryPath, "", dirhash.DefaultHash)
5768
if err != nil {
5869
return ReleaseHash{}, fmt.Errorf("failed to hash dir: %w", err)
5970
}
60-
hash = strings.TrimPrefix(hash, "h1:")
71+
hash = clean(hash)
6172

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

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

90101
return nil
91102
}
103+
104+
func clean(hash string) string {
105+
hash = strings.TrimPrefix(hash, "h1:")
106+
hash = strings.ReplaceAll(hash, "/", "")
107+
hash = strings.ReplaceAll(hash, "=", "")
108+
hash = strings.ReplaceAll(hash, "+", "")
109+
hash = strings.ToLower(hash)
110+
111+
return hash
112+
}
113+
114+
// GetClusterStackHash returns the 7 character hash of the cluster stack content.
115+
func (r ReleaseHash) GetClusterStackHash() string {
116+
return r.ClusterStack[:7]
117+
}

tests/cluster-stacks/docker/ferrol/csctl.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ config:
55
provider:
66
type: docker
77
apiVersion: docker.csctl.clusterstack.x-k8s.io/v1alpha1
8-
config:
9-
dummyKey: dummyValue

0 commit comments

Comments
 (0)