forked from gravitational/teleport
/
aks.go
161 lines (139 loc) · 4.58 KB
/
aks.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fetchers
import (
"context"
"fmt"
"slices"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/cloud/azure"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/srv/discovery/common"
)
type aksFetcher struct {
AKSFetcherConfig
}
// AKSFetcherConfig configures the AKS fetcher.
type AKSFetcherConfig struct {
// Client is the Azure AKS client.
Client azure.AKSClient
// Regions are the regions where the clusters should be located.
Regions []string
// ResourceGroups are the Azure resource groups the clusters must belong to.
ResourceGroups []string
// FilterLabels are the filter criteria.
FilterLabels types.Labels
// Log is the logger.
Log logrus.FieldLogger
}
// CheckAndSetDefaults validates and sets the defaults values.
func (c *AKSFetcherConfig) CheckAndSetDefaults() error {
if c.Client == nil {
return trace.BadParameter("missing Client field")
}
if len(c.Regions) == 0 {
return trace.BadParameter("missing Regions field")
}
if len(c.FilterLabels) == 0 {
return trace.BadParameter("missing FilterLabels field")
}
if c.Log == nil {
c.Log = logrus.WithField(trace.Component, "fetcher:aks")
}
return nil
}
// NewAKSFetcher creates a new AKS fetcher configuration.
func NewAKSFetcher(cfg AKSFetcherConfig) (common.Fetcher, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &aksFetcher{cfg}, nil
}
func (a *aksFetcher) Get(ctx context.Context) (types.ResourcesWithLabels, error) {
clusters, err := a.getAKSClusters(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
var kubeClusters types.KubeClusters
for _, cluster := range clusters {
if !a.isRegionSupported(cluster.Location) {
a.Log.Debugf("Cluster region %q does not match with allowed values.", cluster.Location)
continue
}
kubeCluster, err := services.NewKubeClusterFromAzureAKS(cluster)
if err != nil {
a.Log.WithError(err).Warn("Unable to create Kubernetes cluster from azure.AKSCluster.")
continue
}
if match, reason, err := services.MatchLabels(a.FilterLabels, kubeCluster.GetAllLabels()); err != nil {
a.Log.WithError(err).Warn("Unable to match AKS cluster labels against match labels.")
continue
} else if !match {
a.Log.Debugf("AKS cluster labels does not match the selector: %s.", reason)
continue
}
kubeClusters = append(kubeClusters, kubeCluster)
}
a.rewriteKubeClusters(kubeClusters)
return kubeClusters.AsResources(), nil
}
// rewriteKubeClusters rewrites the discovered kube clusters.
func (a *aksFetcher) rewriteKubeClusters(clusters types.KubeClusters) {
for _, c := range clusters {
common.ApplyAKSNameSuffix(c)
}
}
func (a *aksFetcher) getAKSClusters(ctx context.Context) ([]*azure.AKSCluster, error) {
var (
clusters []*azure.AKSCluster
err error
)
if len(a.ResourceGroups) == 1 && a.ResourceGroups[0] == types.Wildcard {
clusters, err = a.Client.ListAll(ctx)
} else {
var errs []error
for _, resourceGroup := range a.ResourceGroups {
lClusters, lerr := a.Client.ListWithinGroup(ctx, resourceGroup)
if lerr != nil {
errs = append(errs, trace.Wrap(lerr))
continue
}
clusters = append(clusters, lClusters...)
}
err = trace.NewAggregate(errs...)
}
return clusters, trace.Wrap(err)
}
func (a *aksFetcher) isRegionSupported(region string) bool {
return slices.Contains(a.Regions, types.Wildcard) || slices.Contains(a.Regions, region)
}
func (a *aksFetcher) ResourceType() string {
return types.KindKubernetesCluster
}
func (a *aksFetcher) Cloud() string {
return types.CloudAzure
}
func (a *aksFetcher) FetcherType() string {
return types.AzureMatcherKubernetes
}
func (a *aksFetcher) String() string {
return fmt.Sprintf("aksFetcher(ResourceGroups=%v, Regions=%v, FilterLabels=%v)",
a.ResourceGroups, a.Regions, a.FilterLabels)
}