Skip to content

Commit

Permalink
Merge pull request #4 from ksctl/release/v0.1.0
Browse files Browse the repository at this point in the history
🚀   Release [0.1.0]
  • Loading branch information
dipankardas011 authored Jan 26, 2025
2 parents 8ff9694 + a3248e6 commit c27b4ad
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
4 changes: 4 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

managev1 "github.com/ksctl/kcm/api/v1"
"github.com/ksctl/kcm/internal/controller"
"github.com/ksctl/ksctl/v2/pkg/poller"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion config/default/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.23.4

require (
github.com/gookit/goutil v0.6.18
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
Expand All @@ -23,7 +24,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
Expand Down Expand Up @@ -60,7 +60,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
Expand All @@ -74,6 +73,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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.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=
Expand Down Expand Up @@ -164,6 +166,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=
Expand Down
71 changes: 49 additions & 22 deletions internal/controller/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package controller

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"github.com/gookit/goutil/dump"
"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"
Expand All @@ -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)
},
},
}
Expand Down Expand Up @@ -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{},
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -278,26 +299,32 @@ 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)
} else {
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"`
}

0 comments on commit c27b4ad

Please sign in to comment.