From 6a661e17bbfa772f7b83281589c3bb9a469f8225 Mon Sep 17 00:00:00 2001 From: Aniruddha Basak Date: Mon, 5 Feb 2024 22:13:42 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20hook=20server=20service=20?= =?UTF-8?q?account=20and=20scheme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update the hook sever service role Signed-off-by: Aniruddha Basak --- extension/config/default/sa-roles.yaml | 2 + extension/go.mod | 7 +- extension/go.sum | 10 +- extension/handlers/hooks.go | 6 +- extension/main.go | 4 +- .../api/v1alpha1/clusterstack_types.go | 2 +- .../pkg/version/channel.go | 12 +- .../pkg/version/version.go | 128 ++++-------------- extension/vendor/modules.txt | 2 - 9 files changed, 50 insertions(+), 123 deletions(-) diff --git a/extension/config/default/sa-roles.yaml b/extension/config/default/sa-roles.yaml index 8b8cd92bb..1734b3b94 100644 --- a/extension/config/default/sa-roles.yaml +++ b/extension/config/default/sa-roles.yaml @@ -14,6 +14,8 @@ rules: - clusters verbs: - get + - patch + - update - apiGroups: - clusterstack.x-k8s.io resources: diff --git a/extension/go.mod b/extension/go.mod index 5849c8b33..e7babde27 100644 --- a/extension/go.mod +++ b/extension/go.mod @@ -1,12 +1,14 @@ module github.com/SovereignCloudStack/cluster-stack-operator/extension -go 1.19 +go 1.21 + +toolchain go1.21.6 replace github.com/SovereignCloudStack/cluster-stack-operator => ../ require ( + github.com/SovereignCloudStack/cluster-stack-operator v0.1.0-alpha.2 github.com/spf13/pflag v1.0.5 - github.com/sovereigncloudstack/cluster-stack-operator v0.0.0-00010101000000-000000000000 k8s.io/apimachinery v0.29.0 k8s.io/component-base v0.28.4 k8s.io/klog/v2 v2.110.1 @@ -15,7 +17,6 @@ require ( ) require ( - github.com/SovereignCloudStack/cluster-stack-operator v0.1.0-alpha.2 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/blang/semver/v4 v4.0.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect diff --git a/extension/go.sum b/extension/go.sum index fb99b26a7..344e226f7 100644 --- a/extension/go.sum +++ b/extension/go.sum @@ -6,8 +6,6 @@ github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7Y github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA= github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM= -github.com/SovereignCloudStack/cluster-stack-operator v0.1.0-alpha.2 h1:23sNJB4z0wiguCeG0W+GdusBrRNYGcKsUYLZo01wZNs= -github.com/SovereignCloudStack/cluster-stack-operator v0.1.0-alpha.2/go.mod h1:qXyqN2aZ+JCtVIVMNdr736b+3XNmaOhsXjGZuMUUUp0= github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h1:7RFfzj4SSt6nnvCPbCqijJi1nWCd+TqAT3bYCStRC18= github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA= @@ -115,8 +113,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs= -github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM= +github.com/onsi/ginkgo/v2 v2.13.1 h1:LNGfMbR2OVGBfXjvRZIZ2YCTQdGKtPLvuI1rMCCj3OU= +github.com/onsi/ginkgo/v2 v2.13.1/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM= github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8= github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= @@ -162,8 +160,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= +go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= diff --git a/extension/handlers/hooks.go b/extension/handlers/hooks.go index 760580ef4..4e712a6f4 100644 --- a/extension/handlers/hooks.go +++ b/extension/handlers/hooks.go @@ -21,6 +21,7 @@ import ( "fmt" csov1alpha1 "github.com/SovereignCloudStack/cluster-stack-operator/api/v1alpha1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" "sigs.k8s.io/cluster-api/util/conditions" @@ -33,7 +34,7 @@ type ExtensionHandler struct { client client.Client } -func NewExtensionHandlers(client client.Client) *ExtensionHandler { +func NewExtensionHandlers(client client.Client, scheme *runtime.Scheme) *ExtensionHandler { return &ExtensionHandler{ client: client, } @@ -92,6 +93,7 @@ func (e *ExtensionHandler) DoAfterControlPlaneInitialized(ctx context.Context, r key := types.NamespacedName{Name: fmt.Sprintf("cluster-addon-%s", request.Cluster.GetName()), Namespace: request.Cluster.GetNamespace()} clusterAddon := &csov1alpha1.ClusterAddon{} if err := e.client.Get(ctx, key, clusterAddon); err != nil { + log.Error(err, "failed to get the cluster addon") response.SetStatus(runtimehooksv1.ResponseStatusFailure) response.SetMessage(err.Error()) return @@ -110,6 +112,8 @@ func (e *ExtensionHandler) DoAfterControlPlaneInitialized(ctx context.Context, r } }() + clusterAddon.Spec.Hook = "AfterControlPlaneInitialized" + response.SetStatus(runtimehooksv1.ResponseStatusSuccess) return } diff --git a/extension/main.go b/extension/main.go index baeb53af2..652d4f537 100644 --- a/extension/main.go +++ b/extension/main.go @@ -140,14 +140,14 @@ func main() { } restConfig.UserAgent = remote.DefaultClusterAPIUserAgent(controllerName) - client, err := client.New(restConfig, client.Options{}) + client, err := client.New(restConfig, client.Options{Scheme: scheme}) if err != nil { setupLog.Error(err, "error creating client to the cluster") os.Exit(1) } // Create the ExtensionHandlers for the lifecycle hooks - lifecycleExtensionHandlers := handlers.NewExtensionHandlers(client) + lifecycleExtensionHandlers := handlers.NewExtensionHandlers(client, scheme) if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{ Hook: runtimehooksv1.BeforeClusterUpgrade, diff --git a/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/api/v1alpha1/clusterstack_types.go b/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/api/v1alpha1/clusterstack_types.go index e36c3cb5a..2cad61a94 100644 --- a/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/api/v1alpha1/clusterstack_types.go +++ b/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/api/v1alpha1/clusterstack_types.go @@ -39,7 +39,7 @@ type ClusterStackSpec struct { // Channel specifies the release channel of the cluster stack. Defaults to 'stable'. // +kubebuilder:default:=stable - // +kubebuilder:validation:Enum=stable;custom + // +kubebuilder:validation:enum=stable;alpha;beta;rc Channel version.Channel `json:"channel,omitempty"` // Versions is a list of version of the cluster stack that should be available in the management cluster. diff --git a/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/version/channel.go b/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/version/channel.go index 1dd33c6fa..3acc415cd 100644 --- a/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/version/channel.go +++ b/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/version/channel.go @@ -23,12 +23,18 @@ type Channel string const ( // ChannelStable is the stable channel. ChannelStable = Channel("stable") - // ChannelCustom is the custom channel where git hash is used in the versioning. - ChannelCustom = Channel("custom") + // ChannelAlpha is the alpha channel. + ChannelAlpha = Channel("alpha") + // ChannelBeta is the beta channel. + ChannelBeta = Channel("beta") + // ChannelRC is the rc channel. + ChannelRC = Channel("rc") ) // IsValid returns true if the release channel is valid. func (c Channel) IsValid() bool { return c == ChannelStable || - c == ChannelCustom + c == ChannelAlpha || + c == ChannelBeta || + c == ChannelRC } diff --git a/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/version/version.go b/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/version/version.go index ec77f7cf8..69175d105 100644 --- a/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/version/version.go +++ b/extension/vendor/github.com/SovereignCloudStack/cluster-stack-operator/pkg/version/version.go @@ -34,28 +34,27 @@ import ( type Version struct { Major int Channel Channel - Patch string + Patch int } -// ParseVersionString returns a Version struct from a version string like - -// "v1", "v1-alpha-1", "v1-beta-3", etc. -func ParseVersionString(version string) (Version, error) { - var ( - major int - patch string - err error - ) +// New returns a Version struct from a version string +// Sample allowed inputs: "v1-alpha-1", "v1", "v1-alpha-0" +// Sample disallowed inputs: "v1-alpha", "v1-alpha-1.0", "v1-alpha-1.0.0", "v1-alpha.", "v1.0-alpha.1". +func New(version string) (Version, error) { + var major, patch int + var err error channel := ChannelStable - re := regexp.MustCompile(`^v\d+(-\b\w+\b\-\w+)?$`) - match := re.MatchString(version) - if !match { + re := regexp.MustCompile(`^v\d+(-\b\w+\b-\d+)?$`) + match := re.FindStringSubmatch(version) + + if len(match) == 0 { return Version{}, fmt.Errorf("invalid version string %s", version) } // match[0] is the entire string e.g "v1-alpha-1" or "v1" // split match[0] with "-" as the delimiter - ver := strings.Split(version, "-") + ver := strings.Split(match[0], "-") // ver[0] is the major version // trim the "v" prefix and then convert to int @@ -68,57 +67,9 @@ func ParseVersionString(version string) (Version, error) { // ver[2] is the patch if len(ver) == 3 { channel = Channel(ver[1]) - patch = ver[2] - } - clusterStackVersion := Version{ - Major: major, - Channel: channel, - Patch: patch, - } - if err := clusterStackVersion.Validate(); err != nil { - return Version{}, err - } - - return clusterStackVersion, nil -} - -// New returns a Version struct from a version string -// Sample allowed inputs: "v1-alpha.1", "v1", "v1-alpha.0" -// Sample disallowed inputs: "v1-alpha", "v1-alpha-1.0", "v1-alpha-1.0.0", "v1-alpha.", "v1.0-alpha.1". -func New(version string) (Version, error) { - var ( - major int - patch string - err error - ) - channel := ChannelStable - - re := regexp.MustCompile(`^v\d+(-\b\w+\b\.\w+)?$`) - match := re.MatchString(version) - if !match { - return Version{}, fmt.Errorf("invalid version string %s", version) - } - - // match[0] is the entire string e.g "v1-alpha.1" or "v1" - // split match[0] with "-" as the delimiter - ver := strings.Split(version, "-") - - // ver[0] is the major version - // trim the "v" prefix and then convert to int - if major, err = strconv.Atoi(strings.TrimPrefix(ver[0], "v")); err != nil { - return Version{}, fmt.Errorf("invalid major version %s", ver[0]) - } - - // If the length of ver is 2, then the version string is of the form "v1-alpha.1", and split it - - // ver[0] is the channel - // ver[1] is the patch - if len(ver) == 2 { - splittedChannelPatch := strings.Split(ver[1], ".") - if len(splittedChannelPatch) != 2 { - return Version{}, fmt.Errorf("invalid version string %s", version) + if patch, err = strconv.Atoi(ver[2]); err != nil { + return Version{}, fmt.Errorf("invalid patch value in version %s", ver[2]) } - channel = Channel(splittedChannelPatch[0]) - patch = splittedChannelPatch[1] } clusterStackVersion := Version{ @@ -135,15 +86,13 @@ func New(version string) (Version, error) { // FromReleaseTag returns a Version struct from a release tag string. func FromReleaseTag(releaseTag string) (Version, error) { v := strings.Split(releaseTag, "-") - if len(v) != 5 && len(v) != 7 { + if len(v) != 5 && len(v) != 6 { return Version{}, fmt.Errorf("invalid release tag %s", releaseTag) } - // for docker-ferrol-1-26-v1 type tag, v[4] is the version if len(v) == 5 { - return ParseVersionString(v[4]) + return New(v[4]) } - // for docker-ferrol-1-26-v1-alpha-0 type tag, v[4] is the version and v[5] is the release channel + patch version - return ParseVersionString(fmt.Sprintf("%s-%s-%s", v[4], v[5], v[6])) + return New(fmt.Sprintf("%s-%s", v[4], v[5])) } // Validate validates the version. @@ -151,32 +100,15 @@ func (csv *Version) Validate() error { if csv.Major < 0 { return fmt.Errorf("major version should be a non-negative integer") } - - if csv.Channel != ChannelStable { - // Check if the patch is a valid integer - if isInteger(csv.Patch) { - // If it's an integer, check if it's greater than 0 - patchInt, _ := strconv.Atoi(csv.Patch) - if patchInt < 0 { - return fmt.Errorf("patch version should be a non-negative integer") - } - } - - // If it's alpha numeric, check if it's empty - if csv.Patch == "" { - return fmt.Errorf("patch can't empty") - } + if !csv.Channel.IsValid() { + return fmt.Errorf("invalid channel: %s", csv.Channel) + } + if csv.Patch < 0 { + return fmt.Errorf("patch version should be a non-negative integer") } - return nil } -// isInteger checks if the given string is a valid integer. -func isInteger(s string) bool { - _, err := strconv.Atoi(s) - return err == nil -} - // Compare compares two Version structs // Returns 1 if csv is greater than input // Returns -1 if csv is less than input @@ -206,23 +138,9 @@ func (csv Version) Compare(input Version) (int, error) { return 0, nil } -// String converts a Version struct to a string representation. -// If the channel is stable, it returns the version in the format "vMajor". -// Otherwise, it returns the version in the format "vMajor-Channel-Patch". func (csv Version) String() string { if csv.Channel == ChannelStable { return fmt.Sprintf("v%d", csv.Major) } - return fmt.Sprintf("v%d-%s-%s", csv.Major, csv.Channel, csv.Patch) -} - -// StringWithDot converts a Version struct to a string representation. -// If the channel is stable, it returns the version in the format "vMajor". -// Otherwise, it returns the version in the format "vMajor-Channel.Patch", -// similar to String but with a dot separating channel and patch. -func (csv Version) StringWithDot() string { - if csv.Channel == ChannelStable { - return fmt.Sprintf("v%d", csv.Major) - } - return fmt.Sprintf("v%d-%s.%s", csv.Major, csv.Channel, csv.Patch) + return fmt.Sprintf("v%d-%s-%d", csv.Major, csv.Channel, csv.Patch) } diff --git a/extension/vendor/modules.txt b/extension/vendor/modules.txt index a90f24ece..b33a65846 100644 --- a/extension/vendor/modules.txt +++ b/extension/vendor/modules.txt @@ -142,8 +142,6 @@ github.com/prometheus/common/model github.com/prometheus/procfs github.com/prometheus/procfs/internal/fs github.com/prometheus/procfs/internal/util -# github.com/sovereigncloudstack/cluster-stack-operator v0.0.0-00010101000000-000000000000 -## explicit # github.com/spf13/cobra v1.8.0 ## explicit; go 1.15 github.com/spf13/cobra