-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
listing.go
134 lines (124 loc) · 4.32 KB
/
listing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Copyright 2021 Rafael Fernández López <ereslibre@ereslibre.es>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package controllers
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
clientapi "sigs.k8s.io/controller-runtime/pkg/client"
clusterv1alpha1 "github.com/oneinfra/oneinfra/apis/cluster/v1alpha1"
infrav1alpha1 "github.com/oneinfra/oneinfra/apis/infra/v1alpha1"
clusterapi "github.com/oneinfra/oneinfra/internal/pkg/cluster"
"github.com/oneinfra/oneinfra/internal/pkg/component"
componentapi "github.com/oneinfra/oneinfra/internal/pkg/component"
"github.com/oneinfra/oneinfra/internal/pkg/infra"
"github.com/oneinfra/oneinfra/pkg/constants"
)
func listHypervisors(ctx context.Context, client clientapi.Client, connectionPool *infra.HypervisorConnectionPool) (infra.HypervisorMap, error) {
var hypervisorList infrav1alpha1.HypervisorList
if err := client.List(ctx, &hypervisorList); err != nil {
return infra.HypervisorMap{}, err
}
res := infra.HypervisorMap{}
for _, hypervisor := range hypervisorList.Items {
internalHypervisor, err := infra.NewHypervisorFromv1alpha1(&hypervisor, connectionPool)
if err != nil {
klog.Errorf("could not convert hypervisor to internal type: %v", err)
continue
}
res[internalHypervisor.Name] = internalHypervisor
}
return res, nil
}
func listClusters(ctx context.Context, client clientapi.Client) (clusterapi.Map, error) {
var clusterList clusterv1alpha1.ClusterList
if err := client.List(ctx, &clusterList); err != nil {
return clusterapi.Map{}, err
}
res := clusterapi.Map{}
for _, cluster := range clusterList.Items {
internalCluster, err := clusterapi.NewClusterFromv1alpha1(&cluster)
if err != nil {
continue
}
res[internalCluster.Name] = internalCluster
}
return res, nil
}
func getComponent(ctx context.Context, client clientapi.Client, req ctrl.Request) (*component.Component, error) {
var component clusterv1alpha1.Component
if err := client.Get(ctx, req.NamespacedName, &component); err != nil {
return nil, err
}
internalComponent, err := componentapi.NewComponentFromv1alpha1(&component)
if err != nil {
return nil, err
}
return internalComponent, nil
}
func getCluster(ctx context.Context, client clientapi.Client, req ctrl.Request) (*clusterapi.Cluster, error) {
var cluster clusterv1alpha1.Cluster
if err := client.Get(ctx, req.NamespacedName, &cluster); err != nil {
return nil, err
}
internalCluster, err := clusterapi.NewClusterFromv1alpha1(&cluster)
if err != nil {
return nil, err
}
return internalCluster, nil
}
func listComponents(ctx context.Context, client clientapi.Client) (componentapi.List, error) {
var componentList clusterv1alpha1.ComponentList
if err := client.List(ctx, &componentList); err != nil {
return componentapi.List{}, err
}
res := componentapi.List{}
for _, component := range componentList.Items {
internalComponent, err := componentapi.NewComponentFromv1alpha1(&component)
if err != nil {
continue
}
res = append(res, internalComponent)
}
return res, nil
}
func listClusterComponents(ctx context.Context, client clientapi.Client, clusterNamespace, clusterName string) (componentapi.List, error) {
var componentList clusterv1alpha1.ComponentList
err := client.List(
ctx,
&componentList,
&clientapi.ListOptions{
Raw: &metav1.ListOptions{
FieldSelector: fmt.Sprintf("metadata.namespace=%s", clusterNamespace),
LabelSelector: fmt.Sprintf("%s=%s", constants.OneInfraClusterNameLabelName, clusterName),
},
},
)
if err != nil {
return componentapi.List{}, err
}
res := componentapi.List{}
for _, component := range componentList.Items {
internalComponent, err := componentapi.NewComponentFromv1alpha1(&component)
if err != nil {
continue
}
res = append(res, internalComponent)
}
return res, nil
}