Skip to content

Commit

Permalink
chore: override k8s client
Browse files Browse the repository at this point in the history
  • Loading branch information
acid-chicken committed May 22, 2024
1 parent 25d1cc2 commit 9ffb85a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion charts/hariko/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
apiVersion: v2
name: hariko
version: 0.1.
version: 0.2.
2 changes: 1 addition & 1 deletion charts/misskey/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
apiVersion: v2
name: misskey
version: 0.1.
version: 0.2.
68 changes: 44 additions & 24 deletions hariko/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -20,7 +19,13 @@ import (
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/repo"
"helm.sh/helm/v3/pkg/storage/driver"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -188,6 +193,42 @@ func Execute() {
}
}

type RESTClientGetter struct {
genericclioptions.RESTClientGetter
}

func (r RESTClientGetter) ToRESTConfig() (*rest.Config, error) {
return rest.InClusterConfig()
}

func (r RESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
client, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
return memory.NewMemCacheClient(client), nil
}

func (r RESTClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
client, err := r.ToDiscoveryClient()
if err != nil {
return nil, err
}
resources, err := restmapper.GetAPIGroupResources(client)
if err != nil {
return nil, err
}
return restmapper.NewDiscoveryRESTMapper(resources), nil
}

func (r RESTClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
return &clientcmd.DefaultClientConfig
}

func deploy(packageName string, repositoryName string, repositoryURL string, log io.Writer) (*release.Release, error) {
p := getter.All(settings)
c := repo.Entry{
Expand Down Expand Up @@ -222,30 +263,9 @@ func deploy(packageName string, repositoryName string, repositoryURL string, log
return nil, err
}
actionConfig := new(action.Configuration)
actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), "configmap", func(format string, v ...interface{}) {
actionConfig.Init(RESTClientGetter{}, settings.Namespace(), "configmap", func(format string, v ...interface{}) {
fmt.Fprintf(log, format+"\n", v...)
})
if _, ok := actionConfig.Releases.Driver.(*driver.Memory); ok {
return nil, fmt.Errorf("storage driver could not be initialized")
} else {
releases, err := actionConfig.Releases.Driver.List(func(_ *release.Release) bool { return true })
if err != nil {
return nil, err
}
if releases == nil {
log.Write([]byte("no releases found\n"))
}
for _, release := range releases {
data, err := json.Marshal(release)
if err != nil {
log.Write([]byte(fmt.Sprintf("failed to marshal release: %s\n", err.Error())))
} else {
log.Write([]byte("detected release: "))
log.Write(data)
log.Write([]byte("\n"))
}
}
}
client := action.NewUpgrade(actionConfig)
client.Namespace = settings.Namespace()
chartPath, err := client.LocateChart(repositoryName+"/"+packageName, settings)
Expand Down

0 comments on commit 9ffb85a

Please sign in to comment.