-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
clientset.go
201 lines (171 loc) · 7.01 KB
/
clientset.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
197
198
199
200
201
/*
Copyright 2017 The Kubernetes Authors.
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 api
import (
"context"
"fmt"
"net/url"
"strings"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/registry"
"k8s.io/kops/pkg/apis/kops/validation"
kopsinternalversion "k8s.io/kops/pkg/client/clientset_generated/clientset/typed/kops/internalversion"
"k8s.io/kops/pkg/client/simple"
"k8s.io/kops/pkg/client/simple/vfsclientset"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/secrets"
"k8s.io/kops/util/pkg/vfs"
)
// RESTClientset is an implementation of clientset that uses a "real" generated REST client
type RESTClientset struct {
vfsContext *vfs.VFSContext
BaseURL *url.URL
KopsClient kopsinternalversion.KopsInterface
}
func NewRESTClientset(vfsContext *vfs.VFSContext, baseURL *url.URL, kopsClient kopsinternalversion.KopsInterface) *RESTClientset {
return &RESTClientset{
vfsContext: vfsContext,
BaseURL: baseURL,
KopsClient: kopsClient,
}
}
func (c *RESTClientset) VFSContext() *vfs.VFSContext {
return c.vfsContext
}
// GetCluster implements the GetCluster method of Clientset for a kubernetes-API state store
func (c *RESTClientset) GetCluster(ctx context.Context, name string) (*kops.Cluster, error) {
namespace := restNamespaceForClusterName(name)
return c.KopsClient.Clusters(namespace).Get(ctx, name, metav1.GetOptions{})
}
// AddonsFor fetches the AddonsClient for the cluster
func (c *RESTClientset) AddonsFor(cluster *kops.Cluster) simple.AddonsClient {
// We should manage these directly in the cluster
klog.Fatalf("AddonsFor not implemented for RESTClientset")
return nil
}
// CreateCluster implements the CreateCluster method of Clientset for a kubernetes-API state store
func (c *RESTClientset) CreateCluster(ctx context.Context, cluster *kops.Cluster) (*kops.Cluster, error) {
namespace := restNamespaceForClusterName(cluster.Name)
return c.KopsClient.Clusters(namespace).Create(ctx, cluster, metav1.CreateOptions{})
}
// UpdateCluster implements the UpdateCluster method of Clientset for a kubernetes-API state store
func (c *RESTClientset) UpdateCluster(ctx context.Context, cluster *kops.Cluster, status *kops.ClusterStatus) (*kops.Cluster, error) {
klog.Warningf("validating cluster update client side; needs to move to server")
old, err := c.GetCluster(ctx, cluster.Name)
if err != nil {
return nil, err
}
if err := validation.ValidateClusterUpdate(cluster, status, old, c.VFSContext()).ToAggregate(); err != nil {
return nil, err
}
namespace := restNamespaceForClusterName(cluster.Name)
return c.KopsClient.Clusters(namespace).Update(ctx, cluster, metav1.UpdateOptions{})
}
// ConfigBaseFor implements the ConfigBaseFor method of Clientset for a kubernetes-API state store
func (c *RESTClientset) ConfigBaseFor(cluster *kops.Cluster) (vfs.Path, error) {
if cluster.Spec.ConfigStore.Base != "" {
return c.VFSContext().BuildVfsPath(cluster.Spec.ConfigStore.Base)
}
// URL for clusters looks like https://<server>/apis/kops/v1alpha2/namespaces/<cluster>/clusters/<cluster>
// We probably want to add a subresource for full resources
return c.VFSContext().BuildVfsPath(c.BaseURL.String())
}
// ListClusters implements the ListClusters method of Clientset for a kubernetes-API state store
func (c *RESTClientset) ListClusters(ctx context.Context, options metav1.ListOptions) (*kops.ClusterList, error) {
return c.KopsClient.Clusters(metav1.NamespaceAll).List(ctx, options)
}
// InstanceGroupsFor implements the InstanceGroupsFor method of Clientset for a kubernetes-API state store
func (c *RESTClientset) InstanceGroupsFor(cluster *kops.Cluster) kopsinternalversion.InstanceGroupInterface {
namespace := restNamespaceForClusterName(cluster.Name)
return c.KopsClient.InstanceGroups(namespace)
}
func (c *RESTClientset) SecretStore(cluster *kops.Cluster) (fi.SecretStore, error) {
namespace := restNamespaceForClusterName(cluster.Name)
return secrets.NewClientsetSecretStore(cluster, c.KopsClient, namespace), nil
}
func (c *RESTClientset) KeyStore(cluster *kops.Cluster) (fi.CAStore, error) {
namespace := restNamespaceForClusterName(cluster.Name)
return fi.NewClientsetCAStore(cluster, c.KopsClient, namespace), nil
}
func (c *RESTClientset) SSHCredentialStore(cluster *kops.Cluster) (fi.SSHCredentialStore, error) {
namespace := restNamespaceForClusterName(cluster.Name)
return fi.NewClientsetSSHCredentialStore(cluster, c.KopsClient, namespace), nil
}
func (c *RESTClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster) error {
configBase, err := registry.ConfigBase(c.VFSContext(), cluster)
if err != nil {
return err
}
err = vfsclientset.DeleteAllClusterState(ctx, configBase)
if err != nil {
return err
}
name := cluster.Name
namespace := restNamespaceForClusterName(name)
{
keysets, err := c.KopsClient.Keysets(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("error listing Keysets: %v", err)
}
for i := range keysets.Items {
keyset := &keysets.Items[i]
err = c.KopsClient.Keysets(namespace).Delete(ctx, keyset.Name, metav1.DeleteOptions{})
if err != nil {
if errors.IsNotFound(err) {
// Unlikely...
klog.Warningf("Keyset was concurrently deleted")
} else {
return fmt.Errorf("error deleting Keyset %q: %v", keyset.Name, err)
}
}
}
}
{
igs, err := c.KopsClient.InstanceGroups(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("error listing instance groups: %v", err)
}
for i := range igs.Items {
ig := &igs.Items[i]
err = c.KopsClient.InstanceGroups(namespace).Delete(ctx, ig.Name, metav1.DeleteOptions{})
if err != nil {
if errors.IsNotFound(err) {
// Unlikely...
klog.Warningf("instance group was concurrently deleted")
} else {
return fmt.Errorf("error deleting instance group %q: %v", ig.Name, err)
}
}
}
}
err = c.KopsClient.Clusters(namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
if errors.IsNotFound(err) {
// Unlikely...
klog.Warningf("cluster %q was concurrently deleted", name)
} else {
return fmt.Errorf("error deleting cluster%q: %v", name, err)
}
}
return nil
}
func restNamespaceForClusterName(clusterName string) string {
// We are not allowed dots, so we map them to dashes
// This can conflict, but this will simply be a limitation that we pass on to the user
// i.e. it will not be possible to create a.b.example.com and a-b.example.com
namespace := strings.Replace(clusterName, ".", "-", -1)
return namespace
}