Add a list function
This commit is contained in:
		@@ -49,7 +49,7 @@ func (acc *Account) Create(ctx context.Context) error {
 | 
			
		||||
			Labels: map[string]string{
 | 
			
		||||
				"username":       acc.Data.Username,
 | 
			
		||||
				"email-verified": "false",
 | 
			
		||||
				"managed-by": "softplayer",
 | 
			
		||||
				"managed-by":     "softplayer",
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,7 @@ import (
 | 
			
		||||
	"k8s.io/apimachinery/pkg/types"
 | 
			
		||||
	"k8s.io/client-go/rest"
 | 
			
		||||
	ctrl "sigs.k8s.io/controller-runtime"
 | 
			
		||||
	"sigs.k8s.io/controller-runtime/pkg/client"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Environemnt struct {
 | 
			
		||||
@@ -91,6 +92,7 @@ func (env *Environemnt) Create(ctx context.Context) error {
 | 
			
		||||
			Namespace: env.UserID,
 | 
			
		||||
			Labels: map[string]string{
 | 
			
		||||
				"component": "bootstrap",
 | 
			
		||||
				"kind":      "environment",
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
		Data: map[string]string{
 | 
			
		||||
@@ -138,3 +140,34 @@ func (env *Environemnt) Delete(ctx context.Context) error {
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (env *Environemnt) ListEnvs(ctx context.Context) ([]string, error) {
 | 
			
		||||
	conf := &rest.Config{
 | 
			
		||||
		Host:        "https://kubernetes.default.svc.cluster.local:443",
 | 
			
		||||
		BearerToken: env.Token,
 | 
			
		||||
		TLSClientConfig: rest.TLSClientConfig{
 | 
			
		||||
			Insecure: true,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	controller, err := ctrl.NewManager(conf, ctrl.Options{})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []string{}, err
 | 
			
		||||
	}
 | 
			
		||||
	cms := corev1.ConfigMapList{}
 | 
			
		||||
	cl := controller.GetClient()
 | 
			
		||||
 | 
			
		||||
	labels := client.MatchingLabels{
 | 
			
		||||
		"kind": "environment",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ns := client.InNamespace(env.UserID)
 | 
			
		||||
	if err := cl.List(ctx, &cms, labels, ns); err != nil {
 | 
			
		||||
		return []string{}, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	result := []string{}
 | 
			
		||||
	for _, env := range cms.Items {
 | 
			
		||||
		result = append(result, env.Name)
 | 
			
		||||
	}
 | 
			
		||||
	return result, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user