-
Notifications
You must be signed in to change notification settings - Fork 6
/
azure.go
128 lines (107 loc) · 3.28 KB
/
azure.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
package cluster
import (
"context"
"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
capz "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
capi "sigs.k8s.io/cluster-api/api/v1beta1"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
)
func (s *Service) getAllAzure(ctx context.Context, namespace string) (Resource, error) {
var err error
inNamespace := runtimeClient.InNamespace(namespace)
var azureClusters map[string]*capz.AzureCluster
{
clusterCollection := &capz.AzureClusterList{}
err = s.client.List(ctx, clusterCollection, inNamespace)
if apierrors.IsForbidden(err) {
return nil, microerror.Mask(insufficientPermissionsError)
} else if err != nil {
return nil, microerror.Mask(err)
} else if len(clusterCollection.Items) == 0 {
return nil, microerror.Mask(noResourcesError)
}
azureClusters = make(map[string]*capz.AzureCluster, len(clusterCollection.Items))
for _, cluster := range clusterCollection.Items {
c := cluster
azureClusters[cluster.GetName()] = &c
}
}
clusters := &capi.ClusterList{}
{
err = s.client.List(ctx, clusters, inNamespace)
if apierrors.IsForbidden(err) {
return nil, microerror.Mask(insufficientPermissionsError)
} else if err != nil {
return nil, microerror.Mask(err)
} else if len(clusters.Items) == 0 {
return nil, microerror.Mask(noResourcesError)
}
}
clusterCollection := &Collection{}
{
for _, cr := range clusters.Items {
o := cr
if azureCluster, exists := azureClusters[cr.GetName()]; exists {
cr.TypeMeta = metav1.TypeMeta{
APIVersion: "cluster.x-k8s.io/v1beta1",
Kind: "Cluster",
}
azureCluster.TypeMeta = metav1.TypeMeta{
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
Kind: "AzureCluster",
}
c := Cluster{
Cluster: &o,
AzureCluster: azureCluster,
}
clusterCollection.Items = append(clusterCollection.Items, c)
}
}
}
return clusterCollection, nil
}
func (s *Service) getByNameAzure(ctx context.Context, name, namespace string) (Resource, error) {
var err error
labelSelector := runtimeClient.MatchingLabels{
capi.ClusterNameLabel: name,
}
inNamespace := runtimeClient.InNamespace(namespace)
cluster := &Cluster{}
{
crs := &capi.ClusterList{}
err = s.client.List(ctx, crs, labelSelector, inNamespace)
if apierrors.IsForbidden(err) {
return nil, microerror.Mask(insufficientPermissionsError)
} else if err != nil {
return nil, microerror.Mask(err)
}
if len(crs.Items) < 1 {
return nil, microerror.Mask(notFoundError)
}
cluster.Cluster = &crs.Items[0]
cluster.Cluster.TypeMeta = metav1.TypeMeta{
APIVersion: "cluster.x-k8s.io/v1beta1",
Kind: "Cluster",
}
}
{
crs := &capz.AzureClusterList{}
err = s.client.List(ctx, crs, labelSelector, inNamespace)
if apierrors.IsForbidden(err) {
return nil, microerror.Mask(insufficientPermissionsError)
} else if err != nil {
return nil, microerror.Mask(err)
}
if len(crs.Items) < 1 {
return nil, microerror.Mask(notFoundError)
}
cluster.AzureCluster = &crs.Items[0]
cluster.AzureCluster.TypeMeta = metav1.TypeMeta{
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
Kind: "AzureCluster",
}
}
return cluster, nil
}