Add a list function
This commit is contained in:
parent
8305425d47
commit
80003e41ad
@ -97,5 +97,39 @@ func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.EnvironmentId) (
|
||||
}
|
||||
|
||||
func (e *EnvironmentsServer) List(in *empty.Empty, stream proto.Environments_ListServer) error {
|
||||
md, ok := metadata.FromIncomingContext(stream.Context())
|
||||
if !ok {
|
||||
return errors.New("metadata is not provided")
|
||||
}
|
||||
token, ok := md["token"]
|
||||
if !ok {
|
||||
return errors.New("token is not sent via metadata")
|
||||
}
|
||||
|
||||
uuid, ok := md["uuid"]
|
||||
if !ok {
|
||||
return errors.New("used id is not sent via metadata")
|
||||
}
|
||||
|
||||
environment := &controllers.Environemnt{
|
||||
UserID: uuid[0],
|
||||
Controller: e.controller,
|
||||
Token: token[0],
|
||||
}
|
||||
envs, err := environment.ListEnvs(stream.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, env := range envs {
|
||||
if err := stream.Send(&proto.EnvironmentFull{
|
||||
Id: &proto.EnvironmentId{Id: "test"},
|
||||
Data: &proto.EnvironmentData{
|
||||
Name: env,
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user