forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
196 lines (183 loc) · 5.6 KB
/
cluster.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package db
import (
"encoding/json"
"fmt"
"hash/fnv"
"net/url"
"strings"
"github.com/argoproj/argo-cd/common"
appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/util/kube"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
apiv1 "k8s.io/api/core/v1"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/watch"
)
// ListClusters returns list of clusters
func (s *db) ListClusters(ctx context.Context) (*appv1.ClusterList, error) {
listOpts := metav1.ListOptions{}
labelSelector := labels.NewSelector()
req, err := labels.NewRequirement(common.LabelKeySecretType, selection.Equals, []string{common.SecretTypeCluster})
if err != nil {
return nil, err
}
labelSelector = labelSelector.Add(*req)
listOpts.LabelSelector = labelSelector.String()
clusterSecrets, err := s.kubeclientset.CoreV1().Secrets(s.ns).List(listOpts)
if err != nil {
return nil, err
}
clusterList := appv1.ClusterList{
Items: make([]appv1.Cluster, len(clusterSecrets.Items)),
}
for i, clusterSecret := range clusterSecrets.Items {
clusterList.Items[i] = *secretToCluster(&clusterSecret)
}
return &clusterList, nil
}
// CreateCluster creates a cluster
func (s *db) CreateCluster(ctx context.Context, c *appv1.Cluster) (*appv1.Cluster, error) {
err := kube.TestConfig(c.RESTConfig())
if err != nil {
return nil, err
}
secName := serverToSecretName(c.Server)
clusterSecret := &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secName,
Labels: map[string]string{
common.LabelKeySecretType: common.SecretTypeCluster,
},
},
}
clusterSecret.StringData = clusterToStringData(c)
clusterSecret, err = s.kubeclientset.CoreV1().Secrets(s.ns).Create(clusterSecret)
if err != nil {
if apierr.IsAlreadyExists(err) {
return nil, status.Errorf(codes.AlreadyExists, "cluster '%s' already exists", c.Server)
}
return nil, err
}
return secretToCluster(clusterSecret), nil
}
// ClusterEvent contains information about cluster event
type ClusterEvent struct {
Type watch.EventType
Cluster *appv1.Cluster
}
// WatchClusters allow watching for cluster events
func (s *db) WatchClusters(ctx context.Context, callback func(*ClusterEvent)) error {
listOpts := metav1.ListOptions{}
labelSelector := labels.NewSelector()
req, err := labels.NewRequirement(common.LabelKeySecretType, selection.Equals, []string{common.SecretTypeCluster})
if err != nil {
return err
}
labelSelector = labelSelector.Add(*req)
listOpts.LabelSelector = labelSelector.String()
w, err := s.kubeclientset.CoreV1().Secrets(s.ns).Watch(listOpts)
if err != nil {
return err
}
go func() {
<-ctx.Done()
w.Stop()
}()
for next := range w.ResultChan() {
secret := next.Object.(*apiv1.Secret)
cluster := secretToCluster(secret)
callback(&ClusterEvent{
Type: next.Type,
Cluster: cluster,
})
}
return nil
}
func (s *db) getClusterSecret(server string) (*apiv1.Secret, error) {
secName := serverToSecretName(server)
clusterSecret, err := s.kubeclientset.CoreV1().Secrets(s.ns).Get(secName, metav1.GetOptions{})
if err != nil {
if apierr.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "cluster '%s' not found", server)
}
return nil, err
}
return clusterSecret, nil
}
// GetCluster returns a cluster from a query
func (s *db) GetCluster(ctx context.Context, server string) (*appv1.Cluster, error) {
clusterSecret, err := s.getClusterSecret(server)
if err != nil {
return nil, err
}
return secretToCluster(clusterSecret), nil
}
// UpdateCluster updates a cluster
func (s *db) UpdateCluster(ctx context.Context, c *appv1.Cluster) (*appv1.Cluster, error) {
err := kube.TestConfig(c.RESTConfig())
if err != nil {
return nil, err
}
clusterSecret, err := s.getClusterSecret(c.Server)
if err != nil {
return nil, err
}
clusterSecret.StringData = clusterToStringData(c)
clusterSecret, err = s.kubeclientset.CoreV1().Secrets(s.ns).Update(clusterSecret)
if err != nil {
return nil, err
}
return secretToCluster(clusterSecret), nil
}
// Delete deletes a cluster by name
func (s *db) DeleteCluster(ctx context.Context, name string) error {
secName := serverToSecretName(name)
return s.kubeclientset.CoreV1().Secrets(s.ns).Delete(secName, &metav1.DeleteOptions{})
}
// serverToSecretName hashes server address to the secret name using a formula.
// Part of the server address is incorporated for debugging purposes
func serverToSecretName(server string) string {
serverURL, err := url.ParseRequestURI(server)
if err != nil {
panic(err)
}
h := fnv.New32a()
_, _ = h.Write([]byte(server))
host := strings.ToLower(strings.Split(serverURL.Host, ":")[0])
return fmt.Sprintf("cluster-%s-%v", host, h.Sum32())
}
// clusterToStringData converts a cluster object to string data for serialization to a secret
func clusterToStringData(c *appv1.Cluster) map[string]string {
stringData := make(map[string]string)
stringData["server"] = c.Server
if c.Name == "" {
stringData["name"] = c.Server
} else {
stringData["name"] = c.Name
}
configBytes, err := json.Marshal(c.Config)
if err != nil {
panic(err)
}
stringData["config"] = string(configBytes)
return stringData
}
// secretToCluster converts a secret into a repository object
func secretToCluster(s *apiv1.Secret) *appv1.Cluster {
var config appv1.ClusterConfig
err := json.Unmarshal(s.Data["config"], &config)
if err != nil {
panic(err)
}
cluster := appv1.Cluster{
Server: string(s.Data["server"]),
Name: string(s.Data["name"]),
Config: config,
}
return &cluster
}