Add the list method

This commit is contained in:
Nikolai Rodionov 2024-04-30 10:00:06 +02:00
parent 9f2489fc55
commit b61485348a
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
2 changed files with 46 additions and 9 deletions

View File

@ -152,9 +152,36 @@ func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environmen
} }
for _, env := range envs { for _, env := range envs {
provider, err := providers.GetProvider(environment.Data.Provider)
if err != nil {
return err
}
serverType, err := provider.RawServerType(environment.Data.ServerType)
if err != nil {
return err
}
location, err := provider.RawServerLocation(environment.Data.Location)
if err != nil {
return err
}
k8s, err := kubernetes.GetKubernetes(environment.Data.Kubernetes)
if err != nil {
return err
}
if err := stream.Send(&proto.EnvironmentFull{ if err := stream.Send(&proto.EnvironmentFull{
Metadata: &proto.EnvironmentMetadata{ Metadata: &proto.EnvironmentMetadata{
Name: env, Name: env.Data.Name,
Description: env.Data.Description,
},
Spec: &proto.EnvironmentSpec{
Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]),
Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]),
ServerLocation: proto.Location(proto.Location_value[location]),
ServerType: proto.ServerType(proto.ServerType_value[serverType]),
}, },
}); err != nil { }); err != nil {
return err return err

View File

@ -143,7 +143,7 @@ func (env *Environemnt) Delete(ctx context.Context) error {
return nil return nil
} }
func (env *Environemnt) ListEnvs(ctx context.Context) ([]string, error) { func (env *Environemnt) ListEnvs(ctx context.Context) ([]*Environemnt, error) {
env.Controller.GetClient() env.Controller.GetClient()
conf := &rest.Config{ conf := &rest.Config{
Host: "https://kubernetes.default.svc.cluster.local:443", Host: "https://kubernetes.default.svc.cluster.local:443",
@ -154,17 +154,27 @@ func (env *Environemnt) ListEnvs(ctx context.Context) ([]string, error) {
} }
clientset, err := kubernetes.NewForConfig(conf) clientset, err := kubernetes.NewForConfig(conf)
if err != nil { if err != nil {
return []string{}, err return nil, err
} }
secrets, err := clientset.CoreV1().ConfigMaps(env.UserID).List(ctx, metav1.ListOptions{LabelSelector: "kind=environment"}) configmaps, err := clientset.CoreV1().ConfigMaps(env.UserID).List(ctx, metav1.ListOptions{LabelSelector: "kind=environment"})
if err != nil { if err != nil {
return []string{}, err return nil, err
} }
result := []string{} result := []*Environemnt{}
log.Println(secrets) log.Println(configmaps)
for _, env := range secrets.Items { for _, cm := range configmaps.Items {
result = append(result, env.Name) i := &Environemnt{}
data := &EnvironemntData{
Name: cm.GetName(),
}
i.Token = env.Token
i.UserID = env.UserID
i.Data = data
if err := i.Get(ctx); err != nil {
return nil, err
}
result = append(result, i)
} }
return result, nil return result, nil
} }