Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: save app icon to oss #976

Merged
merged 1 commit into from
Apr 3, 2024

Conversation

0xff-dev
Copy link
Collaborator

@0xff-dev 0xff-dev commented Apr 2, 2024

What type of PR is this?

/kind feature

What this PR does / why we need it

refactor: save app icon to oss

Which issue(s) this PR fixes

Fix #960

Special notes for your reviewer

package main

import (
	"bytes"
	"context"
	"crypto/tls"
	"encoding/base64"
	"flag"
	"fmt"
	"net/http"
	"path/filepath"
	"sync"

	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"

	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
)

var (
	endpoint = flag.String("api", "minio-api.172.22.96.167.nip.io", "minio api service address, such as minio-api.172.22.96.167.nip.io")
	username = flag.String("u", "admin", "username")
	password = flag.String("p", "", "password")
)

func main() {
	var kubeconfig *string
	if home := homedir.HomeDir(); home != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()

	tp := http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}
	minioClient, err := minio.New(*endpoint, &minio.Options{
		Creds:     credentials.NewStaticV4(*username, *password, ""),
		Secure:    true,
		Transport: &tp,
	})
	if err != nil {
		panic(err)
	}
	cfg, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err)
	}

	dynamicClient := dynamic.NewForConfigOrDie(cfg)

	namespaceList, err := dynamicClient.Resource(schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}).List(context.TODO(), v1.ListOptions{})
	if err != nil {
		panic(err)
	}

	gvr := schema.GroupVersionResource{
		Group:    "arcadia.kubeagi.k8s.com.cn",
		Version:  "v1alpha1",
		Resource: "applications",
	}

	var wg sync.WaitGroup
	for _, i := range namespaceList.Items {
		wg.Add(1)
		go func(namespace string) {
			defer wg.Done()
			appList, err := dynamicClient.Resource(gvr).List(context.TODO(), v1.ListOptions{})
			if err != nil {
				fmt.Printf("[Error] in namespace %s, failed to list apps %s\n", namespace, err)
				return
			}

			for _, app := range appList.Items {
				icon, found, err := unstructured.NestedString(app.Object, "spec", "icon")
				if err != nil || !found {
					fmt.Printf("[Error] %s/%s not found path spec.icon or error %s\n", app.GetNamespace(), app.GetName(), err)
					continue
				}
				bs, err := ParseBase64ImageBytes(icon)
				if err != nil {
					fmt.Printf("[Error] %s/%s failed to parse base64 image\n", app.GetNamespace(), app.GetName())
					continue
				}
				name := fmt.Sprintf("application/%s/icon/%s", app.GetName(), app.GetName())
				_, err = minioClient.PutObject(context.TODO(), namespace, name, bytes.NewReader(bs), int64(len(bs)), minio.PutObjectOptions{})
				if err != nil {
					fmt.Printf("[Error] %s/%s failed to upload icon to minio error %s\n", app.GetNamespace(), app.GetName(), err)
					continue
				}
				fmt.Printf("[Success] %s/%s migration successful\n\n", app.GetNamespace(), app.GetName())
			}
		}(i.GetName())
	}
	wg.Wait()
	fmt.Println("Done")
}

func ParseBase64ImageBytes(img string) ([]byte, error) {
	i := 0
	for ; i < len(img) && img[i] != ','; i++ {
	}
	cut := img[i+1:]
	return base64.StdEncoding.DecodeString(cut)
}

@0xff-dev 0xff-dev marked this pull request as draft April 3, 2024 01:46
@0xff-dev 0xff-dev force-pushed the refactor-save-app-icon-to-minio branch 2 times, most recently from 16fa8cc to a244b8b Compare April 3, 2024 06:42
@0xff-dev 0xff-dev marked this pull request as ready for review April 3, 2024 06:49
@0xff-dev 0xff-dev force-pushed the refactor-save-app-icon-to-minio branch 3 times, most recently from d53a3c4 to 3234d74 Compare April 3, 2024 09:01
@0xff-dev 0xff-dev force-pushed the refactor-save-app-icon-to-minio branch from 3234d74 to 40e51bf Compare April 3, 2024 09:31
@bjwswang
Copy link
Collaborator

bjwswang commented Apr 3, 2024

@0xff-dev Please make sure the gptstore support this icon link.

@bjwswang bjwswang merged commit fd54e48 into kubeagi:main Apr 3, 2024
10 checks passed
@0xff-dev 0xff-dev deleted the refactor-save-app-icon-to-minio branch April 4, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

should store applicaiton's icon in mino and generate a download link when get covnersations
2 participants