From ceb60cdd79f0c56ac2f67672ce7399bfafa9c61a Mon Sep 17 00:00:00 2001 From: Dipankar Das <65275144+dipankardas011@users.noreply.github.com> Date: Sat, 25 Jan 2025 19:14:04 +0530 Subject: [PATCH 1/4] started work on release v0.1.0 Signed-off-by: Dipankar Das <65275144+dipankardas011@users.noreply.github.com> --- cmd/main.go | 4 ++ config/default/kustomization.yaml | 2 +- go.mod | 6 ++- go.sum | 2 + internal/controller/addons.go | 71 +++++++++++++++++++++---------- 5 files changed, 60 insertions(+), 25 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 07b8430..caef850 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -41,6 +41,7 @@ import ( managev1 "github.com/ksctl/kcm/api/v1" "github.com/ksctl/kcm/internal/controller" + "github.com/ksctl/ksctl/pkg/poller" // +kubebuilder:scaffold:imports ) @@ -240,6 +241,9 @@ func main() { os.Exit(1) } + // Start the Ksctl Poller + poller.InitSharedGithubReleasePoller() + setupLog.Info("starting manager") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { setupLog.Error(err, "problem running manager") diff --git a/config/default/kustomization.yaml b/config/default/kustomization.yaml index 5e98d6e..6b79114 100644 --- a/config/default/kustomization.yaml +++ b/config/default/kustomization.yaml @@ -1,5 +1,5 @@ # Adds namespace to all resources. -namespace: kcm-ksctl-system +namespace: kcm-system # Value of this field is prepended to the # names of all resources, e.g. a deployment named diff --git a/go.mod b/go.mod index ce76402..b381823 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ toolchain go1.23.4 require ( github.com/gookit/goutil v0.6.18 + github.com/ksctl/ksctl v0.0.0-00010101000000-000000000000 github.com/onsi/ginkgo/v2 v2.21.0 github.com/onsi/gomega v1.35.1 k8s.io/api v0.32.0 @@ -14,6 +15,8 @@ require ( sigs.k8s.io/controller-runtime v0.19.4 ) +replace github.com/ksctl/ksctl => ../ksctl + require ( github.com/antlr4-go/antlr/v4 v4.13.0 // indirect github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect @@ -23,7 +26,6 @@ require ( github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect - github.com/evanphx/json-patch v5.9.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.9.0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect @@ -60,7 +62,6 @@ require ( github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/stoewer/go-strcase v1.2.0 // indirect - github.com/stretchr/testify v1.10.0 // indirect github.com/x448/float16 v0.8.4 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect @@ -74,6 +75,7 @@ require ( go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.26.0 // indirect golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect + golang.org/x/mod v0.22.0 // indirect golang.org/x/net v0.30.0 // indirect golang.org/x/oauth2 v0.23.0 // indirect golang.org/x/sync v0.10.0 // indirect diff --git a/go.sum b/go.sum index de8ecd3..c4239bb 100644 --- a/go.sum +++ b/go.sum @@ -164,6 +164,8 @@ golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6R golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= +golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= diff --git a/internal/controller/addons.go b/internal/controller/addons.go index 93c385f..72d92c9 100644 --- a/internal/controller/addons.go +++ b/internal/controller/addons.go @@ -2,12 +2,14 @@ package controller import ( "context" + "encoding/json" "fmt" "io" "net/http" "time" "github.com/gookit/goutil/dump" + "github.com/ksctl/ksctl/pkg/poller" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -19,21 +21,21 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -type AddonURL func(version *string) string +type AddonURL func(version string) string type AddonManifest struct { + Org string + Repo string URL AddonURL Namespace *string } var addonManifests = map[string]AddonManifest{ "stack": { - URL: func(version *string) string { - v := "latest" - if version != nil { - v = *version - } - return fmt.Sprintf("https://github.com/ksctl/ka/releases/download/%s/install.yml", v) + Org: "ksctl", + Repo: "ka", + URL: func(version string) string { + return fmt.Sprintf("https://github.com/ksctl/ka/releases/download/%s/install.yaml", version) }, }, } @@ -71,12 +73,12 @@ func (r *ClusterAddonReconciler) DeleteNamespaceIfExists(ctx context.Context, na func (r *ClusterAddonReconciler) GetData(ctx context.Context) (*corev1.ConfigMap, error) { cf := &corev1.ConfigMap{} - if err := r.Get(ctx, client.ObjectKey{Namespace: "kcm-ksctl-system", Name: "kcm-addons"}, cf); err != nil { + if err := r.Get(ctx, client.ObjectKey{Namespace: "kcm-system", Name: "kcm-addons"}, cf); err != nil { if errors.IsNotFound(err) { cf = &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "kcm-addons", - Namespace: "kcm-ksctl-system", + Namespace: "kcm-system", }, Data: map[string]string{}, } @@ -119,19 +121,27 @@ func (r *ClusterAddonReconciler) HandleAddon(ctx context.Context, addonName stri return nil } - fmt.Println("~~>", cf.Data, addonName, manifest) - if manifest.Namespace != nil { if err := r.CreateNamespaceIfNotExists(ctx, *manifest.Namespace); err != nil { return fmt.Errorf("failed to create namespace for ADDON %s: %w", *manifest.Namespace, err) } } + addonVersion := "" + + if addonVer == nil { + v, err := poller.GetSharedPoller().Get(manifest.Org, manifest.Repo) + if err == nil { + addonVersion = v[0] + } + } else { + addonVersion = *addonVer + } - if err := r.downloadAndOperateManifests(ctx, manifest, r.applyResource, addonVer); err != nil { + if err := r.downloadAndOperateManifests(ctx, manifest, r.applyResource, addonVersion); err != nil { return fmt.Errorf("failed to install addon %s: %w", addonName, err) } - return r.updateAddonStatus(ctx, cf, addonName, false) + return r.updateAddonStatus(ctx, cf, addonName, false, addonVersion) } func (r *ClusterAddonReconciler) HandleAddonDelete(ctx context.Context, addonName string, addonVer *string) error { @@ -149,7 +159,18 @@ func (r *ClusterAddonReconciler) HandleAddonDelete(ctx context.Context, addonNam return fmt.Errorf("addon %s not found in manifest registry", addonName) } - if err := r.downloadAndOperateManifests(ctx, manifest, r.deleteResource, addonVer); err != nil { + addonVersion := "" + + if addonVer == nil { + v := cf.Data[addonName] + _v := AddonState{} + _ = json.Unmarshal([]byte(v), &_v) + addonVersion = _v.Ver + } else { + addonVersion = *addonVer + } + + if err := r.downloadAndOperateManifests(ctx, manifest, r.deleteResource, addonVersion); err != nil { return fmt.Errorf("failed to uninstall addon %s: %w", addonName, err) } @@ -159,14 +180,14 @@ func (r *ClusterAddonReconciler) HandleAddonDelete(ctx context.Context, addonNam } } - return r.updateAddonStatus(ctx, cf, addonName, true) + return r.updateAddonStatus(ctx, cf, addonName, true, addonVersion) } func (r *ClusterAddonReconciler) downloadAndOperateManifests( ctx context.Context, manifest AddonManifest, operator func(ctx context.Context, obj *unstructured.Unstructured) error, - version *string, + version string, ) error { resp, err := http.Get(manifest.URL(version)) if err != nil { @@ -240,12 +261,12 @@ func (r *ClusterAddonReconciler) deleteResource(ctx context.Context, obj *unstru err = dr.Delete(ctx, obj.GetName(), opts) if err != nil { - fmt.Println("########### Failed to delete resource", obj.GetNamespace(), obj.GetName()) + fmt.Println("Failed to delete resource", obj.GetNamespace(), obj.GetName()) dump.Println(obj) return fmt.Errorf("failed to delete resource: %w", err) } - fmt.Println("########### Delete resource", obj.GetNamespace(), obj.GetName()) + fmt.Println("Delete resource", obj.GetNamespace(), obj.GetName()) return nil } @@ -278,17 +299,17 @@ func (r *ClusterAddonReconciler) applyResource(ctx context.Context, obj *unstruc _, err = dr.Apply(ctx, obj.GetName(), obj, opts) if err != nil { - fmt.Println("########### Failed to apply resource", obj.GetNamespace(), obj.GetName()) + fmt.Println("Failed to apply resource", obj.GetNamespace(), obj.GetName()) dump.Println(obj) return fmt.Errorf("failed to apply resource: %w", err) } - fmt.Println("########### Applied resource", obj.GetNamespace(), obj.GetName()) + fmt.Println("Applied resource", obj.GetNamespace(), obj.GetName()) return nil } -func (r *ClusterAddonReconciler) updateAddonStatus(ctx context.Context, cf *corev1.ConfigMap, addonName string, isDelete bool) error { +func (r *ClusterAddonReconciler) updateAddonStatus(ctx context.Context, cf *corev1.ConfigMap, addonName string, isDelete bool, addonVer string) error { return retry.OnError(retry.DefaultRetry, errors.IsConflict, func() error { if isDelete { delete(cf.Data, addonName) @@ -296,8 +317,14 @@ func (r *ClusterAddonReconciler) updateAddonStatus(ctx context.Context, cf *core if cf.Data == nil { cf.Data = map[string]string{} } - cf.Data[addonName] = fmt.Sprintf("installed@%s", time.Now().Format(time.RFC3339)) + v, _ := json.Marshal(AddonState{Ver: addonVer, Timestamp: time.Now().UTC()}) + cf.Data[addonName] = string(v) } return r.Update(ctx, cf) }) } + +type AddonState struct { + Ver string `json:"version"` + Timestamp time.Time `json:"timestamp"` +} From ab91bbef312f670b6a3a4509c3932190295376ea Mon Sep 17 00:00:00 2001 From: Dipankar Das <65275144+dipankardas011@users.noreply.github.com> Date: Sat, 25 Jan 2025 23:13:52 +0530 Subject: [PATCH 2/4] bump(deps): ksctl@v2.0.0-rc.1 Signed-off-by: Dipankar Das <65275144+dipankardas011@users.noreply.github.com> --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b381823..11f24de 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.23.4 require ( github.com/gookit/goutil v0.6.18 - github.com/ksctl/ksctl v0.0.0-00010101000000-000000000000 + github.com/ksctl/ksctl v2.0.0-rc.1+incompatible github.com/onsi/ginkgo/v2 v2.21.0 github.com/onsi/gomega v1.35.1 k8s.io/api v0.32.0 From 4c3217ef99ad880e930bab9f7079ff20bf3e611b Mon Sep 17 00:00:00 2001 From: Dipankar Das <65275144+dipankardas011@users.noreply.github.com> Date: Sat, 25 Jan 2025 23:43:13 +0530 Subject: [PATCH 3/4] bump: ksctl/v2 Signed-off-by: Dipankar Das <65275144+dipankardas011@users.noreply.github.com> --- cmd/main.go | 2 +- go.mod | 4 +--- go.sum | 2 ++ internal/controller/addons.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index caef850..a824b57 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -41,7 +41,7 @@ import ( managev1 "github.com/ksctl/kcm/api/v1" "github.com/ksctl/kcm/internal/controller" - "github.com/ksctl/ksctl/pkg/poller" + "github.com/ksctl/ksctl/v2/pkg/poller" // +kubebuilder:scaffold:imports ) diff --git a/go.mod b/go.mod index 11f24de..2f47c77 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.23.4 require ( github.com/gookit/goutil v0.6.18 - github.com/ksctl/ksctl v2.0.0-rc.1+incompatible + github.com/ksctl/ksctl/v2 v2.0.0-rc.1 github.com/onsi/ginkgo/v2 v2.21.0 github.com/onsi/gomega v1.35.1 k8s.io/api v0.32.0 @@ -15,8 +15,6 @@ require ( sigs.k8s.io/controller-runtime v0.19.4 ) -replace github.com/ksctl/ksctl => ../ksctl - require ( github.com/antlr4-go/antlr/v4 v4.13.0 // indirect github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect diff --git a/go.sum b/go.sum index c4239bb..fe20e48 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/ksctl/ksctl/v2 v2.0.0-rc.1 h1:9wGYIc7EFwMDMRP+RgqLxd2JwxYnamlaULdjrdnpZ/k= +github.com/ksctl/ksctl/v2 v2.0.0-rc.1/go.mod h1:24QA7D2r/7YMVMFlKgpn8xHsIHPmPxaCtjAvJYynLps= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/internal/controller/addons.go b/internal/controller/addons.go index 72d92c9..8761023 100644 --- a/internal/controller/addons.go +++ b/internal/controller/addons.go @@ -9,7 +9,7 @@ import ( "time" "github.com/gookit/goutil/dump" - "github.com/ksctl/ksctl/pkg/poller" + "github.com/ksctl/ksctl/v2/pkg/poller" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" From a3248e62aab80784380fef65cb7ceed1c9289fb5 Mon Sep 17 00:00:00 2001 From: Dipankar Das <65275144+dipankardas011@users.noreply.github.com> Date: Sun, 26 Jan 2025 15:41:54 +0530 Subject: [PATCH 4/4] bump: ksctl v2.0.0-rc.2 Signed-off-by: Dipankar Das <65275144+dipankardas011@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 2f47c77..94b79e2 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.23.4 require ( github.com/gookit/goutil v0.6.18 - github.com/ksctl/ksctl/v2 v2.0.0-rc.1 + github.com/ksctl/ksctl/v2 v2.0.0-rc.2 github.com/onsi/ginkgo/v2 v2.21.0 github.com/onsi/gomega v1.35.1 k8s.io/api v0.32.0 diff --git a/go.sum b/go.sum index fe20e48..8e235c1 100644 --- a/go.sum +++ b/go.sum @@ -84,8 +84,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/ksctl/ksctl/v2 v2.0.0-rc.1 h1:9wGYIc7EFwMDMRP+RgqLxd2JwxYnamlaULdjrdnpZ/k= -github.com/ksctl/ksctl/v2 v2.0.0-rc.1/go.mod h1:24QA7D2r/7YMVMFlKgpn8xHsIHPmPxaCtjAvJYynLps= +github.com/ksctl/ksctl/v2 v2.0.0-rc.2 h1:/8t1QBV0IXZ7aUbE6daBtnwiMkPkn6IZxBXu3LRFf+8= +github.com/ksctl/ksctl/v2 v2.0.0-rc.2/go.mod h1:24QA7D2r/7YMVMFlKgpn8xHsIHPmPxaCtjAvJYynLps= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=